"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const globals_1 = require("@jest/globals");
const iterative_1 = __importDefault(require("../../../src/redteam/providers/iterative"));
const mockGetProvider = globals_1.jest.fn();
const mockGetTargetResponse = globals_1.jest.fn();
const mockCheckPenalizedPhrases = globals_1.jest.fn();
globals_1.jest.mock('../../../src/redteam/providers/shared', () => ({
    redteamProviderManager: {
        getProvider: mockGetProvider,
    },
    getTargetResponse: mockGetTargetResponse,
    checkPenalizedPhrases: mockCheckPenalizedPhrases,
}));
globals_1.jest.mock('../../../src/cliState', () => ({
    config: {
        openai: {
            apiKey: 'test-key',
        },
    },
}));
globals_1.jest.mock('../../../src/providers/openai', () => ({
    OpenAiChatCompletionProvider: globals_1.jest.fn().mockImplementation(() => ({
        id: globals_1.jest.fn().mockReturnValue('mock-openai'),
        callApi: globals_1.jest.fn(),
    })),
}));
globals_1.jest.mock('../../../src/providers', () => ({
    loadApiProviders: globals_1.jest.fn().mockImplementation(() => [
        {
            id: () => 'mock-provider',
            callApi: globals_1.jest.fn(),
        },
    ]),
}));
describe('RedteamIterativeProvider', () => {
    let mockRedteamProvider;
    beforeEach(() => {
        globals_1.jest.clearAllMocks();
        mockRedteamProvider = {
            id: globals_1.jest.fn().mockReturnValue('mock-redteam'),
            callApi: globals_1.jest
                .fn()
                .mockImplementation(async (prompt) => {
                const input = JSON.parse(prompt);
                if (Array.isArray(input) && input[0]?.role === 'system') {
                    return {
                        output: JSON.stringify({
                            improvement: 'test improvement',
                            prompt: 'test prompt',
                        }),
                    };
                }
                else if (Array.isArray(input) && input[0]?.content?.includes('on-topic')) {
                    return {
                        output: JSON.stringify({ onTopic: true }),
                    };
                }
                else {
                    return {
                        output: JSON.stringify({
                            currentResponse: { rating: 5, explanation: 'test' },
                            previousBestResponse: { rating: 0, explanation: 'none' },
                        }),
                    };
                }
            }),
        };
        mockGetProvider.mockImplementation(() => Promise.resolve(mockRedteamProvider));
        mockGetTargetResponse.mockImplementation(() => Promise.resolve({
            output: 'mock target response',
        }));
        mockCheckPenalizedPhrases.mockImplementation(() => false);
    });
    describe('constructor', () => {
        it('should throw if injectVar is not provided', () => {
            expect(() => new iterative_1.default({})).toThrow('Expected injectVar to be set');
        });
        it('should create instance with valid config', () => {
            const provider = new iterative_1.default({ injectVar: 'test' });
            expect(provider).toBeInstanceOf(iterative_1.default);
            expect(provider.id()).toBe('promptfoo:redteam:iterative');
        });
        it('should use default numIterations if not provided', () => {
            const provider = new iterative_1.default({ injectVar: 'test' });
            expect(provider['numIterations']).toBe(10);
        });
        it('should use configured numIterations when provided', () => {
            const provider = new iterative_1.default({
                injectVar: 'test',
                numIterations: '5',
            });
            expect(provider['numIterations']).toBe(5);
        });
        it('should use environment variable for numIterations if set', () => {
            process.env.PROMPTFOO_NUM_JAILBREAK_ITERATIONS = '15';
            const provider = new iterative_1.default({ injectVar: 'test' });
            expect(provider['numIterations']).toBe(15);
            delete process.env.PROMPTFOO_NUM_JAILBREAK_ITERATIONS;
        });
    });
});
//# sourceMappingURL=iterative.test.js.map