"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const cache = __importStar(require("../../src/cache"));
const palm_1 = require("../../src/providers/palm");
const vertexUtil = __importStar(require("../../src/providers/vertexUtil"));
jest.mock('../../src/cache', () => ({
    fetchWithCache: jest.fn(),
}));
jest.mock('../../src/logger');
jest.mock('../../src/providers/vertexUtil', () => ({
    maybeCoerceToGeminiFormat: jest.fn(),
}));
describe('PalmChatProvider', () => {
    let provider;
    beforeEach(() => {
        provider = new palm_1.PalmChatProvider('gemini-pro', {
            config: {
                temperature: 0.7,
                maxOutputTokens: 100,
                topP: 0.9,
                topK: 40,
            },
        });
    });
    afterEach(() => {
        jest.clearAllMocks();
    });
    describe('callGemini', () => {
        it('should call the Gemini API and return the response', async () => {
            const mockResponse = {
                data: {
                    candidates: [{ content: { parts: [{ text: 'response text' }] } }],
                },
            };
            jest.mocked(cache.fetchWithCache).mockResolvedValue(mockResponse);
            jest.mocked(vertexUtil.maybeCoerceToGeminiFormat).mockReturnValue({
                contents: [{ role: 'user', parts: [{ text: 'test prompt' }] }],
                coerced: false,
                systemInstruction: undefined,
            });
            const response = await provider.callGemini('test prompt');
            expect(response).toEqual({
                output: 'response text',
            });
            expect(cache.fetchWithCache).toHaveBeenCalledWith(expect.stringContaining('gemini-pro:generateContent'), expect.objectContaining({
                method: 'POST',
                body: expect.stringContaining('"contents":[{"role":"user","parts":[{"text":"test prompt"}]}]'),
            }), expect.any(Number));
        });
        it('should handle system messages correctly', async () => {
            const mockResponse = {
                data: {
                    candidates: [{ content: { parts: [{ text: 'response text' }] } }],
                },
            };
            jest.mocked(cache.fetchWithCache).mockResolvedValue(mockResponse);
            jest.mocked(vertexUtil.maybeCoerceToGeminiFormat).mockReturnValue({
                contents: [
                    { role: 'system', parts: [{ text: 'system instruction' }] },
                    { role: 'user', parts: [{ text: 'user message' }] },
                ],
                coerced: false,
                systemInstruction: undefined,
            });
            const response = await provider.callGemini('test prompt');
            expect(response).toEqual({
                output: 'response text',
            });
            expect(cache.fetchWithCache).toHaveBeenCalledWith(expect.stringContaining('https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key='), expect.objectContaining({
                method: 'POST',
                headers: expect.objectContaining({
                    'Content-Type': 'application/json',
                }),
                body: expect.stringContaining('"contents":[{"role":"system","parts":[{"text":"system instruction"}]},{"role":"user","parts":[{"text":"user message"}]}]'),
            }), expect.any(Number));
        });
        it('should handle API call errors', async () => {
            jest.mocked(cache.fetchWithCache).mockRejectedValue(new Error('API error'));
            const response = await provider.callGemini('test prompt');
            expect(response).toEqual({
                error: 'API call error: Error: API error',
            });
        });
        it('should handle response schema', async () => {
            provider = new palm_1.PalmChatProvider('gemini-pro', {
                config: {
                    responseSchema: '{"type":"object","properties":{"name":{"type":"string"}}}',
                },
            });
            const mockResponse = {
                data: {
                    candidates: [{ content: { parts: [{ text: '{"name":"John"}' }] } }],
                },
            };
            jest.mocked(cache.fetchWithCache).mockResolvedValue(mockResponse);
            jest.mocked(vertexUtil.maybeCoerceToGeminiFormat).mockReturnValue({
                contents: [{ role: 'user', parts: [{ text: 'test prompt' }] }],
                coerced: false,
                systemInstruction: undefined,
            });
            await provider.callGemini('test prompt');
            expect(cache.fetchWithCache).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
                body: expect.any(String),
            }), expect.any(Number));
        });
    });
});
//# sourceMappingURL=palm.test.js.map