1- import type { SessionPrincipal } from '@sim/auth/principal'
21import { createLogger } from '@sim/logger'
32import {
4- createSerializedBlock ,
5- createSerializedWorkflow ,
6- } from '@sim/testing/factories/serialized-block.factory'
7- import { isolatedVmMock , isolatedVmMockFns } from '@sim/testing/mocks/isolated-vm.mock'
3+ isolatedVmMock ,
4+ isolatedVmMockFns ,
5+ resetIsolatedVmMock ,
6+ } from '@sim/testing/mocks/isolated-vm.mock'
87import { storageServiceMockFns } from '@sim/testing/mocks/storage-service.mock'
98import { uploadsMock } from '@sim/testing/mocks/uploads.mock'
109import { beforeEach , describe , expect , it , vi } from 'vitest'
1110import { classifyFailure } from '@/lib/core/errors/failure-log'
1211import { clearLargeValueCacheForTests } from '@/lib/execution/payloads/cache'
13- import { BlockType , EDGE } from '@/executor/constants'
12+ import { EDGE } from '@/executor/constants'
1413import type { DAG , DAGNode } from '@/executor/dag/builder'
1514import type { EdgeManager } from '@/executor/execution/edge-manager'
16- import { DAGExecutor } from '@/executor/execution/executor '
15+ import { loopWorkflow , runLoopWorkflow } from '@/executor/execution/loop-workflow.test-helpers '
1716import type { BlockStateController } from '@/executor/execution/types'
1817import { LoopOrchestrator } from '@/executor/orchestrators/loop'
1918import type { ExecutionContext , ExecutionResult } from '@/executor/types'
20- import { hasExecutionResult } from '@/executor/utils/errors'
2119import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
22- import type { SerializedLoop , SerializedWorkflow } from '@/serializer/types'
2320
2421const mockUploadFile = storageServiceMockFns . mockUploadFile
2522
@@ -357,8 +354,6 @@ describe('LoopOrchestrator', () => {
357354 } )
358355} )
359356
360- const PRINCIPAL : SessionPrincipal = { kind : 'session' , userId : 'user-1' , sessionId : 'session-1' }
361-
362357const SANDBOX_AT_CAPACITY = {
363358 result : null ,
364359 stdout : '' ,
@@ -375,75 +370,54 @@ const CONDITION_SYNTAX_ERROR = {
375370 error : { message : "Unexpected token ')'" , name : 'SyntaxError' } ,
376371}
377372
378- function loopWorkflow ( loop : Partial < Omit < SerializedLoop , 'id' | 'nodes' > > ) : SerializedWorkflow {
379- const blocks = [
380- createSerializedBlock ( { id : 'start' , name : 'start' , type : BlockType . STARTER } ) ,
381- createSerializedBlock ( { id : 'loop' , name : 'loop' , type : BlockType . LOOP } ) ,
382- createSerializedBlock ( { id : 'tick' , name : 'tick' , type : BlockType . FUNCTION } ) ,
383- ]
384- return {
385- ...createSerializedWorkflow ( blocks , [
386- { source : 'start' , target : 'loop' } ,
387- { source : 'loop' , target : 'tick' , sourceHandle : 'loop-start-source' } ,
388- ] ) ,
389- loops : { loop : { id : 'loop' , nodes : [ 'tick' ] , iterations : 0 , ...loop } } ,
390- }
391- }
392-
393- /** Runs the workflow and returns its result, including the one a failed run attaches to its throw. */
394- async function runWorkflow ( workflow : SerializedWorkflow ) : Promise < ExecutionResult > {
395- try {
396- return await new DAGExecutor ( {
397- workflow,
398- contextExtensions : { workspaceId : 'ws' , executionId : 'exec-1' , principal : PRINCIPAL } ,
399- } ) . execute ( 'wf' )
400- } catch ( error ) {
401- if ( hasExecutionResult ( error ) ) return error . executionResult
402- throw error
403- }
404- }
405-
406373function loopLog ( result : ExecutionResult ) {
407374 return result . logs ?. find ( ( log ) => log . blockId === 'loop' )
408375}
409376
377+ const sandboxAtCapacity = ( ) => mockExecuteInIsolatedVM . mockResolvedValue ( SANDBOX_AT_CAPACITY )
378+ const sandboxRejects = ( ) =>
379+ mockExecuteInIsolatedVM . mockRejectedValue ( new Error ( 'REDIS_URL is required' ) )
380+
410381describe ( 'loop continuation when the sandbox cannot evaluate' , ( ) => {
411382 beforeEach ( ( ) => {
412383 bodyRuns . count = 0
384+ resetIsolatedVmMock ( )
413385 clearLargeValueCacheForTests ( )
414386 mockUploadFile . mockImplementation ( async ( { customKey } ) => ( { key : customKey } ) )
415387 } )
416388
417389 it . each ( [
418- [ 'For' , { loopType : 'for' , iterations : 5 } ] ,
419- [ 'ForEach' , { loopType : 'forEach' , forEachItems : [ 'a' , 'b' , 'c' , 'd' , 'e' ] } ] ,
420- [ 'count-driven Do While' , { loopType : 'doWhile' , iterations : 5 } ] ,
390+ [ 'For' , 'is at capacity' , { loopType : 'for' , iterations : 5 } , sandboxAtCapacity ] ,
391+ [ 'For' , 'rejects' , { loopType : 'for' , iterations : 5 } , sandboxRejects ] ,
392+ [
393+ 'ForEach' ,
394+ 'is at capacity' ,
395+ { loopType : 'forEach' , forEachItems : [ 'a' , 'b' , 'c' , 'd' , 'e' ] } ,
396+ sandboxAtCapacity ,
397+ ] ,
398+ [
399+ 'count-driven Do While' ,
400+ 'is at capacity' ,
401+ { loopType : 'doWhile' , iterations : 5 } ,
402+ sandboxAtCapacity ,
403+ ] ,
421404 ] as const ) (
422- 'runs every iteration of a %s loop while the sandbox is at capacity ' ,
423- async ( _label , loop ) => {
424- mockExecuteInIsolatedVM . mockResolvedValue ( SANDBOX_AT_CAPACITY )
405+ 'runs every iteration of a %s loop when the sandbox %s ' ,
406+ async ( _label , _mode , loop , failSandbox ) => {
407+ failSandbox ( )
425408
426- const result = await runWorkflow ( loopWorkflow ( loop ) )
409+ const result = await runLoopWorkflow ( loopWorkflow ( loop ) )
427410
428411 expect ( result . success ) . toBe ( true )
429412 expect ( bodyRuns . count ) . toBe ( 5 )
430413 expect ( loopLog ( result ) ?. output ?. results ) . toHaveLength ( 5 )
431414 }
432415 )
433416
434- it ( 'runs every iteration of a For loop when the sandbox call rejects' , async ( ) => {
435- mockExecuteInIsolatedVM . mockRejectedValue ( new Error ( 'REDIS_URL is required' ) )
436-
437- const result = await runWorkflow ( loopWorkflow ( { loopType : 'for' , iterations : 5 } ) )
438-
439- expect ( result . success ) . toBe ( true )
440- expect ( bodyRuns . count ) . toBe ( 5 )
441- } )
442-
443417 it ( 'fails a While loop whose condition has a syntax error instead of skipping its body' , async ( ) => {
444418 mockExecuteInIsolatedVM . mockResolvedValue ( CONDITION_SYNTAX_ERROR )
445419
446- const result = await runWorkflow (
420+ const result = await runLoopWorkflow (
447421 loopWorkflow ( { loopType : 'while' , whileCondition : '<loop.index> < 3)' } )
448422 )
449423
@@ -459,24 +433,16 @@ describe('loop continuation when the sandbox cannot evaluate', () => {
459433 } )
460434
461435 it . each ( [
462- [
463- 'answers with a system error' ,
464- ( ) => mockExecuteInIsolatedVM . mockResolvedValue ( SANDBOX_AT_CAPACITY ) ,
465- / a t c a p a c i t y / ,
466- ] ,
467- [
468- 'rejects' ,
469- ( ) => mockExecuteInIsolatedVM . mockRejectedValue ( new Error ( 'REDIS_URL is required' ) ) ,
470- / R E D I S _ U R L i s r e q u i r e d / ,
471- ] ,
436+ [ 'answers with a system error' , sandboxAtCapacity , / a t c a p a c i t y / ] ,
437+ [ 'rejects' , sandboxRejects , / R E D I S _ U R L i s r e q u i r e d / ] ,
472438 ] as const ) (
473439 'fails a Do While loop mid-run when the sandbox %s instead of ending it early' ,
474440 async ( _label , failSandbox , message ) => {
475441 mockExecuteInIsolatedVM . mockResolvedValueOnce ( { result : true } )
476442 mockExecuteInIsolatedVM . mockResolvedValueOnce ( { result : true } )
477443 failSandbox ( )
478444
479- const result = await runWorkflow (
445+ const result = await runLoopWorkflow (
480446 loopWorkflow ( { loopType : 'doWhile' , doWhileCondition : '<loop.index> < 10' } )
481447 )
482448
0 commit comments