2025-07-15 16:10:04 -04:00
|
|
|
/**
|
|
|
|
|
* @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';
|
2025-12-17 09:43:21 -08:00
|
|
|
import type { Config } from '@google/gemini-cli-core';
|
2025-07-15 16:10:04 -04:00
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
2025-12-05 16:12:49 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2025-07-15 16:10:04 -04:00
|
|
|
statsCommand.action(mockContext, '');
|
|
|
|
|
|
|
|
|
|
const expectedDuration = formatDuration(
|
|
|
|
|
endTime.getTime() - startTime.getTime(),
|
|
|
|
|
);
|
2026-01-13 14:15:04 -05:00
|
|
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith({
|
|
|
|
|
type: MessageType.STATS,
|
|
|
|
|
duration: expectedDuration,
|
|
|
|
|
});
|
2025-07-15 16:10:04 -04:00
|
|
|
});
|
|
|
|
|
|
2025-12-17 09:43:21 -08:00
|
|
|
it('should fetch and display quota if config is available', async () => {
|
|
|
|
|
if (!statsCommand.action) throw new Error('Command has no action');
|
|
|
|
|
|
|
|
|
|
const mockQuota = { buckets: [] };
|
|
|
|
|
const mockRefreshUserQuota = vi.fn().mockResolvedValue(mockQuota);
|
|
|
|
|
mockContext.services.config = {
|
|
|
|
|
refreshUserQuota: mockRefreshUserQuota,
|
|
|
|
|
} as unknown as Config;
|
|
|
|
|
|
|
|
|
|
await statsCommand.action(mockContext, '');
|
|
|
|
|
|
|
|
|
|
expect(mockRefreshUserQuota).toHaveBeenCalled();
|
|
|
|
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
quotas: mockQuota,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-15 16:10:04 -04:00
|
|
|
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');
|
|
|
|
|
|
2025-12-05 16:12:49 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2025-07-15 16:10:04 -04:00
|
|
|
modelSubCommand.action(mockContext, '');
|
|
|
|
|
|
2026-01-13 14:15:04 -05:00
|
|
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith({
|
|
|
|
|
type: MessageType.MODEL_STATS,
|
|
|
|
|
});
|
2025-07-15 16:10:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
2025-12-05 16:12:49 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
2025-07-15 16:10:04 -04:00
|
|
|
toolsSubCommand.action(mockContext, '');
|
|
|
|
|
|
2026-01-13 14:15:04 -05:00
|
|
|
expect(mockContext.ui.addItem).toHaveBeenCalledWith({
|
|
|
|
|
type: MessageType.TOOL_STATS,
|
|
|
|
|
});
|
2025-07-15 16:10:04 -04:00
|
|
|
});
|
|
|
|
|
});
|