/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { clearCommand } from './clearCommand.js';
import {} from './types.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
// Mock the telemetry service
vi.mock('@google/gemini-cli-core', async () => {
    const actual = await vi.importActual('@google/gemini-cli-core');
    return {
        ...actual,
        uiTelemetryService: {
            setLastPromptTokenCount: vi.fn(),
        },
    };
});
import { uiTelemetryService } from '@google/gemini-cli-core';
describe('clearCommand', () => {
    let mockContext;
    let mockResetChat;
    beforeEach(() => {
        mockResetChat = vi.fn().mockResolvedValue(undefined);
        const mockGetChatRecordingService = vi.fn();
        vi.clearAllMocks();
        mockContext = createMockCommandContext({
            services: {
                config: {
                    getGeminiClient: () => ({
                        resetChat: mockResetChat,
                        getChat: () => ({
                            getChatRecordingService: mockGetChatRecordingService,
                        }),
                    }),
                    setSessionId: vi.fn(),
                },
            },
        });
    });
    it('should set debug message, reset chat, reset telemetry, and clear UI when config is available', async () => {
        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);
        expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(0);
        expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
        expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
        // Check the order of operations.
        const setDebugMessageOrder = mockContext.ui.setDebugMessage.mock
            .invocationCallOrder[0];
        const resetChatOrder = mockResetChat.mock.invocationCallOrder[0];
        const resetTelemetryOrder = uiTelemetryService.setLastPromptTokenCount.mock.invocationCallOrder[0];
        const clearOrder = mockContext.ui.clear.mock
            .invocationCallOrder[0];
        expect(setDebugMessageOrder).toBeLessThan(resetChatOrder);
        expect(resetChatOrder).toBeLessThan(resetTelemetryOrder);
        expect(resetTelemetryOrder).toBeLessThan(clearOrder);
    });
    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('Clearing terminal.');
        expect(mockResetChat).not.toHaveBeenCalled();
        expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(0);
        expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
        expect(nullConfigContext.ui.clear).toHaveBeenCalledTimes(1);
    });
});
//# sourceMappingURL=clearCommand.test.js.map