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