use process_state::{ProcessState, ProcessStatus};
use std::process::Command;
use tempfile::TempDir;
#[test]
fn test_add_and_remove_process() {
let tmp = TempDir::new().unwrap();
let ns = tmp.path().file_name().unwrap().to_str().unwrap();
let mut state = ProcessState::new(ns).unwrap();
let child = if cfg!(windows) {
Command::new("timeout").arg("2").spawn().unwrap()
} else {
Command::new("sleep").arg("2").spawn().unwrap()
};
state.add_process(&child, "test", "sleep 2").unwrap();
assert_eq!(state.get_all().len(), 1);
assert_eq!(state.get_running().len(), 1);
let pid = child.id();
state.remove_process(pid).unwrap();
assert_eq!(state.get_all().len(), 0);
}
#[test]
fn test_refresh_updates_status() {
let tmp = TempDir::new().unwrap();
let ns = tmp.path().file_name().unwrap().to_str().unwrap();
let mut state = ProcessState::new(ns).unwrap();
let mut child = if cfg!(windows) {
Command::new("timeout").arg("1").spawn().unwrap()
} else {
Command::new("sleep").arg("1").spawn().unwrap()
};
state.add_process(&child, "short", "sleep 1").unwrap();
state.refresh().unwrap();
assert_eq!(state.get_running().len(), 1);
child.wait().unwrap();
state.refresh().unwrap();
let all = state.get_all();
let info = all.first().unwrap();
assert_eq!(info.status, ProcessStatus::Stopped);
}
#[test]
fn test_persistence() {
let tmp = TempDir::new().unwrap();
let ns = tmp.path().file_name().unwrap().to_str().unwrap();
{
let mut state = ProcessState::new(ns).unwrap();
let child = if cfg!(windows) {
Command::new("timeout").arg("1").spawn().unwrap()
} else {
Command::new("sleep").arg("1").spawn().unwrap()
};
state.add_process(&child, "persist", "sleep 1").unwrap();
let all = state.get_all();
let info = all.first().unwrap();
assert_eq!(info.label, "persist");
}
let state2 = ProcessState::new(ns).unwrap();
let all = state2.get_all();
let info = all.first().unwrap();
assert_eq!(info.label, "persist");
}