"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const path_1 = __importDefault(require("path"));
const providers_1 = require("../src/providers");
const http_1 = require("../src/providers/http");
const openai_1 = require("../src/providers/openai");
const pythonCompletion_1 = require("../src/providers/pythonCompletion");
const scriptCompletion_1 = require("../src/providers/scriptCompletion");
const websocket_1 = require("../src/providers/websocket");
jest.mock('fs');
jest.mock('js-yaml');
jest.mock('../src/providers/openai');
jest.mock('../src/providers/http');
jest.mock('../src/providers/websocket');
jest.mock('../src/providers/scriptCompletion');
jest.mock('../src/providers/pythonCompletion');
describe('loadApiProvider', () => {
    beforeEach(() => {
        jest.resetAllMocks();
        jest.spyOn(process, 'exit').mockImplementation((() => { }));
    });
    it('should load echo provider', async () => {
        const provider = await (0, providers_1.loadApiProvider)('echo');
        expect(provider.id()).toBe('echo');
        await expect(provider.callApi('test')).resolves.toEqual({ output: 'test' });
    });
    it('should load file provider from yaml', async () => {
        const yamlContent = {
            id: 'openai:chat:gpt-4',
            config: {
                apiKey: 'test-key',
                temperature: 0.7,
            },
        };
        jest.mocked(fs_1.default.readFileSync).mockReturnValue('yaml content');
        jest.mocked(js_yaml_1.default.load).mockReturnValue(yamlContent);
        const provider = await (0, providers_1.loadApiProvider)('file://test.yaml', {
            basePath: '/test',
        });
        expect(fs_1.default.readFileSync).toHaveBeenCalledWith(path_1.default.join('/test', 'test.yaml'), 'utf8');
        expect(js_yaml_1.default.load).toHaveBeenCalledWith('yaml content');
        expect(provider).toBeDefined();
        expect(openai_1.OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
    });
    it('should load OpenAI chat provider', async () => {
        const provider = await (0, providers_1.loadApiProvider)('openai:chat:gpt-4');
        expect(openai_1.OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
        expect(provider).toBeDefined();
    });
    it('should load HTTP provider', async () => {
        const provider = await (0, providers_1.loadApiProvider)('http://test.com');
        expect(http_1.HttpProvider).toHaveBeenCalledWith('http://test.com', expect.any(Object));
        expect(provider).toBeDefined();
    });
    it('should load WebSocket provider', async () => {
        const provider = await (0, providers_1.loadApiProvider)('ws://test.com');
        expect(websocket_1.WebSocketProvider).toHaveBeenCalledWith('ws://test.com', expect.any(Object));
        expect(provider).toBeDefined();
    });
    it('should load script provider', async () => {
        const provider = await (0, providers_1.loadApiProvider)('exec:test.sh');
        expect(scriptCompletion_1.ScriptCompletionProvider).toHaveBeenCalledWith('test.sh', expect.any(Object));
        expect(provider).toBeDefined();
    });
    it('should load Python provider', async () => {
        const provider = await (0, providers_1.loadApiProvider)('python:test.py');
        expect(pythonCompletion_1.PythonProvider).toHaveBeenCalledWith('test.py', expect.any(Object));
        expect(provider).toBeDefined();
    });
});
describe('loadApiProviders', () => {
    beforeEach(() => {
        jest.resetAllMocks();
    });
    it('should load single provider from string', async () => {
        const providers = await (0, providers_1.loadApiProviders)('echo');
        expect(providers).toHaveLength(1);
        expect(providers[0].id()).toBe('echo');
    });
    it('should load multiple providers from array of strings', async () => {
        const providers = await (0, providers_1.loadApiProviders)(['echo', 'openai:chat:gpt-4']);
        expect(providers).toHaveLength(2);
        expect(providers[0].id()).toBe('echo');
        expect(openai_1.OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
    });
    it('should load provider from function', async () => {
        const customFunction = async (prompt) => ({ output: prompt });
        const providers = await (0, providers_1.loadApiProviders)(customFunction);
        expect(providers).toHaveLength(1);
        expect(providers[0].id()).toBe('custom-function');
        await expect(providers[0].callApi('test')).resolves.toEqual({ output: 'test' });
    });
    it('should load provider from options object', async () => {
        const options = {
            id: 'openai:chat:gpt-4',
            config: {
                apiKey: 'test-key',
            },
        };
        const providers = await (0, providers_1.loadApiProviders)([options]);
        expect(providers).toHaveLength(1);
        expect(openai_1.OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
    });
    it('should throw error for invalid providers list', async () => {
        await expect((0, providers_1.loadApiProviders)({})).rejects.toThrow('Invalid providers list');
    });
});
//# sourceMappingURL=providers.test.js.map