|
1 | | -import { |
2 | | - createExecutionContext, |
3 | | - env, |
4 | | - runInDurableObject, |
5 | | -} from "cloudflare:test"; |
| 1 | +import { env, runInDurableObject } from "cloudflare:test"; |
6 | 2 | import { NonRetryableError } from "cloudflare:workflows"; |
7 | 3 | import { describe, expect, it, vi } from "vitest"; |
8 | 4 | import { InstanceEvent, InstanceStatus } from "../src"; |
| 5 | +import { runWorkflow, runWorkflowDefer } from "./utils"; |
9 | 6 | import type { |
10 | 7 | DatabaseInstance, |
11 | 8 | DatabaseVersion, |
12 | 9 | DatabaseWorkflow, |
13 | 10 | Engine, |
14 | 11 | EngineLogs, |
15 | 12 | } from "../src/engine"; |
16 | | -import type { ProvidedEnv } from "cloudflare:test"; |
17 | | -import type { WorkflowEvent, WorkflowStep } from "cloudflare:workers"; |
18 | | - |
19 | | -async function setWorkflowEntrypoint( |
20 | | - stub: DurableObjectStub<Engine>, |
21 | | - callback: (event: unknown, step: WorkflowStep) => Promise<unknown> |
22 | | -) { |
23 | | - const ctx = createExecutionContext(); |
24 | | - await runInDurableObject(stub, (instance) => { |
25 | | - // @ts-expect-error this is only a stub for WorkflowEntrypoint |
26 | | - instance.env.USER_WORKFLOW = new (class { |
27 | | - constructor( |
28 | | - // eslint-disable-next-line @typescript-eslint/no-shadow |
29 | | - protected ctx: ExecutionContext, |
30 | | - // eslint-disable-next-line @typescript-eslint/no-shadow |
31 | | - protected env: ProvidedEnv |
32 | | - ) {} |
33 | | - public async run( |
34 | | - event: Readonly<WorkflowEvent<unknown>>, |
35 | | - step: WorkflowStep |
36 | | - ): Promise<unknown> { |
37 | | - return await callback(event, step); |
38 | | - } |
39 | | - })(ctx, env); |
40 | | - }); |
41 | | -} |
42 | | - |
43 | | -async function runWorkflow( |
44 | | - instanceId: string, |
45 | | - callback: (event: unknown, step: WorkflowStep) => Promise<unknown> |
46 | | -): Promise<DurableObjectStub<Engine>> { |
47 | | - const engineId = env.ENGINE.idFromName(instanceId); |
48 | | - const engineStub = env.ENGINE.get(engineId); |
49 | | - |
50 | | - await setWorkflowEntrypoint(engineStub, callback); |
51 | | - |
52 | | - await engineStub.init( |
53 | | - 12346, |
54 | | - {} as DatabaseWorkflow, |
55 | | - {} as DatabaseVersion, |
56 | | - {} as DatabaseInstance, |
57 | | - { payload: {}, timestamp: new Date(), instanceId: "some-instance-id" } |
58 | | - ); |
59 | | - |
60 | | - return engineStub; |
61 | | -} |
62 | | - |
63 | | -async function runWorkflowDefer( |
64 | | - instanceId: string, |
65 | | - callback: (event: unknown, step: WorkflowStep) => Promise<unknown> |
66 | | -): Promise<DurableObjectStub<Engine>> { |
67 | | - const engineId = env.ENGINE.idFromName(instanceId); |
68 | | - const engineStub = env.ENGINE.get(engineId); |
69 | | - |
70 | | - await setWorkflowEntrypoint(engineStub, callback); |
71 | | - |
72 | | - void engineStub.init( |
73 | | - 12346, |
74 | | - {} as DatabaseWorkflow, |
75 | | - {} as DatabaseVersion, |
76 | | - {} as DatabaseInstance, |
77 | | - { payload: {}, timestamp: new Date(), instanceId: "some-instance-id" } |
78 | | - ); |
79 | | - |
80 | | - return engineStub; |
81 | | -} |
82 | 13 |
|
83 | 14 | describe("Engine", () => { |
84 | 15 | it("should not retry after NonRetryableError is thrown", async () => { |
|
0 commit comments