#[cfg(test)]
mod tests {
use kasl::libs::{daemon, data_storage::DataStorage};
use std::process::Command;
use std::thread;
use std::time::Duration;
use tempfile::TempDir;
use test_context::{test_context, TestContext};
struct DaemonTestContext {
_temp_dir: TempDir,
}
impl TestContext for DaemonTestContext {
fn setup() -> Self {
let temp_dir = tempfile::tempdir().unwrap();
std::env::set_var("HOME", temp_dir.path());
std::env::set_var("LOCALAPPDATA", temp_dir.path());
DaemonTestContext { _temp_dir: temp_dir }
}
fn teardown(self) {
let kasl_binary = std::env::current_dir().unwrap().join("target").join("debug").join("kasl.exe");
let _ = Command::new(&kasl_binary).args(&["watch", "--stop"]).output();
std::thread::sleep(std::time::Duration::from_millis(500));
}
}
#[test_context(DaemonTestContext)]
#[test]
fn test_daemon_stop_on_windows(_ctx: &mut DaemonTestContext) {
let kasl_binary = std::env::current_dir().unwrap().join("target").join("debug").join("kasl.exe");
let pid_path = DataStorage::new().get_path("kasl-watch.pid").unwrap();
let _ = Command::new(&kasl_binary).args(&["watch", "--stop"]).output();
thread::sleep(Duration::from_millis(500));
let mut child = Command::new(&kasl_binary)
.arg("watch")
.spawn()
.expect("Failed to start watch process");
thread::sleep(Duration::from_millis(2000));
assert!(pid_path.exists(), "PID file should exist after starting watch");
let output = Command::new(&kasl_binary).args(&["watch", "--stop"]).output().expect("Failed to stop watch");
assert!(output.status.success(), "Failed to stop daemon: {:?}", String::from_utf8_lossy(&output.stderr));
thread::sleep(Duration::from_millis(1000));
assert!(!pid_path.exists(), "PID file should be removed after stopping");
let _ = child.kill();
let _ = child.wait();
}
#[test_context(DaemonTestContext)]
#[test]
fn test_no_duplicate_daemons(_ctx: &mut DaemonTestContext) {
let kasl_binary = std::env::current_dir().unwrap().join("target").join("debug").join("kasl.exe");
let pid_path = DataStorage::new().get_path("kasl-watch.pid").unwrap();
let _ = Command::new(&kasl_binary).args(&["watch", "--stop"]).output();
thread::sleep(Duration::from_millis(500));
let mut child1 = Command::new(&kasl_binary)
.arg("watch")
.spawn()
.expect("Failed to start first watch");
thread::sleep(Duration::from_millis(2000));
assert!(pid_path.exists(), "First daemon should create PID file");
let first_pid = std::fs::read_to_string(&pid_path).expect("Failed to read first PID").trim().to_string();
let mut child2 = Command::new(&kasl_binary)
.arg("watch")
.spawn()
.expect("Failed to start second watch");
thread::sleep(Duration::from_millis(2000));
if pid_path.exists() {
let second_pid = std::fs::read_to_string(&pid_path).expect("Failed to read second PID").trim().to_string();
assert_ne!(first_pid, second_pid, "Second daemon should have different PID");
}
let _ = Command::new(&kasl_binary).args(&["watch", "--stop"]).output();
thread::sleep(Duration::from_millis(500));
let _ = child1.kill();
let _ = child1.wait();
let _ = child2.kill();
let _ = child2.wait();
}
#[test_context(DaemonTestContext)]
#[test]
fn test_daemon_is_running_status(_ctx: &mut DaemonTestContext) {
assert!(!daemon::is_running(), "No daemon should be running initially");
let kasl_binary = std::env::current_dir()
.expect("Failed to get current directory")
.join("target")
.join("debug")
.join("kasl.exe");
let _child = Command::new(&kasl_binary)
.args(&["watch"])
.spawn()
.expect("Failed to start daemon");
thread::sleep(Duration::from_millis(2000));
assert!(daemon::is_running(), "Daemon should be running after start");
let _ = Command::new(&kasl_binary).args(&["watch", "--stop"]).output();
thread::sleep(Duration::from_millis(1000));
assert!(!daemon::is_running(), "Daemon should be stopped after stop command");
}
}