11import { rm } from 'node:fs/promises' ;
22import os from 'node:os' ;
33import path from 'node:path' ;
4+ import { WorkflowInvokePayloadSchema } from '@workflow/world' ;
45import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
56import { createWorld } from './index.js' ;
67import { createRun , updateRun } from './test-helpers.js' ;
@@ -14,10 +15,12 @@ describe('re-enqueue active runs on start', () => {
1415 let dataDir : string ;
1516
1617 beforeEach ( ( ) => {
18+ vi . stubEnv ( 'WORKFLOW_QUEUE_NAMESPACE' , undefined ) ;
1719 dataDir = path . join ( os . tmpdir ( ) , `wf-reenqueue-${ Date . now ( ) } ` ) ;
1820 } ) ;
1921
2022 afterEach ( async ( ) => {
23+ vi . unstubAllEnvs ( ) ;
2124 await rm ( dataDir , { recursive : true , force : true } ) ;
2225 } ) ;
2326
@@ -86,6 +89,49 @@ describe('re-enqueue active runs on start', () => {
8689 await world2 . close ( ) ;
8790 } ) ;
8891
92+ it ( 're-enqueues runs to the active queue namespace' , async ( ) => {
93+ vi . stubEnv ( 'WORKFLOW_QUEUE_NAMESPACE' , 'custom' ) ;
94+
95+ const world1 = createWorld ( { dataDir } ) ;
96+ await world1 . start ( ) ;
97+
98+ const pendingRun = await createRun ( world1 , {
99+ deploymentId : 'dpl_1' ,
100+ workflowName : 'myWorkflow' ,
101+ input : new Uint8Array ( [ 1 ] ) ,
102+ } ) ;
103+
104+ await world1 . close ( ) ;
105+
106+ const world2 = createWorld ( { dataDir } ) ;
107+ const namespacedRunIds : string [ ] = [ ] ;
108+ const unnamespacedRunIds : string [ ] = [ ] ;
109+ const namespacedHandler = world2 . createQueueHandler (
110+ '__custom_wkf_workflow_' ,
111+ async ( message ) => {
112+ const body = WorkflowInvokePayloadSchema . parse ( message ) ;
113+ namespacedRunIds . push ( body . runId ) ;
114+ }
115+ ) ;
116+ world2 . registerHandler ( '__custom_wkf_workflow_' , namespacedHandler ) ;
117+ // Capture an incorrectly reconstructed queue without allowing it to enter
118+ // the local queue's retry loop.
119+ world2 . registerHandler ( '__wkf_workflow_' , async ( req ) => {
120+ const body = await req . json ( ) ;
121+ unnamespacedRunIds . push ( body . runId ) ;
122+ return Response . json ( { ok : true } ) ;
123+ } ) ;
124+
125+ await world2 . start ( ) ;
126+
127+ await vi . waitFor ( ( ) => {
128+ expect ( namespacedRunIds ) . toEqual ( [ pendingRun . runId ] ) ;
129+ } ) ;
130+ expect ( unnamespacedRunIds ) . toHaveLength ( 0 ) ;
131+
132+ await world2 . close ( ) ;
133+ } ) ;
134+
89135 it ( 'does nothing when there are no active runs' , async ( ) => {
90136 // Create a world with only completed runs
91137 const world1 = createWorld ( { dataDir } ) ;
0 commit comments