1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
- import * as pythonWrapper from '../src/python/wrapper';
- import { promises as fs } from 'fs';
- import { PythonShell } from 'python-shell';
- jest.mock('../src/esm');
- jest.mock('python-shell');
- describe('Python Wrapper', () => {
- beforeEach(() => {
- jest.clearAllMocks();
- });
- describe('runPython', () => {
- it('should correctly run a Python script with provided arguments', async () => {
- jest.spyOn(fs, 'writeFile').mockResolvedValue();
- jest.spyOn(fs, 'unlink').mockResolvedValue();
- const mockPythonShellRun = PythonShell.run as jest.Mock;
- mockPythonShellRun.mockResolvedValue(['{"type": "final_result", "data": "test result"}']);
- const result = await pythonWrapper.runPython('testScript.py', 'testMethod', [
- 'arg1',
- { key: 'value' },
- ]);
- expect(result).toEqual('test result');
- expect(mockPythonShellRun).toHaveBeenCalledWith('wrapper.py', expect.any(Object));
- expect(mockPythonShellRun.mock.calls[0][1].args).toEqual([
- expect.stringContaining('testScript.py'),
- 'testMethod',
- expect.stringContaining('promptfoo-python-input-json'),
- ]);
- expect(fs.unlink).toHaveBeenCalledTimes(1);
- });
- it('should return an failure reason if the Python script execution fails', async () => {
- const mockPythonShellRun = PythonShell.run as jest.Mock;
- mockPythonShellRun.mockRejectedValue(new Error('Test Error'));
- const result = await pythonWrapper.runPython('testScript.py', 'testMethod', ['arg1']);
- expect(result).toEqual({
- pass: false,
- score: 0,
- reason: 'Failed to execute Python script: Test Error',
- });
- });
- it('should handle Python script returning incorrect result type', async () => {
- jest.spyOn(fs, 'writeFile').mockResolvedValue();
- jest.spyOn(fs, 'unlink').mockResolvedValue();
- const mockPythonShellRun = PythonShell.run as jest.Mock;
- mockPythonShellRun.mockResolvedValue([
- '{"type": "unexpected_result", "data": "test result"}',
- ]);
- await expect(
- pythonWrapper.runPython('testScript.py', 'testMethod', ['arg1']),
- ).rejects.toThrow(
- 'The Python script `call_api` function must return a dict with an `output`',
- );
- });
- });
- describe('runPythonCode', () => {
- it('should execute Python code from a string', async () => {
- const mockPythonShellRun = PythonShell.run as jest.Mock;
- mockPythonShellRun.mockResolvedValue([
- '{"type": "final_result", "data": "execution result"}',
- ]);
- jest.spyOn(fs, 'writeFile').mockResolvedValue();
- const code = 'print("Hello, world!")';
- const result = await pythonWrapper.runPythonCode(code, 'main', []);
- expect(result).toEqual('execution result');
- expect(mockPythonShellRun).toHaveBeenCalledWith('wrapper.py', expect.any(Object));
- expect(fs.writeFile).toHaveBeenCalledWith(expect.stringContaining('.py'), code);
- });
- it('should clean up the temporary file after execution', async () => {
- jest.spyOn(fs, 'writeFile').mockResolvedValue();
- jest.spyOn(fs, 'unlink').mockResolvedValue();
- await pythonWrapper.runPythonCode('print("cleanup test")', 'main', []);
- expect(fs.unlink).toHaveBeenCalledTimes(2);
- });
- });
- });
|