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 * 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 | // Magic Transit is an entitlement-gated product. On the standard testing |
| 17 | // account every Magic tunnel/route call fails with the typed |
| 18 | // `MagicTransitNotOnboarded` error (Cloudflare code 1012, "Magic Transit |
| 19 | // is not onboarded for this account") or `Forbidden` (403) depending on |
| 20 | // token scope. The lifecycle tests below are gated behind an explicit |
| 21 | // opt-in env flag for entitled accounts; the probe test always runs and |
| 22 | // pins the typed tag. |
| 23 | const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_TRANSIT; |
| 24 | |
| 25 | // Entitled accounts must supply a real Cloudflare anycast endpoint that is |
| 26 | // allocated to the account — there is no way to derive one. |
| 27 | const cfEndpoint = process.env.CLOUDFLARE_TEST_MT_CF_ENDPOINT ?? "203.0.113.1"; |
| 28 | |
| 29 | const getTunnel = (accountId: string, greTunnelId: string) => |
| 30 | magicTransit.getGreTunnel({ |
| 31 | accountId, |
| 32 | greTunnelId, |
| 33 | xMagicNewHcTarget: true, |
| 34 | }); |
| 35 | |
| 36 | // Poll until the tunnel is gone after destroy. Cloudflare answers GET for |
| 37 | // a missing tunnel with the typed `GreTunnelNotFound` (code 1029). |
| 38 | const expectGone = (accountId: string, greTunnelId: string) => |
| 39 | getTunnel(accountId, greTunnelId).pipe( |
| 40 | Effect.flatMap(() => Effect.fail({ _tag: "TunnelNotDeleted" } as const)), |
| 41 | Effect.catchTag("GreTunnelNotFound", () => Effect.void), |
| 42 | Effect.retry({ |
| 43 | while: (e) => e._tag === "TunnelNotDeleted", |
| 44 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]), |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | test.provider( |
| 49 | "unentitled accounts surface the typed MagicTransitNotOnboarded error", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 53 | |
| 54 | yield* stack.destroy(); |
| 55 | |
| 56 | const canList = yield* magicTransit |
| 57 | .listGreTunnels({ accountId, xMagicNewHcTarget: true }) |
| 58 | .pipe( |
| 59 | Effect.as(true), |
| 60 | Effect.catchTag(["MagicTransitNotOnboarded", "Forbidden"], () => Effect.succeed(false)), |
| 61 | ); |
| 62 | if (canList) { |
| 63 | // Entitled account — the gated lifecycle test covers real behavior. |
| 64 | yield* Effect.logInfo("account is Magic Transit-entitled; probe test is a no-op"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // The typed tag — not UnknownCloudflareError, not a status check. |
| 69 | const error = yield* magicTransit |
| 70 | .listGreTunnels({ accountId, xMagicNewHcTarget: true }) |
| 71 | .pipe(Effect.flip); |
| 72 | expect(["MagicTransitNotOnboarded", "Forbidden"]).toContain(error._tag); |
| 73 | |
| 74 | const createError = yield* magicTransit |
| 75 | .createGreTunnel({ |
| 76 | accountId, |
| 77 | xMagicNewHcTarget: true, |
| 78 | name: "alch-gre-probe", |
| 79 | cloudflareGreEndpoint: cfEndpoint, |
| 80 | customerGreEndpoint: "198.51.100.10", |
| 81 | interfaceAddress: "10.213.10.10/31", |
| 82 | }) |
| 83 | .pipe(Effect.flip); |
| 84 | expect(["MagicTransitNotOnboarded", "Forbidden"]).toContain(createError._tag); |
| 85 | |
| 86 | yield* stack.destroy(); |
| 87 | }).pipe(logLevel), |
| 88 | { |
| 89 | tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"], |
| 90 | timeout: 120_000, |
| 91 | }, |
| 92 | ); |
| 93 | |
| 94 | // `list()` enumerates every GRE tunnel in the account. On an unentitled |
| 95 | // account the underlying `listGreTunnels` rejects with the typed |
| 96 | // `MagicTransitNotOnboarded` / `Forbidden` tags, which the provider maps to |
| 97 | // `[]` — so the read-only assertion is safe to run unconditionally. On an |
| 98 | // entitled account (CLOUDFLARE_TEST_MAGIC_TRANSIT=1) we deploy a tunnel and |
| 99 | // assert it shows up in the exhaustively-enumerated result. |
| 100 | test.provider( |
| 101 | "list enumerates the deployed GRE tunnels", |
| 102 | (stack) => |
| 103 | Effect.gen(function* () { |
| 104 | yield* stack.destroy(); |
| 105 | |
| 106 | const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.GreTunnel); |
| 107 | |
| 108 | if (!entitled) { |
| 109 | // Unentitled: list() swallows the typed entitlement tag and yields []. |
| 110 | const all = yield* provider.list(); |
| 111 | expect(Array.isArray(all)).toBe(true); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | const tunnel = yield* stack.deploy( |
| 116 | Cloudflare.MagicTransit.GreTunnel("ListGre", { |
| 117 | name: testName("alch-gre-list"), |
| 118 | cloudflareGreEndpoint: cfEndpoint, |
| 119 | customerGreEndpoint: "198.51.100.20", |
| 120 | interfaceAddress: "10.213.20.20/31", |
| 121 | }), |
| 122 | ); |
| 123 | |
| 124 | const all = yield* provider.list(); |
| 125 | expect(all.some((t) => t.tunnelId === tunnel.tunnelId)).toBe(true); |
| 126 | expect(all.some((t) => t.name === testName("alch-gre-list"))).toBe(true); |
| 127 | |
| 128 | yield* stack.destroy(); |
| 129 | yield* expectGone(tunnel.accountId, tunnel.tunnelId); |
| 130 | }).pipe(logLevel), |
| 131 | { |
| 132 | tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"], |
| 133 | timeout: 120_000, |
| 134 | }, |
| 135 | ); |
| 136 | |
| 137 | test.provider.skipIf(!entitled)( |
| 138 | "creates a GRE tunnel, updates mutable props in place, and destroys it", |
| 139 | (stack) => |
| 140 | Effect.gen(function* () { |
| 141 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 142 | |
| 143 | yield* stack.destroy(); |
| 144 | |
| 145 | const tunnel = yield* stack.deploy( |
| 146 | Cloudflare.MagicTransit.GreTunnel("Gre", { |
| 147 | name: testName("alch-gre-test1"), |
| 148 | cloudflareGreEndpoint: cfEndpoint, |
| 149 | customerGreEndpoint: "198.51.100.10", |
| 150 | interfaceAddress: "10.213.10.10/31", |
| 151 | description: "alchemy gre tunnel test", |
| 152 | ttl: 64, |
| 153 | mtu: 1476, |
| 154 | }), |
| 155 | ); |
| 156 | |
| 157 | expect(tunnel.tunnelId).toBeTruthy(); |
| 158 | expect(tunnel.accountId).toEqual(accountId); |
| 159 | expect(tunnel.name).toEqual(testName("alch-gre-test1")); |
| 160 | expect(tunnel.cloudflareGreEndpoint).toEqual(cfEndpoint); |
| 161 | expect(tunnel.customerGreEndpoint).toEqual("198.51.100.10"); |
| 162 | expect(tunnel.interfaceAddress).toEqual("10.213.10.10/31"); |
| 163 | |
| 164 | // Out-of-band verification via the distilled API. |
| 165 | const live = yield* getTunnel(accountId, tunnel.tunnelId); |
| 166 | expect(live.greTunnel?.name).toEqual(testName("alch-gre-test1")); |
| 167 | expect(live.greTunnel?.description).toEqual("alchemy gre tunnel test"); |
| 168 | |
| 169 | // Update mutable props in place — same tunnelId. |
| 170 | const updated = yield* stack.deploy( |
| 171 | Cloudflare.MagicTransit.GreTunnel("Gre", { |
| 172 | name: testName("alch-gre-test1"), |
| 173 | cloudflareGreEndpoint: cfEndpoint, |
| 174 | customerGreEndpoint: "198.51.100.10", |
| 175 | interfaceAddress: "10.213.10.10/31", |
| 176 | description: "alchemy gre tunnel test v2", |
| 177 | ttl: 60, |
| 178 | mtu: 1400, |
| 179 | }), |
| 180 | ); |
| 181 | |
| 182 | expect(updated.tunnelId).toEqual(tunnel.tunnelId); |
| 183 | expect(updated.description).toEqual("alchemy gre tunnel test v2"); |
| 184 | expect(updated.ttl).toEqual(60); |
| 185 | expect(updated.mtu).toEqual(1400); |
| 186 | |
| 187 | // The tunnel name is its routing identity — changing it replaces. |
| 188 | const replaced = yield* stack.deploy( |
| 189 | Cloudflare.MagicTransit.GreTunnel("Gre", { |
| 190 | name: testName("alch-gre-test2"), |
| 191 | cloudflareGreEndpoint: cfEndpoint, |
| 192 | customerGreEndpoint: "198.51.100.10", |
| 193 | interfaceAddress: "10.213.10.10/31", |
| 194 | }), |
| 195 | ); |
| 196 | |
| 197 | expect(replaced.tunnelId).not.toEqual(tunnel.tunnelId); |
| 198 | expect(replaced.name).toEqual(testName("alch-gre-test2")); |
| 199 | yield* expectGone(accountId, tunnel.tunnelId); |
| 200 | |
| 201 | yield* stack.destroy(); |
| 202 | |
| 203 | yield* expectGone(accountId, replaced.tunnelId); |
| 204 | }).pipe(logLevel), |
| 205 | { |
| 206 | tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"], |
| 207 | timeout: 120_000, |
| 208 | }, |
| 209 | ); |