2025-07-07 16:45:44 -04:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { Mock } from 'vitest';
|
|
|
|
|
import { vi, describe, it, expect, beforeEach } from 'vitest';
|
2025-07-07 16:45:44 -04:00
|
|
|
import { clearCommand } from './clearCommand.js';
|
|
|
|
|
import { type CommandContext } from './types.js';
|
|
|
|
|
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
|
2025-07-17 07:14:35 -07:00
|
|
|
|
|
|
|
|
// Mock the telemetry service
|
|
|
|
|
vi.mock('@google/gemini-cli-core', async () => {
|
|
|
|
|
const actual = await vi.importActual('@google/gemini-cli-core');
|
|
|
|
|
return {
|
|
|
|
|
...actual,
|
|
|
|
|
uiTelemetryService: {
|
2025-09-18 11:46:56 -07:00
|
|
|
setLastPromptTokenCount: vi.fn(),
|
2025-07-17 07:14:35 -07:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { GeminiClient } from '@google/gemini-cli-core';
|
|
|
|
|
import { uiTelemetryService } from '@google/gemini-cli-core';
|
2025-07-07 16:45:44 -04:00
|
|
|
|
|
|
|
|
describe('clearCommand', () => {
|
|
|
|
|
let mockContext: CommandContext;
|
|
|
|
|
let mockResetChat: ReturnType<typeof vi.fn>;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
mockResetChat = vi.fn().mockResolvedValue(undefined);
|
2025-11-19 09:32:13 -07:00
|
|
|
const mockGetChatRecordingService = vi.fn();
|
2025-07-17 07:14:35 -07:00
|
|
|
vi.clearAllMocks();
|
2025-07-07 16:45:44 -04:00
|
|
|
|
|
|
|
|
mockContext = createMockCommandContext({
|
|
|
|
|
services: {
|
|
|
|
|
config: {
|
|
|
|
|
getGeminiClient: () =>
|
|
|
|
|
({
|
|
|
|
|
resetChat: mockResetChat,
|
2025-11-19 09:32:13 -07:00
|
|
|
getChat: () => ({
|
|
|
|
|
getChatRecordingService: mockGetChatRecordingService,
|
|
|
|
|
}),
|
2025-07-07 16:45:44 -04:00
|
|
|
}) as unknown as GeminiClient,
|
2025-11-19 09:32:13 -07:00
|
|
|
setSessionId: vi.fn(),
|
2025-12-03 09:04:13 -08:00
|
|
|
getEnableHooks: vi.fn().mockReturnValue(false),
|
|
|
|
|
getMessageBus: vi.fn().mockReturnValue(undefined),
|
2026-01-09 22:18:55 +05:30
|
|
|
getHookSystem: vi.fn().mockReturnValue({
|
|
|
|
|
fireSessionEndEvent: vi.fn().mockResolvedValue(undefined),
|
|
|
|
|
fireSessionStartEvent: vi.fn().mockResolvedValue(undefined),
|
|
|
|
|
}),
|
2025-07-07 16:45:44 -04:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-17 07:14:35 -07:00
|
|
|
it('should set debug message, reset chat, reset telemetry, and clear UI when config is available', async () => {
|
2025-07-07 16:45:44 -04:00
|
|
|
if (!clearCommand.action) {
|
|
|
|
|
throw new Error('clearCommand must have an action.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await clearCommand.action(mockContext, '');
|
|
|
|
|
|
|
|
|
|
expect(mockContext.ui.setDebugMessage).toHaveBeenCalledWith(
|
|
|
|
|
'Clearing terminal and resetting chat.',
|
|
|
|
|
);
|
|
|
|
|
expect(mockContext.ui.setDebugMessage).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
|
|
expect(mockResetChat).toHaveBeenCalledTimes(1);
|
2025-09-18 11:46:56 -07:00
|
|
|
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(0);
|
|
|
|
|
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
|
2025-07-07 16:45:44 -04:00
|
|
|
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
|
|
// Check the order of operations.
|
|
|
|
|
const setDebugMessageOrder = (mockContext.ui.setDebugMessage as Mock).mock
|
|
|
|
|
.invocationCallOrder[0];
|
|
|
|
|
const resetChatOrder = mockResetChat.mock.invocationCallOrder[0];
|
2025-07-17 07:14:35 -07:00
|
|
|
const resetTelemetryOrder = (
|
2025-09-18 11:46:56 -07:00
|
|
|
uiTelemetryService.setLastPromptTokenCount as Mock
|
2025-07-17 07:14:35 -07:00
|
|
|
).mock.invocationCallOrder[0];
|
2025-07-07 16:45:44 -04:00
|
|
|
const clearOrder = (mockContext.ui.clear as Mock).mock
|
|
|
|
|
.invocationCallOrder[0];
|
|
|
|
|
|
|
|
|
|
expect(setDebugMessageOrder).toBeLessThan(resetChatOrder);
|
2025-07-17 07:14:35 -07:00
|
|
|
expect(resetChatOrder).toBeLessThan(resetTelemetryOrder);
|
|
|
|
|
expect(resetTelemetryOrder).toBeLessThan(clearOrder);
|
2025-07-07 16:45:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not attempt to reset chat if config service is not available', async () => {
|
|
|
|
|
if (!clearCommand.action) {
|
|
|
|
|
throw new Error('clearCommand must have an action.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nullConfigContext = createMockCommandContext({
|
|
|
|
|
services: {
|
|
|
|
|
config: null,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await clearCommand.action(nullConfigContext, '');
|
|
|
|
|
|
|
|
|
|
expect(nullConfigContext.ui.setDebugMessage).toHaveBeenCalledWith(
|
2025-07-17 07:14:35 -07:00
|
|
|
'Clearing terminal.',
|
2025-07-07 16:45:44 -04:00
|
|
|
);
|
|
|
|
|
expect(mockResetChat).not.toHaveBeenCalled();
|
2025-09-18 11:46:56 -07:00
|
|
|
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(0);
|
|
|
|
|
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
|
2025-07-07 16:45:44 -04:00
|
|
|
expect(nullConfigContext.ui.clear).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
});
|