@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 203 lines 7.38 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 WAN sites (and their LANs / WANs / ACLs) are entitlement-gated.
17 // On the standard testing account every site call fails with the typed
18 // `MagicWanUnauthorized` error (Cloudflare code 1025) or `MagicWanNotEnabled`
19 // (code 1101, "forbidden: feature not enabled"). The lifecycle test below is gated
20 // behind an explicit opt-in env flag for entitled accounts; the probe
21 // test always runs and pins the typed tag.
22 const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_WAN;
23
24 const getSite = (accountId: string, siteId: string) => magicTransit.getSite({ accountId, siteId });
25
26 // Poll until the site is gone after destroy. Cloudflare answers GET for
27 // a missing site with the typed `SiteNotFound` (404).
28 const expectGone = (accountId: string, siteId: string) =>
29 getSite(accountId, siteId).pipe(
30 Effect.flatMap(() => Effect.fail({ _tag: "SiteNotDeleted" } as const)),
31 Effect.catchTag("SiteNotFound", () => Effect.void),
32 Effect.retry({
33 while: (e) => e._tag === "SiteNotDeleted",
34 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
35 }),
36 );
37
38 test.provider(
39 "unentitled accounts surface the typed Magic WAN entitlement error",
40 (stack) =>
41 Effect.gen(function* () {
42 const { accountId } = yield* yield* CloudflareEnvironment;
43
44 yield* stack.destroy();
45
46 const canList = yield* magicTransit.listSites({ accountId }).pipe(
47 Effect.as(true),
48 Effect.catchTag(["MagicWanUnauthorized", "MagicWanNotEnabled"], () =>
49 Effect.succeed(false),
50 ),
51 );
52 if (canList) {
53 // Entitled account — the gated lifecycle test covers real behavior.
54 yield* Effect.logInfo("account is Magic WAN-entitled; probe test is a no-op");
55 return;
56 }
57
58 // The typed tag — not UnknownCloudflareError, not a status check.
59 const error = yield* magicTransit.listSites({ accountId }).pipe(Effect.flip);
60 expect(["MagicWanUnauthorized", "MagicWanNotEnabled"]).toContain(error._tag);
61
62 const createError = yield* magicTransit
63 .createSite({ accountId, name: "alchemy-site-probe" })
64 .pipe(Effect.flip);
65 expect(["MagicWanUnauthorized", "MagicWanNotEnabled"]).toContain(createError._tag);
66
67 yield* stack.destroy();
68 }).pipe(logLevel),
69 {
70 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
71 timeout: 120_000,
72 },
73 );
74
75 // Canonical `list()` test (account collection): Magic WAN sites are
76 // account-scoped, so `list()` paginates the account-wide sites API and
77 // hydrates each into the `read` Attributes shape. On unentitled accounts
78 // enumeration is rejected with the typed `MagicWanUnauthorized` (1025) or
79 // `MagicWanNotEnabled` (1101) and `list()` returns a well-typed `[]`. On entitled
80 // accounts (CLOUDFLARE_TEST_MAGIC_WAN=1) we deploy a site and assert it
81 // appears in the exhaustively-paginated result.
82 test.provider(
83 "list enumerates account Magic WAN sites",
84 (stack) =>
85 Effect.gen(function* () {
86 yield* stack.destroy();
87
88 const provider = yield* Provider.findProvider(Cloudflare.MagicTransit.MagicSite);
89
90 if (!entitled) {
91 // Unentitled account — `list()` swallows the typed entitlement /
92 // permission rejection and yields an empty, well-typed array.
93 const all = yield* provider.list();
94 expect(all).toEqual([]);
95 yield* stack.destroy();
96 return;
97 }
98
99 const site = yield* stack.deploy(
100 Cloudflare.MagicTransit.MagicSite("ListSite", {
101 description: "alchemy magic site list test",
102 }),
103 );
104 expect(site.siteId).toBeTruthy();
105
106 const all = yield* provider.list();
107 expect(all.some((s) => s.siteId === site.siteId)).toBe(true);
108
109 yield* stack.destroy();
110 }).pipe(logLevel),
111 {
112 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
113 timeout: 180_000,
114 },
115 );
116
117 test.provider.skipIf(!entitled)(
118 "creates a site with WAN, LAN, and ACL, updates in place, and destroys",
119 (stack) =>
120 Effect.gen(function* () {
121 const { accountId } = yield* yield* CloudflareEnvironment;
122
123 yield* stack.destroy();
124
125 const site = yield* stack.deploy(
126 Cloudflare.MagicTransit.MagicSite("Site", {
127 name: testName("alchemy-magic-site"),
128 description: "alchemy magic site test",
129 location: { lat: "37.7749", lon: "-122.4194" },
130 }),
131 );
132
133 expect(site.siteId).toBeTruthy();
134 expect(site.accountId).toEqual(accountId);
135 expect(site.name).toEqual(testName("alchemy-magic-site"));
136 expect(site.description).toEqual("alchemy magic site test");
137
138 // Out-of-band verification via the distilled API.
139 const live = yield* getSite(accountId, site.siteId);
140 expect(live.name).toEqual(testName("alchemy-magic-site"));
141
142 const wan = yield* stack.deploy(
143 Cloudflare.MagicTransit.MagicSiteWan("Wan", {
144 siteId: site.siteId,
145 physport: 1,
146 priority: 10,
147 }),
148 );
149 expect(wan.wanId).toBeTruthy();
150 expect(wan.siteId).toEqual(site.siteId);
151
152 const lan1 = yield* stack.deploy(
153 Cloudflare.MagicTransit.MagicSiteLan("Lan1", {
154 siteId: site.siteId,
155 physport: 2,
156 vlanTag: 10,
157 staticAddressing: { address: "192.168.10.1/24" },
158 }),
159 );
160 const lan2 = yield* stack.deploy(
161 Cloudflare.MagicTransit.MagicSiteLan("Lan2", {
162 siteId: site.siteId,
163 physport: 3,
164 vlanTag: 20,
165 staticAddressing: { address: "192.168.20.1/24" },
166 }),
167 );
168 expect(lan1.lanId).toBeTruthy();
169 expect(lan2.lanId).toBeTruthy();
170
171 const acl = yield* stack.deploy(
172 Cloudflare.MagicTransit.MagicSiteAcl("Acl", {
173 siteId: site.siteId,
174 name: testName("alchemy-site-acl"),
175 lan1: { lanId: lan1.lanId, ports: [443] },
176 lan2: { lanId: lan2.lanId },
177 protocols: ["tcp"],
178 }),
179 );
180 expect(acl.aclId).toBeTruthy();
181 expect(acl.siteId).toEqual(site.siteId);
182 expect(acl.name).toEqual(testName("alchemy-site-acl"));
183
184 // Update the site description in place — same siteId.
185 const updated = yield* stack.deploy(
186 Cloudflare.MagicTransit.MagicSite("Site", {
187 name: testName("alchemy-magic-site"),
188 description: "alchemy magic site test v2",
189 location: { lat: "37.7749", lon: "-122.4194" },
190 }),
191 );
192 expect(updated.siteId).toEqual(site.siteId);
193 expect(updated.description).toEqual("alchemy magic site test v2");
194
195 yield* stack.destroy();
196
197 yield* expectGone(accountId, site.siteId);
198 }).pipe(logLevel),
199 {
200 tags: ["provider:cloudflare", "provider:cloudflare:magictransit", "live"],
201 timeout: 180_000,
202 },
203 );