use cirious_codex_config::watch_config;
use std::fs;
use std::io::Write;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
#[test]
fn test_watch_config_triggers() -> Result<(), Box<dyn std::error::Error>> {
let path = "test_config.toml";
if fs::metadata(path).is_ok() {
fs::remove_file(path)?;
}
fs::File::create(path)?;
let trigger_count = Arc::new(Mutex::new(0));
let trigger_clone = trigger_count.clone();
thread::spawn(move || {
let _ = watch_config(path, move || {
if let Ok(mut count) = trigger_clone.lock() {
*count += 1;
}
});
});
thread::sleep(Duration::from_millis(500));
let mut file = fs::File::create(path)?;
file.write_all(b"key = 'value'")?;
thread::sleep(Duration::from_millis(500));
let count = *trigger_count.lock().map_err(|_| "Mutex poisoned")?;
fs::remove_file(path)?;
assert!(count > 0, "Watcher should have triggered at least once");
Ok(())
}