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 magicTransit from "@distilled.cloud/cloudflare/magic-transit"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import { testName } from "../../names.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Magic WAN sites (and their LANs / ACLs) are entitlement-gated. On the |
| 16 | // standard testing account every site call fails with the typed |
| 17 | // `MagicWanUnauthorized` error (Cloudflare code 1025) or `MagicWanNotEnabled` |
| 18 | // (code 1101, "forbidden: feature not enabled"). The deploy path below is gated behind an |
| 19 | // explicit opt-in env flag for entitled accounts; the read-only `list()` |
| 20 | // assertion always runs because the provider maps those typed tags to `[]`. |
| 21 | const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_WAN; |
| 22 | |
| 23 | // `list()` is a parent fan-out: it enumerates every Magic site (account |
| 24 | // scope) then lists each site's ACLs. On an unentitled account both |
| 25 | // `listSites` and `listSiteAcls` reject with the typed |
| 26 | // `MagicWanUnauthorized` / |
| 27 | // `MagicWanNotEnabled` tags, which the provider swallows to |
| 28 | // `[]` — so the read-only assertion is safe to run unconditionally. On an |
| 29 | // entitled account (CLOUDFLARE_TEST_MAGIC_WAN=1) we deploy a site with two |
| 30 | // LANs and an ACL, then assert the ACL shows up in the result. |
| 31 | test.provider( |
| 32 | "list enumerates the deployed site ACLs", |
| 33 | (stack) => |
| 34 | Effect.gen(function* () { |
| 35 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 36 | |
| 37 | yield* stack.destroy(); |
| 38 | |
| 39 | const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.MagicSiteAcl); |
| 40 | |
| 41 | if (!entitled) { |
| 42 | // Unentitled: list() swallows the typed entitlement tag and yields []. |
| 43 | const all = yield* provider.list(); |
| 44 | expect(Array.isArray(all)).toBe(true); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | const site = yield* stack.deploy( |
| 49 | Cloudflare.MagicTransit.MagicSite("ListSite", { |
| 50 | description: "alchemy site acl list test", |
| 51 | }), |
| 52 | ); |
| 53 | const lan1 = yield* stack.deploy( |
| 54 | Cloudflare.MagicTransit.MagicSiteLan("ListLan1", { |
| 55 | siteId: site.siteId, |
| 56 | physport: 2, |
| 57 | vlanTag: 10, |
| 58 | staticAddressing: { address: "192.168.30.1/24" }, |
| 59 | }), |
| 60 | ); |
| 61 | const lan2 = yield* stack.deploy( |
| 62 | Cloudflare.MagicTransit.MagicSiteLan("ListLan2", { |
| 63 | siteId: site.siteId, |
| 64 | physport: 3, |
| 65 | vlanTag: 20, |
| 66 | staticAddressing: { address: "192.168.40.1/24" }, |
| 67 | }), |
| 68 | ); |
| 69 | const acl = yield* stack.deploy( |
| 70 | Cloudflare.MagicTransit.MagicSiteAcl("ListAcl", { |
| 71 | siteId: site.siteId, |
| 72 | name: testName("alch-acl-list"), |
| 73 | lan1: { lanId: lan1.lanId, ports: [443] }, |
| 74 | lan2: { lanId: lan2.lanId }, |
| 75 | protocols: ["tcp"], |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | const all = yield* provider.list(); |
| 80 | expect(all.some((a) => a.aclId === acl.aclId)).toBe(true); |
| 81 | expect( |
| 82 | all.some((a) => a.name === testName("alch-acl-list") && a.siteId === site.siteId), |
| 83 | ).toBe(true); |
| 84 | // Every hydrated row carries the ambient account scope. |
| 85 | expect(all.every((a) => a.accountId === accountId)).toBe(true); |
| 86 | |
| 87 | yield* stack.destroy(); |
| 88 | }).pipe(logLevel), |
| 89 | { |
| 90 | tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"], |
| 91 | timeout: 180_000, |
| 92 | }, |
| 93 | ); |
| 94 | |
| 95 | test.provider( |
| 96 | "unentitled accounts surface the typed Magic WAN error for ACL listing", |
| 97 | (stack) => |
| 98 | Effect.gen(function* () { |
| 99 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 100 | |
| 101 | yield* stack.destroy(); |
| 102 | |
| 103 | const canList = yield* magicTransit.listSites({ accountId }).pipe( |
| 104 | Effect.as(true), |
| 105 | Effect.catchTag(["MagicWanUnauthorized", "MagicWanNotEnabled"], () => |
| 106 | Effect.succeed(false), |
| 107 | ), |
| 108 | ); |
| 109 | if (canList) { |
| 110 | // Entitled account — the list test above covers real behavior. |
| 111 | yield* Effect.logInfo("account is Magic WAN-entitled; probe test is a no-op"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // The typed tag — not UnknownCloudflareError, not a status check. |
| 116 | const error = yield* magicTransit.listSites({ accountId }).pipe(Effect.flip); |
| 117 | expect(["MagicWanUnauthorized", "MagicWanNotEnabled"]).toContain(error._tag); |
| 118 | |
| 119 | // Despite the gating, list() degrades to an empty array. |
| 120 | const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.MagicSiteAcl); |
| 121 | expect(yield* provider.list()).toEqual([]); |
| 122 | |
| 123 | yield* stack.destroy(); |
| 124 | }).pipe(logLevel), |
| 125 | { |
| 126 | tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"], |
| 127 | timeout: 120_000, |
| 128 | }, |
| 129 | ); |