"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cache_1 = require("../../../src/cache");
const constants_1 = require("../../../src/constants");
const logger_1 = __importDefault(require("../../../src/logger"));
const entities_1 = require("../../../src/redteam/extraction/entities");
const remoteGeneration_1 = require("../../../src/redteam/remoteGeneration");
jest.mock('../../../src/logger', () => ({
    error: jest.fn(),
    debug: jest.fn(),
    warn: jest.fn(),
}));
jest.mock('../../../src/cache', () => ({
    fetchWithCache: jest.fn(),
}));
jest.mock('../../../src/envars', () => {
    const originalModule = jest.requireActual('../../../src/envars');
    return {
        ...originalModule,
        getEnvBool: jest.fn(originalModule.getEnvBool),
    };
});
jest.mock('../../../src/redteam/remoteGeneration', () => ({
    ...jest.requireActual('../../../src/redteam/remoteGeneration'),
    getRemoteGenerationUrl: jest.fn().mockReturnValue('https://api.promptfoo.app/task'),
}));
describe('Entities Extractor', () => {
    let provider;
    let originalEnv;
    beforeAll(() => {
        originalEnv = process.env;
    });
    beforeEach(() => {
        process.env = { ...originalEnv };
        delete process.env.PROMPTFOO_REMOTE_GENERATION_URL;
        provider = {
            callApi: jest.fn().mockResolvedValue({ output: 'Entity: Apple\nEntity: Google' }),
            id: jest.fn().mockReturnValue('test-provider'),
        };
        jest.clearAllMocks();
        jest.mocked(remoteGeneration_1.getRemoteGenerationUrl).mockReturnValue('https://api.promptfoo.app/task');
    });
    afterEach(() => {
        process.env = originalEnv;
    });
    it('should use remote generation when enabled', async () => {
        process.env.OPENAI_API_KEY = undefined;
        process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = 'false';
        jest.mocked(cache_1.fetchWithCache).mockResolvedValue({
            data: { task: 'entities', result: ['Apple', 'Google'] },
            status: 200,
            statusText: 'OK',
            cached: false,
        });
        const result = await (0, entities_1.extractEntities)(provider, ['prompt1', 'prompt2']);
        expect(result).toEqual(['Apple', 'Google']);
        expect(cache_1.fetchWithCache).toHaveBeenCalledWith('https://api.promptfoo.app/task', expect.objectContaining({
            method: 'POST',
            body: JSON.stringify({
                task: 'entities',
                prompts: ['prompt1', 'prompt2'],
                version: constants_1.VERSION,
                email: null,
            }),
        }), expect.any(Number), 'json');
    });
    it('should not fall back to local extraction when remote generation fails', async () => {
        process.env.OPENAI_API_KEY = undefined;
        process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = 'false';
        jest.mocked(cache_1.fetchWithCache).mockRejectedValue(new Error('Remote generation failed'));
        const result = await (0, entities_1.extractEntities)(provider, ['prompt1', 'prompt2']);
        expect(result).toEqual([]);
        expect(provider.callApi).not.toHaveBeenCalled();
        expect(logger_1.default.warn).toHaveBeenCalledWith(expect.stringContaining('Error using remote generation'));
    });
    it('should use local extraction when remote generation is disabled', async () => {
        process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = 'true';
        const result = await (0, entities_1.extractEntities)(provider, ['prompt']);
        expect(result).toEqual(['Apple', 'Google']);
        expect(provider.callApi).toHaveBeenCalledWith(expect.stringContaining('prompt'));
        expect(cache_1.fetchWithCache).not.toHaveBeenCalled();
    });
    it('should log debug message when no entities are found', async () => {
        process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = 'true';
        jest.mocked(provider.callApi).mockResolvedValue({ output: 'No entities found' });
        const result = await (0, entities_1.extractEntities)(provider, ['prompt']);
        expect(result).toEqual([]);
    });
});
//# sourceMappingURL=entities.test.js.map