"use strict";
/**
 * useAI Hook Tests
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_hooks_1 = require("@testing-library/react-hooks");
const useAI_1 = __importDefault(require("../../hooks/useAI"));
const aiService_mock_1 = __importDefault(require("./aiService.mock"));
const ai_1 = require("../../types/ai");
// Mock the aiService module
jest.mock('../../services/aiService', () => aiService_mock_1.default);
describe('useAI Hook', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    it('should initialize with idle state', () => {
        const { result } = (0, react_hooks_1.renderHook)(() => (0, useAI_1.default)());
        expect(result.current.status).toBe(ai_1.AiOperationStatus.IDLE);
        expect(result.current.isIdle).toBe(true);
        expect(result.current.isLoading).toBe(false);
        expect(result.current.isStreaming).toBe(false);
        expect(result.current.isError).toBe(false);
        expect(result.current.isSuccess).toBe(false);
        expect(result.current.messages).toEqual([]);
        expect(result.current.response).toBeNull();
        expect(result.current.error).toBeNull();
    });
    it('should handle chat requests successfully', async () => {
        const { result, waitForNextUpdate } = (0, react_hooks_1.renderHook)(() => (0, useAI_1.default)());
        const chatRequest = {
            messages: [{ role: 'user', content: 'Hello AI' }]
        };
        // Execute chat request
        let chatPromise;
        (0, react_hooks_1.act)(() => {
            chatPromise = result.current.chat(chatRequest);
        });
        // Check loading state
        expect(result.current.status).toBe(ai_1.AiOperationStatus.LOADING);
        expect(result.current.isLoading).toBe(true);
        // Wait for the request to complete
        await waitForNextUpdate();
        // Wait for the promise to resolve
        await chatPromise;
        // Check success state
        expect(result.current.status).toBe(ai_1.AiOperationStatus.SUCCESS);
        expect(result.current.isSuccess).toBe(true);
        expect(result.current.isLoading).toBe(false);
        expect(aiService_mock_1.default.chat).toHaveBeenCalledWith(expect.objectContaining({
            messages: chatRequest.messages,
            provider: 'anthropic',
            model: 'claude-3-haiku-20240307'
        }), expect.any(Object));
        expect(result.current.messages.length).toBe(2);
        expect(result.current.messages[0].role).toBe('user');
        expect(result.current.messages[1].role).toBe('assistant');
        expect(result.current.response).not.toBeNull();
    });
    it('should handle streaming responses', async () => {
        const { result, waitForNextUpdate } = (0, react_hooks_1.renderHook)(() => (0, useAI_1.default)({
            initialConfig: { stream: true }
        }));
        const chatRequest = {
            messages: [{ role: 'user', content: 'Hello AI' }],
            stream: true
        };
        // Execute chat request
        let chatPromise;
        (0, react_hooks_1.act)(() => {
            chatPromise = result.current.chat(chatRequest);
        });
        // Check streaming state
        expect(result.current.status).toBe(ai_1.AiOperationStatus.STREAMING);
        // Wait for the request to complete
        await waitForNextUpdate();
        // Wait for the promise to resolve
        await chatPromise;
        // Check success state
        expect(result.current.status).toBe(ai_1.AiOperationStatus.SUCCESS);
        expect(aiService_mock_1.default.chat).toHaveBeenCalledWith(expect.objectContaining({ stream: true }), expect.objectContaining({
            onStream: expect.any(Function)
        }));
    });
    it('should handle errors', async () => {
        const onError = jest.fn();
        const { result, waitForNextUpdate } = (0, react_hooks_1.renderHook)(() => (0, useAI_1.default)({ onError }));
        const chatRequest = {
            messages: [{ role: 'user', content: 'trigger error' }]
        };
        // Execute chat request
        let error;
        (0, react_hooks_1.act)(() => {
            result.current.chat(chatRequest).catch(e => {
                error = e;
            });
        });
        // Wait for the request to complete
        await waitForNextUpdate();
        // Check error state
        expect(result.current.status).toBe(ai_1.AiOperationStatus.ERROR);
        expect(result.current.isError).toBe(true);
        expect(result.current.error).not.toBeNull();
        expect(result.current.error.code).toBe('mock_error');
        expect(onError).toHaveBeenCalled();
        expect(error).toBeDefined();
    });
    it('should handle document analysis', async () => {
        const { result, waitForNextUpdate } = (0, react_hooks_1.renderHook)(() => (0, useAI_1.default)());
        const document = 'This is a test document for analysis.';
        // Execute analysis
        let analysisPromise;
        (0, react_hooks_1.act)(() => {
            analysisPromise = result.current.analyzeDocument(document, { detailed: true });
        });
        // Check loading state
        expect(result.current.status).toBe(ai_1.AiOperationStatus.LOADING);
        // Wait for the request to complete
        await waitForNextUpdate();
        // Wait for the promise to resolve
        const analysisResult = await analysisPromise;
        // Check success state
        expect(result.current.status).toBe(ai_1.AiOperationStatus.SUCCESS);
        expect(aiService_mock_1.default.analyzeDocument).toHaveBeenCalledWith(document, { detailed: true });
        expect(analysisResult).toBeDefined();
        expect(analysisResult.summary).toBeDefined();
        expect(analysisResult.keyPoints).toBeDefined();
    });
    it('should handle embeddings generation', async () => {
        const { result } = (0, react_hooks_1.renderHook)(() => (0, useAI_1.default)());
        const texts = ['Embed this text'];
        const model = 'text-embedding-ada-002';
        // Execute embeddings request
        const embeddings = await result.current.generateEmbeddings(texts, model);
        expect(aiService_mock_1.default.embeddings).toHaveBeenCalledWith(texts, model);
        expect(embeddings).toBeDefined();
        expect(embeddings.length).toBe(1);
        expect(embeddings[0].length).toBe(128);
    });
    it('should cancel operations', async () => {
        const { result } = (0, react_hooks_1.renderHook)(() => (0, useAI_1.default)());
        const chatRequest = {
            messages: [{ role: 'user', content: 'Hello AI' }],
            stream: true
        };
        // Start a request
        (0, react_hooks_1.act)(() => {
            result.current.chat(chatRequest).catch(() => { });
        });
        // Cancel the request
        (0, react_hooks_1.act)(() => {
            result.current.cancel();
        });
        // Check state after cancellation
        expect(result.current.status).toBe(ai_1.AiOperationStatus.IDLE);
    });
    it('should reset state', async () => {
        const { result, waitForNextUpdate } = (0, react_hooks_1.renderHook)(() => (0, useAI_1.default)());
        // First perform a successful request
        const chatRequest = {
            messages: [{ role: 'user', content: 'Hello AI' }]
        };
        (0, react_hooks_1.act)(() => {
            result.current.chat(chatRequest);
        });
        await waitForNextUpdate();
        // Then reset the state
        (0, react_hooks_1.act)(() => {
            result.current.reset();
        });
        // Check reset state
        expect(result.current.status).toBe(ai_1.AiOperationStatus.IDLE);
        expect(result.current.messages).toEqual([]);
        expect(result.current.response).toBeNull();
        expect(result.current.error).toBeNull();
    });
});
//# sourceMappingURL=useAI.test.js.map