1- import { WAIT_FOREVER , waitUntil } from "async-wait-until" ;
21import { WORKFLOW_ENGINE_BINDING } from "../shared/workflows" ;
32import { internalEnv } from "./env" ;
4-
5- // import { instanceStatusName } from "@cloudflare/workflows-shared";
6-
7- export enum InstanceStatus {
8- Queued = 0 , // Queued and waiting to start
9- Running = 1 ,
10- Paused = 2 , // TODO (WOR-73): Implement pause
11- Errored = 3 , // Stopped due to a user or system Error
12- Terminated = 4 , // Stopped explicitly by user
13- Complete = 5 , // Successful completion
14- // TODO (WOR-71): Sleep
15- }
16-
17- export function instanceStatusName ( status : InstanceStatus ) {
18- switch ( status ) {
19- case InstanceStatus . Queued :
20- return "queued" ;
21- case InstanceStatus . Running :
22- return "running" ;
23- case InstanceStatus . Paused :
24- return "paused" ;
25- case InstanceStatus . Errored :
26- return "errored" ;
27- case InstanceStatus . Terminated :
28- return "terminated" ;
29- case InstanceStatus . Complete :
30- return "complete" ;
31- default :
32- return "unknown" ;
33- }
34- }
3+ // import type { InstanceStatus } from "@cloudflare/workflows-shared/src/instance";
4+ import type { InstanceModifier } from "@cloudflare/workflows-shared/src/modifier" ;
355
366export type StepSelector = {
377 name : string ;
388 index ?: number ;
399} ;
4010
4111export type WorkflowInstanceIntrospector = {
42- modify ( fn : ( m : InstanceModifier ) => void ) : WorkflowInstanceIntrospector ;
12+ modify (
13+ fn : ( m : InstanceModifier ) => Promise < void >
14+ ) : Promise < WorkflowInstanceIntrospector > ;
4315
44- waitForStepResult ( step : StepSelector ) : Promise < any > ;
16+ waitForStepResult ( step : StepSelector ) : Promise < unknown > ;
4517
46- waitUntil ( opts : { status : InstanceStatus } ) : Promise < void > ;
18+ waitForStatus ( status : string ) : Promise < void > ;
19+
20+ cleanUp ( ) : Promise < void > ;
4721} ;
4822
4923/**
@@ -58,12 +32,17 @@ export async function introspectWorkflowInstance(
5832 throw new Error ( "Workflow binding and instance id are required." ) ;
5933 }
6034
61- console . log ( `Introspecting workflow instance: ${ instanceId } ` ) ;
35+ console . log (
36+ `[Vitest-Workflows] Introspecting workflow instance: ${ instanceId } `
37+ ) ;
6238
63- await workflow . create ( { id : instanceId } ) ; // why do I need to create? Worked before without it
39+ // await workflow.create({ id: instanceId });
6440
41+ // @ts -expect-error getWorkflowName() not exposed
6542 const engineBindingName = `${ WORKFLOW_ENGINE_BINDING } ${ ( await workflow . getWorkflowName ( ) ) . toUpperCase ( ) } ` ;
43+ // @ts -expect-error binding created at in runner worker start
6644 const engineStubId = internalEnv [ engineBindingName ] . idFromName ( instanceId ) ;
45+ // @ts -expect-error binding created at in runner worker start
6746 const engineStub = internalEnv [ engineBindingName ] . get ( engineStubId ) ;
6847
6948 const instanceModifier = await engineStub . getInstanceModifier ( ) ;
@@ -84,33 +63,40 @@ class WorkflowInstanceIntrospectorHandle
8463 this . instanceModifier = instanceModifier ;
8564 }
8665
87- async modify (
88- fn : ( m : InstanceModifier ) => void
89- ) : WorkflowInstanceIntrospector {
66+ public async modify (
67+ fn : ( m : InstanceModifier ) => Promise < void >
68+ ) : Promise < WorkflowInstanceIntrospector > {
69+ console . log ( "[Vitest-Workflows] I should go call a modifier" ) ;
9070 await fn ( this . instanceModifier ) ;
91- console . log ( "Should allow modifications" ) ;
9271 return this ;
9372 }
9473
95- async waitForStepResult ( step : StepSelector ) : Promise < unknown > {
96- await waitUntil ( async ( ) => { } ) ;
74+ public async waitForStepResult ( step : StepSelector ) : Promise < unknown > {
75+ console . log ( "waiting for step result of step" , step . name ) ;
76+ // @ts -expect-error waitForStepResult not exposed
77+ const stepResult = await this . engineStub . waitForStepResult (
78+ step . name ,
79+ step . index
80+ ) ;
9781
98- console . log ( "Should await the step result of step" , step . name ) ;
99- return { result : "result" } ;
82+ console . log ( "result of step" , step . name , "awaited" ) ;
83+ return stepResult ;
10084 }
10185
102- async waitUntil ( opts : { status : InstanceStatus } ) : Promise < void > {
103- // console.log("I waited until the Engine reached the status", opts.status);
104- await waitUntil (
105- async ( ) => {
106- const currentStatus = instanceStatusName (
107- await this . engineStub . getStatus ( )
108- ) ;
109- console . log ( "status from engine" , currentStatus ) ;
110- console . log ( "status user wants" , opts . status ) ;
111- return currentStatus === opts . status ;
112- } ,
113- { timeout : WAIT_FOREVER } // Default timeout is 5s, crashes the test after that
114- ) ;
86+ public async waitForStatus ( status : string ) : Promise < void > {
87+ console . log ( "[Vitest-Workflows] waiting for status" ) ;
88+ // @ts -expect-error waitForStatus not exposed
89+ await this . engineStub . waitForStatus ( status ) ;
90+
91+ console . log ( "[Vitest-Workflows] status awaited" ) ;
92+ }
93+
94+ public async cleanUp ( ) : Promise < void > {
95+ // works with isolatedStorage = false
96+ try {
97+ await this . engineStub . abort ( "user called delete" ) ;
98+ } catch {
99+ // do nothing because we want to clean up this instance
100+ }
115101 }
116102}
0 commit comments