use std::path::Path;
use std::process::{Command, Stdio};
use std::time::Duration;
use nibkeep_crypto::rand_core::TryRng;
use nibkeep_testkit::{TestRng, child};
use super::tree_turns::{
TreePlaces, check_tree_after, desk_step, key_in, make_tree_libraries, take_turns,
};
const TREE_CHILD: &str = "NIBKEEP_POWER_LOSS_TREE";
const TREE_LAPTOP: &str = "NIBKEEP_POWER_LOSS_TREE_LAPTOP";
pub(crate) fn tree_rounds(
test: &str,
rounds: u64,
places: &TreePlaces,
laptop: &Path,
mut cut: impl FnMut(),
) {
let rounds = std::env::var("NIBKEEP_POWER_LOSS_ROUNDS")
.ok()
.map_or(rounds, |rounds| rounds.parse().expect("a number"));
let mut rng = TestRng::new(test);
make_tree_libraries(places, &mut rng);
cut();
child::note(&places.directory, &[0, 0, 0]);
check_tree_after(0, places, (0, 0), true);
for round in 1..=rounds {
let mut process = Command::new(std::env::current_exe().expect("this test"))
.args(["--exact", test, "--nocapture"])
.env(TREE_CHILD, &places.directory)
.env(TREE_LAPTOP, laptop)
.stdout(Stdio::null())
.stderr(child::errors(&places.directory))
.spawn()
.expect("starts the child");
let most = if round % 2 == 0 { 400 } else { 3_000 };
let delay = rng.try_next_u64().expect("infallible") % most;
child::kill_after_progress(
&mut process,
&places.directory,
round,
Duration::from_millis(delay),
);
cut();
let [own, key, syncs] = child::noted(&places.directory);
let verify = round % 10 == 0 || round == rounds;
let (own, _) = check_tree_after(round, places, (own, key), verify);
let (_, key) = key_in(&places.on_laptop).expect("the laptop holds the key");
child::note(&places.directory, &[own, key, syncs]);
}
}
pub(crate) fn tree_child() {
if let (Some(directory), Some(laptop)) =
(std::env::var_os(TREE_CHILD), std::env::var_os(TREE_LAPTOP))
{
let places = TreePlaces::new(Path::new(&directory), Path::new(&laptop));
let noted = child::noted(&places.directory);
let [own, _, _] = noted;
let step = desk_step(&places.on_desk);
let mut rng = TestRng::new(&format!("tree child {own} {step}"));
take_turns(&places, noted, &mut rng, &mut |noted| {
child::note(&places.directory, ¬ed);
});
}
}