dynpatch-watcher 0.1.0

File watching and live config reloading for dynpatch
Documentation
//! Integration tests for config hot-reloading

use dynpatch_watcher::config::{ConfigWatcher, HotConfig};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct TestConfig {
    value: i32,
    name: String,
    enabled: bool,
}

impl HotConfig for TestConfig {
    fn validate(&self) -> Result<(), String> {
        if self.value < 0 {
            return Err("value must be non-negative".to_string());
        }
        Ok(())
    }
}

#[test]
#[cfg(feature = "json")]
fn test_config_watcher_json() {
    use tempfile::Builder;
    
    let temp = Builder::new()
        .suffix(".json")
        .tempfile()
        .unwrap();
    
    let config = TestConfig {
        value: 42,
        name: "test".to_string(),
        enabled: true,
    };
    
    std::fs::write(temp.path(), serde_json::to_string(&config).unwrap()).unwrap();

    let watcher = ConfigWatcher::<TestConfig>::new(temp.path()).unwrap();
    let loaded = watcher.get();
    
    assert_eq!(loaded.value, 42);
    assert_eq!(loaded.name, "test");
    assert!(loaded.enabled);
}

#[test]
#[cfg(feature = "toml")]
fn test_config_watcher_toml() {
    use tempfile::Builder;
    
    let temp = Builder::new()
        .suffix(".toml")
        .tempfile()
        .unwrap();
    
    let config = TestConfig {
        value: 100,
        name: "toml-test".to_string(),
        enabled: false,
    };
    
    std::fs::write(temp.path(), toml::to_string(&config).unwrap()).unwrap();

    let watcher = ConfigWatcher::<TestConfig>::new(temp.path()).unwrap();
    let loaded = watcher.get();
    
    assert_eq!(loaded.value, 100);
    assert_eq!(loaded.name, "toml-test");
    assert!(!loaded.enabled);
}

#[test]
#[cfg(feature = "json")]
fn test_config_validation_rejection() {
    use tempfile::Builder;
    
    let temp = Builder::new()
        .suffix(".json")
        .tempfile()
        .unwrap();
    
    // Write valid config first
    let valid_config = TestConfig {
        value: 10,
        name: "valid".to_string(),
        enabled: true,
    };
    std::fs::write(temp.path(), serde_json::to_string(&valid_config).unwrap()).unwrap();

    let watcher = ConfigWatcher::<TestConfig>::new(temp.path()).unwrap();
    
    // Initial config should be valid
    assert_eq!(watcher.get().value, 10);
    
    // Note: Testing actual file change detection would require
    // waiting for the file system watcher, which is complex in tests.
    // The validation logic itself is tested here.
}