|
| 1 | +use rexos::{config::RexosConfig, paths::RexosPaths}; |
| 2 | + |
| 3 | +#[derive(Debug, Clone, serde::Serialize)] |
| 4 | +pub(crate) struct ConfigValidationReport { |
| 5 | + pub(crate) valid: bool, |
| 6 | + pub(crate) config_path: String, |
| 7 | + pub(crate) errors: Vec<String>, |
| 8 | +} |
| 9 | + |
| 10 | +pub(crate) fn validate_config(paths: &RexosPaths) -> ConfigValidationReport { |
| 11 | + let config_path = paths.config_path(); |
| 12 | + let display_path = config_path.display().to_string(); |
| 13 | + let raw = match std::fs::read_to_string(&config_path) { |
| 14 | + Ok(raw) => raw, |
| 15 | + Err(e) => { |
| 16 | + return ConfigValidationReport { |
| 17 | + valid: false, |
| 18 | + config_path: display_path, |
| 19 | + errors: vec![format!("read config failed: {e}")], |
| 20 | + }; |
| 21 | + } |
| 22 | + }; |
| 23 | + |
| 24 | + let cfg: RexosConfig = match toml::from_str(&raw) { |
| 25 | + Ok(cfg) => cfg, |
| 26 | + Err(e) => { |
| 27 | + return ConfigValidationReport { |
| 28 | + valid: false, |
| 29 | + config_path: display_path, |
| 30 | + errors: vec![format!("parse config TOML failed: {e}")], |
| 31 | + }; |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + let mut errors = Vec::new(); |
| 36 | + for (route_name, provider_name) in [ |
| 37 | + ("planning", cfg.router.planning.provider.trim()), |
| 38 | + ("coding", cfg.router.coding.provider.trim()), |
| 39 | + ("summary", cfg.router.summary.provider.trim()), |
| 40 | + ] { |
| 41 | + if provider_name.is_empty() { |
| 42 | + errors.push(format!("router.{route_name}.provider is empty")); |
| 43 | + continue; |
| 44 | + } |
| 45 | + if !cfg.providers.contains_key(provider_name) { |
| 46 | + errors.push(format!( |
| 47 | + "router.{route_name}.provider references unknown provider '{provider_name}'" |
| 48 | + )); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + ConfigValidationReport { |
| 53 | + valid: errors.is_empty(), |
| 54 | + config_path: display_path, |
| 55 | + errors, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use super::*; |
| 62 | + use tempfile::tempdir; |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn validate_config_reports_success_for_default_config() { |
| 66 | + let tmp = tempdir().unwrap(); |
| 67 | + let paths = RexosPaths { |
| 68 | + base_dir: tmp.path().join(".loopforge"), |
| 69 | + }; |
| 70 | + paths.ensure_dirs().unwrap(); |
| 71 | + RexosConfig::ensure_default(&paths).unwrap(); |
| 72 | + |
| 73 | + let report = validate_config(&paths); |
| 74 | + assert!(report.valid, "expected config valid, got {report:?}"); |
| 75 | + assert!( |
| 76 | + report.errors.is_empty(), |
| 77 | + "expected no errors, got {report:?}" |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn validate_config_reports_parse_error_for_invalid_toml() { |
| 83 | + let tmp = tempdir().unwrap(); |
| 84 | + let paths = RexosPaths { |
| 85 | + base_dir: tmp.path().join(".loopforge"), |
| 86 | + }; |
| 87 | + paths.ensure_dirs().unwrap(); |
| 88 | + std::fs::write( |
| 89 | + paths.config_path(), |
| 90 | + "[providers |
| 91 | +broken = true", |
| 92 | + ) |
| 93 | + .unwrap(); |
| 94 | + |
| 95 | + let report = validate_config(&paths); |
| 96 | + assert!(!report.valid, "expected config invalid, got {report:?}"); |
| 97 | + assert!( |
| 98 | + report |
| 99 | + .errors |
| 100 | + .iter() |
| 101 | + .any(|e| e.contains("parse config TOML")), |
| 102 | + "expected parse error, got {report:?}" |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments