| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@/Cloudflare/CloudflareEnvironment"; |
| 3 | import * as Provider from "@/Provider"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import * as workers from "@distilled.cloud/cloudflare/workers"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | // IMPORTANT: the workers.dev subdomain is account-global state — renaming it |
| 19 | // changes the URL of EVERY deployed Worker on the account. This suite is |
| 20 | // strictly READ-ONLY: it only ever deploys the resource pinned to the name |
| 21 | // the account already has, so reconcile and destroy both observe "already |
| 22 | // converged" and never issue a PUT/DELETE. |
| 23 | |
| 24 | // Freshly-minted scoped API tokens propagate eventually-consistently across |
| 25 | // Cloudflare's edge — ride out intermittent 403s on the test's own |
| 26 | // out-of-band calls by retrying the typed `Forbidden` error. |
| 27 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 28 | |
| 29 | const getLiveSubdomain = Effect.gen(function* () { |
| 30 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 31 | return yield* workers.getSubdomain({ accountId }).pipe( |
| 32 | Effect.map((r) => r.subdomain), |
| 33 | Effect.retry({ |
| 34 | while: (e) => e._tag === "Forbidden", |
| 35 | schedule: forbiddenRetrySchedule, |
| 36 | times: 8, |
| 37 | }), |
| 38 | ); |
| 39 | }); |
| 40 | |
| 41 | test.provider( |
| 42 | "adopts the account's existing workers.dev subdomain without mutating it", |
| 43 | (stack) => |
| 44 | Effect.gen(function* () { |
| 45 | yield* stack.destroy(); |
| 46 | |
| 47 | // Observe the account's live subdomain out-of-band. The test account |
| 48 | // always has one registered (every workers.dev deployment relies on |
| 49 | // it) — getSubdomain fails with the typed SubdomainNotFound otherwise, |
| 50 | // which would rightly fail this test. |
| 51 | const liveBefore = yield* getLiveSubdomain; |
| 52 | expect(liveBefore.length).toBeGreaterThan(0); |
| 53 | |
| 54 | // Deploy pinned to the EXISTING name — reconcile observes |
| 55 | // observed === news.subdomain and never issues a PUT. |
| 56 | const sub = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* Cloudflare.Workers.Subdomain("Subdomain", { |
| 59 | subdomain: liveBefore, |
| 60 | }); |
| 61 | }), |
| 62 | ); |
| 63 | |
| 64 | expect(sub.subdomain).toEqual(liveBefore); |
| 65 | // The pre-management name was captured for restore-on-destroy. |
| 66 | expect(sub.initialSubdomain).toEqual(liveBefore); |
| 67 | |
| 68 | // Re-deploy unchanged — still converged, still no mutation. |
| 69 | const again = yield* stack.deploy( |
| 70 | Effect.gen(function* () { |
| 71 | return yield* Cloudflare.Workers.Subdomain("Subdomain", { |
| 72 | subdomain: liveBefore, |
| 73 | }); |
| 74 | }), |
| 75 | ); |
| 76 | expect(again.subdomain).toEqual(liveBefore); |
| 77 | expect(again.initialSubdomain).toEqual(liveBefore); |
| 78 | |
| 79 | // Destroy — observed already equals initialSubdomain, so restore is |
| 80 | // a no-op and the account's subdomain is untouched. |
| 81 | yield* stack.destroy(); |
| 82 | |
| 83 | const liveAfter = yield* getLiveSubdomain; |
| 84 | expect(liveAfter).toEqual(liveBefore); |
| 85 | }).pipe(logLevel), |
| 86 | { |
| 87 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 88 | timeout: 120_000, |
| 89 | }, |
| 90 | ); |
| 91 | |
| 92 | // Canonical `list()` test (account-scoped singleton): the workers.dev |
| 93 | // subdomain is account-global with only a `get`, so `list()` returns a |
| 94 | // one-element array mirroring `read` when the account has a subdomain, or |
| 95 | // `[]` when it doesn't. The test account always has one registered, so we |
| 96 | // assert the result is a single element matching the live subdomain. This is |
| 97 | // strictly read-only and never mutates the account's subdomain. |
| 98 | test.provider( |
| 99 | "list returns the account's workers.dev subdomain singleton", |
| 100 | (stack) => |
| 101 | Effect.gen(function* () { |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | const liveBefore = yield* getLiveSubdomain; |
| 105 | expect(liveBefore.length).toBeGreaterThan(0); |
| 106 | |
| 107 | const provider = yield* Provider.findProvider( |
| 108 | Cloudflare.Workers.Subdomain, |
| 109 | ); |
| 110 | const all = yield* provider.list(); |
| 111 | |
| 112 | expect(all.length).toEqual(1); |
| 113 | expect(all[0].subdomain).toEqual(liveBefore); |
| 114 | expect(all[0].initialSubdomain).toEqual(liveBefore); |
| 115 | expect(typeof all[0].accountId).toEqual("string"); |
| 116 | |
| 117 | yield* stack.destroy(); |
| 118 | }).pipe(logLevel), |
| 119 | { |
| 120 | tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"], |
| 121 | timeout: 120_000, |
| 122 | }, |
| 123 | ); |