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 { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as web3 from "@distilled.cloud/cloudflare/web3"; |
| 7 | import * as zones from "@distilled.cloud/cloudflare/zones"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { testName } from "../../names.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 19 | |
| 20 | // Cloudflare has restricted Web3 gateways for new customers — on the |
| 21 | // standard testing account every create fails with "Account is not entitled |
| 22 | // to create ipfs hostnames." (code 1010), surfaced as the typed |
| 23 | // `Web3HostnameNotEntitled` error. The full lifecycle tests below are gated |
| 24 | // behind an entitled zone id supplied via env. |
| 25 | const entitledZoneId = process.env.CLOUDFLARE_TEST_WEB3_ZONE_ID; |
| 26 | |
| 27 | const resolveZoneId = Effect.gen(function* () { |
| 28 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 29 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 30 | if (!zone) { |
| 31 | return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`)); |
| 32 | } |
| 33 | return zone.id; |
| 34 | }); |
| 35 | |
| 36 | // A freshly minted scoped token propagates eventually-consistently across |
| 37 | // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls. |
| 38 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 39 | |
| 40 | const listHostnames = (zoneId: string) => |
| 41 | web3.listHostnames.items({ zoneId }).pipe( |
| 42 | Stream.runCollect, |
| 43 | Effect.map((chunk) => Array.from(chunk)), |
| 44 | Effect.retry({ |
| 45 | while: (e) => e._tag === "Forbidden", |
| 46 | schedule: forbiddenRetrySchedule, |
| 47 | times: 8, |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | const findHostname = (zoneId: string, name: string) => |
| 52 | listHostnames(zoneId).pipe( |
| 53 | Effect.map((hostnames) => hostnames.find((h) => h.name === name && h.status !== "deleting")), |
| 54 | ); |
| 55 | |
| 56 | const getHostname = (zoneId: string, hostnameId: string) => |
| 57 | web3.getHostname({ zoneId, identifier: hostnameId }).pipe( |
| 58 | Effect.retry({ |
| 59 | while: (e) => e._tag === "Forbidden", |
| 60 | schedule: forbiddenRetrySchedule, |
| 61 | times: 8, |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | // Purge leftovers from interrupted runs so tests start from a clean slate. |
| 66 | const purgeHostname = (zoneId: string, name: string) => |
| 67 | listHostnames(zoneId).pipe( |
| 68 | Effect.flatMap( |
| 69 | Effect.forEach((h) => |
| 70 | h.name === name && h.id |
| 71 | ? web3.deleteHostname({ zoneId, identifier: h.id }).pipe(Effect.catch(() => Effect.void)) |
| 72 | : Effect.void, |
| 73 | ), |
| 74 | ), |
| 75 | ); |
| 76 | |
| 77 | // Deletion is asynchronous — poll (bounded) until no live hostname with the |
| 78 | // given name remains in the zone. |
| 79 | const waitUntilGone = (zoneId: string, name: string) => |
| 80 | findHostname(zoneId, name).pipe( |
| 81 | Effect.repeat({ |
| 82 | schedule: Schedule.spaced("2 seconds"), |
| 83 | until: (h) => h === undefined, |
| 84 | times: 10, |
| 85 | }), |
| 86 | ); |
| 87 | |
| 88 | test.provider( |
| 89 | "surfaces the typed Web3HostnameNotEntitled error on unentitled accounts", |
| 90 | (stack) => |
| 91 | Effect.gen(function* () { |
| 92 | const zoneId = yield* resolveZoneId; |
| 93 | |
| 94 | yield* stack.destroy(); |
| 95 | |
| 96 | // The testing account lacks the Web3 gateway entitlement — the |
| 97 | // distilled call must fail with the typed entitlement tag. |
| 98 | const error = yield* web3 |
| 99 | .createHostname({ |
| 100 | zoneId, |
| 101 | name: `${testName("alchemy-web3-entitlement")}.${zoneName}`, |
| 102 | target: "ipfs", |
| 103 | dnslink: "/ipns/onboarding.ipfs.cloudflare.com", |
| 104 | }) |
| 105 | .pipe( |
| 106 | Effect.retry({ |
| 107 | while: (e) => e._tag === "Forbidden", |
| 108 | schedule: forbiddenRetrySchedule, |
| 109 | times: 8, |
| 110 | }), |
| 111 | Effect.flip, |
| 112 | ); |
| 113 | expect(error._tag).toEqual("Web3HostnameNotEntitled"); |
| 114 | |
| 115 | // The list endpoint is available regardless of entitlement. |
| 116 | const hostnames = yield* listHostnames(zoneId); |
| 117 | expect(Array.isArray(hostnames)).toBe(true); |
| 118 | |
| 119 | yield* stack.destroy(); |
| 120 | }).pipe(logLevel), |
| 121 | { tags: ["provider:cloudflare", "provider:cloudflare:web3", "provider:cloudflare:zone", "live"] }, |
| 122 | ); |
| 123 | |
| 124 | test.provider.skipIf(!entitledZoneId)( |
| 125 | "create, update in place, replace on target change, destroy", |
| 126 | (stack) => |
| 127 | Effect.gen(function* () { |
| 128 | const zoneId = entitledZoneId!; |
| 129 | const zone = yield* zones.getZone({ zoneId }); |
| 130 | const name = `${testName("alchemy-web3-hostname")}.${zone.name}`; |
| 131 | |
| 132 | yield* stack.destroy(); |
| 133 | yield* purgeHostname(zoneId, name); |
| 134 | |
| 135 | // Create an IPFS hostname pinned to a DNSLink. |
| 136 | const created = yield* stack.deploy( |
| 137 | Effect.gen(function* () { |
| 138 | return yield* Cloudflare.Web3.Hostname("Gateway", { |
| 139 | zoneId, |
| 140 | name, |
| 141 | target: "ipfs", |
| 142 | dnslink: "/ipns/onboarding.ipfs.cloudflare.com", |
| 143 | description: "v1", |
| 144 | }); |
| 145 | }), |
| 146 | ); |
| 147 | |
| 148 | expect(created.hostnameId).toBeDefined(); |
| 149 | expect(created.zoneId).toEqual(zoneId); |
| 150 | expect(created.name).toEqual(name); |
| 151 | expect(created.target).toEqual("ipfs"); |
| 152 | expect(created.dnslink).toEqual("/ipns/onboarding.ipfs.cloudflare.com"); |
| 153 | expect(created.description).toEqual("v1"); |
| 154 | expect(["active", "pending"]).toContain(created.status); |
| 155 | |
| 156 | // Out-of-band verification via the distilled API. |
| 157 | const live = yield* getHostname(zoneId, created.hostnameId); |
| 158 | expect(live.name).toEqual(name); |
| 159 | expect(live.target).toEqual("ipfs"); |
| 160 | |
| 161 | // Update mutable fields in place — same physical hostname. |
| 162 | const updated = yield* stack.deploy( |
| 163 | Effect.gen(function* () { |
| 164 | return yield* Cloudflare.Web3.Hostname("Gateway", { |
| 165 | zoneId, |
| 166 | name, |
| 167 | target: "ipfs", |
| 168 | dnslink: "/ipns/onboarding.ipfs.cloudflare.com", |
| 169 | description: "v2", |
| 170 | }); |
| 171 | }), |
| 172 | ); |
| 173 | expect(updated.hostnameId).toEqual(created.hostnameId); |
| 174 | expect(updated.description).toEqual("v2"); |
| 175 | |
| 176 | const patched = yield* getHostname(zoneId, updated.hostnameId); |
| 177 | expect(patched.description).toEqual("v2"); |
| 178 | |
| 179 | // Changing the target replaces the hostname. |
| 180 | const replaced = yield* stack.deploy( |
| 181 | Effect.gen(function* () { |
| 182 | return yield* Cloudflare.Web3.Hostname("Gateway", { zoneId, name, target: "ethereum" }); |
| 183 | }), |
| 184 | ); |
| 185 | expect(replaced.hostnameId).not.toEqual(created.hostnameId); |
| 186 | expect(replaced.target).toEqual("ethereum"); |
| 187 | |
| 188 | yield* stack.destroy(); |
| 189 | |
| 190 | // Deletion is asynchronous — wait (bounded) until the hostname is |
| 191 | // gone from the zone. |
| 192 | const gone = yield* waitUntilGone(zoneId, name); |
| 193 | expect(gone).toBeUndefined(); |
| 194 | }).pipe(logLevel), |
| 195 | { |
| 196 | tags: ["provider:cloudflare", "provider:cloudflare:web3", "provider:cloudflare:zone", "live"], |
| 197 | timeout: 120_000, |
| 198 | }, |
| 199 | ); |
| 200 | |
| 201 | test.provider.skipIf(!entitledZoneId)( |
| 202 | "content list — set entries, update declaratively, reset on destroy", |
| 203 | (stack) => |
| 204 | Effect.gen(function* () { |
| 205 | const zoneId = entitledZoneId!; |
| 206 | const zone = yield* zones.getZone({ zoneId }); |
| 207 | const name = `${testName("alchemy-web3-universal")}.${zone.name}`; |
| 208 | const cid = "QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco"; |
| 209 | |
| 210 | yield* stack.destroy(); |
| 211 | yield* purgeHostname(zoneId, name); |
| 212 | |
| 213 | const program = (entries: Cloudflare.Web3.ContentListEntry[]) => |
| 214 | Effect.gen(function* () { |
| 215 | const gateway = yield* Cloudflare.Web3.Hostname("Universal", { |
| 216 | zoneId, |
| 217 | name, |
| 218 | target: "ipfs_universal_path", |
| 219 | }); |
| 220 | const list = yield* Cloudflare.Web3.HostnameContentList("Blocklist", { |
| 221 | zoneId, |
| 222 | hostnameId: gateway.hostnameId, |
| 223 | entries, |
| 224 | }); |
| 225 | return { gateway, list }; |
| 226 | }); |
| 227 | |
| 228 | // Create the universal-path gateway plus a one-entry blocklist. |
| 229 | const created = yield* stack.deploy( |
| 230 | program([{ type: "cid", content: cid, description: "blocked" }]), |
| 231 | ); |
| 232 | expect(created.list.action).toEqual("block"); |
| 233 | expect(created.list.entries).toHaveLength(1); |
| 234 | expect(created.list.entries[0]?.content).toEqual(cid); |
| 235 | |
| 236 | // Out-of-band verification via the distilled API. |
| 237 | const liveEntries = yield* web3 |
| 238 | .listHostnameIpfsUniversalPathContentListEntries({ |
| 239 | zoneId, |
| 240 | identifier: created.gateway.hostnameId, |
| 241 | }) |
| 242 | .pipe( |
| 243 | Effect.retry({ |
| 244 | while: (e) => e._tag === "Forbidden", |
| 245 | schedule: forbiddenRetrySchedule, |
| 246 | times: 8, |
| 247 | }), |
| 248 | ); |
| 249 | expect(liveEntries.entries ?? []).toHaveLength(1); |
| 250 | expect(liveEntries.entries?.[0]?.content).toEqual(cid); |
| 251 | |
| 252 | // Declarative update — the bulk PUT replaces the whole list. |
| 253 | const updated = yield* stack.deploy( |
| 254 | program([{ type: "content_path", content: `/ipfs/${cid}/wiki` }]), |
| 255 | ); |
| 256 | expect(updated.list.entries).toHaveLength(1); |
| 257 | expect(updated.list.entries[0]?.type).toEqual("content_path"); |
| 258 | |
| 259 | yield* stack.destroy(); |
| 260 | |
| 261 | const gone = yield* waitUntilGone(zoneId, name); |
| 262 | expect(gone).toBeUndefined(); |
| 263 | }).pipe(logLevel), |
| 264 | { |
| 265 | tags: ["provider:cloudflare", "provider:cloudflare:web3", "provider:cloudflare:zone", "live"], |
| 266 | timeout: 120_000, |
| 267 | }, |
| 268 | ); |