@@ -7,7 +7,6 @@ import { NextRequest } from 'next/server'
77import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
88import { createMockRequest } from '@/app/api/__test-utils__/utils'
99
10- const mockFreestyleExecuteScript = vi . fn ( )
1110const mockCreateContext = vi . fn ( )
1211const mockRunInContext = vi . fn ( )
1312const mockLogger = {
@@ -29,27 +28,10 @@ describe('Function Execute API Route', () => {
2928 } ) ) ,
3029 } ) )
3130
32- vi . doMock ( 'freestyle-sandboxes' , ( ) => ( {
33- FreestyleSandboxes : vi . fn ( ) . mockImplementation ( ( ) => ( {
34- executeScript : mockFreestyleExecuteScript ,
35- } ) ) ,
36- } ) )
37-
38- vi . doMock ( '@/lib/env' , ( ) => ( {
39- env : {
40- FREESTYLE_API_KEY : 'test-freestyle-key' ,
41- } ,
42- } ) )
43-
4431 vi . doMock ( '@/lib/logs/console/logger' , ( ) => ( {
4532 createLogger : vi . fn ( ) . mockReturnValue ( mockLogger ) ,
4633 } ) )
4734
48- mockFreestyleExecuteScript . mockResolvedValue ( {
49- result : 'freestyle success' ,
50- logs : [ ] ,
51- } )
52-
5335 mockRunInContext . mockResolvedValue ( 'vm success' )
5436 mockCreateContext . mockReturnValue ( { } )
5537 } )
@@ -228,107 +210,6 @@ describe('Function Execute API Route', () => {
228210 } )
229211 } )
230212
231- describe . skip ( 'Freestyle Execution' , ( ) => {
232- it ( 'should use Freestyle when API key is available' , async ( ) => {
233- const req = createMockRequest ( 'POST' , {
234- code : 'return "freestyle test"' ,
235- } )
236-
237- const { POST } = await import ( '@/app/api/function/execute/route' )
238- await POST ( req )
239-
240- expect ( mockFreestyleExecuteScript ) . toHaveBeenCalled ( )
241- expect ( mockLogger . info ) . toHaveBeenCalledWith (
242- expect . stringMatching ( / \[ .* \] U s i n g F r e e s t y l e f o r c o d e e x e c u t i o n / )
243- )
244- } )
245-
246- it ( 'should handle Freestyle errors and fallback to VM' , async ( ) => {
247- mockFreestyleExecuteScript . mockRejectedValueOnce ( new Error ( 'Freestyle API error' ) )
248-
249- const req = createMockRequest ( 'POST' , {
250- code : 'return "fallback test"' ,
251- } )
252-
253- const { POST } = await import ( '@/app/api/function/execute/route' )
254- const response = await POST ( req )
255-
256- expect ( mockFreestyleExecuteScript ) . toHaveBeenCalled ( )
257- expect ( mockRunInContext ) . toHaveBeenCalled ( )
258- expect ( mockLogger . error ) . toHaveBeenCalledWith (
259- expect . stringMatching ( / \[ .* \] F r e e s t y l e A P I c a l l f a i l e d , f a l l i n g b a c k t o V M : / ) ,
260- expect . any ( Object )
261- )
262- } )
263-
264- it ( 'should handle Freestyle script errors' , async ( ) => {
265- mockFreestyleExecuteScript . mockResolvedValueOnce ( {
266- result : null ,
267- logs : [ { type : 'error' , message : 'ReferenceError: undefined variable' } ] ,
268- } )
269-
270- const req = createMockRequest ( 'POST' , {
271- code : 'return undefinedVariable' ,
272- } )
273-
274- const { POST } = await import ( '@/app/api/function/execute/route' )
275- const response = await POST ( req )
276-
277- expect ( response . status ) . toBe ( 500 )
278- const data = await response . json ( )
279- expect ( data . success ) . toBe ( false )
280- } )
281- } )
282-
283- describe ( 'VM Execution' , ( ) => {
284- it . skip ( 'should use VM when Freestyle API key is not available' , async ( ) => {
285- // Mock no Freestyle API key
286- vi . doMock ( '@/lib/env' , ( ) => ( {
287- env : {
288- FREESTYLE_API_KEY : undefined ,
289- } ,
290- } ) )
291-
292- const req = createMockRequest ( 'POST' , {
293- code : 'return "vm test"' ,
294- } )
295-
296- const { POST } = await import ( '@/app/api/function/execute/route' )
297- await POST ( req )
298-
299- expect ( mockFreestyleExecuteScript ) . not . toHaveBeenCalled ( )
300- expect ( mockRunInContext ) . toHaveBeenCalled ( )
301- expect ( mockLogger . info ) . toHaveBeenCalledWith (
302- expect . stringMatching (
303- / \[ .* \] U s i n g V M f o r c o d e e x e c u t i o n \( n o F r e e s t y l e A P I k e y a v a i l a b l e \) /
304- )
305- )
306- } )
307-
308- it ( 'should handle VM execution errors' , async ( ) => {
309- // Mock no Freestyle API key so it uses VM
310- vi . doMock ( '@/lib/env' , ( ) => ( {
311- env : {
312- FREESTYLE_API_KEY : undefined ,
313- } ,
314- } ) )
315-
316- mockRunInContext . mockRejectedValueOnce ( new Error ( 'VM execution error' ) )
317-
318- const req = createMockRequest ( 'POST' , {
319- code : 'return invalidCode(' ,
320- } )
321-
322- const { POST } = await import ( '@/app/api/function/execute/route' )
323- const response = await POST ( req )
324-
325- expect ( response . status ) . toBe ( 500 )
326- const data = await response . json ( )
327- expect ( data . success ) . toBe ( false )
328- expect ( data . error ) . toContain ( 'VM execution error' )
329- } )
330- } )
331-
332213 describe ( 'Custom Tools' , ( ) => {
333214 it ( 'should handle custom tool execution with direct parameter access' , async ( ) => {
334215 const req = createMockRequest ( 'POST' , {
@@ -651,113 +532,3 @@ SyntaxError: Invalid or unexpected token
651532 } )
652533 } )
653534} )
654-
655- describe ( 'Function Execute API - Template Variable Edge Cases' , ( ) => {
656- beforeEach ( ( ) => {
657- vi . resetModules ( )
658- vi . resetAllMocks ( )
659-
660- vi . doMock ( '@/lib/logs/console/logger' , ( ) => ( {
661- createLogger : vi . fn ( ) . mockReturnValue ( mockLogger ) ,
662- } ) )
663-
664- vi . doMock ( '@/lib/env' , ( ) => ( {
665- env : {
666- FREESTYLE_API_KEY : 'test-freestyle-key' ,
667- } ,
668- } ) )
669-
670- vi . doMock ( 'vm' , ( ) => ( {
671- createContext : mockCreateContext ,
672- Script : vi . fn ( ) . mockImplementation ( ( ) => ( {
673- runInContext : mockRunInContext ,
674- } ) ) ,
675- } ) )
676-
677- vi . doMock ( 'freestyle-sandboxes' , ( ) => ( {
678- FreestyleSandboxes : vi . fn ( ) . mockImplementation ( ( ) => ( {
679- executeScript : mockFreestyleExecuteScript ,
680- } ) ) ,
681- } ) )
682-
683- mockFreestyleExecuteScript . mockResolvedValue ( {
684- result : 'freestyle success' ,
685- logs : [ ] ,
686- } )
687-
688- mockRunInContext . mockResolvedValue ( 'vm success' )
689- mockCreateContext . mockReturnValue ( { } )
690- } )
691-
692- it . skip ( 'should handle nested template variables' , async ( ) => {
693- mockFreestyleExecuteScript . mockResolvedValueOnce ( {
694- result : 'environment-valueparam-value' ,
695- logs : [ ] ,
696- } )
697-
698- const req = createMockRequest ( 'POST' , {
699- code : 'return {{outer}} + <inner>' ,
700- envVars : {
701- outer : 'environment-value' ,
702- } ,
703- params : {
704- inner : 'param-value' ,
705- } ,
706- } )
707-
708- const { POST } = await import ( '@/app/api/function/execute/route' )
709- const response = await POST ( req )
710- const data = await response . json ( )
711-
712- expect ( response . status ) . toBe ( 200 )
713- expect ( data . success ) . toBe ( true )
714- expect ( data . output . result ) . toBe ( 'environment-valueparam-value' )
715- } )
716-
717- it . skip ( 'should prioritize environment variables over params for {{}} syntax' , async ( ) => {
718- mockFreestyleExecuteScript . mockResolvedValueOnce ( {
719- result : 'env-wins' ,
720- logs : [ ] ,
721- } )
722-
723- const req = createMockRequest ( 'POST' , {
724- code : 'return {{conflictVar}}' ,
725- envVars : {
726- conflictVar : 'env-wins' ,
727- } ,
728- params : {
729- conflictVar : 'param-loses' ,
730- } ,
731- } )
732-
733- const { POST } = await import ( '@/app/api/function/execute/route' )
734- const response = await POST ( req )
735- const data = await response . json ( )
736-
737- expect ( response . status ) . toBe ( 200 )
738- expect ( data . success ) . toBe ( true )
739- // Environment variable should take precedence
740- expect ( data . output . result ) . toBe ( 'env-wins' )
741- } )
742-
743- it . skip ( 'should handle missing template variables gracefully' , async ( ) => {
744- mockFreestyleExecuteScript . mockResolvedValueOnce ( {
745- result : '' ,
746- logs : [ ] ,
747- } )
748-
749- const req = createMockRequest ( 'POST' , {
750- code : 'return {{nonexistent}} + <alsoMissing>' ,
751- envVars : { } ,
752- params : { } ,
753- } )
754-
755- const { POST } = await import ( '@/app/api/function/execute/route' )
756- const response = await POST ( req )
757- const data = await response . json ( )
758-
759- expect ( response . status ) . toBe ( 200 )
760- expect ( data . success ) . toBe ( true )
761- expect ( data . output . result ) . toBe ( '' )
762- } )
763- } )
0 commit comments