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
98
99
100
101
|
- import { importModule } from '../src/esm';
- import { processJsFile, transformContext } from '../src/prompts/processors/javascript';
- jest.mock('../src/esm', () => ({
- importModule: jest.fn(),
- }));
- describe('transformContext', () => {
- it('should transform context with provider', () => {
- const context = {
- vars: { key: 'value' },
- provider: { id: () => 'providerId', label: 'providerLabel', callApi: jest.fn() },
- };
- const result = transformContext(context);
- expect(result).toEqual({
- vars: { key: 'value' },
- provider: { id: 'providerId', label: 'providerLabel' },
- });
- });
- it('should throw an error if provider is missing', () => {
- const context = { vars: { key: 'value' } };
- expect(() => transformContext(context)).toThrow('Provider is required');
- });
- });
- describe('processJsFile', () => {
- const mockImportModule = jest.mocked(importModule);
- beforeEach(() => {
- jest.clearAllMocks();
- });
- it('should process a valid JavaScript file without a function name or a label', async () => {
- const filePath = 'file.js';
- const mockFunction = jest.fn(() => 'dummy prompt');
- mockImportModule.mockResolvedValue(mockFunction);
- await expect(processJsFile(filePath, {}, undefined)).resolves.toEqual([
- {
- raw: String(mockFunction),
- label: 'file.js',
- function: expect.any(Function),
- },
- ]);
- expect(mockImportModule).toHaveBeenCalledWith(filePath, undefined);
- });
- it('should process a valid JavaScript file with a label without a function name', async () => {
- const filePath = 'file.js';
- const mockFunction = jest.fn(() => 'dummy prompt');
- mockImportModule.mockResolvedValue(mockFunction);
- await expect(processJsFile(filePath, { label: 'myLabel' }, undefined)).resolves.toEqual([
- {
- raw: String(mockFunction),
- label: 'myLabel',
- function: expect.any(Function),
- },
- ]);
- expect(mockImportModule).toHaveBeenCalledWith(filePath, undefined);
- });
- it('should process a valid JavaScript file with a function name without a label', async () => {
- const filePath = 'file.js';
- const functionName = 'myFunction';
- const mockFunction = () => 'dummy prompt';
- mockImportModule.mockResolvedValue(mockFunction);
- await expect(processJsFile(filePath, {}, functionName)).resolves.toEqual([
- {
- raw: String(mockFunction),
- label: 'file.js:myFunction',
- function: expect.any(Function),
- },
- ]);
- expect(mockImportModule).toHaveBeenCalledWith(filePath, functionName);
- });
- it('should process a valid JavaScript file with a label and a function name', async () => {
- const filePath = 'file.js';
- const functionName = 'myFunction';
- const mockFunction = jest.fn(() => 'dummy prompt');
- mockImportModule.mockResolvedValue(mockFunction);
- await expect(processJsFile(filePath, { label: 'myLabel' }, functionName)).resolves.toEqual([
- {
- raw: String(mockFunction),
- label: 'myLabel',
- function: expect.any(Function),
- },
- ]);
- expect(mockImportModule).toHaveBeenCalledWith(filePath, functionName);
- });
- it('should throw an error if the file cannot be imported', async () => {
- const filePath = 'nonexistent.js';
- mockImportModule.mockImplementation(() => {
- throw new Error('File not found');
- });
- await expect(processJsFile(filePath, {}, undefined)).rejects.toThrow('File not found');
- expect(mockImportModule).toHaveBeenCalledWith(filePath, undefined);
- });
- });
|