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
94
95
96
97
|
- import dedent from 'dedent';
- import * as fs from 'fs';
- import {
- processPythonFile,
- pythonPromptFunction,
- pythonPromptFunctionLegacy,
- } from '../../../src/prompts/processors/python';
- jest.mock('fs');
- jest.mock('../../../src/prompts/processors/python', () => {
- const actual = jest.requireActual('../../../src/prompts/processors/python');
- return {
- ...actual,
- pythonPromptFunction: jest.fn(),
- pythonPromptFunctionLegacy: jest.fn(),
- };
- });
- describe('processPythonFile', () => {
- const mockReadFileSync = jest.mocked(fs.readFileSync);
- it('should process a valid Python file without a function name or label', () => {
- const filePath = 'file.py';
- const fileContent = 'print("Hello, world!")';
- mockReadFileSync.mockReturnValue(fileContent);
- jest.mocked(pythonPromptFunctionLegacy).mockResolvedValueOnce('mocked result');
- expect(processPythonFile(filePath, {}, undefined)).toEqual([
- {
- function: expect.any(Function),
- label: `${filePath}: ${fileContent}`,
- raw: fileContent,
- },
- ]);
- });
- it('should process a valid Python file with a function name without a label', () => {
- const filePath = 'file.py';
- const fileContent = dedent`
- def testFunction(context):
- print("Hello, world!")`;
- mockReadFileSync.mockReturnValue(fileContent);
- jest.mocked(pythonPromptFunction).mockResolvedValueOnce('mocked result');
- expect(processPythonFile(filePath, {}, 'testFunction')).toEqual([
- {
- function: expect.any(Function),
- raw: fileContent,
- label: `file.py:testFunction`,
- },
- ]);
- });
- it('should process a valid Python file with a label without a function name', () => {
- const filePath = 'file.py';
- const fileContent = 'print("Hello, world!")';
- mockReadFileSync.mockReturnValue(fileContent);
- jest.mocked(pythonPromptFunctionLegacy).mockResolvedValueOnce('mocked result');
- expect(processPythonFile(filePath, { label: 'Label' }, undefined)).toEqual([
- {
- function: expect.any(Function),
- label: `Label`,
- raw: fileContent,
- },
- ]);
- });
- it('should process a valid Python file with a label and function name', () => {
- const filePath = 'file.py';
- const fileContent = dedent`
- def testFunction(context):
- print("Hello, world!")`;
- mockReadFileSync.mockReturnValue(fileContent);
- jest.mocked(pythonPromptFunction).mockResolvedValueOnce('mocked result');
- expect(processPythonFile(filePath, { label: 'Label' }, 'testFunction')).toEqual([
- {
- function: expect.any(Function),
- label: `Label`,
- raw: fileContent,
- },
- ]);
- });
- it('should process a valid Python file with config', () => {
- const filePath = 'file.py';
- const fileContent = 'print("Hello, world!")';
- mockReadFileSync.mockReturnValue(fileContent);
- jest.mocked(pythonPromptFunctionLegacy).mockResolvedValueOnce('mocked result');
- const config = { key: 'value' };
- expect(processPythonFile(filePath, { config }, undefined)).toEqual([
- {
- function: expect.any(Function),
- label: `${filePath}: ${fileContent}`,
- raw: fileContent,
- config: { key: 'value' },
- },
- ]);
- });
- });
|