/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { showAuthStatus } from './handler.js';
import { AuthType } from '@hoptrendy/hopcode-core';
import { CODING_PLAN_ENV_KEY } from '../../constants/codingPlan.js';
vi.mock('../../config/settings.js', () => ({
    loadSettings: vi.fn(),
}));
vi.mock('../../utils/stdioHelpers.js', () => ({
    writeStdoutLine: vi.fn(),
    writeStderrLine: vi.fn(),
}));
import { loadSettings } from '../../config/settings.js';
import { writeStdoutLine, writeStderrLine } from '../../utils/stdioHelpers.js';
describe('showAuthStatus', () => {
    beforeEach(() => {
        vi.clearAllMocks();
        vi.spyOn(process, 'exit').mockImplementation((() => undefined));
        delete process.env[CODING_PLAN_ENV_KEY];
        delete process.env['OPENROUTER_API_KEY'];
    });
    afterEach(() => {
        vi.restoreAllMocks();
        delete process.env[CODING_PLAN_ENV_KEY];
        delete process.env['OPENROUTER_API_KEY'];
    });
    const createMockSettings = (merged) => ({
        merged,
        system: { settings: {}, path: '/system.json' },
        systemDefaults: { settings: {}, path: '/system-defaults.json' },
        user: { settings: {}, path: '/user.json' },
        workspace: { settings: {}, path: '/workspace.json' },
        forScope: vi.fn(),
        setValue: vi.fn(),
        isTrusted: true,
    });
    it('should show message when no authentication is configured', async () => {
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({}));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('No authentication method configured'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('qwen auth openrouter'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('qwen auth qwen-oauth'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('qwen auth coding-plan'));
        expect(process.exit).toHaveBeenCalledWith(0);
    });
    it('should show Qwen OAuth status when configured', async () => {
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.HOPCODE_OAUTH,
                },
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('Qwen OAuth'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('Free tier (discontinued 2026-04-15)'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('No longer available'));
        expect(process.exit).toHaveBeenCalledWith(0);
    });
    it('should show Coding Plan status when configured with API key', async () => {
        process.env[CODING_PLAN_ENV_KEY] = 'test-api-key';
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.USE_OPENAI,
                },
            },
            codingPlan: {
                region: 'china',
                version: 'abc123def456',
            },
            model: {
                name: 'qwen3.5-plus',
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('Alibaba Cloud Coding Plan'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('API key configured'));
        expect(process.exit).toHaveBeenCalledWith(0);
    });
    it('should show OpenRouter status when configured with API key', async () => {
        process.env['OPENROUTER_API_KEY'] = 'test-openrouter-key';
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.USE_OPENAI,
                },
            },
            model: {
                name: 'openai/gpt-4o-mini',
            },
            modelProviders: {
                [AuthType.USE_OPENAI]: [
                    {
                        id: 'openai/gpt-4o-mini',
                        name: 'OpenRouter · GPT-4o mini',
                        baseUrl: 'https://openrouter.ai/api/v1',
                        envKey: 'OPENROUTER_API_KEY',
                    },
                ],
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('OpenRouter'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('openai/gpt-4o-mini'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('API key configured'));
        expect(process.exit).toHaveBeenCalledWith(0);
    });
    it('should show OpenRouter as incomplete when API key is missing', async () => {
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.USE_OPENAI,
                },
            },
            modelProviders: {
                [AuthType.USE_OPENAI]: [
                    {
                        id: 'openai/gpt-4o-mini',
                        name: 'OpenRouter · GPT-4o mini',
                        baseUrl: 'https://openrouter.ai/api/v1',
                        envKey: 'OPENROUTER_API_KEY',
                    },
                ],
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('OpenRouter (Incomplete)'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('qwen auth openrouter'));
    });
    it('should show Coding Plan as incomplete when API key is missing', async () => {
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.USE_OPENAI,
                },
            },
            codingPlan: {
                region: 'global',
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('Incomplete'));
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('API key not found'));
    });
    it('should show Coding Plan region for china', async () => {
        process.env[CODING_PLAN_ENV_KEY] = 'test-api-key';
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.USE_OPENAI,
                },
            },
            codingPlan: {
                region: 'china',
            },
            model: {
                name: 'qwen3.5-plus',
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('中国 (China)'));
    });
    it('should show Coding Plan region for global', async () => {
        process.env[CODING_PLAN_ENV_KEY] = 'test-api-key';
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.USE_OPENAI,
                },
            },
            codingPlan: {
                region: 'global',
            },
            model: {
                name: 'qwen3-coder-plus',
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('Global'));
    });
    it('should show current model name', async () => {
        process.env[CODING_PLAN_ENV_KEY] = 'test-api-key';
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.USE_OPENAI,
                },
            },
            codingPlan: {
                region: 'china',
            },
            model: {
                name: 'qwen3.5-plus',
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('qwen3.5-plus'));
    });
    it('should show config version (truncated)', async () => {
        process.env[CODING_PLAN_ENV_KEY] = 'test-api-key';
        vi.mocked(loadSettings).mockReturnValue(createMockSettings({
            security: {
                auth: {
                    selectedType: AuthType.USE_OPENAI,
                },
            },
            codingPlan: {
                region: 'china',
                version: 'abc123def456789',
            },
            model: {
                name: 'qwen3.5-plus',
            },
        }));
        await showAuthStatus();
        expect(writeStdoutLine).toHaveBeenCalledWith(expect.stringContaining('abc123de...'));
    });
    it('should handle errors and exit with code 1', async () => {
        const error = new Error('Settings load failed');
        vi.mocked(loadSettings).mockImplementation(() => {
            throw error;
        });
        await showAuthStatus();
        expect(writeStderrLine).toHaveBeenCalledWith(expect.stringContaining('Failed to check authentication status'));
        expect(process.exit).toHaveBeenCalledWith(1);
    });
});
//# sourceMappingURL=status.test.js.map