"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cli_progress_1 = require("cli-progress");
const cache_1 = require("../../../src/cache");
const logger_1 = __importDefault(require("../../../src/logger"));
const shared_1 = require("../../../src/redteam/providers/shared");
const remoteGeneration_1 = require("../../../src/redteam/remoteGeneration");
const multilingual_1 = require("../../../src/redteam/strategies/multilingual");
jest.mock('../../../src/logger', () => ({
    debug: jest.fn(),
    error: jest.fn(),
    level: 'info',
}));
jest.mock('../../../src/redteam/remoteGeneration', () => ({
    getRemoteGenerationUrl: jest.fn().mockReturnValue('http://test.url'),
    shouldGenerateRemote: jest.fn().mockReturnValue(false),
}));
jest.mock('../../../src/cache', () => ({
    fetchWithCache: jest.fn().mockResolvedValue({
        data: { result: [] },
    }),
}));
jest.spyOn(shared_1.redteamProviderManager, 'getProvider').mockResolvedValue({
    callApi: jest.fn().mockResolvedValue({ output: '{"translation": "Hallo Welt"}' }),
    config: {},
    getOpenAiBody: jest.fn(),
    modelName: 'test-model',
    id: 'test-provider',
    type: 'openai',
    isAvailable: jest.fn().mockResolvedValue(true),
    validateConfig: jest.fn(),
});
jest.mock('cli-progress', () => ({
    Presets: {
        shades_classic: {},
    },
    SingleBar: jest.fn().mockImplementation(() => ({
        increment: jest.fn(),
        start: jest.fn(),
        stop: jest.fn(),
    })),
}));
describe('addMultilingual', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    afterEach(() => {
        jest.resetModules();
    });
    it('should preserve harmCategory and modify assertion metrics', async () => {
        const testCase = {
            assert: [{ type: 'promptfoo:redteam:contracts' }, { type: 'promptfoo:redteam:harmful' }],
            metadata: {
                harmCategory: 'Illegal Activities - Fraud & scams',
                otherField: 'value',
            },
            vars: {
                text: 'Hello world',
            },
        };
        const result = await (0, multilingual_1.addMultilingual)([testCase], 'text', { languages: ['de'] });
        expect(result[0].metadata).toEqual({
            harmCategory: 'Illegal Activities - Fraud & scams',
            otherField: 'value',
        });
        expect(result[0].assert).toEqual([
            {
                metric: 'contracts/Multilingual-DE',
                type: 'promptfoo:redteam:contracts',
            },
            {
                metric: 'harmful/Multilingual-DE',
                type: 'promptfoo:redteam:harmful',
            },
        ]);
    });
    it('should handle test cases without metadata', async () => {
        const testCase = {
            vars: {
                text: 'Hello world',
            },
        };
        const result = await (0, multilingual_1.addMultilingual)([testCase], 'text', { languages: ['de'] });
        expect(result[0].metadata).toEqual({});
    });
    it('should translate text and update vars correctly', async () => {
        const testCase = {
            vars: {
                otherVar: 'test',
                text: 'Hello world',
            },
        };
        const result = await (0, multilingual_1.addMultilingual)([testCase], 'text', { languages: ['de'] });
        expect(result[0].vars).toEqual({
            otherVar: 'test',
            text: 'Hallo Welt',
        });
    });
    it('should increment progress bar when provided', async () => {
        const testCase = {
            vars: {
                text: 'Hello world',
            },
        };
        logger_1.default.level = 'info';
        await (0, multilingual_1.addMultilingual)([testCase], 'text', { languages: ['de'] });
        const mockBarInstance = jest.mocked(cli_progress_1.SingleBar).mock.results[0].value;
        expect(mockBarInstance.increment).toHaveBeenCalledWith(1);
        expect(mockBarInstance.stop).toHaveBeenCalledWith();
    });
    it('should attempt remote generation first', async () => {
        const testCase = {
            vars: { text: 'Hello world' },
        };
        jest.mocked(remoteGeneration_1.shouldGenerateRemote).mockReturnValueOnce(true);
        jest.mocked(cache_1.fetchWithCache).mockResolvedValueOnce({
            cached: false,
            data: {
                result: [
                    {
                        vars: { text: 'Hallo Welt' },
                    },
                ],
            },
            status: 200,
            statusText: 'OK',
        });
        const result = await (0, multilingual_1.addMultilingual)([testCase], 'text', { languages: ['es'] });
        expect(result).toHaveLength(1);
        expect(result[0].vars?.text).toBe('Hallo Welt');
    });
    it('should preserve harmCategory and modify assertion metrics only for redteam types', async () => {
        const testCase = {
            assert: [{ type: 'promptfoo:redteam:harmful' }, { type: 'moderation' }],
            metadata: {
                harmCategory: 'Illegal Activities - Fraud & scams',
                otherField: 'value',
            },
            vars: {
                text: 'Hello world',
            },
        };
        const result = await (0, multilingual_1.addMultilingual)([testCase], 'text', { languages: ['de'] });
        expect(result[0].metadata).toEqual({
            harmCategory: 'Illegal Activities - Fraud & scams',
            otherField: 'value',
        });
        expect(result[0].assert).toEqual([
            {
                metric: 'harmful/Multilingual-DE',
                type: 'promptfoo:redteam:harmful',
            },
            {
                type: 'moderation',
            },
        ]);
    });
});
//# sourceMappingURL=multilingual.test.js.map