@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 199 lines 7.39 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 * as Redacted from "effect/Redacted";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11 import { testName } from "../../names.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // Magic Transit is an entitlement-gated product — see GreTunnel.test.ts.
18 // The probe test always runs and pins the typed gate tag; the lifecycle
19 // test is opt-in for entitled accounts.
20 const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_TRANSIT;
21
22 const cfEndpoint = process.env.CLOUDFLARE_TEST_MT_CF_ENDPOINT ?? "203.0.113.1";
23
24 const getTunnel = (accountId: string, ipsecTunnelId: string) =>
25 magicTransit.getIpsecTunnel({
26 accountId,
27 ipsecTunnelId,
28 xMagicNewHcTarget: true,
29 });
30
31 // Poll until the tunnel is gone after destroy. Cloudflare answers GET for
32 // a missing tunnel with the typed `IpsecTunnelNotFound` (code 1032).
33 const expectGone = (accountId: string, ipsecTunnelId: string) =>
34 getTunnel(accountId, ipsecTunnelId).pipe(
35 Effect.flatMap(() => Effect.fail({ _tag: "TunnelNotDeleted" } as const)),
36 Effect.catchTag("IpsecTunnelNotFound", () => Effect.void),
37 Effect.retry({
38 while: (e) => e._tag === "TunnelNotDeleted",
39 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
40 }),
41 );
42
43 test.provider(
44 "unentitled accounts surface the typed MagicTransitNotOnboarded error",
45 (stack) =>
46 Effect.gen(function* () {
47 const { accountId } = yield* yield* CloudflareEnvironment;
48
49 yield* stack.destroy();
50
51 const canList = yield* magicTransit
52 .listIpsecTunnels({ accountId, xMagicNewHcTarget: true })
53 .pipe(
54 Effect.as(true),
55 Effect.catchTag(["MagicTransitNotOnboarded", "Forbidden"], () => Effect.succeed(false)),
56 );
57 if (canList) {
58 yield* Effect.logInfo("account is Magic Transit-entitled; probe test is a no-op");
59 return;
60 }
61
62 // The typed tag — not UnknownCloudflareError, not a status check.
63 const error = yield* magicTransit
64 .listIpsecTunnels({ accountId, xMagicNewHcTarget: true })
65 .pipe(Effect.flip);
66 expect(["MagicTransitNotOnboarded", "Forbidden"]).toContain(error._tag);
67
68 const createError = yield* magicTransit
69 .createIpsecTunnel({
70 accountId,
71 xMagicNewHcTarget: true,
72 name: "alch-ipsec-probe",
73 cloudflareEndpoint: cfEndpoint,
74 interfaceAddress: "10.213.11.10/31",
75 })
76 .pipe(Effect.flip);
77 expect(["MagicTransitNotOnboarded", "Forbidden"]).toContain(createError._tag);
78
79 yield* stack.destroy();
80 }).pipe(logLevel),
81 {
82 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
83 timeout: 120_000,
84 },
85 );
86
87 test.provider(
88 "list enumerates account IPsec tunnels (read-only [] when unentitled)",
89 (stack) =>
90 Effect.gen(function* () {
91 yield* stack.destroy();
92
93 const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.IpsecTunnel);
94
95 if (!entitled) {
96 // Unentitled accounts can't enumerate tunnels — list() swallows the
97 // typed MagicTransitNotOnboarded/Forbidden gate and returns [].
98 const all = yield* provider.list();
99 expect(Array.isArray(all)).toBe(true);
100 return;
101 }
102
103 const tunnel = yield* stack.deploy(
104 Cloudflare.MagicTransit.IpsecTunnel("ListIpsec", {
105 name: testName("alch-ipsec-list1"),
106 cloudflareEndpoint: cfEndpoint,
107 interfaceAddress: "10.213.12.10/31",
108 psk: Redacted.make("alchemy-test-psk-list"),
109 }),
110 );
111
112 const all = yield* provider.list();
113 expect(all.some((t) => t.tunnelId === tunnel.tunnelId)).toBe(true);
114 const found = all.find((t) => t.tunnelId === tunnel.tunnelId)!;
115 expect(found.name).toEqual(testName("alch-ipsec-list1"));
116 expect(found.accountId).toEqual(tunnel.accountId);
117 // PSK is write-only — list never reads it back.
118 expect(found.psk).toBeUndefined();
119
120 yield* stack.destroy();
121 yield* expectGone(tunnel.accountId, tunnel.tunnelId);
122 }).pipe(logLevel),
123 {
124 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
125 timeout: 120_000,
126 },
127 );
128
129 test.provider.skipIf(!entitled)(
130 "creates an IPsec tunnel, updates mutable props in place, and destroys it",
131 (stack) =>
132 Effect.gen(function* () {
133 const { accountId } = yield* yield* CloudflareEnvironment;
134
135 yield* stack.destroy();
136
137 const tunnel = yield* stack.deploy(
138 Cloudflare.MagicTransit.IpsecTunnel("Ipsec", {
139 name: testName("alch-ipsec-test1"),
140 cloudflareEndpoint: cfEndpoint,
141 customerEndpoint: "198.51.100.20",
142 interfaceAddress: "10.213.11.10/31",
143 description: "alchemy ipsec tunnel test",
144 psk: Redacted.make("alchemy-test-psk-1"),
145 }),
146 );
147
148 expect(tunnel.tunnelId).toBeTruthy();
149 expect(tunnel.accountId).toEqual(accountId);
150 expect(tunnel.name).toEqual(testName("alch-ipsec-test1"));
151 expect(tunnel.cloudflareEndpoint).toEqual(cfEndpoint);
152 // The PSK is write-only — it is carried in state, never read back.
153 expect(tunnel.psk && Redacted.value(tunnel.psk)).toEqual("alchemy-test-psk-1");
154
155 // Out-of-band verification via the distilled API.
156 const live = yield* getTunnel(accountId, tunnel.tunnelId);
157 expect(live.ipsecTunnel?.name).toEqual(testName("alch-ipsec-test1"));
158 expect(live.ipsecTunnel?.description).toEqual("alchemy ipsec tunnel test");
159
160 // Update mutable props in place — same tunnelId.
161 const updated = yield* stack.deploy(
162 Cloudflare.MagicTransit.IpsecTunnel("Ipsec", {
163 name: testName("alch-ipsec-test1"),
164 cloudflareEndpoint: cfEndpoint,
165 customerEndpoint: "198.51.100.20",
166 interfaceAddress: "10.213.11.10/31",
167 description: "alchemy ipsec tunnel test v2",
168 replayProtection: true,
169 psk: Redacted.make("alchemy-test-psk-1"),
170 }),
171 );
172
173 expect(updated.tunnelId).toEqual(tunnel.tunnelId);
174 expect(updated.description).toEqual("alchemy ipsec tunnel test v2");
175 expect(updated.replayProtection).toEqual(true);
176
177 // The tunnel name is unique routing identity — changing it replaces.
178 const replaced = yield* stack.deploy(
179 Cloudflare.MagicTransit.IpsecTunnel("Ipsec", {
180 name: testName("alch-ipsec-test2"),
181 cloudflareEndpoint: cfEndpoint,
182 interfaceAddress: "10.213.11.10/31",
183 psk: Redacted.make("alchemy-test-psk-1"),
184 }),
185 );
186
187 expect(replaced.tunnelId).not.toEqual(tunnel.tunnelId);
188 expect(replaced.name).toEqual(testName("alch-ipsec-test2"));
189 yield* expectGone(accountId, tunnel.tunnelId);
190
191 yield* stack.destroy();
192
193 yield* expectGone(accountId, replaced.tunnelId);
194 }).pipe(logLevel),
195 {
196 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
197 timeout: 120_000,
198 },
199 );