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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
- import * as path from 'path';
- import { runPython } from '../../src/python/pythonUtils';
- import { transform, TransformInputType } from '../../src/util/transform';
- jest.mock('../../src/esm');
- jest.mock('../../src/python/pythonUtils', () => ({
- runPython: jest.fn().mockImplementation(async (filePath, functionName, args) => {
- const [output] = args;
- return output.toUpperCase() + ' FROM PYTHON';
- }),
- }));
- jest.mock('fs', () => ({
- unlink: jest.fn(),
- }));
- jest.mock('glob', () => ({
- globSync: jest.fn(),
- }));
- jest.mock('../../src/database', () => ({
- getDb: jest.fn(),
- }));
- describe('util', () => {
- beforeEach(() => {
- jest.clearAllMocks();
- });
- describe('transform', () => {
- afterEach(() => {
- jest.clearAllMocks();
- jest.resetModules();
- });
- it('transforms output using a direct function', async () => {
- const output = 'original output';
- const context = { vars: { key: 'value' }, prompt: { id: '123' } };
- const transformFunction = 'output.toUpperCase()';
- const transformedOutput = await transform(transformFunction, output, context);
- expect(transformedOutput).toBe('ORIGINAL OUTPUT');
- });
- it('transforms vars using a direct function', async () => {
- const vars = { key: 'value' };
- const context = { vars: {}, prompt: { id: '123' } };
- const transformFunction = 'JSON.stringify(vars)';
- const transformedOutput = await transform(
- transformFunction,
- vars,
- context,
- true,
- TransformInputType.VARS,
- );
- expect(transformedOutput).toBe('{"key":"value"}');
- });
- it('transforms output using an imported function from a file', async () => {
- const output = 'hello';
- const context = { vars: { key: 'value' }, prompt: { id: '123' } };
- jest.doMock(path.resolve('transform.js'), () => (output: string) => output.toUpperCase(), {
- virtual: true,
- });
- const transformFunctionPath = 'file://transform.js';
- const transformedOutput = await transform(transformFunctionPath, output, context);
- expect(transformedOutput).toBe('HELLO');
- });
- it('transforms vars using a direct function from a file', async () => {
- const vars = { key: 'value' };
- const context = { vars: {}, prompt: {} };
- jest.doMock(
- path.resolve('transform.js'),
- () => (vars: any) => ({ ...vars, key: 'transformed' }),
- {
- virtual: true,
- },
- );
- const transformFunctionPath = 'file://transform.js';
- const transformedOutput = await transform(
- transformFunctionPath,
- vars,
- context,
- true,
- TransformInputType.VARS,
- );
- expect(transformedOutput).toEqual({ key: 'transformed' });
- });
- it('throws error if transform function does not return a value', async () => {
- const output = 'test';
- const context = { vars: {}, prompt: {} };
- const transformFunction = ''; // Empty function, returns undefined
- await expect(transform(transformFunction, output, context)).rejects.toThrow(
- 'Transform function did not return a value',
- );
- });
- it('throws error if file does not export a function', async () => {
- const output = 'test';
- const context = { vars: {}, prompt: {} };
- jest.doMock(path.resolve('transform.js'), () => 'banana', { virtual: true });
- const transformFunctionPath = 'file://transform.js';
- await expect(transform(transformFunctionPath, output, context)).rejects.toThrow(
- 'Transform transform.js must export a function, have a default export as a function, or export the specified function "undefined"',
- );
- });
- it('transforms output using a Python file', async () => {
- const output = 'hello';
- const context = { vars: { key: 'value' }, prompt: { id: '123' } };
- const pythonFilePath = 'file://transform.py';
- const transformedOutput = await transform(pythonFilePath, output, context);
- expect(transformedOutput).toBe('HELLO FROM PYTHON');
- });
- it('throws error for unsupported file format', async () => {
- const output = 'test';
- const context = { vars: {}, prompt: {} };
- const unsupportedFilePath = 'file://transform.txt';
- await expect(transform(unsupportedFilePath, output, context)).rejects.toThrow(
- 'Unsupported transform file format: file://transform.txt',
- );
- });
- it('transforms output using a multi-line function', async () => {
- const output = 'hello';
- const context = { vars: { key: 'value' }, prompt: { id: '123' } };
- const multiLineFunction = `
- const uppercased = output.toUpperCase();
- return uppercased + ' WORLD';
- `;
- const transformedOutput = await transform(multiLineFunction, output, context);
- expect(transformedOutput).toBe('HELLO WORLD');
- });
- it('transforms output using a default export function from a file', async () => {
- const output = 'hello';
- const context = { vars: { key: 'value' }, prompt: { id: '123' } };
- jest.doMock(
- path.resolve('transform.js'),
- () => ({
- default: (output: string) => output.toUpperCase() + ' DEFAULT',
- }),
- { virtual: true },
- );
- const transformFunctionPath = 'file://transform.js';
- const transformedOutput = await transform(transformFunctionPath, output, context);
- expect(transformedOutput).toBe('HELLO DEFAULT');
- });
- it('transforms output using a named function from a JavaScript file', async () => {
- const output = 'hello';
- const context = { vars: { key: 'value' }, prompt: { id: '123' } };
- jest.doMock(
- path.resolve('transform.js'),
- () => ({
- namedFunction: (output: string) => output.toUpperCase() + ' NAMED',
- }),
- { virtual: true },
- );
- const transformFunctionPath = 'file://transform.js:namedFunction';
- const transformedOutput = await transform(transformFunctionPath, output, context);
- expect(transformedOutput).toBe('HELLO NAMED');
- });
- it('transforms output using a named function from a Python file', async () => {
- const output = 'hello';
- const context = { vars: { key: 'value' }, prompt: { id: '123' } };
- const pythonFilePath = 'file://transform.py:custom_transform';
- const transformedOutput = await transform(pythonFilePath, output, context);
- expect(transformedOutput).toBe('HELLO FROM PYTHON');
- expect(runPython).toHaveBeenCalledWith(
- expect.stringContaining('transform.py'),
- 'custom_transform',
- [output, expect.any(Object)],
- );
- });
- it('falls back to get_transform for Python files when no function name is provided', async () => {
- const output = 'hello';
- const context = { vars: { key: 'value' }, prompt: { id: '123' } };
- const pythonFilePath = 'file://transform.py';
- const transformedOutput = await transform(pythonFilePath, output, context);
- expect(transformedOutput).toBe('HELLO FROM PYTHON');
- expect(runPython).toHaveBeenCalledWith(
- expect.stringContaining('transform.py'),
- 'get_transform',
- [output, expect.any(Object)],
- );
- });
- it('does not throw error when validateReturn is false and function returns undefined', async () => {
- const output = 'test';
- const context = { vars: {}, prompt: {} };
- const transformFunction = ''; // Empty function, returns undefined
- const result = await transform(transformFunction, output, context, false);
- expect(result).toBeUndefined();
- });
- it('throws error when validateReturn is true and function returns undefined', async () => {
- const output = 'test';
- const context = { vars: {}, prompt: {} };
- const transformFunction = ''; // Empty function, returns undefined
- await expect(transform(transformFunction, output, context, true)).rejects.toThrow(
- 'Transform function did not return a value',
- );
- });
- });
- });
|