| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Provider from "@/Provider"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService( |
| 12 | MinimumLogLevel, |
| 13 | process.env.DEBUG ? "Debug" : "Info", |
| 14 | ); |
| 15 | |
| 16 | // A freshly minted scoped token propagates eventually-consistently across |
| 17 | // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls. |
| 18 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 19 | |
| 20 | const retryForbidden = <A, E extends { _tag: string }, R>( |
| 21 | effect: Effect.Effect<A, E, R>, |
| 22 | ) => |
| 23 | effect.pipe( |
| 24 | Effect.retry({ |
| 25 | while: (e) => e._tag === "Forbidden", |
| 26 | schedule: forbiddenRetrySchedule, |
| 27 | times: 8, |
| 28 | }), |
| 29 | ); |
| 30 | |
| 31 | // `list()` enumerates delegations by fanning out over account-scoped BYOIP |
| 32 | // prefixes (the catalog `listPrefixes` endpoint is available regardless of the |
| 33 | // BYOIP entitlement and returns an empty array on accounts with no onboarded |
| 34 | // prefixes), then listing each prefix's delegations. The result is a |
| 35 | // well-typed `PrefixDelegationAttributes[]` — the exact shape `read` |
| 36 | // produces — and is empty on a non-BYOIP account. |
| 37 | test.provider( |
| 38 | "list enumerates prefix delegations (read-only)", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const provider = yield* Provider.findProvider( |
| 44 | Cloudflare.Addressing.PrefixDelegation, |
| 45 | ); |
| 46 | const all = yield* retryForbidden(provider.list()); |
| 47 | |
| 48 | expect(Array.isArray(all)).toBe(true); |
| 49 | for (const d of all) { |
| 50 | expect(typeof d.delegationId).toBe("string"); |
| 51 | expect(typeof d.prefixId).toBe("string"); |
| 52 | expect(typeof d.accountId).toBe("string"); |
| 53 | expect(typeof d.cidr).toBe("string"); |
| 54 | expect(typeof d.delegatedAccountId).toBe("string"); |
| 55 | } |
| 56 | |
| 57 | yield* stack.destroy(); |
| 58 | }).pipe(logLevel), |
| 59 | { tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"] }, |
| 60 | ); |