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

transform.test.ts 13 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  1. import * as path from 'path';
  2. import logger from '../../src/logger';
  3. import { runPython } from '../../src/python/pythonUtils';
  4. import { TransformInputType, transform } from '../../src/util/transform';
  5. jest.mock('../../src/esm');
  6. jest.mock('../../src/logger', () => ({
  7. __esModule: true,
  8. default: {
  9. error: jest.fn(),
  10. warn: jest.fn(),
  11. info: jest.fn(),
  12. debug: jest.fn(),
  13. },
  14. }));
  15. jest.mock('../../src/python/pythonUtils', () => ({
  16. runPython: jest.fn().mockImplementation(async (filePath, functionName, args) => {
  17. const [output] = args;
  18. return output.toUpperCase() + ' FROM PYTHON';
  19. }),
  20. }));
  21. jest.mock('fs', () => ({
  22. unlink: jest.fn(),
  23. }));
  24. jest.mock('glob', () => ({
  25. globSync: jest.fn(),
  26. }));
  27. jest.mock('../../src/database', () => ({
  28. getDb: jest.fn(),
  29. }));
  30. describe('util', () => {
  31. beforeEach(() => {
  32. jest.clearAllMocks();
  33. });
  34. describe('transform', () => {
  35. afterEach(() => {
  36. jest.clearAllMocks();
  37. jest.resetModules();
  38. });
  39. it('transforms output using a direct function', async () => {
  40. const output = 'original output';
  41. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  42. const transformFunction = 'output.toUpperCase()';
  43. const transformedOutput = await transform(transformFunction, output, context);
  44. expect(transformedOutput).toBe('ORIGINAL OUTPUT');
  45. });
  46. it('transforms vars using a direct function', async () => {
  47. const vars = { key: 'value' };
  48. const context = { vars: {}, prompt: { id: '123' } };
  49. const transformFunction = 'JSON.stringify(vars)';
  50. const transformedOutput = await transform(
  51. transformFunction,
  52. vars,
  53. context,
  54. true,
  55. TransformInputType.VARS,
  56. );
  57. expect(transformedOutput).toBe('{"key":"value"}');
  58. });
  59. it('transforms output using an imported function from a file', async () => {
  60. const output = 'hello';
  61. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  62. jest.doMock(path.resolve('transform.js'), () => (output: string) => output.toUpperCase(), {
  63. virtual: true,
  64. });
  65. const transformFunctionPath = 'file://transform.js';
  66. const transformedOutput = await transform(transformFunctionPath, output, context);
  67. expect(transformedOutput).toBe('HELLO');
  68. });
  69. it('transforms vars using a direct function from a file', async () => {
  70. const vars = { key: 'value' };
  71. const context = { vars: {}, prompt: {} };
  72. jest.doMock(
  73. path.resolve('transform.js'),
  74. () => (vars: any) => ({ ...vars, key: 'transformed' }),
  75. {
  76. virtual: true,
  77. },
  78. );
  79. const transformFunctionPath = 'file://transform.js';
  80. const transformedOutput = await transform(
  81. transformFunctionPath,
  82. vars,
  83. context,
  84. true,
  85. TransformInputType.VARS,
  86. );
  87. expect(transformedOutput).toEqual({ key: 'transformed' });
  88. });
  89. it('throws error if transform function does not return a value', async () => {
  90. const output = 'test';
  91. const context = { vars: {}, prompt: {} };
  92. const transformFunction = ''; // Empty function, returns undefined
  93. await expect(transform(transformFunction, output, context)).rejects.toThrow(
  94. 'Transform function did not return a value',
  95. );
  96. });
  97. it('throws error if file does not export a function', async () => {
  98. const output = 'test';
  99. const context = { vars: {}, prompt: {} };
  100. jest.doMock(path.resolve('transform.js'), () => 'banana', { virtual: true });
  101. const transformFunctionPath = 'file://transform.js';
  102. await expect(transform(transformFunctionPath, output, context)).rejects.toThrow(
  103. 'Transform transform.js must export a function, have a default export as a function, or export the specified function "undefined"',
  104. );
  105. expect(logger.error).toHaveBeenCalledWith(
  106. expect.stringContaining('Error loading transform function from file:'),
  107. );
  108. });
  109. it('transforms output using a Python file', async () => {
  110. const output = 'hello';
  111. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  112. const pythonFilePath = 'file://transform.py';
  113. const transformedOutput = await transform(pythonFilePath, output, context);
  114. expect(transformedOutput).toBe('HELLO FROM PYTHON');
  115. });
  116. it('throws error for unsupported file format', async () => {
  117. const output = 'test';
  118. const context = { vars: {}, prompt: {} };
  119. const unsupportedFilePath = 'file://transform.txt';
  120. await expect(transform(unsupportedFilePath, output, context)).rejects.toThrow(
  121. 'Unsupported transform file format: file://transform.txt',
  122. );
  123. expect(logger.error).toHaveBeenCalledWith(
  124. expect.stringContaining('Error loading transform function from file:'),
  125. );
  126. });
  127. it('transforms output using a multi-line function', async () => {
  128. const output = 'hello';
  129. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  130. const multiLineFunction = `
  131. const uppercased = output.toUpperCase();
  132. return uppercased + ' WORLD';
  133. `;
  134. const transformedOutput = await transform(multiLineFunction, output, context);
  135. expect(transformedOutput).toBe('HELLO WORLD');
  136. });
  137. it('transforms output using a default export function from a file', async () => {
  138. const output = 'hello';
  139. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  140. jest.doMock(
  141. path.resolve('transform.js'),
  142. () => ({
  143. default: (output: string) => output.toUpperCase() + ' DEFAULT',
  144. }),
  145. { virtual: true },
  146. );
  147. const transformFunctionPath = 'file://transform.js';
  148. const transformedOutput = await transform(transformFunctionPath, output, context);
  149. expect(transformedOutput).toBe('HELLO DEFAULT');
  150. });
  151. it('transforms output using a named function from a JavaScript file', async () => {
  152. const output = 'hello';
  153. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  154. jest.doMock(
  155. path.resolve('transform.js'),
  156. () => ({
  157. namedFunction: (output: string) => output.toUpperCase() + ' NAMED',
  158. }),
  159. { virtual: true },
  160. );
  161. const transformFunctionPath = 'file://transform.js:namedFunction';
  162. const transformedOutput = await transform(transformFunctionPath, output, context);
  163. expect(transformedOutput).toBe('HELLO NAMED');
  164. });
  165. it('transforms output using a named function from a Python file', async () => {
  166. const output = 'hello';
  167. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  168. const pythonFilePath = 'file://transform.py:custom_transform';
  169. const transformedOutput = await transform(pythonFilePath, output, context);
  170. expect(transformedOutput).toBe('HELLO FROM PYTHON');
  171. expect(runPython).toHaveBeenCalledWith(
  172. expect.stringContaining('transform.py'),
  173. 'custom_transform',
  174. [output, expect.any(Object)],
  175. );
  176. });
  177. it('falls back to get_transform for Python files when no function name is provided', async () => {
  178. const output = 'hello';
  179. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  180. const pythonFilePath = 'file://transform.py';
  181. const transformedOutput = await transform(pythonFilePath, output, context);
  182. expect(transformedOutput).toBe('HELLO FROM PYTHON');
  183. expect(runPython).toHaveBeenCalledWith(
  184. expect.stringContaining('transform.py'),
  185. 'get_transform',
  186. [output, expect.any(Object)],
  187. );
  188. });
  189. it('does not throw error when validateReturn is false and function returns undefined', async () => {
  190. const output = 'test';
  191. const context = { vars: {}, prompt: {} };
  192. const transformFunction = ''; // Empty function, returns undefined
  193. const result = await transform(transformFunction, output, context, false);
  194. expect(result).toBeUndefined();
  195. });
  196. it('throws error when validateReturn is true and function returns undefined', async () => {
  197. const output = 'test';
  198. const context = { vars: {}, prompt: {} };
  199. const transformFunction = ''; // Empty function, returns undefined
  200. await expect(transform(transformFunction, output, context, true)).rejects.toThrow(
  201. 'Transform function did not return a value',
  202. );
  203. });
  204. it('does not throw error when validateReturn is false and function returns null', async () => {
  205. const output = 'test';
  206. const context = { vars: {}, prompt: {} };
  207. const transformFunction = 'null'; // Will be wrapped with "return" automatically
  208. const result = await transform(transformFunction, output, context, false);
  209. expect(result).toBeNull();
  210. });
  211. it('throws error when validateReturn is true and function returns null', async () => {
  212. const output = 'test';
  213. const context = { vars: {}, prompt: {} };
  214. const transformFunction = 'null'; // Will be wrapped with "return" automatically
  215. await expect(transform(transformFunction, output, context, true)).rejects.toThrow(
  216. 'Transform function did not return a value',
  217. );
  218. });
  219. it('handles file transform function errors gracefully', async () => {
  220. const output = 'test';
  221. const context = { vars: {}, prompt: {} };
  222. const errorMessage = 'File not found';
  223. jest.doMock(
  224. path.resolve('transform.js'),
  225. () => {
  226. throw new Error(errorMessage);
  227. },
  228. { virtual: true },
  229. );
  230. const transformFunctionPath = 'file://transform.js';
  231. await expect(transform(transformFunctionPath, output, context)).rejects.toThrow(errorMessage);
  232. expect(logger.error).toHaveBeenCalledWith(
  233. `Error loading transform function from file: ${errorMessage}`,
  234. );
  235. });
  236. it('handles inline transform function errors gracefully', async () => {
  237. const output = 'test';
  238. const context = { vars: {}, prompt: {} };
  239. const invalidFunction = 'invalid javascript code {';
  240. await expect(transform(invalidFunction, output, context)).rejects.toThrow(
  241. 'Unexpected identifier',
  242. );
  243. expect(logger.error).toHaveBeenCalledWith(
  244. expect.stringContaining('Error creating inline transform function:'),
  245. );
  246. });
  247. describe('file path handling', () => {
  248. it('handles absolute paths in transform files', async () => {
  249. const output = 'hello';
  250. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  251. const mockPath = path.resolve('transform.js');
  252. jest.doMock(mockPath, () => (output: string) => output.toUpperCase(), {
  253. virtual: true,
  254. });
  255. const transformFunctionPath = 'file://transform.js';
  256. const transformedOutput = await transform(transformFunctionPath, output, context);
  257. expect(transformedOutput).toBe('HELLO');
  258. });
  259. it('handles file URLs in transform files', async () => {
  260. const output = 'hello';
  261. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  262. const mockPath = path.resolve('transform.js');
  263. jest.doMock(mockPath, () => (output: string) => output.toUpperCase(), {
  264. virtual: true,
  265. });
  266. const transformFunctionPath = 'file://transform.js';
  267. const transformedOutput = await transform(transformFunctionPath, output, context);
  268. expect(transformedOutput).toBe('HELLO');
  269. });
  270. it('handles Python files with absolute paths', async () => {
  271. const output = 'hello';
  272. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  273. const pythonFilePath = 'file://transform.py';
  274. const transformedOutput = await transform(pythonFilePath, output, context);
  275. expect(transformedOutput).toBe('HELLO FROM PYTHON');
  276. expect(runPython).toHaveBeenCalledWith(
  277. expect.stringContaining('transform.py'),
  278. 'get_transform',
  279. [output, expect.any(Object)],
  280. );
  281. });
  282. it('handles complex nested paths', async () => {
  283. const output = 'hello';
  284. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  285. const mockPath = path.resolve('deeply/nested/path/with spaces/transform.js');
  286. jest.doMock(mockPath, () => (output: string) => output.toUpperCase(), {
  287. virtual: true,
  288. });
  289. const transformFunctionPath = 'file://deeply/nested/path/with spaces/transform.js';
  290. const transformedOutput = await transform(transformFunctionPath, output, context);
  291. expect(transformedOutput).toBe('HELLO');
  292. });
  293. it('handles paths with special characters', async () => {
  294. const output = 'hello';
  295. const context = { vars: { key: 'value' }, prompt: { id: '123' } };
  296. const mockPath = path.resolve('path/with-hyphens/and_underscores/transform.js');
  297. jest.doMock(mockPath, () => (output: string) => output.toUpperCase(), {
  298. virtual: true,
  299. });
  300. const transformFunctionPath = 'file://path/with-hyphens/and_underscores/transform.js';
  301. const transformedOutput = await transform(transformFunctionPath, output, context);
  302. expect(transformedOutput).toBe('HELLO');
  303. });
  304. });
  305. });
  306. });
Tip!

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

Comments

Loading...