| 1 | import { adopt } from "@/AdoptPolicy"; |
| 2 | import * as Cloudflare from "@/Cloudflare"; |
| 3 | import { CloudflareEnvironment } from "@/Cloudflare/CloudflareEnvironment"; |
| 4 | import { findZoneByName } from "@/Cloudflare/Zone/lookup"; |
| 5 | import * as Provider from "@/Provider"; |
| 6 | import * as Test from "@/Test/Alchemy"; |
| 7 | import { expect } from "alchemy-test"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { MinimumLogLevel } from "effect/References"; |
| 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 | const zoneName = |
| 19 | process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us"; |
| 20 | |
| 21 | const resolveZone = Effect.gen(function* () { |
| 22 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 23 | const zone = yield* findZoneByName({ accountId, name: zoneName }); |
| 24 | if (!zone) { |
| 25 | return yield* Effect.die( |
| 26 | new Error(`zone "${zoneName}" not found in account`), |
| 27 | ); |
| 28 | } |
| 29 | return zone; |
| 30 | }); |
| 31 | |
| 32 | // Cloudflare has restricted Web3 gateways for new customers; on accounts |
| 33 | // without the entitlement, creation fails with the typed |
| 34 | // `Web3HostnameNotEntitled` error (Cloudflare code 10000-class "Web3 |
| 35 | // gateways are not enabled"). The deploy+presence test is therefore gated |
| 36 | // behind a Web3-entitled account flag; the read-only list assertion below |
| 37 | // runs unconditionally. |
| 38 | const web3Entitled = !!process.env.CLOUDFLARE_TEST_WEB3; |
| 39 | |
| 40 | // Read-only: `list()` enumerates Web3 hostnames across every zone via |
| 41 | // `listAllZones`. On a non-entitled account each per-zone list answers |
| 42 | // `Forbidden` and is skipped, so the result is a well-typed (possibly |
| 43 | // empty) array. This always runs and proves the enumeration shape. |
| 44 | test.provider( |
| 45 | "list returns a well-typed array of web3 hostnames", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | const provider = yield* Provider.findProvider(Cloudflare.Web3.Hostname); |
| 49 | const all = yield* provider.list(); |
| 50 | |
| 51 | expect(Array.isArray(all)).toBe(true); |
| 52 | for (const item of all) { |
| 53 | expect(typeof item.hostnameId).toBe("string"); |
| 54 | expect(typeof item.zoneId).toBe("string"); |
| 55 | expect(typeof item.name).toBe("string"); |
| 56 | } |
| 57 | |
| 58 | // Keep the destroy bookend so the harness state stays clean. |
| 59 | yield* stack.destroy(); |
| 60 | }).pipe(logLevel), |
| 61 | { tags: ["provider:cloudflare", "provider:cloudflare:web3", "live"] }, |
| 62 | ); |
| 63 | |
| 64 | // Deploy a real Web3 hostname and assert `list()` surfaces it. Gated behind |
| 65 | // a Web3-entitled account (`CLOUDFLARE_TEST_WEB3=1`); on the default |
| 66 | // (non-entitled) testing account creation fails with the typed |
| 67 | // `Web3HostnameNotEntitled` error. |
| 68 | test.provider.skipIf(!web3Entitled)( |
| 69 | "list surfaces a deployed web3 hostname (entitled account)", |
| 70 | (stack) => |
| 71 | Effect.gen(function* () { |
| 72 | const zone = yield* resolveZone; |
| 73 | const name = `alchemy-web3-list.${zone.name}`; |
| 74 | |
| 75 | yield* stack.destroy(); |
| 76 | |
| 77 | const hostname = yield* stack.deploy( |
| 78 | Effect.gen(function* () { |
| 79 | return yield* Cloudflare.Web3.Hostname("ListHostname", { |
| 80 | zoneId: zone.id, |
| 81 | name, |
| 82 | target: "ipfs_universal_path", |
| 83 | }).pipe(adopt(true)); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | const provider = yield* Provider.findProvider(Cloudflare.Web3.Hostname); |
| 88 | const all = yield* provider.list(); |
| 89 | |
| 90 | expect(all.some((h) => h.hostnameId === hostname.hostnameId)).toBe(true); |
| 91 | |
| 92 | yield* stack.destroy(); |
| 93 | }).pipe(logLevel), |
| 94 | { |
| 95 | tags: [ |
| 96 | "provider:cloudflare", |
| 97 | "provider:cloudflare:web3", |
| 98 | "provider:cloudflare:zone", |
| 99 | "live", |
| 100 | ], |
| 101 | timeout: 120_000, |
| 102 | }, |
| 103 | ); |