|
| 1 | +use crate::migration::core::AmbiguityResolver; |
| 2 | +use crate::migration::target::TargetError; |
| 3 | + |
| 4 | +use super::VersionStepOutput; |
| 5 | + |
| 6 | +const STEP_ID: &str = "add `[stats]` section with default persist_path"; |
| 7 | + |
| 8 | +pub fn apply( |
| 9 | + _target_name: &str, |
| 10 | + table: &mut toml::value::Table, |
| 11 | + _resolver: &mut dyn AmbiguityResolver, |
| 12 | +) -> Result<VersionStepOutput, TargetError> { |
| 13 | + let mut output = VersionStepOutput::default(); |
| 14 | + |
| 15 | + if !table.contains_key("stats") { |
| 16 | + let mut stats = toml::value::Table::new(); |
| 17 | + stats.insert( |
| 18 | + "persist_path".to_string(), |
| 19 | + toml::Value::String("/etc/clawshell/stats.json".to_string()), |
| 20 | + ); |
| 21 | + table.insert("stats".to_string(), toml::Value::Table(stats)); |
| 22 | + output.applied_steps.push(STEP_ID.to_string()); |
| 23 | + } |
| 24 | + |
| 25 | + Ok(output) |
| 26 | +} |
| 27 | + |
| 28 | +#[cfg(test)] |
| 29 | +mod tests { |
| 30 | + use super::*; |
| 31 | + use crate::migration::core::{AmbiguityResolutionError, AmbiguousChoice, MigrationIssue}; |
| 32 | + |
| 33 | + #[derive(Debug)] |
| 34 | + struct NoopResolver; |
| 35 | + |
| 36 | + impl AmbiguityResolver for NoopResolver { |
| 37 | + fn resolve( |
| 38 | + &mut self, |
| 39 | + _issue: &MigrationIssue, |
| 40 | + ) -> Result<AmbiguousChoice, AmbiguityResolutionError> { |
| 41 | + Ok(AmbiguousChoice::ApplyRecommended) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + #[test] |
| 46 | + fn test_injects_stats_when_missing() { |
| 47 | + let mut table: toml::value::Table = toml::from_str( |
| 48 | + r#" |
| 49 | +[server] |
| 50 | +host = "127.0.0.1" |
| 51 | +[upstream] |
| 52 | +openai_base_url = "https://api.openai.com" |
| 53 | +"#, |
| 54 | + ) |
| 55 | + .unwrap(); |
| 56 | + |
| 57 | + let mut resolver = NoopResolver; |
| 58 | + let output = apply("clawshell", &mut table, &mut resolver).unwrap(); |
| 59 | + assert_eq!(output.applied_steps.len(), 1); |
| 60 | + assert!(output.applied_steps[0].contains("[stats]")); |
| 61 | + |
| 62 | + let stats = table.get("stats").unwrap().as_table().unwrap(); |
| 63 | + assert_eq!( |
| 64 | + stats.get("persist_path").unwrap().as_str().unwrap(), |
| 65 | + "/etc/clawshell/stats.json" |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_skips_when_stats_already_present() { |
| 71 | + let mut table: toml::value::Table = toml::from_str( |
| 72 | + r#" |
| 73 | +[server] |
| 74 | +host = "127.0.0.1" |
| 75 | +[upstream] |
| 76 | +openai_base_url = "https://api.openai.com" |
| 77 | +[stats] |
| 78 | +persist_path = "/custom/path/stats.json" |
| 79 | +"#, |
| 80 | + ) |
| 81 | + .unwrap(); |
| 82 | + |
| 83 | + let mut resolver = NoopResolver; |
| 84 | + let output = apply("clawshell", &mut table, &mut resolver).unwrap(); |
| 85 | + assert!(output.applied_steps.is_empty()); |
| 86 | + |
| 87 | + let stats = table.get("stats").unwrap().as_table().unwrap(); |
| 88 | + assert_eq!( |
| 89 | + stats.get("persist_path").unwrap().as_str().unwrap(), |
| 90 | + "/custom/path/stats.json" |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
0 commit comments