-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathexecute.test.ts
More file actions
381 lines (337 loc) · 11.3 KB
/
execute.test.ts
File metadata and controls
381 lines (337 loc) · 11.3 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* @vitest-environment jsdom
*
* Function Execute Tool Unit Tests
*
* This file contains unit tests for the Function Execute tool,
* which runs JavaScript code in a secure sandbox.
*/
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { ToolTester } from '../__test-utils__/test-tools'
import { functionExecuteTool } from './execute'
describe('Function Execute Tool', () => {
let tester: ToolTester
beforeEach(() => {
tester = new ToolTester(functionExecuteTool)
process.env.NEXT_PUBLIC_APP_URL = 'http://localhost:3000'
})
afterEach(() => {
tester.cleanup()
vi.resetAllMocks()
process.env.NEXT_PUBLIC_APP_URL = undefined
})
describe('Request Construction', () => {
test('should set correct URL for code execution', () => {
// Since this is an internal route, actual URL will be the concatenated base URL + path
expect(tester.getRequestUrl({})).toBe('/api/function/execute')
})
test('should include correct headers for JSON payload', () => {
const headers = tester.getRequestHeaders({
code: 'return 42',
})
expect(headers['Content-Type']).toBe('application/json')
})
test('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: {},
blockData: {},
blockNameMapping: {},
isCustomTool: false,
timeout: 5000,
workflowId: undefined,
})
})
test('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: {},
blockData: {},
blockNameMapping: {},
isCustomTool: false,
workflowId: undefined,
})
})
test('should use default timeout and memory limit when not provided', () => {
const body = tester.getRequestBody({
code: 'return 42',
})
expect(body).toEqual({
code: 'return 42',
timeout: 10000,
envVars: {},
blockData: {},
blockNameMapping: {},
isCustomTool: false,
workflowId: undefined,
})
})
})
describe('Response Handling', () => {
test('should process successful code execution response', async () => {
// Setup a successful response
tester.setup({
success: true,
output: {
result: 42,
stdout: 'console.log output',
},
})
// Execute the tool
const result = await tester.execute({
code: 'console.log("output"); return 42;',
})
// Check the result
expect(result.success).toBe(true)
expect(result.output.result).toBe(42)
expect(result.output.stdout).toBe('console.log output')
})
test('should handle execution errors', async () => {
// Setup error response
tester.setup(
{
success: false,
error: 'Syntax error in code',
},
{ ok: false, status: 400 }
)
// Execute the tool with invalid code
const result = await tester.execute({
code: 'invalid javascript code!!!',
})
// Check error handling
expect(result.success).toBe(false)
expect(result.error).toBeDefined()
expect(result.error).toBe('Syntax error in code')
})
test('should handle timeout errors', async () => {
// Setup timeout error response
tester.setup(
{
success: false,
error: 'Code execution timed out',
},
{ ok: false, status: 408 }
)
// Execute the tool with code that would time out
const result = await tester.execute({
code: 'while(true) {}',
timeout: 1000,
})
// Check error handling
expect(result.success).toBe(false)
expect(result.error).toBe('Code execution timed out')
})
})
describe('Enhanced Error Handling', () => {
test('should handle enhanced syntax error with line content', async () => {
// Setup enhanced error response with debug information
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 }
)
// Execute the tool with syntax error
const result = await tester.execute({
code: 'const obj = {\n name: "test",\n description: "This has a missing closing quote\n};\nreturn obj;',
})
// Check enhanced error handling
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)')
})
test('should handle enhanced runtime error with line and column', async () => {
// Setup enhanced runtime error response
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 }
)
// Execute the tool with runtime error
const result = await tester.execute({
code: 'const obj = null;\nreturn obj.someMethod();',
})
// Check enhanced error handling
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')
})
test('should handle enhanced error information in tool response', async () => {
// Setup enhanced error response with full debug info
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 }
)
// Execute the tool with reference error
const result = await tester.execute({
code: 'return undefinedVar',
})
// Check that the tool properly captures enhanced error
expect(result.success).toBe(false)
expect(result.error).toBe(
'Reference Error: Line 1: `return undefinedVar` - undefinedVar is not defined'
)
})
test('should preserve debug information in error object', async () => {
// Setup enhanced error response
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 }
)
// Execute the tool
const result = await tester.execute({
code: 'valid line\ninvalid syntax here',
})
// Check that enhanced error information is available
expect(result.success).toBe(false)
expect(result.error).toBe('Syntax Error: Line 2 - Invalid syntax')
// Note: In this test framework, debug information would be available
// in the response object, but the tool transforms it into the error message
})
test('should handle enhanced error without line information', async () => {
// Setup error response without line information
tester.setup(
{
success: false,
error: 'Generic error message',
debug: {
errorType: 'Error',
stack: 'Error: Generic error message...',
},
},
{ ok: false, status: 500 }
)
// Execute the tool
const result = await tester.execute({
code: 'return "test";',
})
// Check error handling without enhanced line info
expect(result.success).toBe(false)
expect(result.error).toBe('Generic error message')
})
test('should provide line-specific error message when available', async () => {
// Setup enhanced error response with line info
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 }
)
// Execute the tool
const result = await tester.execute({
code: 'const obj = {};\nobj.nonExistentMethod();',
})
// Check that enhanced error message is provided
expect(result.success).toBe(false)
expect(result.error).toContain('Line 5:20')
expect(result.error).toContain('obj.nonExistentMethod()')
})
})
describe('Edge Cases', () => {
test('should handle empty code input', async () => {
// Execute with empty code - this should still pass through to the API
await tester.execute({
code: '',
})
// Just verify the request was made with empty code
const body = tester.getRequestBody({ code: '' })
expect(body.code).toBe('')
})
test('should handle extremely short timeout', async () => {
// Edge case with very short timeout
const body = tester.getRequestBody({
code: 'return 42',
timeout: 1, // 1ms timeout
})
// Should still pass through the short timeout
expect(body.timeout).toBe(1)
})
})
})