#[cfg(test)]
mod tests {
use kasl::libs::{daemon, data_storage::DataStorage};
use serial_test::serial;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::thread;
use std::time::Duration;
use tempfile::TempDir;
use test_context::{TestContext, test_context};
struct DaemonTestContext {
dir: PathBuf,
_temp_dir: TempDir,
}
impl TestContext for DaemonTestContext {
fn setup() -> Self {
let temp_dir = tempfile::tempdir().unwrap();
unsafe {
std::env::set_var("HOME", temp_dir.path());
}
unsafe {
std::env::set_var("LOCALAPPDATA", temp_dir.path());
}
DaemonTestContext {
dir: temp_dir.path().to_path_buf(),
_temp_dir: temp_dir,
}
}
fn teardown(self) {
let _ = kasl_cmd(&self.dir).args(["watch", "--stop"]).output();
thread::sleep(Duration::from_millis(500));
}
}
fn kasl_cmd(dir: &Path) -> Command {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_kasl"));
cmd.env("HOME", dir)
.env("LOCALAPPDATA", dir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
cmd
}
#[test_context(DaemonTestContext)]
#[serial]
#[test]
fn test_daemon_stop(ctx: &mut DaemonTestContext) {
let pid_path = DataStorage::new().get_path("kasl-watch.pid").unwrap();
let mut child = kasl_cmd(&ctx.dir).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 = kasl_cmd(&ctx.dir).args(["watch", "--stop"]).output().expect("Failed to stop watch");
assert!(output.status.success(), "watch --stop should succeed");
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)]
#[serial]
#[test]
fn test_no_duplicate_daemons(ctx: &mut DaemonTestContext) {
let pid_path = DataStorage::new().get_path("kasl-watch.pid").unwrap();
let mut child1 = kasl_cmd(&ctx.dir).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 = kasl_cmd(&ctx.dir).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 _ = kasl_cmd(&ctx.dir).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)]
#[serial]
#[test]
fn test_daemon_is_running_status(ctx: &mut DaemonTestContext) {
assert!(!daemon::is_running(), "No daemon should be running initially");
let mut child = kasl_cmd(&ctx.dir).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 _ = kasl_cmd(&ctx.dir).args(["watch", "--stop"]).output();
thread::sleep(Duration::from_millis(1000));
assert!(!daemon::is_running(), "Daemon should be stopped after stop command");
let _ = child.kill();
let _ = child.wait();
}
}