-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathexecute.test.ts
More file actions
361 lines (317 loc) · 10.4 KB
/
execute.test.ts
File metadata and controls
361 lines (317 loc) · 10.4 KB
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
359
360
361
/**
* @vitest-environment node
*
* Function Execute Tool Unit Tests
*
* This file contains unit tests for the Function Execute tool,
* which runs JavaScript code in a secure sandbox.
*/
import { ToolTester } from '@sim/testing/builders'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
import { functionExecuteTool } from '@/tools/function/execute'
describe('Function Execute Tool', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let tester: ToolTester<any, any>
beforeEach(() => {
tester = new ToolTester(functionExecuteTool as any)
process.env.NEXT_PUBLIC_APP_URL = 'http://localhost:3000'
})
afterEach(() => {
tester.cleanup()
vi.resetAllMocks()
process.env.NEXT_PUBLIC_APP_URL = undefined
})
describe('Request Construction', () => {
it.concurrent('should set correct URL for code execution', () => {
expect(tester.getRequestUrl({})).toBe('/api/function/execute')
})
it.concurrent('should include correct headers for JSON payload', () => {
const headers = tester.getRequestHeaders({
code: 'return 42',
})
expect(headers['Content-Type']).toBe('application/json')
})
it.concurrent('should format single string code correctly', () => {
const body = tester.getRequestBody({
code: 'return 42',
envVars: {},
isCustomTool: false,
timeout: 5000,
workflowId: undefined,
})
expect(body).toEqual({
code: 'return 42',
envVars: {},
workflowVariables: {},
blockData: {},
blockNameMapping: {},
blockOutputSchemas: {},
isCustomTool: false,
language: 'javascript',
timeout: 5000,
workflowId: undefined,
userId: undefined,
})
})
it.concurrent('should format array of code blocks correctly', () => {
const body = tester.getRequestBody({
code: [
{ content: 'const x = 40;', id: 'block1' },
{ content: 'const y = 2;', id: 'block2' },
{ content: 'return x + y;', id: 'block3' },
],
envVars: {},
isCustomTool: false,
timeout: 10000,
workflowId: undefined,
})
expect(body).toEqual({
code: 'const x = 40;\nconst y = 2;\nreturn x + y;',
timeout: 10000,
envVars: {},
workflowVariables: {},
blockData: {},
blockNameMapping: {},
blockOutputSchemas: {},
isCustomTool: false,
language: 'javascript',
workflowId: undefined,
userId: undefined,
})
})
it.concurrent('should use default timeout and memory limit when not provided', () => {
const body = tester.getRequestBody({
code: 'return 42',
})
expect(body).toEqual({
code: 'return 42',
timeout: DEFAULT_EXECUTION_TIMEOUT_MS,
envVars: {},
workflowVariables: {},
blockData: {},
blockNameMapping: {},
blockOutputSchemas: {},
isCustomTool: false,
language: 'javascript',
workflowId: undefined,
userId: undefined,
})
})
})
describe('Response Handling', () => {
it.concurrent('should process successful code execution response', async () => {
tester.setup({
success: true,
output: {
result: 42,
stdout: 'console.log output',
},
})
const result = await tester.execute({
code: 'console.log("output"); return 42;',
})
expect(result.success).toBe(true)
expect(result.output.result).toBe(42)
expect(result.output.stdout).toBe('console.log output')
})
it.concurrent('should handle execution errors', async () => {
tester.setup(
{
success: false,
error: 'Syntax error in code',
},
{ ok: false, status: 400 }
)
const result = await tester.execute({
code: 'invalid javascript code!!!',
})
expect(result.success).toBe(false)
expect(result.error).toBeDefined()
expect(result.error).toBe('Syntax error in code')
})
it.concurrent('should handle timeout errors', async () => {
tester.setup(
{
success: false,
error: 'Code execution timed out',
},
{ ok: false, status: 408 }
)
const result = await tester.execute({
code: 'while(true) {}',
timeout: 1000,
})
expect(result.success).toBe(false)
expect(result.error).toBe('Code execution timed out')
})
})
describe('Error Handling', () => {
it.concurrent('should handle syntax error with line content', async () => {
tester.setup(
{
success: false,
error:
'Syntax Error: Line 3: `description: "This has a missing closing quote` - Invalid or unexpected token (Check for missing quotes, brackets, or semicolons)',
output: {
result: null,
stdout: '',
executionTime: 5,
},
debug: {
line: 3,
column: undefined,
errorType: 'SyntaxError',
lineContent: 'description: "This has a missing closing quote',
stack: 'user-function.js:5\n description: "This has a missing closing quote\n...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'const obj = {\n name: "test",\n description: "This has a missing closing quote\n};\nreturn obj;',
})
expect(result.success).toBe(false)
expect(result.error).toContain('Syntax Error')
expect(result.error).toContain('Line 3')
expect(result.error).toContain('description: "This has a missing closing quote')
expect(result.error).toContain('Invalid or unexpected token')
expect(result.error).toContain('(Check for missing quotes, brackets, or semicolons)')
})
it.concurrent('should handle runtime error with line and column', async () => {
tester.setup(
{
success: false,
error:
"Type Error: Line 2:16: `return obj.someMethod();` - Cannot read properties of null (reading 'someMethod')",
output: {
result: null,
stdout: 'ERROR: {}\n',
executionTime: 12,
},
debug: {
line: 2,
column: 16,
errorType: 'TypeError',
lineContent: 'return obj.someMethod();',
stack: 'TypeError: Cannot read properties of null...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'const obj = null;\nreturn obj.someMethod();',
})
expect(result.success).toBe(false)
expect(result.error).toContain('Type Error')
expect(result.error).toContain('Line 2:16')
expect(result.error).toContain('return obj.someMethod();')
expect(result.error).toContain('Cannot read properties of null')
})
it.concurrent('should handle error information in tool response', async () => {
tester.setup(
{
success: false,
error: 'Reference Error: Line 1: `return undefinedVar` - undefinedVar is not defined',
output: {
result: null,
stdout: '',
executionTime: 3,
},
debug: {
line: 1,
column: 7,
errorType: 'ReferenceError',
lineContent: 'return undefinedVar',
stack: 'ReferenceError: undefinedVar is not defined...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'return undefinedVar',
})
expect(result.success).toBe(false)
expect(result.error).toBe(
'Reference Error: Line 1: `return undefinedVar` - undefinedVar is not defined'
)
})
it.concurrent('should preserve debug information in error object', async () => {
tester.setup(
{
success: false,
error: 'Syntax Error: Line 2 - Invalid syntax',
debug: {
line: 2,
column: 5,
errorType: 'SyntaxError',
lineContent: 'invalid syntax here',
stack: 'SyntaxError: Invalid syntax...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'valid line\ninvalid syntax here',
})
expect(result.success).toBe(false)
expect(result.error).toBe('Syntax Error: Line 2 - Invalid syntax')
})
it.concurrent('should handle enhanced error without line information', async () => {
tester.setup(
{
success: false,
error: 'Generic error message',
debug: {
errorType: 'Error',
stack: 'Error: Generic error message...',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'return "test";',
})
expect(result.success).toBe(false)
expect(result.error).toBe('Generic error message')
})
it.concurrent('should provide line-specific error message when available', async () => {
tester.setup(
{
success: false,
error:
'Type Error: Line 5:20: `obj.nonExistentMethod()` - obj.nonExistentMethod is not a function',
debug: {
line: 5,
column: 20,
errorType: 'TypeError',
lineContent: 'obj.nonExistentMethod()',
},
},
{ ok: false, status: 500 }
)
const result = await tester.execute({
code: 'const obj = {};\nobj.nonExistentMethod();',
})
expect(result.success).toBe(false)
expect(result.error).toContain('Line 5:20')
expect(result.error).toContain('obj.nonExistentMethod()')
})
})
describe('Edge Cases', () => {
it.concurrent('should handle empty code input', async () => {
await tester.execute({
code: '',
})
const body = tester.getRequestBody({ code: '' }) as { code: string }
expect(body.code).toBe('')
})
it.concurrent('should handle extremely short timeout', async () => {
const body = tester.getRequestBody({
code: 'return 42',
timeout: 1,
}) as { timeout: number }
expect(body.timeout).toBe(1)
})
})
})