|
1 | 1 | use std::ffi::OsString; |
2 | 2 |
|
3 | 3 | use ccusage_cli::{ |
4 | | - AgentCommandArgs, AgentReportKind, CliConfig, Command, DailyArgs, SharedArgs, SourceArgs, |
| 4 | + AgentCommandArgs, AgentReportKind, CliConfig, CodexSpeed, Command, DailyArgs, SharedArgs, |
| 5 | + SourceArgs, |
5 | 6 | }; |
6 | 7 | use ccusage_config::ConfigContext; |
7 | 8 | use ccusage_core::BUILT_IN_AGENT_NAMES; |
@@ -33,6 +34,45 @@ pub(crate) fn resolve( |
33 | 34 | }, |
34 | 35 | ); |
35 | 36 | } |
| 37 | + let mut global_source = SourceArgs { |
| 38 | + shared: unified.shared.clone(), |
| 39 | + ..SourceArgs::default() |
| 40 | + }; |
| 41 | + config.apply_agent_args( |
| 42 | + &mut global_source.codex_speed, |
| 43 | + Some(&mut global_source.pi_path), |
| 44 | + Some(&mut global_source.open_claw_path), |
| 45 | + ); |
| 46 | + if global_source.codex_speed != CodexSpeed::Auto { |
| 47 | + unified.sources.insert( |
| 48 | + "codex".to_string(), |
| 49 | + SourceArgs { |
| 50 | + shared: global_source.shared.clone(), |
| 51 | + codex_speed: global_source.codex_speed, |
| 52 | + ..SourceArgs::default() |
| 53 | + }, |
| 54 | + ); |
| 55 | + } |
| 56 | + if let Some(pi_path) = global_source.pi_path { |
| 57 | + unified.sources.insert( |
| 58 | + "pi".to_string(), |
| 59 | + SourceArgs { |
| 60 | + shared: global_source.shared.clone(), |
| 61 | + pi_path: Some(pi_path), |
| 62 | + ..SourceArgs::default() |
| 63 | + }, |
| 64 | + ); |
| 65 | + } |
| 66 | + if let Some(open_claw_path) = global_source.open_claw_path { |
| 67 | + unified.sources.insert( |
| 68 | + "openclaw".to_string(), |
| 69 | + SourceArgs { |
| 70 | + shared: global_source.shared, |
| 71 | + open_claw_path: Some(open_claw_path), |
| 72 | + ..SourceArgs::default() |
| 73 | + }, |
| 74 | + ); |
| 75 | + } |
36 | 76 | for agent in BUILT_IN_AGENT_NAMES { |
37 | 77 | let Some(view) = config.for_source(agent) else { |
38 | 78 | continue; |
@@ -123,6 +163,14 @@ mod tests { |
123 | 163 | } |
124 | 164 | }"#; |
125 | 165 |
|
| 166 | + const GLOBAL_AGENT_CONFIG: &str = r#"{ |
| 167 | + "defaults": { |
| 168 | + "speed": "fast", |
| 169 | + "piPath": "/global/pi/sessions", |
| 170 | + "openClawPath": "/global/openclaw" |
| 171 | + } |
| 172 | + }"#; |
| 173 | + |
126 | 174 | fn try_unified_with_config( |
127 | 175 | args: &[&str], |
128 | 176 | config_contents: &str, |
@@ -177,6 +225,128 @@ mod tests { |
177 | 225 | assert!(!unified.sources.contains_key("amp")); |
178 | 226 | } |
179 | 227 |
|
| 228 | + #[test] |
| 229 | + fn applies_global_agent_defaults_to_relevant_unified_sources_for_each_report() { |
| 230 | + let cases = [ |
| 231 | + ("bare default", &[][..], AgentReportKind::Daily), |
| 232 | + ("daily", &["daily"][..], AgentReportKind::Daily), |
| 233 | + ("weekly", &["weekly"][..], AgentReportKind::Weekly), |
| 234 | + ("monthly", &["monthly"][..], AgentReportKind::Monthly), |
| 235 | + ("session", &["session"][..], AgentReportKind::Session), |
| 236 | + ]; |
| 237 | + |
| 238 | + for (name, args, kind) in cases { |
| 239 | + let unified = unified_with_config(args, GLOBAL_AGENT_CONFIG); |
| 240 | + |
| 241 | + assert_eq!(unified.kind, kind, "{name}"); |
| 242 | + assert_eq!( |
| 243 | + unified |
| 244 | + .sources |
| 245 | + .keys() |
| 246 | + .map(String::as_str) |
| 247 | + .collect::<Vec<_>>(), |
| 248 | + ["codex", "openclaw", "pi"], |
| 249 | + "{name}" |
| 250 | + ); |
| 251 | + |
| 252 | + let codex = &unified.sources["codex"]; |
| 253 | + assert_eq!(codex.codex_speed, CodexSpeed::Fast, "{name}"); |
| 254 | + assert!(codex.pi_path.is_none(), "{name}"); |
| 255 | + assert!(codex.open_claw_path.is_none(), "{name}"); |
| 256 | + |
| 257 | + let pi = &unified.sources["pi"]; |
| 258 | + assert_eq!(pi.pi_path.as_deref(), Some("/global/pi/sessions"), "{name}"); |
| 259 | + assert_eq!(pi.codex_speed, CodexSpeed::Auto, "{name}"); |
| 260 | + assert!(pi.open_claw_path.is_none(), "{name}"); |
| 261 | + |
| 262 | + let openclaw = &unified.sources["openclaw"]; |
| 263 | + assert_eq!( |
| 264 | + openclaw.open_claw_path.as_deref(), |
| 265 | + Some("/global/openclaw"), |
| 266 | + "{name}" |
| 267 | + ); |
| 268 | + assert_eq!(openclaw.codex_speed, CodexSpeed::Auto, "{name}"); |
| 269 | + assert!(openclaw.pi_path.is_none(), "{name}"); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + #[test] |
| 274 | + fn source_agent_sections_override_global_defaults_and_cli_overrides_shared_defaults() { |
| 275 | + let config = r#"{ |
| 276 | + "defaults": { |
| 277 | + "since": "20260101", |
| 278 | + "offline": false, |
| 279 | + "speed": "fast", |
| 280 | + "piPath": "/global/pi/sessions", |
| 281 | + "openClawPath": "/global/openclaw" |
| 282 | + }, |
| 283 | + "codex": { |
| 284 | + "defaults": { "speed": "standard" }, |
| 285 | + "commands": { "daily": { "speed": "flex" } } |
| 286 | + }, |
| 287 | + "pi": { "defaults": { "piPath": "/source/pi/sessions" } }, |
| 288 | + "openclaw": { |
| 289 | + "commands": { "daily": { "openClawPath": "/source/openclaw" } } |
| 290 | + } |
| 291 | + }"#; |
| 292 | + let unified = unified_with_config(&["daily", "--since", "20260401", "--offline"], config); |
| 293 | + |
| 294 | + assert_eq!(unified.shared.since.as_deref(), Some("20260401")); |
| 295 | + assert!(unified.shared.offline); |
| 296 | + assert_eq!( |
| 297 | + unified |
| 298 | + .sources |
| 299 | + .keys() |
| 300 | + .map(String::as_str) |
| 301 | + .collect::<Vec<_>>(), |
| 302 | + ["codex", "openclaw", "pi"] |
| 303 | + ); |
| 304 | + for source in unified.sources.values() { |
| 305 | + assert_eq!(source.shared.since.as_deref(), Some("20260401")); |
| 306 | + assert!(source.shared.offline); |
| 307 | + } |
| 308 | + assert_eq!(unified.sources["codex"].codex_speed, CodexSpeed::Flex); |
| 309 | + assert_eq!( |
| 310 | + unified.sources["pi"].pi_path.as_deref(), |
| 311 | + Some("/source/pi/sessions") |
| 312 | + ); |
| 313 | + assert_eq!( |
| 314 | + unified.sources["openclaw"].open_claw_path.as_deref(), |
| 315 | + Some("/source/openclaw") |
| 316 | + ); |
| 317 | + } |
| 318 | + |
| 319 | + #[test] |
| 320 | + fn applies_global_report_level_agent_options_without_source_sections() { |
| 321 | + let config = r#"{ |
| 322 | + "defaults": { |
| 323 | + "speed": "standard", |
| 324 | + "piPath": "/global/pi/sessions", |
| 325 | + "openClawPath": "/global/openclaw" |
| 326 | + }, |
| 327 | + "commands": { "daily": { "speed": "flex" } } |
| 328 | + }"#; |
| 329 | + let unified = unified_with_config(&["daily"], config); |
| 330 | + |
| 331 | + assert_eq!(unified.sources["codex"].codex_speed, CodexSpeed::Flex); |
| 332 | + assert_eq!( |
| 333 | + unified.sources["pi"].pi_path.as_deref(), |
| 334 | + Some("/global/pi/sessions") |
| 335 | + ); |
| 336 | + assert_eq!( |
| 337 | + unified.sources["openclaw"].open_claw_path.as_deref(), |
| 338 | + Some("/global/openclaw") |
| 339 | + ); |
| 340 | + assert_eq!( |
| 341 | + unified |
| 342 | + .sources |
| 343 | + .keys() |
| 344 | + .map(String::as_str) |
| 345 | + .collect::<Vec<_>>(), |
| 346 | + ["codex", "openclaw", "pi"] |
| 347 | + ); |
| 348 | + } |
| 349 | + |
180 | 350 | #[test] |
181 | 351 | fn keeps_command_line_flags_above_source_sections() { |
182 | 352 | let unified = unified_with_config( |
|
0 commit comments