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 { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as loadBalancers from "@distilled.cloud/cloudflare/load-balancers"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { testName } from "../../names.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | // Load Balancing is a paid add-on subscription. The testing account does |
| 17 | // not have it: pool creation is rejected with "Internal error creating or |
| 18 | // modifying pool. Access Failed." (Cloudflare code 1002), surfaced as the |
| 19 | // typed `PoolAccessFailed` error. Set CLOUDFLARE_LB_ENABLED=1 once the |
| 20 | // subscription is provisioned to run the full lifecycle tests. |
| 21 | const lbEnabled = !!process.env.CLOUDFLARE_LB_ENABLED; |
| 22 | |
| 23 | // Deterministic per-test physical names — reused on every run. |
| 24 | const NAME_ENTITLEMENT = testName("alchemy-lb-pool-entitlement-probe"); |
| 25 | const NAME_LIFECYCLE = testName("alchemy-lb-pool-lifecycle"); |
| 26 | const NAME_MONITOR = testName("alchemy-lb-pool-lifecycle-monitor"); |
| 27 | |
| 28 | // Freshly minted scoped tokens propagate eventually-consistently across |
| 29 | // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls. |
| 30 | const forbiddenRetrySchedule = Schedule.exponential("500 millis"); |
| 31 | |
| 32 | const getPool = (accountId: string, poolId: string) => |
| 33 | loadBalancers.getPool({ accountId, poolId }).pipe( |
| 34 | Effect.retry({ |
| 35 | while: (e) => e._tag === "Forbidden", |
| 36 | schedule: forbiddenRetrySchedule, |
| 37 | times: 8, |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | const expectGone = (accountId: string, poolId: string) => |
| 42 | getPool(accountId, poolId).pipe( |
| 43 | Effect.flatMap(() => Effect.fail({ _tag: "PoolNotDeleted" } as const)), |
| 44 | // A missing pool surfaces as the typed `PoolNotFound` (Cloudflare error |
| 45 | // code 1001) — that's the success condition here. |
| 46 | Effect.catchTag("PoolNotFound", () => Effect.void), |
| 47 | Effect.retry({ |
| 48 | while: (e) => e._tag === "PoolNotDeleted", |
| 49 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 50 | }), |
| 51 | ); |
| 52 | |
| 53 | // Unentitlement probe: pins the typed plan-gate rejection, so it must skip |
| 54 | // on entitled accounts — there the create would succeed instead of failing. |
| 55 | test.provider.skipIf(lbEnabled)( |
| 56 | "surfaces the typed PoolAccessFailed error without the LB subscription", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | const error = yield* loadBalancers |
| 64 | .createPool({ |
| 65 | accountId, |
| 66 | name: NAME_ENTITLEMENT, |
| 67 | origins: [{ name: "origin-1", address: "203.0.113.10" }], |
| 68 | }) |
| 69 | .pipe( |
| 70 | Effect.retry({ |
| 71 | while: (e) => e._tag === "Forbidden", |
| 72 | schedule: forbiddenRetrySchedule, |
| 73 | times: 8, |
| 74 | }), |
| 75 | Effect.flip, |
| 76 | ); |
| 77 | expect(error._tag).toEqual("PoolAccessFailed"); |
| 78 | |
| 79 | yield* stack.destroy(); |
| 80 | }).pipe(logLevel), |
| 81 | { tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"] }, |
| 82 | ); |
| 83 | |
| 84 | test.provider.skipIf(!lbEnabled)( |
| 85 | "create, update in place, and destroy a health-checked pool", |
| 86 | (stack) => |
| 87 | Effect.gen(function* () { |
| 88 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 89 | |
| 90 | yield* stack.destroy(); |
| 91 | |
| 92 | const initial = yield* stack.deploy( |
| 93 | Effect.gen(function* () { |
| 94 | const monitor = yield* Cloudflare.LoadBalancer.Monitor("Monitor", { |
| 95 | description: NAME_MONITOR, |
| 96 | type: "https", |
| 97 | path: "/health", |
| 98 | expectedCodes: "2xx", |
| 99 | }); |
| 100 | const pool = yield* Cloudflare.LoadBalancer.Pool("Pool", { |
| 101 | name: NAME_LIFECYCLE, |
| 102 | origins: [{ name: "origin-1", address: "203.0.113.10" }], |
| 103 | monitor: monitor.monitorId, |
| 104 | description: "v1", |
| 105 | }); |
| 106 | return { monitor, pool }; |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | expect(initial.pool.poolId).toBeTruthy(); |
| 111 | expect(initial.pool.accountId).toEqual(accountId); |
| 112 | expect(initial.pool.name).toEqual(NAME_LIFECYCLE); |
| 113 | expect(initial.pool.monitor).toEqual(initial.monitor.monitorId); |
| 114 | |
| 115 | const live = yield* getPool(accountId, initial.pool.poolId); |
| 116 | expect(live.name).toEqual(NAME_LIFECYCLE); |
| 117 | expect(live.description).toEqual("v1"); |
| 118 | expect((live.origins ?? []).map((o) => o.address)).toEqual(["203.0.113.10"]); |
| 119 | |
| 120 | // Mutable props (origins, weights, description) update in place — |
| 121 | // same poolId. Keep the monitor deployed across every step so the |
| 122 | // engine never has to replace and drop a dependency in one deploy. |
| 123 | const updated = yield* stack.deploy( |
| 124 | Effect.gen(function* () { |
| 125 | const monitor = yield* Cloudflare.LoadBalancer.Monitor("Monitor", { |
| 126 | description: NAME_MONITOR, |
| 127 | type: "https", |
| 128 | path: "/health", |
| 129 | expectedCodes: "2xx", |
| 130 | }); |
| 131 | const pool = yield* Cloudflare.LoadBalancer.Pool("Pool", { |
| 132 | name: NAME_LIFECYCLE, |
| 133 | origins: [ |
| 134 | { name: "origin-1", address: "203.0.113.10", weight: 0.7 }, |
| 135 | { name: "origin-2", address: "203.0.113.11", weight: 0.3 }, |
| 136 | ], |
| 137 | monitor: monitor.monitorId, |
| 138 | description: "v2", |
| 139 | }); |
| 140 | return { monitor, pool }; |
| 141 | }), |
| 142 | ); |
| 143 | expect(updated.pool.poolId).toEqual(initial.pool.poolId); |
| 144 | |
| 145 | const synced = yield* getPool(accountId, updated.pool.poolId); |
| 146 | expect(synced.description).toEqual("v2"); |
| 147 | expect((synced.origins ?? []).map((o) => o.address).sort()).toEqual([ |
| 148 | "203.0.113.10", |
| 149 | "203.0.113.11", |
| 150 | ]); |
| 151 | |
| 152 | yield* stack.destroy(); |
| 153 | |
| 154 | yield* expectGone(accountId, initial.pool.poolId); |
| 155 | }).pipe(logLevel), |
| 156 | { |
| 157 | tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"], |
| 158 | timeout: 120_000, |
| 159 | }, |
| 160 | ); |
| 161 | |
| 162 | // Account-collection list: enumerate every pool in the account and assert |
| 163 | // the deployed one is present. Gated like the lifecycle test — without the |
| 164 | // LB subscription, the deploy fails with the typed `PoolAccessFailed`. |
| 165 | test.provider.skipIf(!lbEnabled)( |
| 166 | "list enumerates the deployed pool", |
| 167 | (stack) => |
| 168 | Effect.gen(function* () { |
| 169 | yield* stack.destroy(); |
| 170 | |
| 171 | const deployed = yield* stack.deploy( |
| 172 | Effect.gen(function* () { |
| 173 | return yield* Cloudflare.LoadBalancer.Pool("ListPool", { |
| 174 | name: NAME_LIFECYCLE, |
| 175 | origins: [{ name: "origin-1", address: "203.0.113.10" }], |
| 176 | }); |
| 177 | }), |
| 178 | ); |
| 179 | |
| 180 | const provider = yield* Provider.findProvider(Cloudflare.LoadBalancer.Pool); |
| 181 | const all = yield* provider.list(); |
| 182 | |
| 183 | expect(all.some((p) => p.poolId === deployed.poolId)).toBe(true); |
| 184 | |
| 185 | yield* stack.destroy(); |
| 186 | }).pipe(logLevel), |
| 187 | { |
| 188 | tags: ["provider:cloudflare", "provider:cloudflare:loadbalancer", "live"], |
| 189 | timeout: 120_000, |
| 190 | }, |
| 191 | ); |