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 { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as workers from "@distilled.cloud/cloudflare/workers"; |
| 6 | import { isResourceState, State, type ResourceState } from "alchemy/State"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { testName } from "../../names.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | const script = (marker: string) => ` |
| 18 | export default { |
| 19 | async fetch() { |
| 20 | return new Response(${JSON.stringify(marker)}); |
| 21 | } |
| 22 | }`; |
| 23 | |
| 24 | const HEX_ID = /^[0-9a-f]{32}$/; |
| 25 | |
| 26 | /** The live script's tag straight from the account's script listing. */ |
| 27 | const liveScriptTag = (accountId: string, scriptName: string) => |
| 28 | workers.listScripts.items({ accountId }).pipe( |
| 29 | Stream.filter((s) => s.id === scriptName), |
| 30 | Stream.runHead, |
| 31 | Effect.map(Option.getOrUndefined), |
| 32 | Effect.map((s) => s?.tag ?? undefined), |
| 33 | ); |
| 34 | |
| 35 | test.provider( |
| 36 | "workerId is the immutable script ID and survives updates", |
| 37 | (stack) => |
| 38 | Effect.gen(function* () { |
| 39 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 40 | |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const deployWith = (marker: string) => |
| 44 | stack.deploy(Cloudflare.Worker("IdWorker", { script: script(marker) })); |
| 45 | |
| 46 | const v1 = yield* deployWith("v1"); |
| 47 | // The immutable hex ID — decisively not the script name. |
| 48 | expect(v1.workerId).toMatch(HEX_ID); |
| 49 | expect(v1.workerId).not.toEqual(v1.workerName); |
| 50 | // It is exactly what Cloudflare reports as the script's tag. |
| 51 | expect(yield* liveScriptTag(accountId, v1.workerName)).toEqual(v1.workerId); |
| 52 | |
| 53 | // A code update keeps both identifiers. |
| 54 | const v2 = yield* deployWith("v2"); |
| 55 | expect(v2.workerId).toEqual(v1.workerId); |
| 56 | expect(v2.workerName).toEqual(v1.workerName); |
| 57 | |
| 58 | yield* stack.destroy(); |
| 59 | }).pipe(logLevel), |
| 60 | { |
| 61 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 62 | timeout: 240_000, |
| 63 | }, |
| 64 | ); |
| 65 | |
| 66 | test.provider( |
| 67 | "legacy state carrying the script name in workerId heals on a no-change redeploy", |
| 68 | (stack) => |
| 69 | Effect.gen(function* () { |
| 70 | yield* stack.destroy(); |
| 71 | |
| 72 | const deployWith = (marker: string) => |
| 73 | stack.deploy(Cloudflare.Worker("LegacyIdWorker", { script: script(marker) })); |
| 74 | |
| 75 | const v1 = yield* deployWith("v1"); |
| 76 | const realId = v1.workerId; |
| 77 | expect(realId).toMatch(HEX_ID); |
| 78 | |
| 79 | // Rewrite the persisted row into the pre-rename shape: older betas |
| 80 | // stored the script *name* in `workerId`. |
| 81 | const state = yield* yield* State; |
| 82 | const stage = stack.stage; |
| 83 | const fqns = yield* state.list({ stack: stack.name, stage }); |
| 84 | const rows = yield* Effect.forEach(fqns, (fqn) => |
| 85 | state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))), |
| 86 | ); |
| 87 | const workerRow = rows.find( |
| 88 | (r): r is { fqn: string; row: ResourceState } => |
| 89 | isResourceState(r.row) && r.row.resourceType === "Cloudflare.Worker", |
| 90 | ); |
| 91 | if (!workerRow) { |
| 92 | return yield* Effect.die(new Error("no Worker state row after deploy")); |
| 93 | } |
| 94 | const attr = workerRow.row.attr as { workerId?: string } | undefined; |
| 95 | expect(attr?.workerId).toEqual(realId); |
| 96 | yield* state.set({ |
| 97 | stack: stack.name, |
| 98 | stage, |
| 99 | fqn: workerRow.fqn, |
| 100 | value: { |
| 101 | ...workerRow.row, |
| 102 | attr: { ...(attr ?? {}), workerId: v1.workerName }, |
| 103 | }, |
| 104 | }); |
| 105 | |
| 106 | // A NO-CHANGE redeploy must heal: diff detects the legacy shape and |
| 107 | // plans an update even though no prop changed, and reconcile records |
| 108 | // the real immutable ID again (from the upload response). |
| 109 | const healed = yield* deployWith("v1"); |
| 110 | expect(healed.workerId).toEqual(realId); |
| 111 | expect(healed.workerId).not.toEqual(healed.workerName); |
| 112 | |
| 113 | yield* stack.destroy(); |
| 114 | }).pipe(logLevel), |
| 115 | { |
| 116 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 117 | timeout: 240_000, |
| 118 | }, |
| 119 | ); |
| 120 | |
| 121 | test.provider( |
| 122 | "state loss: adoption re-records the same immutable ID", |
| 123 | (stack) => |
| 124 | Effect.gen(function* () { |
| 125 | yield* stack.destroy(); |
| 126 | |
| 127 | // A deterministic name: with auto-generated names, state loss mints a |
| 128 | // fresh instance id (and so a fresh script) — adoption is only |
| 129 | // meaningful when the physical name is stable. |
| 130 | const deploy = () => |
| 131 | stack.deploy( |
| 132 | Cloudflare.Worker("AdoptIdWorker", { |
| 133 | name: testName("alchemy-test-workerid-adopt"), |
| 134 | script: script("v1"), |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | const v1 = yield* deploy(); |
| 139 | expect(v1.workerId).toMatch(HEX_ID); |
| 140 | |
| 141 | // Simulate state loss: the script lives on in Cloudflare, but the |
| 142 | // engine has no row for it. |
| 143 | const state = yield* yield* State; |
| 144 | const stage = stack.stage; |
| 145 | const fqns = yield* state.list({ stack: stack.name, stage }); |
| 146 | const rows = yield* Effect.forEach(fqns, (fqn) => |
| 147 | state.get({ stack: stack.name, stage, fqn }).pipe(Effect.map((row) => ({ fqn, row }))), |
| 148 | ); |
| 149 | const workerRow = rows.find( |
| 150 | (r) => isResourceState(r.row) && r.row.resourceType === "Cloudflare.Worker", |
| 151 | ); |
| 152 | if (!workerRow) { |
| 153 | return yield* Effect.die(new Error("no Worker state row after deploy")); |
| 154 | } |
| 155 | yield* state.delete({ stack: stack.name, stage, fqn: workerRow.fqn }); |
| 156 | |
| 157 | // The re-deploy upserts the same script — same name, same immutable |
| 158 | // ID (proof the script was adopted, not replaced). |
| 159 | const readopted = yield* deploy(); |
| 160 | expect(readopted.workerName).toEqual(v1.workerName); |
| 161 | expect(readopted.workerId).toEqual(v1.workerId); |
| 162 | |
| 163 | yield* stack.destroy(); |
| 164 | }).pipe(logLevel), |
| 165 | { |
| 166 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 167 | timeout: 240_000, |
| 168 | }, |
| 169 | ); |
| 170 | |
| 171 | test.provider( |
| 172 | "a version worker carries its parent script's workerId", |
| 173 | (stack) => |
| 174 | Effect.gen(function* () { |
| 175 | yield* stack.destroy(); |
| 176 | |
| 177 | const { parent, preview } = yield* stack.deploy( |
| 178 | Effect.gen(function* () { |
| 179 | const parent = yield* Cloudflare.Worker("IdVersionParent", { |
| 180 | script: script("parent"), |
| 181 | }); |
| 182 | const preview = yield* Cloudflare.Worker("IdVersionPreview", { |
| 183 | script: script("preview"), |
| 184 | version: { parent, message: "workerId test" }, |
| 185 | }); |
| 186 | return { parent, preview }; |
| 187 | }), |
| 188 | ); |
| 189 | |
| 190 | expect(parent.workerId).toMatch(HEX_ID); |
| 191 | // The version worker owns no script of its own — it carries the |
| 192 | // parent's immutable ID (resolved through the listing lookup), so |
| 193 | // Access preview destinations protect its preview URLs. |
| 194 | expect(preview.workerId).toEqual(parent.workerId); |
| 195 | expect(preview.versionOf).toEqual(parent.workerName); |
| 196 | |
| 197 | yield* stack.destroy(); |
| 198 | }).pipe(logLevel), |
| 199 | { |
| 200 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 201 | timeout: 240_000, |
| 202 | }, |
| 203 | ); |