use oxios_kernel::{DaemonManager, DaemonStatus};
#[test]
fn stale_pidfile_detected_and_cleaned() {
let tmp = tempfile::tempdir().unwrap();
let pid_file = tmp.path().join("oxios.pid");
std::fs::write(&pid_file, "999999").unwrap();
let dm = DaemonManager::new(pid_file.to_str().unwrap(), tmp.path().to_str().unwrap());
assert!(
matches!(dm.status(), DaemonStatus::Stale { .. }),
"dead PID in pidfile should be Stale"
);
dm.cleanup().unwrap();
assert!(
matches!(dm.status(), DaemonStatus::Stopped),
"after cleanup, should be Stopped"
);
}
#[test]
fn no_pidfile_is_stopped() {
let tmp = tempfile::tempdir().unwrap();
let dm = DaemonManager::new(
tmp.path().join("nonexistent.pid").to_str().unwrap(),
tmp.path().to_str().unwrap(),
);
assert!(matches!(dm.status(), DaemonStatus::Stopped));
}
#[cfg(unix)]
#[test]
fn fresh_pidfile_reports_running() {
let tmp = tempfile::tempdir().unwrap();
let pid_file = tmp.path().join("oxios.pid");
std::fs::write(&pid_file, std::process::id().to_string()).unwrap();
let dm = DaemonManager::new(pid_file.to_str().unwrap(), tmp.path().to_str().unwrap());
assert!(
matches!(dm.status(), DaemonStatus::Running { .. }),
"current PID in pidfile should be Running"
);
dm.cleanup().unwrap();
}
#[cfg(unix)]
#[test]
fn orphan_detection_finds_listener() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let tmp = tempfile::tempdir().unwrap();
let dm = DaemonManager::new(
tmp.path().join("oxios.pid").to_str().unwrap(),
tmp.path().to_str().unwrap(),
)
.with_probe_port(port);
let status = dm.status();
drop(listener);
match status {
DaemonStatus::Orphaned { port: p } => assert_eq!(p, port),
other => panic!("expected Orphaned, got {other:?}"),
}
}
#[test]
fn no_listener_is_stopped() {
let tmp = tempfile::tempdir().unwrap();
let dm = DaemonManager::new(
tmp.path().join("oxios.pid").to_str().unwrap(),
tmp.path().to_str().unwrap(),
)
.with_probe_port(1);
assert!(matches!(dm.status(), DaemonStatus::Stopped));
}
#[test]
fn daemon_status_display() {
assert_eq!(
DaemonStatus::Running { pid: 42 }.to_string(),
"running (PID 42)"
);
assert_eq!(
DaemonStatus::Stale { pid: 99 }.to_string(),
"stale (PID 99 dead)"
);
assert_eq!(DaemonStatus::Stopped.to_string(), "stopped");
assert_eq!(
DaemonStatus::Orphaned { port: 4200 }.to_string(),
"orphaned (no pidfile, port 4200 in use)"
);
}