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 urlNormalization from "@distilled.cloud/cloudflare/url-normalization"; |
| 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 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 16 | |
| 17 | const resolveZoneId = Effect.gen(function* () { |
| 18 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 19 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 20 | if (!zone) { |
| 21 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 22 | } |
| 23 | return zone.id; |
| 24 | }); |
| 25 | |
| 26 | // The scoped API token the test harness mints propagates eventually- |
| 27 | // consistently across Cloudflare's edge — a fresh token intermittently 403s |
| 28 | // with "Unable to authenticate request". Ride out the blips on the test's |
| 29 | // own out-of-band calls by retrying the typed `Forbidden` error (part of |
| 30 | // each url-normalization operation's error union via distilled patches). |
| 31 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 32 | |
| 33 | const getUrlNormalization = (zoneId: string) => |
| 34 | urlNormalization.getUrlNormalization({ zoneId }).pipe( |
| 35 | Effect.retry({ |
| 36 | while: (e) => e._tag === "Forbidden", |
| 37 | schedule: forbiddenRetrySchedule, |
| 38 | times: 8, |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | // Normalize the singleton to Cloudflare defaults so each run starts from |
| 43 | // the same cloud state regardless of what a previous (possibly interrupted) |
| 44 | // run left behind. DELETE is the API's true reset operation. |
| 45 | const resetToDefaults = (zoneId: string) => |
| 46 | urlNormalization.deleteUrlNormalization({ zoneId }).pipe( |
| 47 | Effect.retry({ |
| 48 | while: (e) => e._tag === "Forbidden", |
| 49 | schedule: forbiddenRetrySchedule, |
| 50 | times: 8, |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | describe.sequential( |
| 55 | "UrlNormalization", |
| 56 | { |
| 57 | tags: [ |
| 58 | "provider:cloudflare", |
| 59 | "provider:cloudflare:urlnormalization", |
| 60 | "provider:cloudflare:zone", |
| 61 | "live", |
| 62 | ], |
| 63 | }, |
| 64 | () => { |
| 65 | test.provider("configures URL normalization and resets to defaults on destroy", (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | const zoneId = yield* resolveZoneId; |
| 68 | |
| 69 | yield* stack.destroy(); |
| 70 | yield* resetToDefaults(zoneId); |
| 71 | |
| 72 | const created = yield* stack.deploy( |
| 73 | Effect.gen(function* () { |
| 74 | return yield* Cloudflare.UrlNormalization.UrlNormalization("UrlNormalization", { |
| 75 | zoneId, |
| 76 | scope: "both", |
| 77 | type: "rfc3986", |
| 78 | }); |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | expect(created.zoneId).toEqual(zoneId); |
| 83 | expect(created.scope).toEqual("both"); |
| 84 | expect(created.type).toEqual("rfc3986"); |
| 85 | |
| 86 | // Out-of-band verification against the live API. |
| 87 | const live = yield* getUrlNormalization(zoneId); |
| 88 | expect(live.scope).toEqual("both"); |
| 89 | expect(live.type).toEqual("rfc3986"); |
| 90 | |
| 91 | yield* stack.destroy(); |
| 92 | |
| 93 | // Destroy issued the true reset op — the zone is back to Cloudflare |
| 94 | // defaults. |
| 95 | const reset = yield* getUrlNormalization(zoneId); |
| 96 | expect(reset.scope).toEqual("incoming"); |
| 97 | expect(reset.type).toEqual("cloudflare"); |
| 98 | }).pipe(logLevel), |
| 99 | ); |
| 100 | |
| 101 | test.provider("updates scope and type in place", (stack) => |
| 102 | Effect.gen(function* () { |
| 103 | const zoneId = yield* resolveZoneId; |
| 104 | |
| 105 | yield* stack.destroy(); |
| 106 | yield* resetToDefaults(zoneId); |
| 107 | |
| 108 | const initial = yield* stack.deploy( |
| 109 | Effect.gen(function* () { |
| 110 | return yield* Cloudflare.UrlNormalization.UrlNormalization("UrlNormalization", { |
| 111 | zoneId, |
| 112 | scope: "both", |
| 113 | type: "rfc3986", |
| 114 | }); |
| 115 | }), |
| 116 | ); |
| 117 | |
| 118 | expect(initial.scope).toEqual("both"); |
| 119 | expect(initial.type).toEqual("rfc3986"); |
| 120 | |
| 121 | // Same singleton updated in place via a full-replace PUT. |
| 122 | const updated = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | return yield* Cloudflare.UrlNormalization.UrlNormalization("UrlNormalization", { |
| 125 | zoneId, |
| 126 | scope: "incoming", |
| 127 | type: "cloudflare", |
| 128 | }); |
| 129 | }), |
| 130 | ); |
| 131 | |
| 132 | expect(updated.scope).toEqual("incoming"); |
| 133 | expect(updated.type).toEqual("cloudflare"); |
| 134 | |
| 135 | const live = yield* getUrlNormalization(zoneId); |
| 136 | expect(live.scope).toEqual("incoming"); |
| 137 | expect(live.type).toEqual("cloudflare"); |
| 138 | |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const reset = yield* getUrlNormalization(zoneId); |
| 142 | expect(reset.scope).toEqual("incoming"); |
| 143 | expect(reset.type).toEqual("cloudflare"); |
| 144 | }).pipe(logLevel), |
| 145 | ); |
| 146 | |
| 147 | test.provider("applies Cloudflare defaults when scope and type are omitted", (stack) => |
| 148 | Effect.gen(function* () { |
| 149 | const zoneId = yield* resolveZoneId; |
| 150 | |
| 151 | yield* stack.destroy(); |
| 152 | // Start from a non-default state so the deploy has to converge. |
| 153 | yield* urlNormalization |
| 154 | .putUrlNormalization({ zoneId, scope: "none", type: "rfc3986" }) |
| 155 | .pipe( |
| 156 | Effect.retry({ |
| 157 | while: (e) => e._tag === "Forbidden", |
| 158 | schedule: forbiddenRetrySchedule, |
| 159 | times: 8, |
| 160 | }), |
| 161 | ); |
| 162 | |
| 163 | const created = yield* stack.deploy( |
| 164 | Effect.gen(function* () { |
| 165 | return yield* Cloudflare.UrlNormalization.UrlNormalization("UrlNormalization", { |
| 166 | zoneId, |
| 167 | }); |
| 168 | }), |
| 169 | ); |
| 170 | |
| 171 | // Omitted props fall back to Cloudflare's documented defaults. |
| 172 | expect(created.scope).toEqual("incoming"); |
| 173 | expect(created.type).toEqual("cloudflare"); |
| 174 | |
| 175 | const live = yield* getUrlNormalization(zoneId); |
| 176 | expect(live.scope).toEqual("incoming"); |
| 177 | expect(live.type).toEqual("cloudflare"); |
| 178 | |
| 179 | yield* stack.destroy(); |
| 180 | |
| 181 | const reset = yield* getUrlNormalization(zoneId); |
| 182 | expect(reset.scope).toEqual("incoming"); |
| 183 | expect(reset.type).toEqual("cloudflare"); |
| 184 | }).pipe(logLevel), |
| 185 | ); |
| 186 | |
| 187 | // Canonical `list()` test (zone-scoped singleton): there is no account-wide |
| 188 | // API for this per-zone setting, so `list()` enumerates every zone via |
| 189 | // `listAllZones` and reads the singleton in each. Assert the result is |
| 190 | // non-empty and contains the standing test zone. |
| 191 | test.provider("list enumerates URL normalization across all zones", (stack) => |
| 192 | Effect.gen(function* () { |
| 193 | const zoneId = yield* resolveZoneId; |
| 194 | |
| 195 | const provider = yield* Provider.findProvider(Cloudflare.UrlNormalization.UrlNormalization); |
| 196 | const all = yield* provider.list(); |
| 197 | |
| 198 | expect(all.length).toBeGreaterThan(0); |
| 199 | expect(all.some((s) => s.zoneId === zoneId)).toBe(true); |
| 200 | |
| 201 | // `stack` is unused here (the singleton always exists on every zone), |
| 202 | // but keep the destroy bookend so the harness state stays clean. |
| 203 | yield* stack.destroy(); |
| 204 | }).pipe(logLevel), |
| 205 | ); |
| 206 | }, |
| 207 | ); |