Skip to content

Commit 057be6b

Browse files
committed
test(claude): cover the TTL split of equal duplicates in CLI JSON
Assert `cacheCreation1hTokens` and `totalCost` for `claude daily` and `claude session` in calculate mode when one copy of a message carries the 5m/1h split and the other does not, in both orders. On main, daily already kept the split through its cost comparison while session kept the unsplit copy, so session reported no 1h tokens and a lower cost. Requested by Cubic on #2060.
1 parent 95ebe37 commit 057be6b

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎rust/crates/ccusage/tests/claude_cli.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,3 +1127,37 @@ fn breakdowns_report_one_hour_cache_creation_tokens_only_for_models_with_a_split
11271127
);
11281128
}
11291129
}
1130+
1131+
#[test]
1132+
fn breakdowns_keep_the_one_hour_split_from_an_otherwise_equal_duplicate() {
1133+
let unsplit = r#"{"timestamp":"2026-09-15T12:00:00.000Z","sessionId":"session-a","requestId":"request-a","message":{"id":"message-a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":2,"cache_creation_input_tokens":400}}}"#;
1134+
let split = r#"{"timestamp":"2026-09-15T12:00:00.000Z","sessionId":"session-a","requestId":"request-a","message":{"id":"message-a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":2,"cache_creation_input_tokens":400,"cache_creation":{"ephemeral_5m_input_tokens":100,"ephemeral_1h_input_tokens":300}}}}"#;
1135+
1136+
for lines in [[unsplit, split], [split, unsplit]] {
1137+
let fixture = Fixture::new();
1138+
let _ = fixture.write_file("projects/project-a/session-a/chat.jsonl", lines.join("\n"));
1139+
1140+
let mut costs = Vec::new();
1141+
for (report, pointer) in [("daily", "/daily/0"), ("session", "/sessions/0")] {
1142+
let args = [
1143+
"claude",
1144+
report,
1145+
"--json",
1146+
"--offline",
1147+
"--mode",
1148+
"calculate",
1149+
"--timezone",
1150+
"UTC",
1151+
];
1152+
let json: serde_json::Value =
1153+
serde_json::from_str(&run_ccusage(&fixture, &args, None)).unwrap();
1154+
let row = json.pointer(pointer).unwrap();
1155+
let breakdown = &row["modelBreakdowns"][0];
1156+
1157+
assert_eq!(breakdown["cacheCreationTokens"], 400, "{args:?}: {row}");
1158+
assert_eq!(breakdown["cacheCreation1hTokens"], 300, "{args:?}: {row}");
1159+
costs.push(row["totalCost"].as_f64().unwrap());
1160+
}
1161+
assert_eq!(costs[0], costs[1], "daily and session cost: {costs:?}");
1162+
}
1163+
}

0 commit comments

Comments
 (0)