The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 durableObjects from "@distilled.cloud/cloudflare/durable-objects"; |
| 6 | import * as workers from "@distilled.cloud/cloudflare/workers"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Exit from "effect/Exit"; |
| 10 | import * as Fiber from "effect/Fiber"; |
| 11 | import * as HttpClient from "effect/http/HttpClient"; |
| 12 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 13 | import { MinimumLogLevel } from "effect/References"; |
| 14 | import * as Schedule from "effect/Schedule"; |
| 15 | import * as Stream from "effect/Stream"; |
| 16 | import { getWorkerTags } from "../Utils/Worker.ts"; |
| 17 | import { requestWorker } from "../Utils/WorkerRequest.ts"; |
| 18 | import SharedBillingWorker from "./fixtures/shared-billing-worker.ts"; |
| 19 | import SharedNoneWorker from "./fixtures/shared-none-worker.ts"; |
| 20 | import SharedWorker from "./fixtures/shared-worker.ts"; |
| 21 | import UpgradeNextWorker from "./fixtures/upgrade-next-worker.ts"; |
| 22 | import UpgradeNoneWorker from "./fixtures/upgrade-none-worker.ts"; |
| 23 | import UpgradePlainWorker from "./fixtures/upgrade-plain-worker.ts"; |
| 24 | import UpgradeWorker from "./fixtures/upgrade-worker.ts"; |
| 25 | |
| 26 | const { test } = Test.make({ |
| 27 | providers: Cloudflare.providers(), |
| 28 | }); |
| 29 | |
| 30 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 31 | |
| 32 | /** A route answered with something other than 200. */ |
| 33 | class UpgradeRequestFailed extends Data.TaggedError("UpgradeRequestFailed")<{ |
| 34 | readonly status: number; |
| 35 | readonly body: string; |
| 36 | }> {} |
| 37 | |
| 38 | /** Call a route; right after a deploy, retry until the Worker serves it. */ |
| 39 | const call = (url: string, path: string, method: "GET" | "POST" = "GET") => |
| 40 | requestWorker( |
| 41 | (method === "GET" ? HttpClientRequest.get : HttpClientRequest.post)(`${url}${path}`).pipe( |
| 42 | HttpClientRequest.setHeader("connection", "close"), |
| 43 | ), |
| 44 | ).pipe( |
| 45 | Effect.flatMap((res) => |
| 46 | res.status === 200 |
| 47 | ? res.json |
| 48 | : Effect.flatMap(res.text, (body) => |
| 49 | Effect.fail(new UpgradeRequestFailed({ status: res.status, body })), |
| 50 | ), |
| 51 | ), |
| 52 | Effect.retry({ schedule: Schedule.spaced("1 second"), times: 10 }), |
| 53 | ); |
| 54 | |
| 55 | /** Wait until the deployed Worker answers with the given Domain version. */ |
| 56 | const waitForVersion = (url: string, version: number | null) => |
| 57 | call(url, "/version").pipe( |
| 58 | Effect.repeat({ |
| 59 | schedule: Schedule.spaced("2 seconds"), |
| 60 | until: (body) => (body as { version: number | null }).version === version, |
| 61 | times: 30, |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | /** Poll a route until `until` holds. */ |
| 66 | const poll = (url: string, path: string, until: (body: unknown) => boolean) => |
| 67 | call(url, path).pipe(Effect.repeat({ schedule: Schedule.spaced("1 second"), until, times: 30 })); |
| 68 | |
| 69 | /** The Durable Object classes Cloudflare hosts for a script, by class name. */ |
| 70 | const namespacesOf = Effect.fn(function* (scriptName: string) { |
| 71 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 72 | const namespaces = yield* durableObjects.listNamespaces |
| 73 | .items({ accountId }) |
| 74 | .pipe(Stream.runCollect); |
| 75 | return Object.fromEntries( |
| 76 | namespaces.flatMap((namespace) => |
| 77 | namespace.script === scriptName && namespace.class ? [[namespace.class, namespace.id]] : [], |
| 78 | ), |
| 79 | ); |
| 80 | }); |
| 81 | |
| 82 | /** |
| 83 | * Read `path` every 250 ms, without retrying, until interrupted: every answer |
| 84 | * the Worker gave while a deploy rolled out. |
| 85 | */ |
| 86 | const watch = (url: string, path: string, answers: Array<{ status: number; body: string }>) => |
| 87 | HttpClient.execute( |
| 88 | HttpClientRequest.get(`${url}${path}`).pipe(HttpClientRequest.setHeader("connection", "close")), |
| 89 | ).pipe( |
| 90 | Effect.flatMap((res) => Effect.map(res.text, (body) => ({ status: res.status, body }))), |
| 91 | Effect.catch((error) => Effect.succeed({ status: 0, body: String(error) })), |
| 92 | Effect.tap((answer) => Effect.sync(() => answers.push(answer))), |
| 93 | Effect.repeat(Schedule.spaced("250 millis")), |
| 94 | ); |
| 95 | |
| 96 | const tagsOf = Effect.fn(function* (scriptName: string) { |
| 97 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 98 | return yield* getWorkerTags(scriptName, accountId); |
| 99 | }); |
| 100 | |
| 101 | /** |
| 102 | * A Worker deployed before Domain versions existed named its classes after |
| 103 | * the definition alone (`AccountAggregate`). Upgrading renames them after |
| 104 | * the model (`Billing.AccountAggregate`) and keeps every account, view row |
| 105 | * and saga run. |
| 106 | */ |
| 107 | test.provider( |
| 108 | "upgrading a model deployed before Domain versions keeps its data", |
| 109 | (stack) => |
| 110 | Effect.gen(function* () { |
| 111 | yield* stack.destroy(); |
| 112 | const before = yield* stack.deploy( |
| 113 | Effect.gen(function* () { |
| 114 | return { worker: yield* UpgradeNoneWorker }; |
| 115 | }), |
| 116 | ); |
| 117 | const url = before.worker.url!; |
| 118 | const scriptName = before.worker.workerName; |
| 119 | yield* waitForVersion(url, null); |
| 120 | |
| 121 | yield* call(url, "/Billing/open/a-1/sam", "POST"); |
| 122 | yield* poll(url, "/Billing/owners/a-1", (body) => (body as any)?.owner === "sam"); |
| 123 | // a run that waits for its approval across the upgrade |
| 124 | yield* call(url, "/saga/s-1/kim", "POST"); |
| 125 | yield* poll(url, "/saga/s-1", (body) => (body as any)._tag === "Running"); |
| 126 | |
| 127 | const legacy = yield* namespacesOf(scriptName); |
| 128 | expect(Object.keys(legacy).sort()).toEqual([ |
| 129 | "AccountAggregate", |
| 130 | "ApproveWorkflow", |
| 131 | "DomainDirectory", |
| 132 | "OwnersView", |
| 133 | "OwnersViewRun", |
| 134 | ]); |
| 135 | expect( |
| 136 | (yield* tagsOf(scriptName)).filter((tag) => tag.startsWith("alchemy:domain:")), |
| 137 | ).toEqual([]); |
| 138 | |
| 139 | // Read the account throughout the upgrade: while Cloudflare rolls the |
| 140 | // new code out, every request still answers with it. |
| 141 | const answers: Array<{ status: number; body: string }> = []; |
| 142 | const watcher = yield* Effect.forkChild(watch(url, "/Billing/state/a-1", answers)); |
| 143 | yield* stack.deploy( |
| 144 | Effect.gen(function* () { |
| 145 | return { worker: yield* UpgradeWorker }; |
| 146 | }), |
| 147 | ); |
| 148 | yield* waitForVersion(url, 0); |
| 149 | yield* Effect.sleep("5 seconds"); |
| 150 | yield* Fiber.interrupt(watcher); |
| 151 | expect(answers.length).toBeGreaterThan(10); |
| 152 | expect(answers.filter((answer) => answer.status !== 200)).toEqual([]); |
| 153 | expect(new Set(answers.map((answer) => answer.body))).toEqual( |
| 154 | new Set([JSON.stringify({ owner: "sam" })]), |
| 155 | ); |
| 156 | |
| 157 | // the same namespaces, under the model's names |
| 158 | expect(yield* namespacesOf(scriptName)).toEqual({ |
| 159 | "Billing.AccountAggregate": legacy.AccountAggregate, |
| 160 | "Billing.ApproveWorkflow": legacy.ApproveWorkflow, |
| 161 | "Billing.DomainDirectory": legacy.DomainDirectory, |
| 162 | "Billing.OwnersView": legacy.OwnersView, |
| 163 | "Billing.OwnersViewRun": legacy.OwnersViewRun, |
| 164 | }); |
| 165 | expect(yield* tagsOf(scriptName)).toContain("alchemy:domain:v0"); |
| 166 | |
| 167 | expect(yield* call(url, "/Billing/state/a-1")).toEqual({ owner: "sam" }); |
| 168 | expect(yield* call(url, "/Billing/owners/a-1")).toEqual({ owner: "sam" }); |
| 169 | // the run started before the upgrade finishes after it |
| 170 | yield* call(url, "/approve/s-1/lee", "POST"); |
| 171 | yield* poll(url, "/saga/s-1", (body) => (body as any)._tag === "Completed"); |
| 172 | expect(yield* call(url, "/Billing/state/s-1")).toEqual({ owner: "kim:lee" }); |
| 173 | const upgraded = yield* namespacesOf(scriptName); |
| 174 | |
| 175 | // A later code change at the same version moves nothing. |
| 176 | yield* stack.deploy( |
| 177 | Effect.gen(function* () { |
| 178 | return { worker: yield* UpgradeNextWorker }; |
| 179 | }), |
| 180 | ); |
| 181 | yield* waitForVersion(url, 0); |
| 182 | expect(yield* namespacesOf(scriptName)).toEqual(upgraded); |
| 183 | expect(yield* tagsOf(scriptName)).toContain("alchemy:domain:v0"); |
| 184 | expect(yield* call(url, "/Billing/state/a-1")).toEqual({ owner: "sam" }); |
| 185 | |
| 186 | // A Worker a newer Alchemy deployed (version 1) is never moved back. |
| 187 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 188 | yield* workers.patchScriptScriptAndVersionSetting({ |
| 189 | accountId, |
| 190 | scriptName, |
| 191 | settings: { |
| 192 | // Cloudflare checks every class is still declared on any settings change. |
| 193 | exports: Object.fromEntries( |
| 194 | Object.keys(upgraded).map((className) => [ |
| 195 | className, |
| 196 | { type: "durable-object", storage: "sqlite" }, |
| 197 | ]), |
| 198 | ), |
| 199 | tags: (yield* tagsOf(scriptName)).map((tag) => |
| 200 | tag === "alchemy:domain:v0" ? "alchemy:domain:v1" : tag, |
| 201 | ), |
| 202 | }, |
| 203 | }); |
| 204 | const downgrade = yield* stack |
| 205 | .deploy( |
| 206 | Effect.gen(function* () { |
| 207 | return { worker: yield* UpgradeWorker }; |
| 208 | }), |
| 209 | ) |
| 210 | .pipe(Effect.exit); |
| 211 | expect(Exit.isFailure(downgrade)).toBe(true); |
| 212 | expect(String(downgrade)).toContain("DomainVersionTooNew"); |
| 213 | expect(yield* namespacesOf(scriptName)).toEqual(upgraded); |
| 214 | expect(yield* tagsOf(scriptName)).toContain("alchemy:domain:v1"); |
| 215 | |
| 216 | // Nor are its classes deleted by older code that hosts no model at all. |
| 217 | const removed = yield* stack |
| 218 | .deploy( |
| 219 | Effect.gen(function* () { |
| 220 | return { worker: yield* UpgradePlainWorker }; |
| 221 | }), |
| 222 | ) |
| 223 | .pipe(Effect.exit); |
| 224 | expect(Exit.isFailure(removed)).toBe(true); |
| 225 | expect(String(removed)).toContain("DomainVersionTooNew"); |
| 226 | expect(yield* namespacesOf(scriptName)).toEqual(upgraded); |
| 227 | |
| 228 | yield* stack.destroy(); |
| 229 | }).pipe(logLevel), |
| 230 | { timeout: 600_000 }, |
| 231 | ); |
| 232 | |
| 233 | /** |
| 234 | * Two models that listed the same aggregate shared its class before Domain |
| 235 | * versions existed. Only one can keep that data, so the upgrade fails until |
| 236 | * one model sets `legacyData`. |
| 237 | */ |
| 238 | test.provider( |
| 239 | "upgrading models that shared a class needs one to keep its data", |
| 240 | (stack) => |
| 241 | Effect.gen(function* () { |
| 242 | yield* stack.destroy(); |
| 243 | const before = yield* stack.deploy( |
| 244 | Effect.gen(function* () { |
| 245 | return { worker: yield* SharedNoneWorker }; |
| 246 | }), |
| 247 | ); |
| 248 | const url = before.worker.url!; |
| 249 | const scriptName = before.worker.workerName; |
| 250 | yield* waitForVersion(url, null); |
| 251 | // one class: Audit sees what Billing wrote |
| 252 | yield* call(url, "/Billing/open/a-1/sam", "POST"); |
| 253 | expect(yield* call(url, "/Audit/state/a-1")).toEqual({ owner: "sam" }); |
| 254 | const legacy = yield* namespacesOf(scriptName); |
| 255 | |
| 256 | const ambiguous = yield* stack |
| 257 | .deploy( |
| 258 | Effect.gen(function* () { |
| 259 | return { worker: yield* SharedWorker }; |
| 260 | }), |
| 261 | ) |
| 262 | .pipe(Effect.exit); |
| 263 | expect(Exit.isFailure(ambiguous)).toBe(true); |
| 264 | expect(String(ambiguous)).toContain("AmbiguousDomainLegacyData"); |
| 265 | // nothing changed on Cloudflare |
| 266 | expect(yield* namespacesOf(scriptName)).toEqual(legacy); |
| 267 | |
| 268 | yield* stack.deploy( |
| 269 | Effect.gen(function* () { |
| 270 | return { worker: yield* SharedBillingWorker }; |
| 271 | }), |
| 272 | ); |
| 273 | yield* waitForVersion(url, 0); |
| 274 | const upgraded = yield* namespacesOf(scriptName); |
| 275 | expect(upgraded["Billing.AccountAggregate"]).toBe(legacy.AccountAggregate); |
| 276 | expect(upgraded["Audit.AccountAggregate"]).toBeDefined(); |
| 277 | expect(upgraded["Audit.AccountAggregate"]).not.toBe(legacy.AccountAggregate); |
| 278 | expect(yield* call(url, "/Billing/state/a-1")).toEqual({ owner: "sam" }); |
| 279 | expect(yield* call(url, "/Audit/state/a-1")).toEqual({ owner: null }); |
| 280 | |
| 281 | yield* stack.destroy(); |
| 282 | }).pipe(logLevel), |
| 283 | { timeout: 600_000 }, |
| 284 | ); |
| 285 | |
| 286 | /** |
| 287 | * Removing every model from a Worker at the current version is allowed: its |
| 288 | * classes are deleted like any Durable Object the Worker stops hosting, and |
| 289 | * its version tag goes with them. |
| 290 | */ |
| 291 | test.provider( |
| 292 | "removing every model from a Worker deletes its classes and version", |
| 293 | (stack) => |
| 294 | Effect.gen(function* () { |
| 295 | yield* stack.destroy(); |
| 296 | const { worker } = yield* stack.deploy( |
| 297 | Effect.gen(function* () { |
| 298 | return { worker: yield* UpgradeWorker }; |
| 299 | }), |
| 300 | ); |
| 301 | const scriptName = worker.workerName; |
| 302 | yield* waitForVersion(worker.url!, 0); |
| 303 | expect(yield* tagsOf(scriptName)).toContain("alchemy:domain:v0"); |
| 304 | |
| 305 | yield* stack.deploy( |
| 306 | Effect.gen(function* () { |
| 307 | return { worker: yield* UpgradePlainWorker }; |
| 308 | }), |
| 309 | ); |
| 310 | expect(yield* namespacesOf(scriptName)).toEqual({}); |
| 311 | expect( |
| 312 | (yield* tagsOf(scriptName)).filter((tag) => tag.startsWith("alchemy:domain:")), |
| 313 | ).toEqual([]); |
| 314 | |
| 315 | yield* stack.destroy(); |
| 316 | }).pipe(logLevel), |
| 317 | { timeout: 300_000 }, |
| 318 | ); |