import { WorkerEntrypoint } from "cloudflare:workers"; import type { WorkflowEvent, WorkflowStep } from "cloudflare:workers"; // Test entry point — re-exports everything from src/index.ts and adds // a TestWorkflow class that can be bound as USER_WORKFLOW via serviceBindings. // This allows the workflow entrypoint to survive DO aborts (unlike the old // setWorkflowEntrypoint pattern which manually mutated instance.env). // // NOTE: We extend WorkerEntrypoint (not WorkflowEntrypoint) because workerd // only recognises WorkerEntrypoint subclasses for service-binding RPC. // WorkflowEntrypoint is a higher-level abstraction used by the Workflows // platform; for our test harness the engine just needs a target with a // callable run() method. export * from "../src/index"; type WorkflowCallback = ( event: unknown, step: WorkflowStep ) => Promise; let __testWorkflowCallback: WorkflowCallback | undefined; /** * Set the workflow callback that TestWorkflow.run() will delegate to. * Call this before creating or restarting a workflow instance in tests. */ export function setTestWorkflowCallback( cb: WorkflowCallback | undefined ): void { __testWorkflowCallback = cb; } /** * A WorkerEntrypoint subclass for tests that delegates run() to a * module-level callback. Configured as the USER_WORKFLOW service binding * in vitest.config.ts so it survives DO aborts (unlike manual env injection). */ export class TestWorkflow extends WorkerEntrypoint { async run( event: Readonly>, step: WorkflowStep ): Promise { if (!__testWorkflowCallback) { throw new Error( "TestWorkflow callback not set — call setTestWorkflowCallback() before running the workflow" ); } return await __testWorkflowCallback(event, step); } }