Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

wrapper.test.ts 2.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. import fs from 'fs';
  2. import { runPython, state, validatePythonPath } from '../../src/python/pythonUtils';
  3. import { runPythonCode } from '../../src/python/wrapper';
  4. jest.mock('../../src/esm');
  5. jest.mock('python-shell');
  6. jest.mock('fs', () => ({
  7. writeFileSync: jest.fn(),
  8. readFileSync: jest.fn(),
  9. unlinkSync: jest.fn(),
  10. }));
  11. jest.mock('../../src/python/pythonUtils', () => {
  12. const originalModule = jest.requireActual('../../src/python/pythonUtils');
  13. return {
  14. ...originalModule,
  15. validatePythonPath: jest.fn(),
  16. runPython: jest.fn(originalModule.runPython),
  17. state: {
  18. cachedPythonPath: '/usr/bin/python3',
  19. },
  20. };
  21. });
  22. describe('wrapper', () => {
  23. beforeAll(() => {
  24. delete process.env.PROMPTFOO_PYTHON;
  25. });
  26. beforeEach(() => {
  27. jest.clearAllMocks();
  28. jest
  29. .mocked(validatePythonPath)
  30. .mockImplementation((pythonPath: string, isExplicit: boolean): Promise<string> => {
  31. state.cachedPythonPath = pythonPath;
  32. return Promise.resolve(pythonPath);
  33. });
  34. });
  35. describe('runPythonCode', () => {
  36. it('should clean up the temporary files after execution', async () => {
  37. const mockWriteFileSync = jest.fn();
  38. const mockUnlinkSync = jest.fn();
  39. const mockRunPython = jest.fn().mockResolvedValue('cleanup test');
  40. jest.spyOn(fs, 'writeFileSync').mockImplementation(mockWriteFileSync);
  41. jest.spyOn(fs, 'unlinkSync').mockImplementation(mockUnlinkSync);
  42. jest.mocked(runPython).mockImplementation(mockRunPython);
  43. await runPythonCode('print("cleanup test")', 'main', []);
  44. expect(mockWriteFileSync).toHaveBeenCalledTimes(1);
  45. expect(mockWriteFileSync).toHaveBeenCalledWith(
  46. expect.stringContaining('temp-python-code-'),
  47. 'print("cleanup test")',
  48. );
  49. expect(mockRunPython).toHaveBeenCalledTimes(1);
  50. expect(mockRunPython).toHaveBeenCalledWith(
  51. expect.stringContaining('temp-python-code-'),
  52. 'main',
  53. [],
  54. );
  55. expect(mockUnlinkSync).toHaveBeenCalledTimes(1);
  56. expect(mockUnlinkSync).toHaveBeenCalledWith(expect.stringContaining('temp-python-code-'));
  57. });
  58. it('should execute Python code from a string and read the output file', async () => {
  59. const mockOutput = { type: 'final_result', data: 'execution result' };
  60. jest.spyOn(fs, 'writeFileSync').mockReturnValue();
  61. jest.spyOn(fs, 'unlinkSync').mockReturnValue();
  62. const mockRunPython = jest.mocked(runPython);
  63. mockRunPython.mockResolvedValue(mockOutput.data);
  64. const code = 'print("Hello, world!")';
  65. const result = await runPythonCode(code, 'main', []);
  66. expect(result).toBe('execution result');
  67. expect(mockRunPython).toHaveBeenCalledWith(expect.stringContaining('.py'), 'main', []);
  68. expect(fs.writeFileSync).toHaveBeenCalledWith(expect.stringContaining('.py'), code);
  69. });
  70. });
  71. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...