import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
vi.mock('@anthropic-ai/sdk', () => ({
    default: vi.fn().mockImplementation(() => ({
        messages: {
            create: vi.fn().mockResolvedValue({
                content: [{ type: 'text', text: 'Anthropic response text' }],
            }),
        },
    })),
}));
vi.mock('openai', () => ({
    default: vi.fn().mockImplementation(() => ({
        chat: {
            completions: {
                create: vi.fn().mockResolvedValue({
                    choices: [{ message: { content: 'OpenAI response text' } }],
                }),
            },
        },
    })),
}));
import { runWithAnthropic } from '../anthropic-runner.js';
import { runWithOpenAI } from '../openai-runner.js';
import { runWithProvider } from '../provider-runner.js';
describe('runWithAnthropic', () => {
    beforeEach(() => {
        process.env.ANTHROPIC_API_KEY = 'test-key';
    });
    afterEach(() => {
        delete process.env.ANTHROPIC_API_KEY;
    });
    it('returns text from Anthropic response', async () => {
        const result = await runWithAnthropic('Do something', 'claude-haiku-4-5-20251001');
        expect(result).toBe('Anthropic response text');
    });
    it('uses default model when none specified', async () => {
        const result = await runWithAnthropic('Do something');
        expect(typeof result).toBe('string');
    });
    it('throws if ANTHROPIC_API_KEY not set', async () => {
        delete process.env.ANTHROPIC_API_KEY;
        await expect(runWithAnthropic('test')).rejects.toThrow(/ANTHROPIC_API_KEY/);
    });
});
describe('runWithOpenAI', () => {
    let originalKey;
    beforeEach(() => {
        originalKey = process.env.OPENAI_API_KEY;
        process.env.OPENAI_API_KEY = 'test-key';
    });
    afterEach(() => {
        if (originalKey === undefined)
            delete process.env.OPENAI_API_KEY;
        else
            process.env.OPENAI_API_KEY = originalKey;
    });
    it('returns text from OpenAI response', async () => {
        const result = await runWithOpenAI('Do something', 'gpt-4o');
        expect(result).toBe('OpenAI response text');
    });
    it('throws if OPENAI_API_KEY not set', async () => {
        delete process.env.OPENAI_API_KEY;
        await expect(runWithOpenAI('test')).rejects.toThrow(/OPENAI_API_KEY/);
    });
});
describe('runWithProvider', () => {
    const mockClaudeCode = vi.fn().mockResolvedValue('claude-code result');
    const baseAgent = { id: 'a', name: 'A', icon: 'robot', prompt: '' };
    beforeEach(() => {
        process.env.ANTHROPIC_API_KEY = 'test-key';
        process.env.OPENAI_API_KEY = 'test-key';
    });
    it('calls anthropic runner for provider: anthropic', async () => {
        const result = await runWithProvider('prompt', { ...baseAgent, provider: 'anthropic', model: 'claude-haiku-4-5-20251001' }, '/cwd', mockClaudeCode);
        expect(result).toBe('Anthropic response text');
    });
    it('calls openai runner for provider: openai', async () => {
        const result = await runWithProvider('prompt', { ...baseAgent, provider: 'openai', model: 'gpt-4o' }, '/cwd', mockClaudeCode);
        expect(result).toBe('OpenAI response text');
    });
    it('calls spawnClaudeCode for provider: claude-code (default)', async () => {
        const result = await runWithProvider('prompt', baseAgent, '/cwd', mockClaudeCode);
        expect(mockClaudeCode).toHaveBeenCalledWith('prompt', '/cwd');
        expect(result).toBe('claude-code result');
    });
});
