|
1 | | -import { beforeEach } from "node:test"; |
2 | 1 | import { env, introspectWorkflow, SELF } from "cloudflare:test"; |
3 | | -import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { expect, it } from "vitest"; |
4 | 3 |
|
5 | 4 | const STATUS_COMPLETE = "complete"; |
| 5 | +const STEP_NAME = "AI content scan"; |
| 6 | +const mockResult = { violationScore: 0 }; |
6 | 7 |
|
7 | | -describe("Test Workflow", () => { |
8 | | - it("should be able to trigger a workflow", async () => { |
9 | | - // With `using` to ensure Workflow instances cleanup: |
10 | | - await using introspector = await introspectWorkflow(env.TEST_WORKFLOW); |
11 | | - const res = await SELF.fetch("https://mock-worker.local"); |
| 8 | +it("workflow should be able to reach the end and be successful", async () => { |
| 9 | + // This example shows how to implicitly cleanUp Workflow instances |
12 | 10 |
|
13 | | - expect(res.status).toBe(200); |
| 11 | + // CONFIG with `using` to ensure Workflow instances cleanup: |
| 12 | + await using introspector = await introspectWorkflow(env.MODERATOR); |
| 13 | + introspector.modifyAll(async (m) => { |
| 14 | + await m.disableSleeps(); |
| 15 | + await m.mockStepResult({ name: STEP_NAME }, mockResult); |
14 | 16 | }); |
15 | 17 |
|
16 | | - it("workflow should reach the end and be successful", async () => { |
17 | | - // With `using` to ensure Workflow instances cleanup: |
18 | | - await using introspector = await introspectWorkflow(env.TEST_WORKFLOW); |
19 | | - const res = await SELF.fetch("https://mock-worker.local"); |
| 18 | + await SELF.fetch(`https://mock-worker.local/moderate`); |
20 | 19 |
|
21 | | - const json = await res.json<{ id: string }>(); |
| 20 | + const instances = introspector.get(); |
| 21 | + expect(instances.length).toBe(1); |
22 | 22 |
|
23 | | - await vi.waitUntil(async () => { |
24 | | - const res = await SELF.fetch(`https://mock-worker.local?id=${json.id}`); |
| 23 | + // ASSERTIONS: |
| 24 | + const instance = instances[0]; |
| 25 | + expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual( |
| 26 | + mockResult |
| 27 | + ); |
| 28 | + await instance.waitForStatus(STATUS_COMPLETE); |
25 | 29 |
|
26 | | - const statusJson = await res.json<{ status: string }>(); |
27 | | - expect(statusJson.status).toBe(STATUS_COMPLETE); |
28 | | - return true; |
29 | | - }, 1000); |
30 | | - }); |
31 | | - |
32 | | - it("workflow should reach the end and be successful with introspector", async () => { |
33 | | - // CONFIG with `using` to ensure Workflow instances cleanup: |
34 | | - await using introspector = await introspectWorkflow(env.TEST_WORKFLOW); |
35 | | - |
36 | | - await SELF.fetch("https://mock-worker.local"); |
37 | | - |
38 | | - const instances = introspector.get(); |
39 | | - expect(instances.length).toBe(1); |
40 | | - |
41 | | - // ASSERTIONS: |
42 | | - const instance = instances[0]; |
43 | | - await instance.waitForStatus(STATUS_COMPLETE); |
44 | | - |
45 | | - // CLEANUP: ensured by `using` |
46 | | - }); |
| 30 | + // CLEANUP: ensured by `using` |
47 | 31 | }); |
48 | 32 |
|
49 | | -describe("Test long Workflow", () => { |
50 | | - const STEP_NAME = "my step"; |
51 | | - const mockResult = "mocked result"; |
| 33 | +it("workflow batch should be able to reach the end and be successful", async () => { |
| 34 | + // This example shows how to explicitly cleanUp Workflow instances |
52 | 35 |
|
53 | | - it("workflow should be able to introspect and reach the end and be successful", async () => { |
54 | | - // CONFIG with `using` to ensure Workflow instances cleanup: |
55 | | - await using introspector = await introspectWorkflow(env.TEST_LONG_WORKFLOW); |
56 | | - introspector.modifyAll(async (m) => { |
57 | | - await m.disableSleeps(); |
58 | | - await m.mockStepResult({ name: STEP_NAME }, mockResult); |
59 | | - }); |
| 36 | + // CONFIG: |
| 37 | + let introspector = await introspectWorkflow(env.MODERATOR); |
| 38 | + introspector.modifyAll(async (m) => { |
| 39 | + await m.disableSleeps(); |
| 40 | + await m.mockStepResult({ name: STEP_NAME }, mockResult); |
| 41 | + }); |
60 | 42 |
|
61 | | - await SELF.fetch("https://mock-worker.local/long-workflow"); |
| 43 | + await SELF.fetch(`https://mock-worker.local/moderate-batch`); |
62 | 44 |
|
63 | | - const instances = introspector.get(); |
64 | | - expect(instances.length).toBe(1); |
| 45 | + const instances = introspector.get(); |
| 46 | + expect(instances.length).toBe(3); |
65 | 47 |
|
66 | | - // ASSERTIONS: |
67 | | - const instance = instances[0]; |
| 48 | + // ASSERTIONS: |
| 49 | + for (const instance of instances) { |
68 | 50 | expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual( |
69 | 51 | mockResult |
70 | 52 | ); |
71 | 53 | await instance.waitForStatus(STATUS_COMPLETE); |
| 54 | + } |
72 | 55 |
|
73 | | - // CLEANUP: ensured by `using` |
74 | | - }); |
75 | | - |
76 | | - it("workflow batch should be able to introspect and reach the end and be successful (explicit cleanup)", async () => { |
77 | | - // CONFIG: |
78 | | - let introspector = await introspectWorkflow(env.TEST_LONG_WORKFLOW); |
79 | | - introspector.modifyAll(async (m) => { |
80 | | - await m.disableSleeps(); |
81 | | - await m.mockStepResult({ name: STEP_NAME }, mockResult); |
82 | | - }); |
83 | | - |
84 | | - await SELF.fetch("https://mock-worker.local/long-workflow-batch"); |
85 | | - |
86 | | - const instances = introspector.get(); |
87 | | - expect(instances.length).toBe(3); |
88 | | - |
89 | | - // ASSERTIONS: |
90 | | - for (const instance of instances) { |
91 | | - expect(await instance.waitForStepResult({ name: STEP_NAME })).toEqual( |
92 | | - mockResult |
93 | | - ); |
94 | | - await instance.waitForStatus(STATUS_COMPLETE); |
95 | | - } |
96 | | - |
97 | | - // CLEANUP: |
98 | | - // Workflow introspector should be cleaned at the end of/after each test, if no `using` keyword is used for the introspector |
99 | | - // Cleans up all intercepted instances |
100 | | - await introspector.cleanUp(); |
101 | | - }); |
| 56 | + // CLEANUP: |
| 57 | + // Workflow introspector should be cleaned at the end of/after each test, if no `using` keyword is used for the introspector |
| 58 | + // Cleans up all intercepted instances |
| 59 | + await introspector.cleanUp(); |
102 | 60 | }); |
0 commit comments