use std::fs; use std::path::PathBuf; use crate::config::{save, load, validate, Auth, Config, Grafana, Hub, Satellite}; fn sample() -> Config { Config { hub: Hub { host: "hub.example.com".into(), user: "deploy".into(), path: "/opt/sope/hub".into(), grafana_port: 3000, grafana_bind: "0.0.0.0".into(), ingest_bind: "0.0.0.0".into(), }, auth: Auth { token: "secret-token".into(), }, satellites: vec![Satellite { name: "node-a".into(), host: "10.0.0.1".into(), user: "deploy".into(), path: "/opt/sope/sat".into(), }], grafana: Grafana::default(), } } #[test] fn save_roundtrip() { let dir = std::env::temp_dir().join(format!("sope-cfg-{}", std::process::id())); let _ = fs::remove_dir_all(&dir); fs::create_dir_all(&dir).unwrap(); let path = dir.join("sope.toml"); let cfg = sample(); save(&path, &cfg).unwrap(); assert!(path.exists()); let loaded = load(&path).unwrap(); assert_eq!(loaded.hub.host, "hub.example.com"); assert_eq!(loaded.satellites.len(), 1); save(&path, &cfg).unwrap(); let bak = PathBuf::from(format!("{}.bak", path.display())); assert!(path.with_extension("toml.bak").exists() || bak.exists()); let _ = fs::remove_dir_all(&dir); } #[test] fn validate_rejects_duplicate_satellite_names() { let mut cfg = sample(); cfg.satellites.push(Satellite { name: "node-a".into(), host: "10.0.0.2".into(), user: "deploy".into(), path: "/opt/sope/sat".into(), }); assert!(validate(&cfg).is_err()); } #[test] fn validate_rejects_colocated_path_collision() { let mut cfg = sample(); cfg.satellites[0].host = cfg.hub.host.clone(); cfg.satellites[0].path = cfg.hub.path.clone(); assert!(validate(&cfg).is_err()); } #[test] fn validate_allows_colocated_distinct_paths() { let mut cfg = sample(); cfg.satellites[0].host = cfg.hub.host.clone(); cfg.satellites[0].path = "/opt/sope/sat".into(); assert!(validate(&cfg).is_ok()); assert!(cfg.same_host(&cfg.satellites[0])); }