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 { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup"; |
| 4 | import { describe, expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as rum from "@distilled.cloud/cloudflare/rum"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Use a different zone than Site.test.ts — both suites manage zone-scoped |
| 16 | // Web Analytics sites, and Cloudflare keys those to the zone, so running |
| 17 | // the two files in parallel against the same zone would interfere. |
| 18 | // |
| 19 | // RUM rules require a zone-based (orange-clouded) site: only those own an |
| 20 | // implicit ruleset (gray-clouded host-based sites do not). A zone-based site |
| 21 | // is account-unique per zone, so creating "another" for the same zone simply |
| 22 | // returns the existing site + ruleset. Every case in this file therefore |
| 23 | // shares the SAME site + ruleset on `alchemy-test-3.us`. Under the global |
| 24 | // concurrent test config that collision is fatal — a sibling's cold read |
| 25 | // adopts the shared site (`OwnedBySomeoneElse`) and all cases pile rules onto |
| 26 | // the one ruleset (account-wide `MaxRulesExceeded`). So they MUST run |
| 27 | // serially: `describe.sequential` keeps each create→update→delete fully |
| 28 | // isolated in time, and `cleanupLeftoverSites` self-heals against any |
| 29 | // foreign/leftover site for the zone before each deploy. |
| 30 | const zoneName = "alchemy-test-3.us"; |
| 31 | |
| 32 | // The scoped API token the test harness mints propagates eventually- |
| 33 | // consistently across Cloudflare's edge — ride out 403 blips |
| 34 | // (`Forbidden`, declared in the distilled error union) on the test's |
| 35 | // own out-of-band verification calls. |
| 36 | const listRules = (accountId: string, rulesetId: string) => |
| 37 | rum.listRules({ accountId, rulesetId }).pipe( |
| 38 | Effect.retry({ |
| 39 | while: (e) => e._tag === "Forbidden", |
| 40 | schedule: Schedule.exponential("500 millis"), |
| 41 | times: 8, |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | const findRule = (accountId: string, rulesetId: string, ruleId: string) => |
| 46 | listRules(accountId, rulesetId).pipe( |
| 47 | Effect.map((response) => (response.rules ?? []).find((rule) => rule.id === ruleId)), |
| 48 | ); |
| 49 | |
| 50 | // A deleted rule disappears from the ruleset's rule list; once the parent |
| 51 | // site is destroyed the whole ruleset is gone (`RulesetNotFound`). Both are |
| 52 | // the success condition here. Poll on a fixed cadence with a hard recur cap |
| 53 | // (~45s total) so that if the rule never disappears the test fails fast with |
| 54 | // a clear `RuleNotDeleted` predicate failure instead of an exponential |
| 55 | // backoff ballooning past the per-test timeout. |
| 56 | const expectGone = (accountId: string, rulesetId: string, ruleId: string) => |
| 57 | findRule(accountId, rulesetId, ruleId).pipe( |
| 58 | Effect.flatMap((rule) => |
| 59 | rule === undefined ? Effect.void : Effect.fail({ _tag: "RuleNotDeleted", ruleId } as const), |
| 60 | ), |
| 61 | Effect.catchTag("RulesetNotFound", () => Effect.void), |
| 62 | Effect.retry({ |
| 63 | while: (e) => e._tag === "RuleNotDeleted", |
| 64 | schedule: Schedule.spaced("3 seconds"), |
| 65 | times: 15, |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | // The free plan caps Web Analytics rule creation at a tiny account-wide |
| 70 | // quota (`MaxRulesExceeded`, HTTP 409 / code 10012 |
| 71 | // `web_analytics.configuration.api.maxRulesError`). Empirically the cap |
| 72 | // behaves like a rolling created-rules counter: deleting rules does not |
| 73 | // promptly free it, so repeated test runs can exhaust it for a while even |
| 74 | // when zero rules exist on the account. Ride out short contention with a |
| 75 | // bounded retry on the typed tag, then surface `undefined` so the test can |
| 76 | // skip gracefully instead of failing on a platform quota. |
| 77 | const retryingQuota = <A, R>(deploy: Effect.Effect<A, any, R>) => |
| 78 | deploy.pipe( |
| 79 | Effect.retry({ |
| 80 | while: (e) => e._tag === "MaxRulesExceeded", |
| 81 | schedule: Schedule.spaced("6 seconds"), |
| 82 | times: 5, |
| 83 | }), |
| 84 | Effect.catchTag("MaxRulesExceeded", () => Effect.succeed(undefined)), |
| 85 | ); |
| 86 | |
| 87 | const quotaExhausted = (stack: { destroy: () => Effect.Effect<void, any> }) => |
| 88 | Effect.gen(function* () { |
| 89 | yield* Effect.logWarning( |
| 90 | "skipping Rule assertions: account-wide Web Analytics rule quota " + |
| 91 | "is exhausted (MaxRulesExceeded, code 10012, " + |
| 92 | "web_analytics.configuration.api.maxRulesError)", |
| 93 | ); |
| 94 | yield* stack.destroy(); |
| 95 | }); |
| 96 | |
| 97 | // A failed earlier run (or a foreign stack) can leave a zone-based site |
| 98 | // measuring the test zone; the Site provider's cold read would then surface |
| 99 | // it as `Unowned` and refuse the deploy (`OwnedBySomeoneElse`). Delete any |
| 100 | // leftovers for this zone out-of-band before deploying so the test cannot |
| 101 | // wedge on a site it doesn't recognise as its own. |
| 102 | const cleanupLeftoverSites = (accountId: string, zoneTag: string) => |
| 103 | Effect.gen(function* () { |
| 104 | const sites = yield* rum.listSiteInfos({ accountId, perPage: 100 }).pipe( |
| 105 | Effect.retry({ |
| 106 | while: (e) => e._tag === "Forbidden", |
| 107 | schedule: Schedule.exponential("500 millis"), |
| 108 | times: 8, |
| 109 | }), |
| 110 | ); |
| 111 | for (const site of sites.result ?? []) { |
| 112 | if (site.ruleset?.zoneTag === zoneTag && site.siteTag) { |
| 113 | yield* rum |
| 114 | .deleteSiteInfo({ accountId, siteId: site.siteTag }) |
| 115 | .pipe(Effect.catchTag("SiteNotFound", () => Effect.void)); |
| 116 | } |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | // One program deploying the zone-based site (which owns the implicit |
| 121 | // ruleset) and the rule under it. The rule's `rulesetId` references the |
| 122 | // site's output, so the engine orders rule-last on deploy (and first on |
| 123 | // destroy). |
| 124 | const program = (zoneId: string, rule: Omit<Cloudflare.Rum.RuleProps, "rulesetId">) => |
| 125 | Effect.gen(function* () { |
| 126 | const site = yield* Cloudflare.Rum.Site("RuleSite", { zoneTag: zoneId }); |
| 127 | const ruleResource = yield* Cloudflare.Rum.Rule("Rule", { |
| 128 | rulesetId: site.rulesetId.as<string>(), |
| 129 | ...rule, |
| 130 | }); |
| 131 | return { site, rule: ruleResource }; |
| 132 | }); |
| 133 | |
| 134 | // These cases all manage the single account-unique zone-based site + |
| 135 | // ruleset on `alchemy-test-3.us`, so they must not run concurrently. |
| 136 | describe.sequential( |
| 137 | "Rule", |
| 138 | { tags: ["provider:cloudflare", "provider:cloudflare:rum", "provider:cloudflare:zone", "live"] }, |
| 139 | () => { |
| 140 | test.provider("create, update in place, and delete a rule", (stack) => |
| 141 | Effect.gen(function* () { |
| 142 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 143 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 144 | if (!zone) { |
| 145 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 146 | } |
| 147 | |
| 148 | yield* stack.destroy(); |
| 149 | yield* cleanupLeftoverSites(accountId, zone.id); |
| 150 | |
| 151 | const initial = yield* retryingQuota( |
| 152 | stack.deploy(program(zone.id, { host: zoneName, paths: ["/blog/*"], inclusive: false })), |
| 153 | ); |
| 154 | if (initial === undefined) return yield* quotaExhausted(stack); |
| 155 | |
| 156 | expect(initial.site.rulesetId).toBeTruthy(); |
| 157 | expect(initial.rule.id).toBeTruthy(); |
| 158 | expect(initial.rule.rulesetId).toEqual(initial.site.rulesetId); |
| 159 | expect(initial.rule.accountId).toEqual(accountId); |
| 160 | expect(initial.rule.host).toEqual(zoneName); |
| 161 | expect(initial.rule.paths).toEqual(["/blog/*"]); |
| 162 | expect(initial.rule.inclusive).toEqual(false); |
| 163 | expect(initial.rule.isPaused).toEqual(false); |
| 164 | |
| 165 | // Verify out-of-band against the live API. |
| 166 | const live = yield* findRule(accountId, initial.rule.rulesetId, initial.rule.id); |
| 167 | expect(live).toBeDefined(); |
| 168 | expect(live?.host).toEqual(zoneName); |
| 169 | expect(live?.paths).toEqual(["/blog/*"]); |
| 170 | expect(live?.inclusive).toEqual(false); |
| 171 | |
| 172 | // Update paths and pause the rule in place — same rule id. |
| 173 | const updated = yield* stack.deploy( |
| 174 | program(zone.id, { |
| 175 | host: zoneName, |
| 176 | paths: ["/blog/*", "/admin/*"], |
| 177 | inclusive: false, |
| 178 | isPaused: true, |
| 179 | }), |
| 180 | ); |
| 181 | expect(updated.rule.id).toEqual(initial.rule.id); |
| 182 | expect(updated.rule.paths).toEqual(["/blog/*", "/admin/*"]); |
| 183 | expect(updated.rule.isPaused).toEqual(true); |
| 184 | |
| 185 | const liveUpdated = yield* findRule(accountId, updated.rule.rulesetId, updated.rule.id); |
| 186 | expect(liveUpdated?.paths).toEqual(["/blog/*", "/admin/*"]); |
| 187 | expect(liveUpdated?.isPaused).toEqual(true); |
| 188 | |
| 189 | // Redeploying identical props is a no-op (still the same rule). |
| 190 | const noop = yield* stack.deploy( |
| 191 | program(zone.id, { |
| 192 | host: zoneName, |
| 193 | paths: ["/blog/*", "/admin/*"], |
| 194 | inclusive: false, |
| 195 | isPaused: true, |
| 196 | }), |
| 197 | ); |
| 198 | expect(noop.rule.id).toEqual(initial.rule.id); |
| 199 | |
| 200 | const rulesetId = initial.rule.rulesetId; |
| 201 | yield* stack.destroy(); |
| 202 | |
| 203 | yield* expectGone(accountId, rulesetId, initial.rule.id); |
| 204 | }).pipe(logLevel), |
| 205 | ); |
| 206 | |
| 207 | // Canonical `list()` test: rules live under a Site's implicit ruleset and |
| 208 | // there is no account-wide rule list, so `list()` enumerates every site |
| 209 | // (paginated) and fans out the per-ruleset rule list. Deploy a real rule, |
| 210 | // then assert it appears in the exhaustively-enumerated result. Gated behind |
| 211 | // the same account-wide `MaxRulesExceeded` quota the other cases handle. |
| 212 | test.provider("list enumerates rules across all rulesets", (stack) => |
| 213 | Effect.gen(function* () { |
| 214 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 215 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 216 | if (!zone) { |
| 217 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 218 | } |
| 219 | |
| 220 | yield* stack.destroy(); |
| 221 | yield* cleanupLeftoverSites(accountId, zone.id); |
| 222 | |
| 223 | const deployed = yield* retryingQuota( |
| 224 | stack.deploy( |
| 225 | program(zone.id, { host: zoneName, paths: ["/list-test/*"], inclusive: false }), |
| 226 | ), |
| 227 | ); |
| 228 | if (deployed === undefined) return yield* quotaExhausted(stack); |
| 229 | |
| 230 | const provider = yield* Provider.findProvider(Cloudflare.Rum.Rule); |
| 231 | const all = yield* provider.list(); |
| 232 | |
| 233 | expect( |
| 234 | all.some((r) => r.id === deployed.rule.id && r.rulesetId === deployed.rule.rulesetId), |
| 235 | ).toBe(true); |
| 236 | |
| 237 | yield* stack.destroy(); |
| 238 | }).pipe(logLevel), |
| 239 | ); |
| 240 | |
| 241 | test.provider("recreates after out-of-band delete", (stack) => |
| 242 | Effect.gen(function* () { |
| 243 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 244 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 245 | if (!zone) { |
| 246 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 247 | } |
| 248 | |
| 249 | yield* stack.destroy(); |
| 250 | yield* cleanupLeftoverSites(accountId, zone.id); |
| 251 | |
| 252 | const initial = yield* retryingQuota( |
| 253 | stack.deploy(program(zone.id, { host: zoneName, paths: ["/heal/*"], inclusive: false })), |
| 254 | ); |
| 255 | if (initial === undefined) return yield* quotaExhausted(stack); |
| 256 | |
| 257 | // Delete the rule out-of-band. A redeploy with identical props is a |
| 258 | // planner no-op, so change a prop to force reconcile — it must observe |
| 259 | // the rule as missing and recreate it instead of failing. |
| 260 | yield* rum |
| 261 | .deleteRule({ accountId, rulesetId: initial.rule.rulesetId, ruleId: initial.rule.id }) |
| 262 | .pipe( |
| 263 | Effect.retry({ |
| 264 | while: (e) => e._tag === "Forbidden", |
| 265 | schedule: Schedule.exponential("500 millis"), |
| 266 | times: 8, |
| 267 | }), |
| 268 | ); |
| 269 | |
| 270 | const healed = yield* retryingQuota( |
| 271 | stack.deploy( |
| 272 | program(zone.id, { host: zoneName, paths: ["/heal-v2/*"], inclusive: false }), |
| 273 | ), |
| 274 | ); |
| 275 | if (healed === undefined) return yield* quotaExhausted(stack); |
| 276 | |
| 277 | expect(healed.rule.id).not.toEqual(initial.rule.id); |
| 278 | expect(healed.rule.paths).toEqual(["/heal-v2/*"]); |
| 279 | |
| 280 | const live = yield* findRule(accountId, healed.rule.rulesetId, healed.rule.id); |
| 281 | expect(live?.paths).toEqual(["/heal-v2/*"]); |
| 282 | |
| 283 | const rulesetId = healed.rule.rulesetId; |
| 284 | yield* stack.destroy(); |
| 285 | |
| 286 | yield* expectGone(accountId, rulesetId, healed.rule.id); |
| 287 | }).pipe(logLevel), |
| 288 | ); |
| 289 | }, |
| 290 | ); |