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

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

Comments

Loading...