"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const index_js_1 = require("./index.js");
// Mock logger
vitest_1.vi.mock('../../utils/logger.js', () => ({
    createModuleLogger: () => ({
        info: vitest_1.vi.fn(),
        warn: vitest_1.vi.fn(),
        error: vitest_1.vi.fn(),
        debug: vitest_1.vi.fn(),
    }),
}));
// Mock file-ops for glossary persistence
const mockReadJSON = vitest_1.vi.fn();
const mockAtomicWriteJSON = vitest_1.vi.fn();
vitest_1.vi.mock('../data-store/file-ops.js', () => ({
    readJSON: (...args) => mockReadJSON(...args),
    atomicWriteJSON: (...args) => mockAtomicWriteJSON(...args),
}));
function createMockOpenRouterClient(responseContent = 'A woman on the beach') {
    return {
        chatCompletion: vitest_1.vi.fn().mockResolvedValue({
            id: 'test-id',
            choices: [{ message: { role: 'assistant', content: responseContent }, finish_reason: 'stop' }],
            usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
        }),
    };
}
function createMockDataStore(dataDir = '/tmp/test-data') {
    return { dataDir };
}
(0, vitest_1.describe)('TranslationModule', () => {
    let module;
    let mockClient;
    let mockStore;
    (0, vitest_1.beforeEach)(() => {
        vitest_1.vi.clearAllMocks();
        mockReadJSON.mockResolvedValue(null);
        mockAtomicWriteJSON.mockResolvedValue(undefined);
        mockClient = createMockOpenRouterClient();
        mockStore = createMockDataStore();
        module = new index_js_1.TranslationModule(mockStore, mockClient);
    });
    (0, vitest_1.describe)('translatePrompt', () => {
        (0, vitest_1.it)('should translate a Turkish prompt to English via OpenRouter', async () => {
            const result = await module.translatePrompt('Plajda yürüyen bir kadın');
            (0, vitest_1.expect)(result.original).toBe('Plajda yürüyen bir kadın');
            (0, vitest_1.expect)(result.translated).toBe('A woman on the beach');
            (0, vitest_1.expect)(mockClient.chatCompletion).toHaveBeenCalledOnce();
            const callArgs = mockClient.chatCompletion.mock.calls[0];
            (0, vitest_1.expect)(callArgs[0]).toHaveLength(2);
            (0, vitest_1.expect)(callArgs[0][0].role).toBe('system');
            (0, vitest_1.expect)(callArgs[0][1].role).toBe('user');
            (0, vitest_1.expect)(callArgs[0][1].content).toBe('Plajda yürüyen bir kadın');
            (0, vitest_1.expect)(callArgs[1]).toEqual({ temperature: 0.7, max_tokens: 500 });
        });
        (0, vitest_1.it)('should apply glossary terms before sending to OpenRouter', async () => {
            mockReadJSON.mockResolvedValue({ plaj: 'beach', kadın: 'woman' });
            await module.translatePrompt('plaj ve kadın');
            const callArgs = mockClient.chatCompletion.mock.calls[0];
            // Glossary should have replaced terms in the user message
            (0, vitest_1.expect)(callArgs[0][1].content).toBe('beach ve woman');
        });
        (0, vitest_1.it)('should return original prompt as fallback when OpenRouter fails', async () => {
            mockClient.chatCompletion.mockRejectedValue(new Error('API error'));
            const result = await module.translatePrompt('Plajda yürüyen bir kadın');
            (0, vitest_1.expect)(result.original).toBe('Plajda yürüyen bir kadın');
            (0, vitest_1.expect)(result.translated).toBe('Plajda yürüyen bir kadın');
        });
        (0, vitest_1.it)('should return fallback when OpenRouter returns empty content', async () => {
            mockClient = createMockOpenRouterClient('');
            module = new index_js_1.TranslationModule(mockStore, mockClient);
            const result = await module.translatePrompt('Test prompt');
            (0, vitest_1.expect)(result.original).toBe('Test prompt');
            (0, vitest_1.expect)(result.translated).toBe('Test prompt');
        });
        (0, vitest_1.it)('should return fallback when OpenRouter returns null content', async () => {
            mockClient = createMockOpenRouterClient(null);
            module = new index_js_1.TranslationModule(mockStore, mockClient);
            const result = await module.translatePrompt('Test prompt');
            (0, vitest_1.expect)(result.original).toBe('Test prompt');
            (0, vitest_1.expect)(result.translated).toBe('Test prompt');
        });
    });
    (0, vitest_1.describe)('getGlossary', () => {
        (0, vitest_1.it)('should return empty object when no glossary file exists', async () => {
            mockReadJSON.mockResolvedValue(null);
            const glossary = await module.getGlossary();
            (0, vitest_1.expect)(glossary).toEqual({});
        });
        (0, vitest_1.it)('should return glossary from file', async () => {
            const terms = { plaj: 'beach', 'spor salonu': 'gym' };
            mockReadJSON.mockResolvedValue(terms);
            const glossary = await module.getGlossary();
            (0, vitest_1.expect)(glossary).toEqual(terms);
        });
    });
    (0, vitest_1.describe)('updateGlossary', () => {
        (0, vitest_1.it)('should write the full glossary to file', async () => {
            const terms = { plaj: 'beach', kafe: 'cafe' };
            await module.updateGlossary(terms);
            (0, vitest_1.expect)(mockAtomicWriteJSON).toHaveBeenCalledWith(vitest_1.expect.stringContaining('glossary.json'), terms);
        });
    });
    (0, vitest_1.describe)('addGlossaryTerm', () => {
        (0, vitest_1.it)('should add a term to existing glossary', async () => {
            mockReadJSON.mockResolvedValue({ plaj: 'beach' });
            await module.addGlossaryTerm('kafe', 'cafe');
            (0, vitest_1.expect)(mockAtomicWriteJSON).toHaveBeenCalledWith(vitest_1.expect.stringContaining('glossary.json'), { plaj: 'beach', kafe: 'cafe' });
        });
        (0, vitest_1.it)('should add a term when glossary is empty', async () => {
            mockReadJSON.mockResolvedValue(null);
            await module.addGlossaryTerm('plaj', 'beach');
            (0, vitest_1.expect)(mockAtomicWriteJSON).toHaveBeenCalledWith(vitest_1.expect.stringContaining('glossary.json'), { plaj: 'beach' });
        });
    });
    (0, vitest_1.describe)('removeGlossaryTerm', () => {
        (0, vitest_1.it)('should remove a term from glossary', async () => {
            mockReadJSON.mockResolvedValue({ plaj: 'beach', kafe: 'cafe' });
            await module.removeGlossaryTerm('plaj');
            (0, vitest_1.expect)(mockAtomicWriteJSON).toHaveBeenCalledWith(vitest_1.expect.stringContaining('glossary.json'), { kafe: 'cafe' });
        });
        (0, vitest_1.it)('should handle removing non-existent term gracefully', async () => {
            mockReadJSON.mockResolvedValue({ plaj: 'beach' });
            await module.removeGlossaryTerm('nonexistent');
            (0, vitest_1.expect)(mockAtomicWriteJSON).toHaveBeenCalledWith(vitest_1.expect.stringContaining('glossary.json'), { plaj: 'beach' });
        });
    });
    (0, vitest_1.describe)('applyGlossary', () => {
        (0, vitest_1.it)('should replace Turkish terms with English equivalents', () => {
            const glossary = { plaj: 'beach', kadın: 'woman' };
            const result = module.applyGlossary('plaj ve kadın', glossary);
            (0, vitest_1.expect)(result).toBe('beach ve woman');
        });
        (0, vitest_1.it)('should be case-insensitive', () => {
            const glossary = { plaj: 'beach' };
            const result = module.applyGlossary('PLAJ sahnesinde', glossary);
            (0, vitest_1.expect)(result).toBe('beach sahnesinde');
        });
        (0, vitest_1.it)('should replace longer terms first to avoid partial matches', () => {
            const glossary = { 'spor salonu': 'gym', spor: 'sport' };
            const result = module.applyGlossary('spor salonu ve spor', glossary);
            (0, vitest_1.expect)(result).toBe('gym ve sport');
        });
        (0, vitest_1.it)('should return text unchanged when glossary is empty', () => {
            const result = module.applyGlossary('plajda yürüyen', {});
            (0, vitest_1.expect)(result).toBe('plajda yürüyen');
        });
        (0, vitest_1.it)('should handle special regex characters in terms', () => {
            const glossary = { 'test (özel)': 'test (special)' };
            const result = module.applyGlossary('bu bir test (özel) durum', glossary);
            (0, vitest_1.expect)(result).toBe('bu bir test (special) durum');
        });
    });
});
//# sourceMappingURL=index.test.js.map