|
| 1 | +import { rm } from "fs/promises"; |
| 2 | +import { resolve } from "path"; |
| 3 | +import { fetch } from "undici"; |
| 4 | +import { afterAll, beforeAll, describe, it, vi } from "vitest"; |
| 5 | +import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived"; |
| 6 | + |
| 7 | +describe("Workflows", () => { |
| 8 | + let ip: string, |
| 9 | + port: number, |
| 10 | + stop: (() => Promise<unknown>) | undefined, |
| 11 | + getOutput: () => string; |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + // delete previous run contents because of persistence |
| 15 | + await rm(resolve(__dirname, "..") + "/.wrangler", { |
| 16 | + force: true, |
| 17 | + recursive: true, |
| 18 | + }); |
| 19 | + ({ ip, port, stop, getOutput } = await runWranglerDev( |
| 20 | + resolve(__dirname, ".."), |
| 21 | + ["--port=0", "--inspector-port=0"] |
| 22 | + )); |
| 23 | + }); |
| 24 | + |
| 25 | + afterAll(async () => { |
| 26 | + await stop?.(); |
| 27 | + }); |
| 28 | + |
| 29 | + async function fetchJson(url: string) { |
| 30 | + const response = await fetch(url, { |
| 31 | + headers: { |
| 32 | + "MF-Disable-Pretty-Error": "1", |
| 33 | + }, |
| 34 | + }); |
| 35 | + const text = await response.text(); |
| 36 | + |
| 37 | + try { |
| 38 | + return JSON.parse(text); |
| 39 | + } catch (err) { |
| 40 | + throw new Error(`Couldn't parse JSON:\n\n${text}`); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + it("creates two instances with same id in two different workflows", async ({ |
| 45 | + expect, |
| 46 | + }) => { |
| 47 | + const createResult = { |
| 48 | + id: "test", |
| 49 | + status: { |
| 50 | + status: "running", |
| 51 | + output: [], |
| 52 | + }, |
| 53 | + }; |
| 54 | + |
| 55 | + await Promise.all([ |
| 56 | + expect( |
| 57 | + fetchJson(`http://${ip}:${port}/create?workflowName=1&id=test`) |
| 58 | + ).resolves.toStrictEqual(createResult), |
| 59 | + expect( |
| 60 | + fetchJson(`http://${ip}:${port}/create?workflowName=2&id=test`) |
| 61 | + ).resolves.toStrictEqual(createResult), |
| 62 | + ]); |
| 63 | + |
| 64 | + const firstResult = { |
| 65 | + id: "test", |
| 66 | + status: { |
| 67 | + status: "running", |
| 68 | + output: [{ output: "First step result" }], |
| 69 | + }, |
| 70 | + }; |
| 71 | + await Promise.all([ |
| 72 | + vi.waitFor( |
| 73 | + async () => { |
| 74 | + await expect( |
| 75 | + fetchJson(`http://${ip}:${port}/status?workflowName=1&id=test`) |
| 76 | + ).resolves.toStrictEqual(firstResult); |
| 77 | + }, |
| 78 | + { timeout: 5000 } |
| 79 | + ), |
| 80 | + vi.waitFor( |
| 81 | + async () => { |
| 82 | + await expect( |
| 83 | + fetchJson(`http://${ip}:${port}/status?workflowName=2&id=test`) |
| 84 | + ).resolves.toStrictEqual(firstResult); |
| 85 | + }, |
| 86 | + { timeout: 5000 } |
| 87 | + ), |
| 88 | + ]); |
| 89 | + |
| 90 | + await Promise.all([ |
| 91 | + await vi.waitFor( |
| 92 | + async () => { |
| 93 | + await expect( |
| 94 | + fetchJson(`http://${ip}:${port}/status?workflowName=1&id=test`) |
| 95 | + ).resolves.toStrictEqual({ |
| 96 | + id: "test", |
| 97 | + status: { |
| 98 | + status: "complete", |
| 99 | + output: [ |
| 100 | + { output: "First step result" }, |
| 101 | + { output: "workflow1" }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + }); |
| 105 | + }, |
| 106 | + { timeout: 5000 } |
| 107 | + ), |
| 108 | + await vi.waitFor( |
| 109 | + async () => { |
| 110 | + await expect( |
| 111 | + fetchJson(`http://${ip}:${port}/status?workflowName=2&id=test`) |
| 112 | + ).resolves.toStrictEqual({ |
| 113 | + id: "test", |
| 114 | + status: { |
| 115 | + status: "complete", |
| 116 | + output: [ |
| 117 | + { output: "First step result" }, |
| 118 | + { output: "workflow2" }, |
| 119 | + ], |
| 120 | + }, |
| 121 | + }); |
| 122 | + }, |
| 123 | + { timeout: 5000 } |
| 124 | + ), |
| 125 | + ]); |
| 126 | + }); |
| 127 | +}); |
0 commit comments