Skip to content

Commit e254379

Browse files
committed
test(qwen): cover the session --until boundary through the CLI
The hand-rolled Qwen unit test replayed load, summarize, and filter itself, so it would keep passing if `qwen session` stopped applying the window. Replace it with an integration test that runs the real binary with `--since`/`--until` in America/Los_Angeles, asserts the JSON report, and snapshots the session table.
1 parent 745b3e6 commit e254379

3 files changed

Lines changed: 89 additions & 30 deletions

File tree

‎rust/adapters/qwen/src/lib.rs‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,6 @@ mod tests {
9292
assert_eq!(entries[0].extra_total_tokens, 10);
9393
}
9494

95-
#[test]
96-
fn qwen_session_report_keeps_sessions_last_active_on_the_until_day() {
97-
let fixture = fs_fixture!({
98-
// 2026-02-24T02:00Z is still 2026-02-23 in America/Los_Angeles (UTC-8).
99-
"projects/myProject/chats/until-day.jsonl": r#"{"type":"assistant","model":"qwen3-coder-plus","timestamp":"2026-02-24T02:00:00.000Z","sessionId":"until-day","usageMetadata":{"promptTokenCount":100,"candidatesTokenCount":50}}"#,
100-
"projects/myProject/chats/after-day.jsonl": r#"{"type":"assistant","model":"qwen3-coder-plus","timestamp":"2026-02-24T18:00:00.000Z","sessionId":"after-day","usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":5}}"#,
101-
});
102-
103-
let shared = SharedArgs {
104-
mode: CostMode::Display,
105-
timezone: Some("America/Los_Angeles".to_string()),
106-
since: Some("20260223".to_string()),
107-
until: Some("20260223".to_string()),
108-
..SharedArgs::default()
109-
};
110-
let _guard = EnvVarGuard::set("QWEN_DATA_DIR", fixture.root());
111-
let entries = load_entries(&shared).unwrap();
112-
let mut rows = summarize_entries(&entries, AgentReportKind::Session).unwrap();
113-
filter_session_summaries_by_last_activity(&mut rows, &shared);
114-
let report = report_from_rows(&rows, AgentReportKind::Session);
115-
116-
assert_eq!(report["sessions"].as_array().map(Vec::len), Some(1));
117-
assert_eq!(report["sessions"][0]["sessionId"], "until-day");
118-
assert_eq!(
119-
report["sessions"][0]["lastActivity"],
120-
"2026-02-24T02:00:00.000Z"
121-
);
122-
assert_eq!(report["totals"]["totalTokens"], 150);
123-
}
124-
12595
#[test]
12696
fn builds_qwen_daily_json_report_with_reasoning_in_total() {
12797
let fixture = fs_fixture!({
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use ccusage_test_support::{Fixture, fs_fixture};
2+
use serde_json::Value;
3+
4+
/// Two sessions in America/Los_Angeles (UTC-8): the first is last active at
5+
/// 2026-02-24T02:00Z, still 2026-02-23 locally, and the second a day later.
6+
fn qwen_fixture() -> Fixture {
7+
fs_fixture!({
8+
"qwen/projects/myProject/chats/until-day.jsonl": r#"{"type":"assistant","model":"qwen3-coder-plus","timestamp":"2026-02-24T02:00:00.000Z","sessionId":"until-day","usageMetadata":{"promptTokenCount":100,"candidatesTokenCount":50}}"#,
9+
"qwen/projects/myProject/chats/after-day.jsonl": r#"{"type":"assistant","model":"qwen3-coder-plus","timestamp":"2026-02-24T18:00:00.000Z","sessionId":"after-day","usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":5}}"#,
10+
})
11+
}
12+
13+
const WINDOW_ARGS: [&str; 12] = [
14+
"qwen",
15+
"session",
16+
"--since",
17+
"20260223",
18+
"--until",
19+
"20260223",
20+
"--timezone",
21+
"America/Los_Angeles",
22+
"--mode",
23+
"display",
24+
"--offline",
25+
"--no-color",
26+
];
27+
28+
#[test]
29+
fn qwen_session_window_keeps_sessions_last_active_on_the_until_day() {
30+
let fixture = qwen_fixture();
31+
32+
let json_args = WINDOW_ARGS
33+
.iter()
34+
.copied()
35+
.chain(["--json"])
36+
.collect::<Vec<_>>();
37+
let report: Value = serde_json::from_str(&run_cli(&fixture, &json_args)).unwrap();
38+
39+
assert_eq!(report["sessions"].as_array().map(Vec::len), Some(1));
40+
assert_eq!(report["sessions"][0]["sessionId"], "until-day");
41+
assert_eq!(
42+
report["sessions"][0]["lastActivity"],
43+
"2026-02-24T02:00:00.000Z"
44+
);
45+
assert_eq!(report["totals"]["totalTokens"], 150);
46+
47+
insta::assert_snapshot!(
48+
"qwen_cli_session_window_table",
49+
run_cli(&fixture, &WINDOW_ARGS)
50+
);
51+
}
52+
53+
fn run_cli(fixture: &Fixture, args: &[&str]) -> String {
54+
let output = std::process::Command::new(env!("CARGO_BIN_EXE_ccusage"))
55+
.env_clear()
56+
.env("HOME", fixture.path("home"))
57+
.env("USERPROFILE", fixture.path("userprofile"))
58+
.env("XDG_CONFIG_HOME", fixture.path("xdg-config"))
59+
.env("QWEN_DATA_DIR", fixture.path("qwen"))
60+
.env("NO_COLOR", "1")
61+
.env("COLUMNS", "240")
62+
.args(args)
63+
.output()
64+
.expect("failed to run ccusage");
65+
assert!(
66+
output.status.success(),
67+
"ccusage {args:?} failed: {}",
68+
String::from_utf8_lossy(&output.stderr)
69+
);
70+
String::from_utf8(output.stdout).expect("CLI stdout was not UTF-8")
71+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
source: crates/ccusage/tests/qwen_cli.rs
3+
expression: "run_cli(&fixture, &WINDOW_ARGS)"
4+
---
5+
6+
╭────────────────────────────────────────────╮
7+
│ │
8+
│ Qwen Token Usage Report │
9+
│ │
10+
╰────────────────────────────────────────────╯
11+
12+
┌───────────┬────────────────────┬───────────┬───────────┬───────────────┬─────────────┬───────────────┬─────────────┬───────────────┐
13+
│ Session │ Models │ Input │ Output │ Cache Create │ Cache Read │ Total Tokens │ Cost (USD) │ Last Activity │
14+
├───────────┼────────────────────┼───────────┼───────────┼───────────────┼─────────────┼───────────────┼─────────────┼───────────────┤
15+
│ until-day │ - qwen3-coder-plus │ 100 │ 50 │ 0 │ 0 │ 150 │ $0.00 │ 2026-02-24 │
16+
├───────────┼────────────────────┼───────────┼───────────┼───────────────┼─────────────┼───────────────┼─────────────┼───────────────┤
17+
│ Total │ │ 100 │ 50 │ 0 │ 0 │ 150 │ $0.00 │ │
18+
└───────────┴────────────────────┴───────────┴───────────┴───────────────┴─────────────┴───────────────┴─────────────┴───────────────┘

0 commit comments

Comments
 (0)