/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { keyCommand } from './keyCommand.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
describe('keyCommand', () => {
    let mockContext;
    let mockActiveProvider;
    let mockProviderManager;
    let mockConfig;
    beforeEach(() => {
        vi.clearAllMocks();
        // Create mock active provider
        mockActiveProvider = {
            name: 'test-provider',
            setApiKey: vi.fn(),
            isPaidMode: vi.fn().mockReturnValue(true),
        };
        // Create mock provider manager
        mockProviderManager = {
            getActiveProvider: vi.fn().mockReturnValue(mockActiveProvider),
            getActiveProviderName: vi.fn().mockReturnValue('test-provider'),
        };
        // Create mock config with provider manager
        mockConfig = {
            getProviderManager: vi.fn().mockReturnValue(mockProviderManager),
            setEphemeralSetting: vi.fn(),
            refreshAuth: vi.fn(),
            getSettingsService: vi.fn().mockReturnValue(null), // Return null to use fallback behavior
        };
        mockContext = createMockCommandContext({
            services: {
                config: mockConfig,
            },
        });
    });
    it('should set API key when provided', async () => {
        if (!keyCommand.action) {
            throw new Error('keyCommand must have an action.');
        }
        const testApiKey = 'test-api-key-12345';
        const result = await keyCommand.action(mockContext, testApiKey);
        expect(mockActiveProvider.setApiKey).toHaveBeenCalledWith(testApiKey);
        // setEphemeralSetting is no longer called - auth is stored via SettingsService through activeProvider.setApiKey()
        expect(result).toEqual({
            type: 'message',
            messageType: 'info',
            content: `API key updated for provider 'test-provider'\nWARNING: You are now in PAID MODE - API usage will be charged to your account`,
        });
    });
    it('should remove API key when empty string is provided', async () => {
        if (!keyCommand.action) {
            throw new Error('keyCommand must have an action.');
        }
        const result = await keyCommand.action(mockContext, '');
        expect(mockActiveProvider.setApiKey).toHaveBeenCalledWith('');
        // setEphemeralSetting is no longer called - auth is stored via SettingsService through activeProvider.setApiKey()
        expect(result).toEqual({
            type: 'message',
            messageType: 'info',
            content: `API key removed for provider 'test-provider'`,
        });
    });
    it('should remove API key when "none" is provided', async () => {
        if (!keyCommand.action) {
            throw new Error('keyCommand must have an action.');
        }
        const result = await keyCommand.action(mockContext, 'none');
        expect(mockActiveProvider.setApiKey).toHaveBeenCalledWith('');
        // setEphemeralSetting is no longer called - auth is stored via SettingsService through activeProvider.setApiKey()
        expect(result).toEqual({
            type: 'message',
            messageType: 'info',
            content: `API key removed for provider 'test-provider'`,
        });
    });
    it('should handle errors gracefully', async () => {
        if (!keyCommand.action) {
            throw new Error('keyCommand must have an action.');
        }
        // Mock a provider that doesn't support setApiKey
        mockActiveProvider.setApiKey = undefined;
        const result = await keyCommand.action(mockContext, 'invalid-key');
        expect(result).toEqual({
            type: 'message',
            messageType: 'error',
            content: `Provider 'test-provider' does not support API key updates`,
        });
    });
    it('should trigger payment mode check when successful and callback is available', async () => {
        if (!keyCommand.action) {
            throw new Error('keyCommand must have an action.');
        }
        const mockCheckPaymentModeChange = vi.fn();
        const extendedContext = {
            ...mockContext,
            checkPaymentModeChange: mockCheckPaymentModeChange,
        };
        vi.useFakeTimers();
        await keyCommand.action(extendedContext, 'api-key-123');
        expect(mockCheckPaymentModeChange).not.toHaveBeenCalled();
        // Fast-forward the timer
        vi.advanceTimersByTime(100);
        expect(mockCheckPaymentModeChange).toHaveBeenCalledTimes(1);
        vi.useRealTimers();
    });
    it('should not trigger payment mode check when unsuccessful', async () => {
        if (!keyCommand.action) {
            throw new Error('keyCommand must have an action.');
        }
        // Mock a provider that doesn't support setApiKey
        mockActiveProvider.setApiKey = undefined;
        const mockCheckPaymentModeChange = vi.fn();
        const extendedContext = {
            ...mockContext,
            checkPaymentModeChange: mockCheckPaymentModeChange,
        };
        vi.useFakeTimers();
        await keyCommand.action(extendedContext, 'invalid-key');
        vi.advanceTimersByTime(100);
        expect(mockCheckPaymentModeChange).not.toHaveBeenCalled();
        vi.useRealTimers();
    });
});
//# sourceMappingURL=keyCommand.test.js.map