Skip to content

Commit 67d204d

Browse files
committed
test(ccusage): cover Gemini CLI fixtures
Address CodeRabbit review feedback for the Gemini CLI adapter follow-up. The all-agent CLI fixture now includes a representative Gemini JSONL log so detection snapshots exercise the new adapter. A direct Gemini daily JSON snapshot verifies command routing, cache-read normalization, and reasoning token JSON output. Gemini path discovery now treats individual recursive file probes as best-effort with byethrow Result handling, so one unreadable data root does not abort detection for other configured roots. The Gemini docs tree also shows both JSON and JSONL files to match parser support.
1 parent c2efa65 commit 67d204d

6 files changed

Lines changed: 162 additions & 31 deletions

File tree

‎apps/ccusage/src/adapter/gemini/paths.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path';
33
import process from 'node:process';
44
import { collectFilesRecursive, hasFileRecursive } from '@ccusage/internal/fs';
55
import { compareStrings } from '@ccusage/internal/sort';
6+
import { Result } from '@praha/byethrow';
67
import { createFixture } from 'fs-fixture';
78
import { getExistingDirectories, normalizePathList } from '../path-list.ts';
89

@@ -15,11 +16,33 @@ export function getGeminiPaths(): string[] {
1516
);
1617
}
1718

19+
async function collectFilesRecursiveSafe(
20+
directoryPath: string,
21+
extension: '.json' | '.jsonl',
22+
): Promise<string[]> {
23+
const result = await Result.try({
24+
try: collectFilesRecursive(directoryPath, { extension }),
25+
catch: (error) => error,
26+
});
27+
return Result.isFailure(result) ? [] : result.value;
28+
}
29+
30+
async function hasFileRecursiveSafe(
31+
directoryPath: string,
32+
extension: '.json' | '.jsonl',
33+
): Promise<boolean> {
34+
const result = await Result.try({
35+
try: hasFileRecursive(directoryPath, { extension }),
36+
catch: (error) => error,
37+
});
38+
return Result.isFailure(result) ? false : result.value;
39+
}
40+
1841
export async function discoverGeminiLogFiles(): Promise<string[]> {
1942
const fileGroups = await Promise.all(
2043
getGeminiPaths().flatMap((geminiPath) => [
21-
collectFilesRecursive(geminiPath, { extension: '.json' }),
22-
collectFilesRecursive(geminiPath, { extension: '.jsonl' }),
44+
collectFilesRecursiveSafe(geminiPath, '.json'),
45+
collectFilesRecursiveSafe(geminiPath, '.jsonl'),
2346
]),
2447
);
2548
return fileGroups.flat().sort(compareStrings);
@@ -28,8 +51,8 @@ export async function discoverGeminiLogFiles(): Promise<string[]> {
2851
export async function detectGeminiLogFiles(): Promise<boolean> {
2952
const results = await Promise.all(
3053
getGeminiPaths().flatMap((geminiPath) => [
31-
hasFileRecursive(geminiPath, { extension: '.json' }),
32-
hasFileRecursive(geminiPath, { extension: '.jsonl' }),
54+
hasFileRecursiveSafe(geminiPath, '.json'),
55+
hasFileRecursiveSafe(geminiPath, '.jsonl'),
3356
]),
3457
);
3558
return results.some(Boolean);

‎apps/ccusage/test/cli-output.test.ts‎

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,33 @@ function createAgentFixtureTree() {
142142
].join('\n'),
143143
},
144144
},
145+
gemini: {
146+
project: {
147+
chats: {
148+
'gemini-session.jsonl': [
149+
JSON.stringify({
150+
sessionId: 'gemini-session',
151+
projectHash: 'project',
152+
startTime: '2026-01-02T00:00:00.000Z',
153+
}),
154+
JSON.stringify({
155+
id: 'gemini-message',
156+
timestamp: '2026-01-02T00:00:00.000Z',
157+
type: 'gemini',
158+
model: 'gemini-test-model',
159+
tokens: {
160+
input: 110,
161+
output: 50,
162+
cached: 10,
163+
thoughts: 5,
164+
tool: 0,
165+
total: 165,
166+
},
167+
}),
168+
].join('\n'),
169+
},
170+
},
171+
},
145172
opencode: {
146173
storage: {
147174
message: {
@@ -401,14 +428,21 @@ describe('ccusage all-agent CLI', () => {
401428
expect.objectContaining({
402429
agent: 'all',
403430
cacheCreationTokens: 80,
404-
cacheReadTokens: 50,
405-
inputTokens: 500,
406-
outputTokens: 250,
431+
cacheReadTokens: 60,
432+
inputTokens: 600,
433+
outputTokens: 300,
407434
period: '2026-01-02',
408-
totalTokens: 870,
435+
totalTokens: 1035,
409436
}),
410437
);
411-
expect(output.daily[0]?.metadata?.agents).toEqual(['amp', 'claude', 'codex', 'opencode', 'pi']);
438+
expect(output.daily[0]?.metadata?.agents).toEqual([
439+
'amp',
440+
'claude',
441+
'codex',
442+
'gemini',
443+
'opencode',
444+
'pi',
445+
]);
412446
});
413447

414448
it('passes agent namespace config to all-agent loaders', async () => {
@@ -444,14 +478,20 @@ describe('ccusage all-agent CLI', () => {
444478
expect.objectContaining({
445479
agent: 'all',
446480
cacheCreationTokens: 80,
447-
cacheReadTokens: 40,
448-
inputTokens: 400,
449-
outputTokens: 200,
481+
cacheReadTokens: 50,
482+
inputTokens: 500,
483+
outputTokens: 250,
450484
period: '2026-01-02',
451-
totalTokens: 720,
485+
totalTokens: 885,
452486
}),
453487
);
454-
expect(output.daily[0]?.metadata?.agents).toEqual(['amp', 'claude', 'opencode', 'pi']);
488+
expect(output.daily[0]?.metadata?.agents).toEqual([
489+
'amp',
490+
'claude',
491+
'gemini',
492+
'opencode',
493+
'pi',
494+
]);
455495
});
456496

457497
it('runs Codex daily JSON through the main ccusage namespace instead of the deprecated standalone wrapper', async () => {
@@ -482,6 +522,42 @@ describe('ccusage all-agent CLI', () => {
482522
]);
483523
});
484524

525+
it('runs Gemini daily JSON through the main ccusage namespace', async () => {
526+
await using fixture = await createFixture(createAgentFixtureTree());
527+
528+
const result = runCcusage(
529+
['gemini', 'daily', '--offline', '--json', '--since', '20260102', '--until', '20260102'],
530+
createAgentCliEnv(fixture.path),
531+
);
532+
533+
expect(result.status).toBe(0);
534+
expect(result.stderr).toBe('');
535+
const stdout = getStdout(result).replace(/\n$/u, '');
536+
await mkdir(snapshotRoot, { recursive: true });
537+
await expect(stdout).toMatchFileSnapshot(
538+
path.join(snapshotRoot, 'gemini-direct-daily-json.txt'),
539+
);
540+
541+
const output = JSON.parse(stdout) as {
542+
daily: Array<{
543+
inputTokens: number;
544+
outputTokens: number;
545+
cacheReadTokens: number;
546+
totalTokens: number;
547+
reasoningTokens?: number;
548+
}>;
549+
};
550+
expect(output.daily).toEqual([
551+
expect.objectContaining({
552+
inputTokens: 100,
553+
outputTokens: 50,
554+
cacheReadTokens: 10,
555+
totalTokens: 165,
556+
reasoningTokens: 5,
557+
}),
558+
]);
559+
});
560+
485561
it('runs Claude daily JSON through the main ccusage namespace while preserving the Claude Code optimized loader output shape', async () => {
486562
await using fixture = await createFixture(createAgentFixtureTree());
487563

@@ -643,10 +719,11 @@ describe('ccusage all-agent CLI', () => {
643719
await mkdir(snapshotRoot, { recursive: true });
644720
await expect(output).toMatchFileSnapshot(path.join(snapshotRoot, 'all-agent-daily-table.txt'));
645721
expect(output).toContain('Coding (Agent) CLI Usage Report - Daily');
646-
expect(output).toContain('Detected: Amp, Claude, Codex, OpenCode, pi-agent');
722+
expect(output).toContain('Detected: Amp, Claude, Codex, Gemini, OpenCode, pi-agent');
647723
expect(output.match(/2026-01-02/gu)).toHaveLength(1);
648724
expect(output).toContain('Amp');
649725
expect(output).toContain('Codex');
726+
expect(output).toContain('Gemini');
650727
expect(output).toContain('OpenCode');
651728
expect(output).toContain('pi-agent');
652729
expect(output).toContain('$');

‎apps/ccusage/test/snapshots/cli-output/all-agent-daily-json.txt‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,33 @@
77
"[pi] claude-opus-4-20250514",
88
"claude-haiku-4-5-20251001",
99
"claude-sonnet-4-20250514",
10+
"gemini-test-model",
1011
"gpt-5"
1112
],
12-
"inputTokens": 500,
13-
"outputTokens": 250,
13+
"inputTokens": 600,
14+
"outputTokens": 300,
1415
"cacheCreationTokens": 80,
15-
"cacheReadTokens": 50,
16-
"totalTokens": 870,
16+
"cacheReadTokens": 60,
17+
"totalTokens": 1035,
1718
"totalCost": 0.25098975,
1819
"metadata": {
1920
"agents": [
2021
"amp",
2122
"claude",
2223
"codex",
24+
"gemini",
2325
"opencode",
2426
"pi"
2527
]
2628
}
2729
}
2830
],
2931
"totals": {
30-
"inputTokens": 500,
31-
"outputTokens": 250,
32+
"inputTokens": 600,
33+
"outputTokens": 300,
3234
"cacheCreationTokens": 80,
33-
"cacheReadTokens": 50,
34-
"totalTokens": 870,
35+
"cacheReadTokens": 60,
36+
"totalTokens": 1035,
3537
"totalCost": 0.25098975
3638
}
3739
}

‎apps/ccusage/test/snapshots/cli-output/all-agent-daily-table.txt‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11

2-
╭────────────────────────────────────────────────────╮
3-
│ │
4-
│ Coding (Agent) CLI Usage Report - Daily │
5-
│ Detected: Amp, Claude, Codex, OpenCode, pi-agent │
6-
│ │
7-
╰────────────────────────────────────────────────────╯
2+
╭────────────────────────────────────────────────────────────╮
3+
│ │
4+
│ Coding (Agent) CLI Usage Report - Daily │
5+
│ Detected: Amp, Claude, Codex, Gemini, OpenCode, pi-agent │
6+
│ │
7+
╰────────────────────────────────────────────────────────────╯
88

99
┌────────────┬────────────┬──────────────┬──────────────┐
1010
│ Date │ Agent │ Total Tokens │ Cost (USD) │
1111
├────────────┼────────────┼──────────────┼──────────────┤
12-
│ 2026-01-02 │ All │ 870 │ $0.25 │
12+
│ 2026-01-02 │ All │ 1,035 │ $0.25 │
1313
├────────────┼────────────┼──────────────┼──────────────┤
1414
│ │ - Amp │ 180 │ $0.00 │
1515
├────────────┼────────────┼──────────────┼──────────────┤
1616
│ │ - Claude │ 180 │ $0.25 │
1717
├────────────┼────────────┼──────────────┼──────────────┤
1818
│ │ - Codex │ 150 │ $0.00 │
1919
├────────────┼────────────┼──────────────┼──────────────┤
20+
│ │ - Gemini │ 165 │ $0.00 │
21+
├────────────┼────────────┼──────────────┼──────────────┤
2022
│ │ - OpenCode │ 180 │ $0.00 │
2123
├────────────┼────────────┼──────────────┼──────────────┤
2224
│ │ - pi-agent │ 180 │ $0.00 │
2325
├────────────┼────────────┼──────────────┼──────────────┤
24-
│ Total │ │ 870 │ $0.25 │
26+
│ Total │ │ 1,035 │ $0.25 │
2527
└────────────┴────────────┴──────────────┴──────────────┘
2628

2729
[ccusage] ℹ Running in Compact Mode
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"daily": [
3+
{
4+
"date": "2026-01-02",
5+
"inputTokens": 100,
6+
"outputTokens": 50,
7+
"cacheCreationTokens": 0,
8+
"cacheReadTokens": 10,
9+
"totalTokens": 165,
10+
"totalCost": 0,
11+
"modelsUsed": [
12+
"gemini-test-model"
13+
],
14+
"reasoningTokens": 5,
15+
"toolTokens": 0
16+
}
17+
],
18+
"totals": {
19+
"inputTokens": 100,
20+
"outputTokens": 50,
21+
"cacheCreationTokens": 0,
22+
"cacheReadTokens": 10,
23+
"totalTokens": 165,
24+
"totalCost": 0
25+
}
26+
}

‎docs/guide/gemini/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ GEMINI_DATA_DIR="$HOME/.gemini/tmp,/backup/gemini/tmp" ccusage gemini daily
3131
~/.gemini/tmp/
3232
└── */
3333
└── chats/
34+
├── *.json
3435
└── *.jsonl
3536
```
3637

0 commit comments

Comments
 (0)