@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 197 lines 7.34 KB
Raw
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 — see GreTunnel.test.ts.
17 // The probe test always runs and pins the typed gate tag; the lifecycle
18 // test is opt-in for entitled accounts.
19 const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_TRANSIT;
20
21 const cfEndpoint = process.env.CLOUDFLARE_TEST_MT_CF_ENDPOINT ?? "203.0.113.1";
22
23 const getRoute = (accountId: string, routeId: string) =>
24 magicTransit.getRoute({ accountId, routeId });
25
26 // Poll until the route is gone after destroy. Cloudflare answers GET for
27 // a missing route with the typed `RouteNotFound` (code 1020).
28 const expectGone = (accountId: string, routeId: string) =>
29 getRoute(accountId, routeId).pipe(
30 Effect.flatMap(() => Effect.fail({ _tag: "RouteNotDeleted" } as const)),
31 Effect.catchTag("RouteNotFound", () => Effect.void),
32 Effect.retry({
33 while: (e) => e._tag === "RouteNotDeleted",
34 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
35 }),
36 );
37
38 test.provider(
39 "unentitled accounts surface the typed MagicTransitNotOnboarded error",
40 (stack) =>
41 Effect.gen(function* () {
42 const { accountId } = yield* yield* CloudflareEnvironment;
43
44 yield* stack.destroy();
45
46 const canList = yield* magicTransit.listRoutes({ accountId }).pipe(
47 Effect.as(true),
48 Effect.catchTag(["MagicTransitNotOnboarded", "Forbidden"], () => Effect.succeed(false)),
49 );
50 if (canList) {
51 yield* Effect.logInfo("account is Magic Transit-entitled; probe test is a no-op");
52 return;
53 }
54
55 // The typed tag — not UnknownCloudflareError, not a status check.
56 const error = yield* magicTransit.listRoutes({ accountId }).pipe(Effect.flip);
57 expect(["MagicTransitNotOnboarded", "Forbidden"]).toContain(error._tag);
58
59 const createError = yield* magicTransit
60 .createRoute({ accountId, prefix: "10.100.0.0/24", nexthop: "10.213.10.11", priority: 100 })
61 .pipe(Effect.flip);
62 expect(["MagicTransitNotOnboarded", "Forbidden"]).toContain(createError._tag);
63
64 yield* stack.destroy();
65 }).pipe(logLevel),
66 { tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"], timeout: 120_000 },
67 );
68
69 // Read-only: list() must resolve via the typed provider and return a
70 // well-typed array even on unentitled accounts (the account-scoped
71 // `MagicTransitNotOnboarded` gate is mapped to `[]`).
72 test.provider(
73 "list returns a well-typed array of routes",
74 (stack) =>
75 Effect.gen(function* () {
76 yield* stack.destroy();
77
78 const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.MagicStaticRoute);
79 const all = yield* provider.list();
80 expect(Array.isArray(all)).toBe(true);
81 for (const route of all) {
82 expect(typeof route.routeId).toBe("string");
83 expect(typeof route.accountId).toBe("string");
84 }
85
86 yield* stack.destroy();
87 }).pipe(logLevel),
88 { tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"] },
89 );
90
91 test.provider.skipIf(!entitled)(
92 "list enumerates the deployed static route",
93 (stack) =>
94 Effect.gen(function* () {
95 const { accountId } = yield* yield* CloudflareEnvironment;
96
97 yield* stack.destroy();
98
99 yield* stack.deploy(
100 Cloudflare.MagicTransit.GreTunnel("ListRouteGre", {
101 name: testName("alch-gre-listroute1"),
102 cloudflareGreEndpoint: cfEndpoint,
103 customerGreEndpoint: "198.51.100.31",
104 interfaceAddress: "10.213.14.10/31",
105 }),
106 );
107
108 const route = yield* stack.deploy(
109 Cloudflare.MagicTransit.MagicStaticRoute("ListRoute", {
110 prefix: "10.114.0.0/24",
111 nexthop: "10.213.14.11",
112 priority: 100,
113 }),
114 );
115
116 const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.MagicStaticRoute);
117 const all = yield* provider.list();
118
119 const found = all.find((r) => r.routeId === route.routeId);
120 expect(found).toBeDefined();
121 expect(found?.accountId).toEqual(accountId);
122 expect(found?.prefix).toEqual("10.114.0.0/24");
123 expect(found?.nexthop).toEqual("10.213.14.11");
124
125 yield* stack.destroy();
126
127 yield* expectGone(accountId, route.routeId);
128 }).pipe(logLevel),
129 { tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"], timeout: 120_000 },
130 );
131
132 test.provider.skipIf(!entitled)(
133 "routes a prefix over a GRE tunnel, updates in place, and destroys",
134 (stack) =>
135 Effect.gen(function* () {
136 const { accountId } = yield* yield* CloudflareEnvironment;
137
138 yield* stack.destroy();
139
140 // A static route's nexthop must live on a Magic tunnel interface.
141 const tunnel = yield* stack.deploy(
142 Cloudflare.MagicTransit.GreTunnel("RouteGre", {
143 name: testName("alch-gre-route1"),
144 cloudflareGreEndpoint: cfEndpoint,
145 customerGreEndpoint: "198.51.100.30",
146 interfaceAddress: "10.213.12.10/31",
147 }),
148 );
149
150 const route = yield* stack.deploy(
151 Cloudflare.MagicTransit.MagicStaticRoute("Route", {
152 prefix: "10.112.0.0/24",
153 nexthop: "10.213.12.11",
154 priority: 100,
155 description: "alchemy static route test",
156 }),
157 );
158
159 expect(route.routeId).toBeTruthy();
160 expect(route.accountId).toEqual(accountId);
161 expect(route.prefix).toEqual("10.112.0.0/24");
162 expect(route.nexthop).toEqual("10.213.12.11");
163 expect(route.priority).toEqual(100);
164
165 // Out-of-band verification via the distilled API.
166 const live = yield* getRoute(accountId, route.routeId);
167 expect(live.route?.prefix).toEqual("10.112.0.0/24");
168 expect(live.route?.description).toEqual("alchemy static route test");
169
170 // Update mutable props in place — same routeId.
171 const updated = yield* stack.deploy(
172 Cloudflare.MagicTransit.MagicStaticRoute("Route", {
173 prefix: "10.112.0.0/24",
174 nexthop: "10.213.12.11",
175 priority: 150,
176 description: "alchemy static route test v2",
177 }),
178 );
179
180 expect(updated.routeId).toEqual(route.routeId);
181 expect(updated.priority).toEqual(150);
182 expect(updated.description).toEqual("alchemy static route test v2");
183
184 yield* stack.destroy();
185
186 yield* expectGone(accountId, route.routeId);
187 // The tunnel is destroyed alongside the route.
188 const tunnelGone = yield* magicTransit
189 .getGreTunnel({ accountId, greTunnelId: tunnel.tunnelId, xMagicNewHcTarget: true })
190 .pipe(
191 Effect.as(false),
192 Effect.catchTag("GreTunnelNotFound", () => Effect.succeed(true)),
193 );
194 expect(tunnelGone).toBe(true);
195 }).pipe(logLevel),
196 { tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"], timeout: 120_000 },
197 );