use std::time::SystemTime;
use pretty_assertions::assert_eq;
use super::*;
const WATCHER_SESSION: &str = "5a7c9e1b-3d4f-4a6b-8c0d-2e4f6a8b0c1d";
fn wake_dir() -> (tempfile::TempDir, WakeDir) {
let home = tempfile::tempdir().unwrap();
let dir = WakeDir::in_home(home.path());
crate::subagent::ensure_private_directory(dir.path()).unwrap();
(home, dir)
}
fn set_modified(path: &Path, modified: SystemTime) {
File::options()
.write(true)
.open(path)
.unwrap()
.set_modified(modified)
.unwrap();
}
#[cfg(unix)]
fn mode(path: &Path) -> u32 {
use std::os::unix::fs::PermissionsExt;
std::fs::metadata(path).unwrap().permissions().mode() & 0o777
}
#[tokio::test]
async fn starting_a_wait_prunes_only_old_unusable_files() {
let (_home, dir) = wake_dir();
let (live, ended) = (uuid::Uuid::new_v4(), uuid::Uuid::new_v4());
let live_lock = dir.path().join(format!("{live}.lock"));
let held = crate::daemon::create_private_file(&live_lock).unwrap();
held.lock().unwrap();
let old = SystemTime::now() - EXPIRED_AFTER - Duration::from_secs(60);
let record = || format!("{}.watcher", uuid::Uuid::new_v4());
let files = [
(
"old finished record",
record(),
format!("{live}\nmake exited 0"),
old,
),
(
"old waiting record of a live parent",
record(),
live.to_string(),
old,
),
(
"old waiting record of an ended parent",
record(),
ended.to_string(),
old,
),
(
"new finished record",
record(),
format!("{ended}\nmake exited 0"),
SystemTime::now(),
),
(
"old signal of a live parent",
live.to_string(),
"make exited 0".into(),
old,
),
(
"old signal of an ended parent",
ended.to_string(),
"make exited 0".into(),
old,
),
(
"old lock of an ended parent",
format!("{ended}.lock"),
String::new(),
old,
),
(
"old staging file",
format!("{ended}.tmp"),
String::new(),
old,
),
]
.map(|(name, file, contents, modified)| {
let path = dir.path().join(file);
std::fs::write(&path, contents).unwrap();
set_modified(&path, modified);
(name, path)
});
set_modified(&live_lock, old);
let signal = WakeSignal::parse(&dir, &uuid::Uuid::new_v4().to_string()).unwrap();
signal.fire("cargo test exited 0").unwrap();
let summary = signal
.wait(WATCHER_SESSION, &CancellationToken::new())
.await;
assert_eq!(
(
summary.as_deref(),
files.map(|(name, path)| (name, path.exists())),
live_lock.exists(),
),
(
Some("cargo test exited 0"),
[
("old finished record", false),
("old waiting record of a live parent", true),
("old waiting record of an ended parent", false),
("new finished record", true),
("old signal of a live parent", true),
("old signal of an ended parent", false),
("old lock of an ended parent", false),
("old staging file", false),
],
true,
)
);
}
#[tokio::test]
async fn failed_result_record_keeps_the_signal_for_the_restarted_watcher() {
let (_home, dir) = wake_dir();
let signal = WakeSignal::parse(&dir, &uuid::Uuid::new_v4().to_string()).unwrap();
signal.fire("cargo test exited 0").unwrap();
std::fs::create_dir(dir.path().join(format!("{WATCHER_SESSION}.watcher-tmp"))).unwrap();
let first = signal
.wait(WATCHER_SESSION, &CancellationToken::new())
.await;
let restarted = signal
.wait(WATCHER_SESSION, &CancellationToken::new())
.await;
assert_eq!(
(first.as_deref(), restarted.as_deref()),
(Some("cargo test exited 0"), Some("cargo test exited 0"))
);
}
#[cfg(unix)]
#[tokio::test]
async fn wake_files_are_private() {
let home = tempfile::tempdir().unwrap();
let dir = WakeDir::in_home(home.path());
let (signal, _lock) = WakeSignal::create(&dir).unwrap();
signal.fire("deploy --token secret exited 0").unwrap();
let modes_before_wait = (
mode(dir.path()),
mode(&signal.lock_path()),
mode(&signal.path()),
);
signal
.wait(WATCHER_SESSION, &CancellationToken::new())
.await
.unwrap();
let record = WaitRecord::of(&dir, WATCHER_SESSION).unwrap();
assert_eq!(
(modes_before_wait, mode(&record.path)),
((0o700, 0o600, 0o600), 0o600)
);
}