import { describe, expect, it, vi } from 'vitest';
function status(tool, installed) {
    return {
        tool,
        installed,
        executable: installed ? tool : null,
        version: installed ? '1.0.0' : null,
        installing: false,
        error: installed ? null : 'not installed',
    };
}
describe('applyCurrentAiConfigToInstalledTools', () => {
    it('forces current unified AI config and skips uninstalled tools', async () => {
        const applyClaudeCodeConfig = vi.fn(async (config) => config);
        const applyCodexConfig = vi.fn(async (config) => config);
        const applyOpenCodeConfig = vi.fn(async (config) => config);
        const applyAcodeConfig = vi.fn(async (config) => config);
        const { applyCurrentAiConfigToInstalledTools } = await import('./ai-source-apply.js');
        const result = await applyCurrentAiConfigToInstalledTools({
            template: {
                baseUrl: 'https://current.example.com',
                apiKey: 'sk-current',
                model: 'gpt-current',
            },
            requestedSources: {
                claudecode: { env: { ANTHROPIC_BASE_URL: 'https://stale.example.com' } },
                codex: { model: 'stale-codex' },
                opencode: { provider: { default: { options: { baseURL: 'https://stale.example.com/v1' } } } },
            },
            toolStatus: {
                acode: status('acode', true),
                claude: status('claude', true),
                codex: status('codex', false),
                opencode: status('opencode', true),
            },
            aiConfigService: {
                applyAcodeConfig,
                applyClaudeCodeConfig,
                applyCodexConfig,
                applyOpenCodeConfig,
            },
        });
        expect(applyClaudeCodeConfig).toHaveBeenCalledWith({
            env: {
                ANTHROPIC_BASE_URL: 'https://current.example.com',
                ANTHROPIC_AUTH_TOKEN: 'sk-current',
                ANTHROPIC_MODEL: 'gpt-current',
            },
        });
        expect(applyCodexConfig).not.toHaveBeenCalled();
        expect(applyOpenCodeConfig).toHaveBeenCalledWith(expect.objectContaining({
            provider: expect.objectContaining({
                default: expect.objectContaining({
                    model: 'gpt-current',
                    options: expect.objectContaining({
                        baseURL: 'https://current.example.com/v1',
                        apiKey: 'sk-current',
                    }),
                }),
            }),
        }));
        expect(applyAcodeConfig).toHaveBeenCalledOnce();
        expect(result.applied.codex).toEqual([]);
        expect(result.skippedTools).toEqual(['codex']);
    }, 15_000);
    it('applies Claude Code config when the installed tool status uses the claudecode alias', async () => {
        const applyClaudeCodeConfig = vi.fn(async (config) => config);
        const { applyCurrentAiConfigToInstalledTools } = await import('./ai-source-apply.js');
        await applyCurrentAiConfigToInstalledTools({
            template: {
                baseUrl: 'https://current.example.com',
                apiKey: 'sk-current',
                model: 'gpt-current',
            },
            toolStatus: {
                claudecode: status('claudecode', true),
            },
            aiConfigService: {
                applyAcodeConfig: vi.fn(async (config) => config),
                applyClaudeCodeConfig,
                applyCodexConfig: vi.fn(async (config) => config),
                applyOpenCodeConfig: vi.fn(async (config) => config),
            },
        });
        expect(applyClaudeCodeConfig).toHaveBeenCalledOnce();
    });
    it('applies explicit per-tool AI sources when no unified template is present', async () => {
        const applyClaudeCodeConfig = vi.fn(async (config) => config);
        const applyCodexConfig = vi.fn(async (config) => config);
        const applyOpenCodeConfig = vi.fn(async (config) => config);
        const applyAcodeConfig = vi.fn(async (config) => config);
        const { applyCurrentAiConfigToInstalledTools } = await import('./ai-source-apply.js');
        const result = await applyCurrentAiConfigToInstalledTools({
            template: null,
            requestedSources: {
                claudecode: {
                    env: {
                        ANTHROPIC_BASE_URL: 'https://explicit.example.com',
                        ANTHROPIC_AUTH_TOKEN: 'sk-explicit',
                        ANTHROPIC_MODEL: 'claude-explicit',
                    },
                },
                codex: { model: 'gpt-explicit' },
                opencode: {
                    provider: {
                        default: {
                            options: { baseURL: 'https://explicit.example.com/v1', apiKey: 'sk-explicit' },
                        },
                    },
                },
            },
            toolStatus: {
                claude: status('claude', true),
                codex: status('codex', true),
                opencode: status('opencode', true),
            },
            aiConfigService: {
                applyAcodeConfig,
                applyClaudeCodeConfig,
                applyCodexConfig,
                applyOpenCodeConfig,
            },
        });
        expect(applyClaudeCodeConfig).toHaveBeenCalledWith({
            env: {
                ANTHROPIC_BASE_URL: 'https://explicit.example.com',
                ANTHROPIC_AUTH_TOKEN: 'sk-explicit',
                ANTHROPIC_MODEL: 'claude-explicit',
            },
        });
        expect(applyCodexConfig).toHaveBeenCalledWith({ model: 'gpt-explicit' });
        expect(applyOpenCodeConfig).toHaveBeenCalledOnce();
        expect(result.generatedFromUnifiedTemplate).toBe(false);
        expect(result.applied.claudecode).toEqual(['env']);
        expect(result.applied.codex).toEqual(['model']);
        expect(result.applied.opencode).toEqual(['provider']);
    });
});
