The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { CloudflareApiLive } from "@alchemy.run/cloudflare/Providers"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Exit from "effect/Exit"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import { getWorkerTags } from "../Utils/Worker.ts"; |
| 12 | import { requestWorker } from "../Utils/WorkerRequest.ts"; |
| 13 | import DuplicateWorker from "./fixtures/duplicate-worker.ts"; |
| 14 | import Stack from "./fixtures/isolation-stack.ts"; |
| 15 | import TwiceWorker from "./fixtures/twice-worker.ts"; |
| 16 | |
| 17 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 18 | providers: Cloudflare.providers(), |
| 19 | }); |
| 20 | |
| 21 | const stack = beforeAll(deploy(Stack)); |
| 22 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 23 | |
| 24 | /** An isolation route answered with something other than 200. */ |
| 25 | class IsolationRequestFailed extends Data.TaggedError("IsolationRequestFailed")<{ |
| 26 | readonly status: number; |
| 27 | }> {} |
| 28 | |
| 29 | /** Call a route; right after a deploy, retry until the Worker serves it. */ |
| 30 | const call = (url: string, method: "GET" | "POST", path: string) => |
| 31 | requestWorker( |
| 32 | (method === "GET" ? HttpClientRequest.get : HttpClientRequest.post)(`${url}${path}`).pipe( |
| 33 | HttpClientRequest.setHeader("connection", "close"), |
| 34 | ), |
| 35 | ).pipe( |
| 36 | Effect.filterOrFail( |
| 37 | (res) => res.status === 200, |
| 38 | (res) => new IsolationRequestFailed({ status: res.status }), |
| 39 | ), |
| 40 | Effect.flatMap((res) => res.json), |
| 41 | Effect.retry({ schedule: Schedule.spaced("1 second"), times: 10 }), |
| 42 | ); |
| 43 | |
| 44 | test( |
| 45 | "two models with the same definitions in one Worker keep separate data", |
| 46 | Effect.gen(function* () { |
| 47 | const { url } = yield* stack; |
| 48 | const base = url.replace(/\/+$/, ""); |
| 49 | const id = `a-${yield* Effect.sync(() => crypto.randomUUID())}`; |
| 50 | |
| 51 | yield* call(base, "POST", `/Billing/open/${id}/sam`); |
| 52 | yield* call(base, "POST", `/Audit/open/${id}/alex`); |
| 53 | |
| 54 | // each model's aggregate holds only its own command |
| 55 | expect(yield* call(base, "GET", `/Billing/state/${id}`)).toEqual({ owner: "sam" }); |
| 56 | expect(yield* call(base, "GET", `/Audit/state/${id}`)).toEqual({ owner: "alex" }); |
| 57 | |
| 58 | // each model's view hears only its own aggregate's events |
| 59 | const owners = (model: string, owner: string) => |
| 60 | call(base, "GET", `/${model}/owners/${id}`).pipe( |
| 61 | Effect.repeat({ |
| 62 | schedule: Schedule.spaced("1 second"), |
| 63 | until: (state) => (state as { owner?: string } | null)?.owner === owner, |
| 64 | times: 30, |
| 65 | }), |
| 66 | ); |
| 67 | expect(yield* owners("Billing", "sam")).toEqual({ owner: "sam" }); |
| 68 | expect(yield* owners("Audit", "alex")).toEqual({ owner: "alex" }); |
| 69 | }), |
| 70 | { timeout: 180_000 }, |
| 71 | ); |
| 72 | |
| 73 | test( |
| 74 | "two models running the same saga in one Worker keep separate runs", |
| 75 | Effect.gen(function* () { |
| 76 | const { url } = yield* stack; |
| 77 | const base = url.replace(/\/+$/, ""); |
| 78 | const key = `s-${yield* Effect.sync(() => crypto.randomUUID())}`; |
| 79 | |
| 80 | // the same key in both models: two runs, each with its own input |
| 81 | expect(yield* call(base, "POST", `/saga/Sales/${key}/sam`)).toEqual({ owner: "sam" }); |
| 82 | expect(yield* call(base, "POST", `/saga/Support/${key}/alex`)).toEqual({ owner: "alex" }); |
| 83 | }), |
| 84 | { timeout: 180_000 }, |
| 85 | ); |
| 86 | |
| 87 | test( |
| 88 | "a Worker hosting models records the Domain version it was deployed with", |
| 89 | Effect.gen(function* () { |
| 90 | const { workerName } = yield* stack; |
| 91 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 92 | expect( |
| 93 | (yield* getWorkerTags(workerName, accountId)).filter((tag) => |
| 94 | tag.startsWith("alchemy:domain:"), |
| 95 | ), |
| 96 | ).toEqual(["alchemy:domain:v0"]); |
| 97 | }).pipe(Effect.provide(CloudflareApiLive()), Effect.scoped), |
| 98 | { timeout: 60_000 }, |
| 99 | ); |
| 100 | |
| 101 | /** |
| 102 | * Model names are unique per Worker, even when each model's Layer builds its |
| 103 | * own platform: two different models named alike would share classes. |
| 104 | */ |
| 105 | test.provider( |
| 106 | "two different models with one name in a Worker fail; one model hosted twice shares its data", |
| 107 | (scratch) => |
| 108 | Effect.gen(function* () { |
| 109 | yield* scratch.destroy(); |
| 110 | const duplicate = yield* scratch |
| 111 | .deploy( |
| 112 | Effect.gen(function* () { |
| 113 | return { worker: yield* DuplicateWorker }; |
| 114 | }), |
| 115 | ) |
| 116 | .pipe(Effect.exit); |
| 117 | expect(Exit.isFailure(duplicate)).toBe(true); |
| 118 | expect(String(duplicate)).toContain("Model Billing is hosted twice on one platform"); |
| 119 | |
| 120 | const { worker } = yield* scratch.deploy( |
| 121 | Effect.gen(function* () { |
| 122 | return { worker: yield* TwiceWorker }; |
| 123 | }), |
| 124 | ); |
| 125 | const base = worker.url!.replace(/\/+$/, ""); |
| 126 | yield* call(base, "POST", "/open/a-1/sam"); |
| 127 | expect(yield* call(base, "GET", "/state/a-1")).toEqual({ owner: "sam" }); |
| 128 | |
| 129 | yield* scratch.destroy(); |
| 130 | }), |
| 131 | { timeout: 300_000 }, |
| 132 | ); |