"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const pythonUtils_1 = require("../../src/python/pythonUtils");
const wrapper_1 = require("../../src/python/wrapper");
jest.mock('../../src/esm');
jest.mock('../../src/logger');
jest.mock('python-shell');
jest.mock('fs', () => ({
    writeFileSync: jest.fn(),
    readFileSync: jest.fn(),
    unlinkSync: jest.fn(),
}));
jest.mock('../../src/python/pythonUtils', () => {
    const originalModule = jest.requireActual('../../src/python/pythonUtils');
    return {
        ...originalModule,
        validatePythonPath: jest.fn(),
        runPython: jest.fn(originalModule.runPython),
        state: {
            cachedPythonPath: '/usr/bin/python3',
        },
    };
});
describe('wrapper', () => {
    beforeAll(() => {
        delete process.env.PROMPTFOO_PYTHON;
    });
    beforeEach(() => {
        jest.clearAllMocks();
        jest
            .mocked(pythonUtils_1.validatePythonPath)
            .mockImplementation((pythonPath, isExplicit) => {
            pythonUtils_1.state.cachedPythonPath = pythonPath;
            return Promise.resolve(pythonPath);
        });
    });
    describe('runPythonCode', () => {
        it('should clean up the temporary files after execution', async () => {
            const mockWriteFileSync = jest.fn();
            const mockUnlinkSync = jest.fn();
            const mockRunPython = jest.fn().mockResolvedValue('cleanup test');
            jest.spyOn(fs_1.default, 'writeFileSync').mockImplementation(mockWriteFileSync);
            jest.spyOn(fs_1.default, 'unlinkSync').mockImplementation(mockUnlinkSync);
            jest.mocked(pythonUtils_1.runPython).mockImplementation(mockRunPython);
            await (0, wrapper_1.runPythonCode)('print("cleanup test")', 'main', []);
            expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
            expect(mockWriteFileSync).toHaveBeenCalledWith(expect.stringContaining('temp-python-code-'), 'print("cleanup test")');
            expect(mockRunPython).toHaveBeenCalledTimes(1);
            expect(mockRunPython).toHaveBeenCalledWith(expect.stringContaining('temp-python-code-'), 'main', []);
            expect(mockUnlinkSync).toHaveBeenCalledTimes(1);
            expect(mockUnlinkSync).toHaveBeenCalledWith(expect.stringContaining('temp-python-code-'));
        });
        it('should execute Python code from a string and read the output file', async () => {
            const mockOutput = { type: 'final_result', data: 'execution result' };
            jest.spyOn(fs_1.default, 'writeFileSync').mockReturnValue();
            jest.spyOn(fs_1.default, 'unlinkSync').mockReturnValue();
            const mockRunPython = jest.mocked(pythonUtils_1.runPython);
            mockRunPython.mockResolvedValue(mockOutput.data);
            const code = 'print("Hello, world!")';
            const result = await (0, wrapper_1.runPythonCode)(code, 'main', []);
            expect(result).toBe('execution result');
            expect(mockRunPython).toHaveBeenCalledWith(expect.stringContaining('.py'), 'main', []);
            expect(fs_1.default.writeFileSync).toHaveBeenCalledWith(expect.stringContaining('.py'), code);
        });
    });
});
//# sourceMappingURL=wrapper.test.js.map