Skip to content

Commit 154db13

Browse files
committed
test(usage): cover tier preservation when settings omit the tier
The loader covered explicit tier transitions and unclassified rollouts, but not the contract that a thread_settings_applied event carrying no service_tier key leaves the recorded tier in place. Codex emits exactly that shape for auto-review threads, so the gap sat on the case the behaviour exists for. The fixture also pins the opposite case in the same sequence: a tier that is present but unrecognized still clears the recorded value.
1 parent 3f35031 commit 154db13

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

  • rust/crates/ccusage/src/adapter/codex

‎rust/crates/ccusage/src/adapter/codex/loader.rs‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,77 @@ mod tests {
258258
assert_eq!(events[3].service_tier, Some(crate::CodexServiceTier::Fast));
259259
}
260260

261+
/// Codex emits `thread_settings_applied` without a `service_tier` key for
262+
/// auto-review threads. Such an event says nothing about the tier, so usage
263+
/// after it keeps the tier the rollout already recorded. A tier that is
264+
/// present but unrecognized is the opposite case and clears it.
265+
#[test]
266+
fn keeps_recorded_service_tier_when_a_later_settings_event_omits_it() {
267+
let settings = |timestamp: &str, thread_settings: serde_json::Value| {
268+
json!({
269+
"timestamp": timestamp,
270+
"type": "event_msg",
271+
"payload": {
272+
"type": "thread_settings_applied",
273+
"thread_settings": thread_settings,
274+
},
275+
})
276+
.to_string()
277+
};
278+
let token_count = |timestamp: &str, input_tokens: u64| {
279+
json!({
280+
"timestamp": timestamp,
281+
"type": "event_msg",
282+
"payload": {
283+
"type": "token_count",
284+
"info": {
285+
"model": "gpt-5.6-sol",
286+
"last_token_usage": {
287+
"input_tokens": input_tokens,
288+
"cached_input_tokens": 0,
289+
"output_tokens": 1,
290+
"total_tokens": input_tokens + 1,
291+
},
292+
},
293+
},
294+
})
295+
.to_string()
296+
};
297+
let fixture = fs_fixture!({
298+
"session.jsonl": [
299+
settings("2026-07-22T00:00:00.000Z", json!({"service_tier": "priority"})),
300+
token_count("2026-07-22T00:00:01.000Z", 10),
301+
// Auto-review thread settings: no service_tier key at all.
302+
settings("2026-07-22T00:00:02.000Z", json!({"model": "codex-auto-review"})),
303+
token_count("2026-07-22T00:00:03.000Z", 20),
304+
// Recognized tier again, then an unrecognized one that clears it.
305+
settings("2026-07-22T00:00:04.000Z", json!({"service_tier": "standard"})),
306+
token_count("2026-07-22T00:00:05.000Z", 30),
307+
settings("2026-07-22T00:00:06.000Z", json!({"service_tier": "flex"})),
308+
token_count("2026-07-22T00:00:07.000Z", 40),
309+
]
310+
.join("\n"),
311+
});
312+
313+
let events = load_codex_events_from_directory(fixture.root(), true).unwrap();
314+
315+
assert_eq!(events.len(), 4);
316+
assert_eq!(events[0].service_tier, Some(crate::CodexServiceTier::Fast));
317+
assert_eq!(
318+
events[1].service_tier,
319+
Some(crate::CodexServiceTier::Fast),
320+
"an omitted service_tier must not clear the recorded tier"
321+
);
322+
assert_eq!(
323+
events[2].service_tier,
324+
Some(crate::CodexServiceTier::Standard)
325+
);
326+
assert_eq!(
327+
events[3].service_tier, None,
328+
"an unrecognized service_tier must clear the recorded tier"
329+
);
330+
}
331+
261332
#[test]
262333
fn leaves_unmarked_and_unsupported_service_tiers_unclassified() {
263334
let fixture = fs_fixture!({

0 commit comments

Comments
 (0)