The best way to control Claude Code, Codex and more from any computer or phone

main
ts 45 lines 1.88 KB
Raw
1 // Alchemy modifications are licensed under Apache-2.0.
2 // This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
3 import { WorkerEntrypoint } from "cloudflare:workers";
4 import type { WorkflowEvent, WorkflowStep } from "cloudflare:workers";
5
6 // Test entry point — re-exports everything from src/index.ts and adds
7 // a TestWorkflow class that can be bound as USER_WORKFLOW via serviceBindings.
8 // This allows the workflow entrypoint to survive DO aborts (unlike the old
9 // setWorkflowEntrypoint pattern which manually mutated instance.env).
10 //
11 // NOTE: We extend WorkerEntrypoint (not WorkflowEntrypoint) because workerd
12 // only recognises WorkerEntrypoint subclasses for service-binding RPC.
13 // WorkflowEntrypoint is a higher-level abstraction used by the Workflows
14 // platform; for our test harness the engine just needs a target with a
15 // callable run() method.
16
17 export * from "../index.ts";
18
19 type WorkflowCallback = (event: unknown, step: WorkflowStep) => Promise<unknown>;
20
21 let __testWorkflowCallback: WorkflowCallback | undefined;
22
23 /**
24 * Set the workflow callback that TestWorkflow.run() will delegate to.
25 * Call this before creating or restarting a workflow instance in tests.
26 */
27 export function setTestWorkflowCallback(cb: WorkflowCallback | undefined): void {
28 __testWorkflowCallback = cb;
29 }
30
31 /**
32 * A WorkerEntrypoint subclass for tests that delegates run() to a
33 * module-level callback. Configured as the USER_WORKFLOW service binding
34 * in vitest.config.ts so it survives DO aborts (unlike manual env injection).
35 */
36 export class TestWorkflow extends WorkerEntrypoint {
37 async run(event: Readonly<WorkflowEvent<unknown>>, step: WorkflowStep): Promise<unknown> {
38 if (!__testWorkflowCallback) {
39 throw new Error(
40 "TestWorkflow callback not set — call setTestWorkflowCallback() before running the workflow",
41 );
42 }
43 return await __testWorkflowCallback(event, step);
44 }
45 }