Skip to content

Commit 2bcd6f5

Browse files
LeeYudokclaude
andcommitted
fix(config): surface invalid config date bounds for every command
Date bound errors shared the pi-store error field, whose gate only exposes errors to commands that read named pi stores. Split the two fields so invalid config since/until values are rejected for blocks, statusline, all, and agent reports as well, while pi-store parse errors stay gated to the commands that consume them. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 68431a1 commit 2bcd6f5

1 file changed

Lines changed: 73 additions & 19 deletions

File tree

‎rust/crates/ccusage-config/src/config.rs‎

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ pub struct ConfigContext {
2929
value: Option<Value>,
3030
command: ConfigCommand,
3131
pi_stores: Vec<NamedPiStore>,
32-
error: Option<String>,
32+
pi_store_error: Option<String>,
33+
date_bound_error: Option<String>,
3334
}
3435

3536
impl ConfigContext {
3637
pub fn from_args(args: &[String]) -> Self {
3738
let command = detect_config_command(args);
3839
let value = load_config_value(scan_config_path(args).as_deref());
39-
let (pi_stores, error) = value
40+
let (pi_stores, pi_store_error) = value
4041
.as_ref()
4142
.map(parse_named_pi_stores)
4243
.transpose()
@@ -46,18 +47,26 @@ impl ConfigContext {
4647
value,
4748
command,
4849
pi_stores,
49-
error,
50+
pi_store_error,
51+
date_bound_error: None,
5052
};
51-
if context.error.is_none() {
52-
context.error = context.date_bound_error();
53-
}
53+
context.date_bound_error = context.detect_date_bound_error();
5454
context
5555
}
5656

57+
/// Named pi stores only reach commands that read them, so their parse error
58+
/// stays gated on those commands.
59+
fn active_pi_store_error(&self) -> Option<&str> {
60+
self.command_uses_named_pi_stores()
61+
.then_some(self.pi_store_error.as_deref())
62+
.flatten()
63+
}
64+
5765
/// Config bounds bypass the CLI parser, so they need the same rejection the
5866
/// parser gives `--since` / `--until`. `apply_shared_options` cannot fail,
59-
/// so the check runs here and surfaces through `config_error`.
60-
fn date_bound_error(&self) -> Option<String> {
67+
/// so the check runs here and surfaces through `config_error` for every
68+
/// command that applies shared options, not just the pi-store readers.
69+
fn detect_date_bound_error(&self) -> Option<String> {
6170
self.option_maps().into_iter().find_map(|options| {
6271
let options = SharedOptions::from_map(options);
6372
[("since", options.since), ("until", options.until)]
@@ -389,7 +398,7 @@ fn apply_config_to_shared(shared: &mut SharedArgs, config: &ConfigContext) {
389398
for options in config.option_maps() {
390399
apply_shared_options(shared, SharedOptions::from_map(options));
391400
}
392-
if config.error.is_none() {
401+
if config.pi_store_error.is_none() {
393402
shared.pi_stores = config.pi_stores.clone();
394403
}
395404
}
@@ -509,9 +518,8 @@ fn apply_config_to_agent_args(
509518

510519
impl ccusage_cli::CliConfig for ConfigContext {
511520
fn config_error(&self) -> Option<&str> {
512-
self.command_uses_named_pi_stores()
513-
.then_some(self.error.as_deref())
514-
.flatten()
521+
self.active_pi_store_error()
522+
.or(self.date_bound_error.as_deref())
515523
}
516524

517525
fn apply_shared(&self, shared: &mut SharedArgs) {
@@ -545,7 +553,7 @@ impl ccusage_cli::CliConfig for ConfigContext {
545553
}
546554

547555
fn apply_shared_options(shared: &mut SharedArgs, options: SharedOptions) {
548-
// Invalid bounds are rejected in `ConfigContext::date_bound_error`, so an
556+
// Invalid bounds are rejected in `ConfigContext::detect_date_bound_error`, so an
549557
// unnormalizable value here is left for that error to report.
550558
if let Some(since) = options.since.as_deref().and_then(normalize_date_bound) {
551559
shared.since = Some(since);
@@ -1191,6 +1199,45 @@ mod tests {
11911199
);
11921200
}
11931201

1202+
#[test]
1203+
fn rejects_config_date_bounds_for_commands_without_named_pi_stores() {
1204+
for command in [
1205+
vec!["blocks"],
1206+
vec!["statusline"],
1207+
vec!["all", "daily"],
1208+
vec!["codex", "daily"],
1209+
] {
1210+
let config = config_context_for_command(
1211+
r#"{ "defaults": { "since": "2026-02-30" } }"#,
1212+
&command,
1213+
);
1214+
1215+
assert_eq!(
1216+
config.config_error(),
1217+
Some(
1218+
"Invalid ccusage config: since '2026-02-30' is not a valid date. Expected YYYY-MM-DD or YYYYMMDD"
1219+
),
1220+
"expected {command:?} to reject the invalid config date bound"
1221+
);
1222+
}
1223+
}
1224+
1225+
#[test]
1226+
fn keeps_named_pi_store_errors_gated_to_commands_that_read_them() {
1227+
let raw = r#"{ "pi": { "stores": [{ "name": "omp" }] } }"#;
1228+
1229+
assert_eq!(
1230+
config_context_for_command(raw, &["daily"]).config_error(),
1231+
Some(
1232+
"Invalid ccusage config: pi.stores[0] must contain string fields 'name' and 'path'"
1233+
)
1234+
);
1235+
assert_eq!(
1236+
config_context_for_command(raw, &["blocks"]).config_error(),
1237+
None
1238+
);
1239+
}
1240+
11941241
#[test]
11951242
fn normalizes_valid_config_date_bounds() {
11961243
let config = config_context_from_json(
@@ -1214,18 +1261,25 @@ mod tests {
12141261
report: report.to_string(),
12151262
},
12161263
pi_stores: Vec::new(),
1217-
error: None,
1264+
pi_store_error: None,
1265+
date_bound_error: None,
12181266
}
12191267
}
12201268

12211269
fn config_context_from_json(raw: &str) -> ConfigContext {
1270+
config_context_for_command(raw, &["daily"])
1271+
}
1272+
1273+
fn config_context_for_command(raw: &str, command: &[&str]) -> ConfigContext {
12221274
let fixture = fs_fixture!({
12231275
"ccusage.json": raw,
12241276
});
1225-
ConfigContext::from_args(&[
1226-
"daily".to_string(),
1227-
"--config".to_string(),
1228-
fixture.path("ccusage.json").to_string_lossy().into_owned(),
1229-
])
1277+
let mut args = command
1278+
.iter()
1279+
.map(ToString::to_string)
1280+
.collect::<Vec<String>>();
1281+
args.push("--config".to_string());
1282+
args.push(fixture.path("ccusage.json").to_string_lossy().into_owned());
1283+
ConfigContext::from_args(&args)
12301284
}
12311285
}

0 commit comments

Comments
 (0)