1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { statsCommand } from './statsCommand.js';
import { type CommandContext } from './types.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import { MessageType } from '../types.js';
import { formatDuration } from '../utils/formatters.js';
describe('statsCommand', () => {
let mockContext: CommandContext;
const startTime = new Date('2025-07-14T10:00:00.000Z');
const endTime = new Date('2025-07-14T10:00:30.000Z');
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(endTime);
// 1. Create the mock context with all default values
mockContext = createMockCommandContext();
// 2. Directly set the property on the created mock context
mockContext.session.stats.sessionStartTime = startTime;
});
it('should display general session stats when run with no subcommand', () => {
if (!statsCommand.action) throw new Error('Command has no action');
statsCommand.action(mockContext, '');
const expectedDuration = formatDuration(
endTime.getTime() - startTime.getTime(),
);
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
{
type: MessageType.STATS,
duration: expectedDuration,
},
expect.any(Number),
);
});
it('should display model stats when using the "model" subcommand', () => {
const modelSubCommand = statsCommand.subCommands?.find(
(sc) => sc.name === 'model',
);
if (!modelSubCommand?.action) throw new Error('Subcommand has no action');
modelSubCommand.action(mockContext, '');
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
{
type: MessageType.MODEL_STATS,
},
expect.any(Number),
);
});
it('should display tool stats when using the "tools" subcommand', () => {
const toolsSubCommand = statsCommand.subCommands?.find(
(sc) => sc.name === 'tools',
);
if (!toolsSubCommand?.action) throw new Error('Subcommand has no action');
toolsSubCommand.action(mockContext, '');
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
{
type: MessageType.TOOL_STATS,
},
expect.any(Number),
);
});
});
|