use std::time::Duration;
use super::{SweepMarker, marker_path, read_pass, record_finish, record_start};
const SWEEP: &str = "worktree-reclaim";
#[test]
fn a_started_sweep_reads_as_running_from_another_process() {
let root = tempfile::TempDir::new().expect("tempdir");
record_start(&marker_path(root.path(), SWEEP));
let pass = read_pass(root.path(), SWEEP).expect("a marker was written");
assert!(
pass.running,
"a started pass that has not finished is running"
);
assert_eq!(pass.last_pass, None, "no pass has completed yet");
}
#[test]
fn a_finished_pass_reads_as_idle_with_its_duration() {
let root = tempfile::TempDir::new().expect("tempdir");
let path = marker_path(root.path(), SWEEP);
record_start(&path);
record_finish(&path, Duration::from_millis(1_500));
let pass = read_pass(root.path(), SWEEP).expect("a marker was written");
assert!(!pass.running, "a finished pass is not in flight");
assert_eq!(pass.last_pass, Some(Duration::from_millis(1_500)));
}
#[test]
fn a_marker_whose_writer_is_gone_reads_as_idle() {
let root = tempfile::TempDir::new().expect("tempdir");
let path = marker_path(root.path(), SWEEP);
std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
let orphan = SweepMarker {
pid: u32::MAX,
started_unix_ms: 1,
finished_unix_ms: None,
last_pass_ms: None,
};
std::fs::write(&path, serde_json::to_vec(&orphan).expect("encode")).expect("write");
let pass = read_pass(root.path(), SWEEP).expect("a marker exists");
assert!(
!pass.running,
"an unfinished marker whose writer is gone must not report a live pass"
);
}
#[test]
fn an_unreadable_marker_reads_as_nothing() {
let root = tempfile::TempDir::new().expect("tempdir");
assert!(
read_pass(root.path(), SWEEP).is_none(),
"no marker is not an idle sweep"
);
let path = marker_path(root.path(), SWEEP);
std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
std::fs::write(&path, b"{ not json").expect("write");
assert!(
read_pass(root.path(), SWEEP).is_none(),
"a corrupt marker is discarded, never guessed at"
);
}