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 { WorkerVersionConfigError } from "@alchemy.run/cloudflare/Workers/WorkerProvider"; |
| 4 | import { describe, expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as workers from "@distilled.cloud/cloudflare/workers"; |
| 7 | import { State } from "alchemy/State"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as pathe from "pathe"; |
| 12 | import { expectUrlContains } from "../Utils/Http.ts"; |
| 13 | import { waitForWorkerToBeDeleted } from "../Utils/Worker.ts"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 16 | |
| 17 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 18 | |
| 19 | const script = (marker: string) => |
| 20 | `export default { fetch() { return new Response("${marker}"); } };`; |
| 21 | |
| 22 | /** |
| 23 | * The most recent deployment of a script — Cloudflare returns deployments |
| 24 | * newest-first. Retried briefly: a deployment created moments ago can lag |
| 25 | * the list endpoint. |
| 26 | */ |
| 27 | const latestDeployment = Effect.fn(function* (scriptName: string) { |
| 28 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 29 | const { deployments } = yield* workers.listScriptDeployments({ accountId, scriptName }); |
| 30 | return deployments[0]; |
| 31 | }); |
| 32 | |
| 33 | describe.concurrent( |
| 34 | "Cloudflare.Worker version", |
| 35 | { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] }, |
| 36 | () => { |
| 37 | for (const traffic of [undefined, 100] as const) { |
| 38 | test.provider( |
| 39 | `full deployment preserves annotations with traffic ${traffic ?? "omitted"}`, |
| 40 | (stack) => |
| 41 | Effect.gen(function* () { |
| 42 | yield* stack.destroy(); |
| 43 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 44 | |
| 45 | // Check both the initial upload and an update to the same script. |
| 46 | for (const tag of ["v1.0.0", "v1.0.1"]) { |
| 47 | const message = `Release ${tag}`; |
| 48 | const worker = yield* stack.deploy( |
| 49 | Cloudflare.Worker("AnnotatedWorker", { |
| 50 | script: script(tag), |
| 51 | version: { traffic, tag, message }, |
| 52 | }), |
| 53 | ); |
| 54 | const { deployment, version } = yield* Effect.gen(function* () { |
| 55 | const deployment = yield* latestDeployment(worker.workerName); |
| 56 | const version = yield* workers.getScriptScriptAndVersionSetting({ |
| 57 | accountId, |
| 58 | scriptName: worker.workerName, |
| 59 | }); |
| 60 | return { deployment, version }; |
| 61 | }).pipe( |
| 62 | Effect.repeat({ |
| 63 | schedule: Schedule.spaced("1 second"), |
| 64 | times: 10, |
| 65 | until: ({ version }) => |
| 66 | version.annotations?.workersTag === tag && |
| 67 | version.annotations?.workersMessage === message, |
| 68 | }), |
| 69 | ); |
| 70 | expect(deployment?.versions).toHaveLength(1); |
| 71 | expect(deployment?.versions[0].percentage).toEqual(100); |
| 72 | expect(worker.deploymentId).toBeDefined(); |
| 73 | expect(worker.versionId).toBeDefined(); |
| 74 | expect(worker.deploymentId).toEqual(deployment?.id); |
| 75 | expect(worker.versionId).toEqual(deployment?.versions[0].versionId); |
| 76 | expect(version.annotations?.workersTag).toEqual(tag); |
| 77 | expect(version.annotations?.workersMessage).toEqual(message); |
| 78 | } |
| 79 | |
| 80 | yield* stack.destroy(); |
| 81 | }).pipe(logLevel), |
| 82 | { timeout: 120_000 }, |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | test.provider( |
| 87 | "preview version of a parent worker, promoted to a canary, then released", |
| 88 | (stack) => |
| 89 | Effect.gen(function* () { |
| 90 | yield* stack.destroy(); |
| 91 | |
| 92 | // Deploy a parent worker and a zero-traffic preview version of it |
| 93 | // in one stack — `version.parent` takes the parent resource, which |
| 94 | // also orders the deploy (parent first) and the destroy (version |
| 95 | // released before the parent script is deleted). |
| 96 | const v1 = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | const parent = yield* Cloudflare.Worker("VersionParent", { |
| 99 | script: script("parent-marker-v1"), |
| 100 | }); |
| 101 | const preview = yield* Cloudflare.Worker("VersionPreview", { |
| 102 | script: script("preview-marker-v1"), |
| 103 | version: { parent, message: "alchemy preview test" }, |
| 104 | }); |
| 105 | return { parent, preview }; |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | // The preview owns no script — it is a version of the parent's. |
| 110 | expect(v1.preview.versionOf).toEqual(v1.parent.workerName); |
| 111 | expect(v1.preview.workerName).toEqual(v1.parent.workerName); |
| 112 | expect(v1.preview.versionId).toBeDefined(); |
| 113 | // Zero traffic: no deployment was created. |
| 114 | expect(v1.preview.deploymentId).toBeUndefined(); |
| 115 | // Primary URL is the *aliased* preview URL (stable across deploys); |
| 116 | // the per-version URL (`<version-prefix>-...`) rides in urls. |
| 117 | expect(v1.preview.versionAlias).toBeDefined(); |
| 118 | expect(v1.preview.url).toEqual( |
| 119 | expect.stringContaining(`https://${v1.preview.versionAlias}-${v1.parent.workerName}.`), |
| 120 | ); |
| 121 | expect(v1.preview.urls[1]).toMatch( |
| 122 | new RegExp(`^https://[0-9a-f]{8}-${v1.parent.workerName}\\.`), |
| 123 | ); |
| 124 | |
| 125 | // The parent's live deployment is untouched (100% on its own |
| 126 | // version, not the preview's). |
| 127 | const liveAfterPreview = yield* latestDeployment(v1.parent.workerName); |
| 128 | expect(liveAfterPreview?.versions).toHaveLength(1); |
| 129 | expect(liveAfterPreview?.versions[0].percentage).toEqual(100); |
| 130 | expect(liveAfterPreview?.versions[0].versionId).not.toEqual(v1.preview.versionId); |
| 131 | |
| 132 | // Both URLs serve their own code. |
| 133 | yield* expectUrlContains(v1.parent.url!, "parent-marker-v1", { |
| 134 | label: "parent serves its own code", |
| 135 | }); |
| 136 | yield* expectUrlContains(v1.preview.url!, "preview-marker-v1", { |
| 137 | label: "preview URL serves the version's code", |
| 138 | }); |
| 139 | |
| 140 | // Promote the version to a canary at 25% of the parent's traffic. |
| 141 | const v2 = yield* stack.deploy( |
| 142 | Effect.gen(function* () { |
| 143 | const parent = yield* Cloudflare.Worker("VersionParent", { |
| 144 | script: script("parent-marker-v1"), |
| 145 | }); |
| 146 | const canary = yield* Cloudflare.Worker("VersionPreview", { |
| 147 | script: script("preview-marker-v1"), |
| 148 | version: { parent, traffic: 25 }, |
| 149 | }); |
| 150 | return { parent, canary }; |
| 151 | }), |
| 152 | ); |
| 153 | |
| 154 | expect(v2.canary.versionId).toBeDefined(); |
| 155 | expect(v2.canary.deploymentId).toBeDefined(); |
| 156 | // The aliased preview URL is stable: the same alias re-points at |
| 157 | // the newly uploaded version. |
| 158 | expect(v2.canary.versionAlias).toEqual(v1.preview.versionAlias); |
| 159 | expect(v2.canary.url).toEqual(v1.preview.url); |
| 160 | const liveWithCanary = yield* latestDeployment(v2.parent.workerName); |
| 161 | expect(liveWithCanary?.id).toEqual(v2.canary.deploymentId); |
| 162 | expect(liveWithCanary?.versions.map((v) => v.percentage).sort()).toEqual([25, 75]); |
| 163 | expect( |
| 164 | liveWithCanary?.versions.find((v) => v.versionId === v2.canary.versionId)?.percentage, |
| 165 | ).toEqual(25); |
| 166 | |
| 167 | // Remove the canary from the stack — delete restores 100% of |
| 168 | // traffic to the version that held the majority. |
| 169 | const v3 = yield* stack.deploy( |
| 170 | Effect.gen(function* () { |
| 171 | const parent = yield* Cloudflare.Worker("VersionParent", { |
| 172 | script: script("parent-marker-v1"), |
| 173 | }); |
| 174 | return { parent }; |
| 175 | }), |
| 176 | ); |
| 177 | |
| 178 | const liveAfterRelease = yield* latestDeployment(v3.parent.workerName); |
| 179 | expect(liveAfterRelease?.versions).toHaveLength(1); |
| 180 | expect(liveAfterRelease?.versions[0].percentage).toEqual(100); |
| 181 | expect(liveAfterRelease?.versions[0].versionId).not.toEqual(v2.canary.versionId); |
| 182 | yield* expectUrlContains(v3.parent.url!, "parent-marker-v1", { |
| 183 | label: "parent still serves its own code after canary release", |
| 184 | }); |
| 185 | |
| 186 | yield* stack.destroy(); |
| 187 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 188 | yield* waitForWorkerToBeDeleted(v3.parent.workerName, accountId); |
| 189 | }).pipe(logLevel), |
| 190 | { timeout: 300_000 }, |
| 191 | ); |
| 192 | |
| 193 | test.provider( |
| 194 | "preview version carries its own static assets", |
| 195 | (stack) => |
| 196 | Effect.gen(function* () { |
| 197 | yield* stack.destroy(); |
| 198 | |
| 199 | const { parent, preview } = yield* stack.deploy( |
| 200 | Effect.gen(function* () { |
| 201 | const parent = yield* Cloudflare.Worker("AssetsVersionParent", { |
| 202 | script: script("assets-parent-marker"), |
| 203 | }); |
| 204 | const preview = yield* Cloudflare.Worker("AssetsVersionPreview", { |
| 205 | script: |
| 206 | "export default { fetch(request, env) { return env.ASSETS.fetch(request); } };", |
| 207 | assets: { |
| 208 | directory: pathe.resolve(import.meta.dirname, "assets"), |
| 209 | runWorkerFirst: true, |
| 210 | }, |
| 211 | version: { parent }, |
| 212 | }); |
| 213 | return { parent, preview }; |
| 214 | }), |
| 215 | ); |
| 216 | |
| 217 | expect(preview.versionOf).toEqual(parent.workerName); |
| 218 | expect(preview.hash?.assets).toBeDefined(); |
| 219 | yield* expectUrlContains( |
| 220 | new URL("/test.txt", preview.url!).toString(), |
| 221 | "This is a test file from assets.", |
| 222 | { label: "preview URL serves the version's static assets" }, |
| 223 | ); |
| 224 | yield* expectUrlContains(parent.url!, "assets-parent-marker", { |
| 225 | label: "preview assets leave the parent deployment untouched", |
| 226 | }); |
| 227 | |
| 228 | yield* stack.destroy(); |
| 229 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 230 | yield* waitForWorkerToBeDeleted(parent.workerName, accountId); |
| 231 | }).pipe(logLevel), |
| 232 | { timeout: 240_000 }, |
| 233 | ); |
| 234 | |
| 235 | test.provider( |
| 236 | "version.parent accepts a literal script name", |
| 237 | (stack) => |
| 238 | Effect.gen(function* () { |
| 239 | yield* stack.destroy(); |
| 240 | |
| 241 | const { parent } = yield* stack.deploy( |
| 242 | Effect.gen(function* () { |
| 243 | const parent = yield* Cloudflare.Worker("StringParent", { |
| 244 | script: script("string-parent-v1"), |
| 245 | }); |
| 246 | return { parent }; |
| 247 | }), |
| 248 | ); |
| 249 | |
| 250 | // Reference the parent by its physical script name — the escape |
| 251 | // hatch for scripts not managed in any reachable stack state. |
| 252 | const parentName = parent.workerName; |
| 253 | const { preview } = yield* stack.deploy( |
| 254 | Effect.gen(function* () { |
| 255 | yield* Cloudflare.Worker("StringParent", { script: script("string-parent-v1") }); |
| 256 | const preview = yield* Cloudflare.Worker("StringPreview", { |
| 257 | script: script("string-preview-v1"), |
| 258 | version: { parent: parentName }, |
| 259 | }); |
| 260 | return { preview }; |
| 261 | }), |
| 262 | ); |
| 263 | |
| 264 | expect(preview.versionOf).toEqual(parentName); |
| 265 | yield* expectUrlContains(preview.url!, "string-preview-v1", { |
| 266 | label: "string-parent preview URL serves the version's code", |
| 267 | }); |
| 268 | |
| 269 | yield* stack.destroy(); |
| 270 | }).pipe(logLevel), |
| 271 | { timeout: 240_000 }, |
| 272 | ); |
| 273 | |
| 274 | test.provider( |
| 275 | "gradual rollout splits traffic between a worker's own versions", |
| 276 | (stack) => |
| 277 | Effect.gen(function* () { |
| 278 | yield* stack.destroy(); |
| 279 | |
| 280 | // First deploy: version.traffic is set, but there is no previous |
| 281 | // live version to split against — deploys at 100%. |
| 282 | const v1 = yield* stack.deploy( |
| 283 | Effect.gen(function* () { |
| 284 | return yield* Cloudflare.Worker("RolloutWorker", { |
| 285 | script: script("rollout-v1"), |
| 286 | version: { traffic: 50 }, |
| 287 | }); |
| 288 | }), |
| 289 | ); |
| 290 | const liveV1 = yield* latestDeployment(v1.workerName); |
| 291 | expect(liveV1?.versions).toHaveLength(1); |
| 292 | expect(liveV1?.versions[0].percentage).toEqual(100); |
| 293 | yield* expectUrlContains(v1.url!, "rollout-v1", { label: "first deploy serves at 100%" }); |
| 294 | |
| 295 | // Second deploy with new code at 50%: the new version and the |
| 296 | // previously-live version split the traffic. |
| 297 | const v2 = yield* stack.deploy( |
| 298 | Effect.gen(function* () { |
| 299 | return yield* Cloudflare.Worker("RolloutWorker", { |
| 300 | script: script("rollout-v2"), |
| 301 | version: { traffic: 50, message: "canary rollout" }, |
| 302 | }); |
| 303 | }), |
| 304 | ); |
| 305 | expect(v2.versionId).toBeDefined(); |
| 306 | expect(v2.deploymentId).toBeDefined(); |
| 307 | const liveV2 = yield* latestDeployment(v2.workerName); |
| 308 | expect(liveV2?.id).toEqual(v2.deploymentId); |
| 309 | expect(liveV2?.versions.map((v) => v.percentage).sort()).toEqual([50, 50]); |
| 310 | expect(liveV2?.versions.some((v) => v.versionId === v2.versionId)).toBe(true); |
| 311 | // urls ordering during a rollout: the stable workers.dev URL stays |
| 312 | // primary; the uploaded version's preview URL trails. |
| 313 | expect(v2.urls[0]).toMatch(new RegExp(`^https://${v2.workerName}\\..*\\.workers\\.dev$`)); |
| 314 | expect(v2.urls[v2.urls.length - 1]).toMatch( |
| 315 | new RegExp( |
| 316 | `^https://${v2.versionId!.split("-")[0]}-${v2.workerName}\\..*\\.workers\\.dev$`, |
| 317 | ), |
| 318 | ); |
| 319 | |
| 320 | // Promote: traffic back to the default full cutover. |
| 321 | const v3 = yield* stack.deploy( |
| 322 | Effect.gen(function* () { |
| 323 | return yield* Cloudflare.Worker("RolloutWorker", { script: script("rollout-v3") }); |
| 324 | }), |
| 325 | ); |
| 326 | const liveV3 = yield* latestDeployment(v3.workerName); |
| 327 | expect(liveV3?.versions).toHaveLength(1); |
| 328 | expect(liveV3?.versions[0].percentage).toEqual(100); |
| 329 | yield* expectUrlContains(v3.url!, "rollout-v3", { |
| 330 | label: "full deploy serves the new version everywhere", |
| 331 | }); |
| 332 | |
| 333 | yield* stack.destroy(); |
| 334 | }).pipe(logLevel), |
| 335 | { timeout: 300_000 }, |
| 336 | ); |
| 337 | |
| 338 | test.provider( |
| 339 | "Worker.URL on a version worker resolves to the aliased preview URL", |
| 340 | (stack) => |
| 341 | Effect.gen(function* () { |
| 342 | yield* stack.destroy(); |
| 343 | |
| 344 | const { preview } = yield* stack.deploy( |
| 345 | Effect.gen(function* () { |
| 346 | const parent = yield* Cloudflare.Worker("SelfUrlParent", { |
| 347 | script: script("self-url-parent-v1"), |
| 348 | }); |
| 349 | // Async version worker binding its own URL via env — the |
| 350 | // self_url sentinel must lower into the aliased preview URL, |
| 351 | // which is known before the version exists. |
| 352 | const preview = yield* Cloudflare.Worker("SelfUrlPreview", { |
| 353 | script: `export default { fetch(request, env) { return new Response(env.PUBLIC_URL); } };`, |
| 354 | env: { PUBLIC_URL: Cloudflare.Worker.URL }, |
| 355 | version: { parent }, |
| 356 | }); |
| 357 | return { preview }; |
| 358 | }), |
| 359 | ); |
| 360 | |
| 361 | expect(preview.versionAlias).toBeDefined(); |
| 362 | expect(preview.url).toEqual(expect.stringContaining(`https://${preview.versionAlias}-`)); |
| 363 | // The deployed version reports its own aliased preview URL. |
| 364 | yield* expectUrlContains(preview.url!, preview.url!, { |
| 365 | label: "version's PUBLIC_URL equals its aliased preview URL", |
| 366 | }); |
| 367 | |
| 368 | yield* stack.destroy(); |
| 369 | }).pipe(logLevel), |
| 370 | { timeout: 240_000 }, |
| 371 | ); |
| 372 | |
| 373 | // State-migration: version workers deployed by the pre-url-redesign |
| 374 | // provider (#948) persisted their preview URLs in a `domains` list with |
| 375 | // no `urls`/`domain` attributes, and a metadata hash computed over the |
| 376 | // old url/subdomain surface. A props-identical redeploy must migrate the |
| 377 | // record as a one-time update and settle back to noop. |
| 378 | test.provider( |
| 379 | "props-identical redeploy migrates pre-redesign version-worker state", |
| 380 | (stack) => |
| 381 | Effect.gen(function* () { |
| 382 | yield* stack.destroy(); |
| 383 | |
| 384 | const program = Effect.gen(function* () { |
| 385 | const parent = yield* Cloudflare.Worker("MigrParent", { |
| 386 | script: script("migr-parent-v1"), |
| 387 | }); |
| 388 | const preview = yield* Cloudflare.Worker("MigrPreview", { |
| 389 | script: script("migr-preview-v1"), |
| 390 | version: { parent }, |
| 391 | }); |
| 392 | return { parent, preview }; |
| 393 | }); |
| 394 | |
| 395 | const actionOf = (plan: any, logicalId: string) => |
| 396 | (Object.values(plan.resources) as any[]).find( |
| 397 | (node: any) => node.resource.LogicalId === logicalId, |
| 398 | )?.action; |
| 399 | |
| 400 | const v1 = yield* stack.deploy(program); |
| 401 | expect(v1.preview.urls).toHaveLength(2); |
| 402 | |
| 403 | // Rewrite the version worker's record to the #948-era shape: |
| 404 | // preview URLs in `domains`, no `urls`/`domain`, legacy hash. |
| 405 | yield* Effect.gen(function* () { |
| 406 | const state = yield* yield* State; |
| 407 | const key = { stack: stack.name, stage: stack.stage, fqn: "MigrPreview" }; |
| 408 | const current = yield* state.get(key); |
| 409 | expect(current).toBeDefined(); |
| 410 | const attr = { |
| 411 | ...(current as any).attr, |
| 412 | domains: [...(current as any).attr.urls], |
| 413 | hash: { ...(current as any).attr.hash, metadata: "legacy" }, |
| 414 | }; |
| 415 | delete attr.urls; |
| 416 | delete attr.domain; |
| 417 | yield* state.set({ ...key, value: { ...(current as any), attr } }); |
| 418 | }).pipe(Effect.provide(stack.state)); |
| 419 | |
| 420 | // Identical props still plan as an update, driven by the metadata |
| 421 | // hash surface change alone. |
| 422 | const migrationPlan = yield* stack.plan(program); |
| 423 | expect(actionOf(migrationPlan, "MigrPreview")).toBe("update"); |
| 424 | |
| 425 | const v2 = yield* stack.deploy(program); |
| 426 | // The aliased preview URL is deterministic, so it survives the |
| 427 | // migration byte-for-byte; the per-version URL is re-minted by the |
| 428 | // migration's version upload. |
| 429 | expect(v2.preview.urls[0]).toEqual(v1.preview.urls[0]); |
| 430 | expect(v2.preview.urls[1]).toMatch( |
| 431 | new RegExp(`^https://[0-9a-f]{8}-${v2.parent.workerName}\\.`), |
| 432 | ); |
| 433 | expect(v2.preview.domain).toBeUndefined(); |
| 434 | |
| 435 | // One-time: the same program now settles as a full noop. |
| 436 | const settledPlan = yield* stack.plan(program); |
| 437 | expect(actionOf(settledPlan, "MigrParent")).toBe("noop"); |
| 438 | expect(actionOf(settledPlan, "MigrPreview")).toBe("noop"); |
| 439 | |
| 440 | yield* stack.destroy(); |
| 441 | }).pipe(logLevel), |
| 442 | { timeout: 300_000 }, |
| 443 | ); |
| 444 | |
| 445 | test.provider( |
| 446 | "rejects script-level settings on a version worker", |
| 447 | (stack) => |
| 448 | Effect.gen(function* () { |
| 449 | yield* stack.destroy(); |
| 450 | |
| 451 | const { parent } = yield* stack.deploy( |
| 452 | Effect.gen(function* () { |
| 453 | const parent = yield* Cloudflare.Worker("InvalidParent", { |
| 454 | script: script("invalid-parent-v1"), |
| 455 | }); |
| 456 | return { parent }; |
| 457 | }), |
| 458 | ); |
| 459 | |
| 460 | const error = yield* stack |
| 461 | .deploy( |
| 462 | Effect.gen(function* () { |
| 463 | yield* Cloudflare.Worker("InvalidParent", { script: script("invalid-parent-v1") }); |
| 464 | return yield* Cloudflare.Worker("InvalidPreview", { |
| 465 | script: script("invalid-preview-v1"), |
| 466 | version: { parent: parent.workerName }, |
| 467 | crons: ["*/5 * * * *"], |
| 468 | }); |
| 469 | }), |
| 470 | ) |
| 471 | .pipe(Effect.flip); |
| 472 | |
| 473 | expect(error).toBeInstanceOf(WorkerVersionConfigError); |
| 474 | expect(String(error)).toContain("script-level settings"); |
| 475 | |
| 476 | yield* stack.destroy(); |
| 477 | }).pipe(logLevel), |
| 478 | { timeout: 180_000 }, |
| 479 | ); |
| 480 | }, |
| 481 | ); |