Skip to content

Commit a01d64f

Browse files
unhappychoiceclaude
andcommitted
test(fetcher/codex): cover render_body branches for each shape
skill checklist calls out per-shape branch coverage; the initial cuts relied on `sample_body(*s).is_some()` plus a handful of badge-specific asserts, which didn't actually exercise the render_body match arms. codex_usage: adds direct tests for text / text_block / markdown / entries / bars / number_series — including the empty-state fall-throughs (no rows → "no recent" line, no rows → single placeholder entry). codex_subscription: adds text_block and markdown coverage with their plan-line prefix and empty-snapshot fallbacks. No behavioural change; +12 tests. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 07939bb commit a01d64f

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

‎src/fetcher/codex/subscription.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,44 @@ mod tests {
715715
assert!(t.events[0].title.starts_with("5h"));
716716
}
717717

718+
#[test]
719+
fn text_block_body_includes_plan_line_and_one_per_window() {
720+
let Body::TextBlock(b) = text_block_body(&snap_full()) else {
721+
panic!("expected text_block");
722+
};
723+
assert_eq!(b.lines.len(), 3);
724+
assert!(b.lines[0].contains("pro"));
725+
assert!(b.lines[1].starts_with("5h"));
726+
assert!(b.lines[2].starts_with("7d"));
727+
}
728+
729+
#[test]
730+
fn text_block_body_falls_back_when_no_data() {
731+
let Body::TextBlock(b) = text_block_body(&Snapshot::default()) else {
732+
panic!("expected text_block");
733+
};
734+
assert_eq!(b.lines.len(), 1);
735+
assert!(b.lines[0].contains("n/a"));
736+
}
737+
738+
#[test]
739+
fn markdown_body_emphasises_plan_and_each_window() {
740+
let Body::MarkdownTextBlock(m) = markdown_body(&snap_full()) else {
741+
panic!("expected markdown");
742+
};
743+
assert!(m.value.contains("**plan**: `pro`"));
744+
assert!(m.value.contains("- **5h**"));
745+
assert!(m.value.contains("- **7d**"));
746+
}
747+
748+
#[test]
749+
fn markdown_body_falls_back_when_empty() {
750+
let Body::MarkdownTextBlock(m) = markdown_body(&Snapshot::default()) else {
751+
panic!("expected markdown");
752+
};
753+
assert!(m.value.contains("unavailable"));
754+
}
755+
718756
#[test]
719757
fn status_for_bucketed_thresholds() {
720758
assert_eq!(status_for(0.0), Status::Ok);

‎src/fetcher/codex/usage.rs‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,4 +851,116 @@ mod tests {
851851
assert!(t.value.contains("Today"));
852852
assert!(t.value.contains("3k"), "value was {:?}", t.value);
853853
}
854+
855+
fn snap_with_rows() -> Snapshot {
856+
Snapshot {
857+
rows: vec![
858+
UsageRow {
859+
label: "splashboard".into(),
860+
tokens: 2_100_000,
861+
cost: 7.2,
862+
},
863+
UsageRow {
864+
label: "playground".into(),
865+
tokens: 1_300_000,
866+
cost: 5.14,
867+
},
868+
],
869+
total_cost: 12.34,
870+
total_tokens: 3_400_000,
871+
daily_tokens: vec![0, 100_000, 250_000, 3_400_000],
872+
since_label: "Today",
873+
}
874+
}
875+
876+
#[test]
877+
fn text_body_renders_headline_with_total_tokens_and_cost() {
878+
let Body::Text(t) = text_body(&snap_with_rows()) else {
879+
panic!("expected text");
880+
};
881+
assert!(t.value.starts_with("Today"));
882+
assert!(t.value.contains("3.4M"));
883+
assert!(t.value.contains("$12.34"));
884+
}
885+
886+
#[test]
887+
fn text_block_body_lists_headline_then_one_line_per_row() {
888+
let Body::TextBlock(b) = text_block_body(&snap_with_rows()) else {
889+
panic!("expected text_block");
890+
};
891+
assert_eq!(b.lines.len(), 3);
892+
assert!(b.lines[0].contains("$12.34"));
893+
assert!(b.lines[1].starts_with("splashboard"));
894+
assert!(b.lines[2].starts_with("playground"));
895+
}
896+
897+
#[test]
898+
fn text_block_body_falls_back_to_quiet_line_when_no_activity() {
899+
// No rows AND no tokens at all → the fetcher must surface a placeholder line, otherwise
900+
// a quiet user just sees a bare "Today: no sessions" headline with nothing under it.
901+
let snap = Snapshot {
902+
since_label: "Today",
903+
..Default::default()
904+
};
905+
let Body::TextBlock(b) = text_block_body(&snap) else {
906+
panic!("expected text_block");
907+
};
908+
assert!(b.lines.iter().any(|l| l.contains("no recent")));
909+
}
910+
911+
#[test]
912+
fn markdown_body_emphasises_headline_and_each_row() {
913+
let Body::MarkdownTextBlock(m) = markdown_body(&snap_with_rows()) else {
914+
panic!("expected markdown");
915+
};
916+
assert!(m.value.starts_with("**Today"));
917+
assert!(m.value.contains("- **splashboard**"));
918+
}
919+
920+
#[test]
921+
fn entries_body_falls_back_to_headline_when_no_rows() {
922+
// Mirrors claude_code_usage: entries shape must always emit at least one row so
923+
// grid_table doesn't render a blank box.
924+
let snap = Snapshot {
925+
since_label: "7d",
926+
..Default::default()
927+
};
928+
let Body::Entries(e) = entries_body(&snap) else {
929+
panic!("expected entries");
930+
};
931+
assert_eq!(e.items.len(), 1);
932+
assert_eq!(e.items[0].key, "7d");
933+
}
934+
935+
#[test]
936+
fn entries_body_carries_one_item_per_row_with_token_and_cost_value() {
937+
let Body::Entries(e) = entries_body(&snap_with_rows()) else {
938+
panic!("expected entries");
939+
};
940+
assert_eq!(e.items.len(), 2);
941+
assert_eq!(e.items[0].key, "splashboard");
942+
let value = e.items[0].value.as_deref().unwrap_or_default();
943+
assert!(value.contains("2.1M"));
944+
assert!(value.contains("$"));
945+
}
946+
947+
#[test]
948+
fn bars_body_uses_token_count_as_value_and_cost_string_as_label() {
949+
// chart_bar ranks by the integer value field; list_ranking surfaces the value_label
950+
// verbatim. We want bars to rank by token count and *display* cost.
951+
let Body::Bars(b) = bars_body(&snap_with_rows()) else {
952+
panic!("expected bars");
953+
};
954+
assert_eq!(b.bars.len(), 2);
955+
assert_eq!(b.bars[0].value, 2_100_000);
956+
assert!(b.bars[0].value_label.as_deref().unwrap_or("").contains("$"));
957+
}
958+
959+
#[test]
960+
fn number_series_body_carries_daily_tokens_in_order() {
961+
let Body::NumberSeries(n) = number_series_body(&snap_with_rows()) else {
962+
panic!("expected number_series");
963+
};
964+
assert_eq!(n.values, vec![0, 100_000, 250_000, 3_400_000]);
965+
}
854966
}

0 commit comments

Comments
 (0)