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

main
ts 72 lines 2.53 KB
Raw
1 // Alchemy modifications are licensed under Apache-2.0.
2 // This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
3 // Alchemy modifications: uses Array<T> syntax for non-tuple array types to match the repository convention.
4 import { env } from "cloudflare:workers";
5 import type { WorkflowStep } from "cloudflare:workers";
6 import type { DatabaseInstance, DatabaseVersion, DatabaseWorkflow, Engine } from "../engine.ts";
7 import { setTestWorkflowCallback } from "./test-entry.ts";
8
9 // Track fire-and-forget init() RPC promises so they can be settled
10 // in afterAll hooks before vitest tears down miniflare
11 const pendingInits: Array<Promise<unknown>> = [];
12
13 /**
14 * Await all tracked init promises (with a timeout safety net).
15 * Call this in afterAll() for every test file that uses runWorkflow().
16 */
17 export async function settlePendingWorkflows(): Promise<void> {
18 await Promise.allSettled(pendingInits);
19 pendingInits.length = 0;
20 }
21
22 export async function runWorkflow(
23 instanceId: string,
24 callback: (event: unknown, step: WorkflowStep) => Promise<unknown>,
25 ): Promise<DurableObjectStub<Engine>> {
26 const engineId = env.ENGINE.idFromName(instanceId);
27 const engineStub = env.ENGINE.get(engineId);
28
29 setTestWorkflowCallback(callback);
30
31 // Fire-and-forget: suppress all rejections to prevent unhandled
32 // rejections inside workerd (which crash the HTTP server on Windows).
33 // Abort errors (pause, terminate, restart) are expected; workflow
34 // errors (NonRetryableError, step failures) are also expected for
35 // tests that intentionally trigger them. Tests validate correctness
36 // through separate log/status assertions, not through init()'s result.
37 const initPromise = engineStub
38 .init(
39 12346,
40 {} as DatabaseWorkflow,
41 {} as DatabaseVersion,
42 { id: instanceId } as DatabaseInstance,
43 { payload: {}, timestamp: new Date(), instanceId, workflowName: "" },
44 )
45 .catch(() => {});
46
47 pendingInits.push(initPromise);
48
49 return engineStub;
50 }
51
52 export async function runWorkflowAndAwait(
53 instanceId: string,
54 callback: (event: unknown, step: WorkflowStep) => Promise<unknown>,
55 ): Promise<DurableObjectStub<Engine>> {
56 const engineId = env.ENGINE.idFromName(instanceId);
57 const engineStub = env.ENGINE.get(engineId);
58
59 setTestWorkflowCallback(callback);
60
61 await engineStub
62 .init(
63 12346,
64 {} as DatabaseWorkflow,
65 {} as DatabaseVersion,
66 { id: instanceId } as DatabaseInstance,
67 { payload: {}, timestamp: new Date(), instanceId, workflowName: "" },
68 )
69 .catch(() => {});
70
71 return engineStub;
72 }