@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 224 lines 8.29 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 mcn from "@distilled.cloud/cloudflare/magic-cloud-networking";
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 Cloud Networking is an entitlement-gated add-on (Magic WAN family).
17 // On the standard testing account every MCN call fails with the typed
18 // `FeatureNotEnabled` error (HTTP 403, Cloudflare code 1012 "feature not
19 // enabled"). On-ramps additionally provision real cloud-side infrastructure
20 // (VPN gateways / Transit Gateways) inside a wired CloudIntegration, so the
21 // lifecycle test needs an entitled account AND a discovered VPC, supplied
22 // via env. The probe test always runs and pins the typed tag.
23 const entitled = !!process.env.CLOUDFLARE_TEST_MAGIC_CLOUD_NETWORKING;
24 // Discovered VPC resource id + region to connect (entitled accounts only).
25 const vpcId = process.env.CLOUDFLARE_TEST_MCN_VPC_ID;
26 const vpcRegion = process.env.CLOUDFLARE_TEST_MCN_VPC_REGION;
27
28 // The scoped API token the test harness mints propagates eventually-
29 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
30 // declared in the distilled error union) on out-of-band calls. The retry
31 // is bounded so an unentitled account fails fast with the typed tag.
32 const getOnRamp = (accountId: string, onrampId: string) =>
33 mcn.getOnRamp({ accountId, onrampId }).pipe(
34 Effect.retry({
35 while: (e) => e._tag === "Forbidden",
36 schedule: Schedule.exponential("500 millis"),
37 times: 5,
38 }),
39 );
40
41 // Poll until the on-ramp is gone after destroy. Cloudflare answers GET for
42 // a missing on-ramp with the typed `OnRampNotFound` (404). Deletion tears
43 // down cloud-side infra, so the window is generous but bounded.
44 const expectGone = (accountId: string, onrampId: string) =>
45 getOnRamp(accountId, onrampId).pipe(
46 Effect.flatMap(() => Effect.fail({ _tag: "OnRampNotDeleted" } as const)),
47 Effect.catchTag("OnRampNotFound", () => Effect.void),
48 Effect.retry({
49 while: (e) => e._tag === "OnRampNotDeleted",
50 schedule: Schedule.spaced("5 seconds"),
51 times: 10,
52 }),
53 );
54
55 test.provider(
56 "unentitled accounts surface the typed FeatureNotEnabled error",
57 (stack) =>
58 Effect.gen(function* () {
59 const { accountId } = yield* yield* CloudflareEnvironment;
60
61 yield* stack.destroy();
62
63 const canList = yield* mcn.listOnRamps({ accountId }).pipe(
64 Effect.as(true),
65 Effect.catchTag("FeatureNotEnabled", () => Effect.succeed(false)),
66 );
67 if (canList) {
68 // Entitled account — the gated lifecycle test covers real behavior.
69 yield* Effect.logInfo("account is MCN-entitled; probe test is a no-op");
70 return;
71 }
72
73 // The typed tag — not UnknownCloudflareError, not a status check.
74 const error = yield* mcn.listOnRamps({ accountId }).pipe(Effect.flip);
75 expect(error._tag).toEqual("FeatureNotEnabled");
76
77 const createError = yield* mcn
78 .createOnRamp({
79 accountId,
80 name: "alchemy-mcn-probe",
81 cloudType: "AWS",
82 type: "OnrampTypeSingle",
83 dynamicRouting: false,
84 installRoutesInCloud: false,
85 installRoutesInMagicWan: false,
86 })
87 .pipe(Effect.flip);
88 expect(createError._tag).toEqual("FeatureNotEnabled");
89
90 yield* stack.destroy();
91 }).pipe(logLevel),
92 { tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"] },
93 );
94
95 test.provider.skipIf(!entitled || !vpcId || !vpcRegion)(
96 "creates an on-ramp, updates mutable props in place, and destroys it",
97 (stack) =>
98 Effect.gen(function* () {
99 const { accountId } = yield* yield* CloudflareEnvironment;
100
101 yield* stack.destroy();
102
103 const onramp = yield* stack.deploy(
104 Cloudflare.MagicCloudNetworking.OnRamp("Ramp", {
105 name: testName("alchemy-mcn-onramp"),
106 cloudType: "AWS",
107 type: "OnrampTypeSingle",
108 region: vpcRegion!,
109 vpc: vpcId!,
110 dynamicRouting: false,
111 installRoutesInCloud: false,
112 installRoutesInMagicWan: false,
113 description: "alchemy on-ramp test",
114 }),
115 );
116
117 expect(onramp.onRampId).toBeDefined();
118 expect(onramp.accountId).toEqual(accountId);
119 expect(onramp.name).toEqual(testName("alchemy-mcn-onramp"));
120 expect(onramp.cloudType).toEqual("AWS");
121 expect(onramp.type).toEqual("OnrampTypeSingle");
122
123 // Out-of-band verification via the distilled API.
124 const live = yield* getOnRamp(accountId, onramp.onRampId);
125 expect(live.name).toEqual(testName("alchemy-mcn-onramp"));
126
127 // Update mutable props in place — same onRampId.
128 const updated = yield* stack.deploy(
129 Cloudflare.MagicCloudNetworking.OnRamp("Ramp", {
130 name: testName("alchemy-mcn-onramp-v2"),
131 cloudType: "AWS",
132 type: "OnrampTypeSingle",
133 region: vpcRegion!,
134 vpc: vpcId!,
135 dynamicRouting: false,
136 installRoutesInCloud: false,
137 installRoutesInMagicWan: true,
138 description: "alchemy on-ramp test v2",
139 }),
140 );
141
142 expect(updated.onRampId).toEqual(onramp.onRampId);
143 expect(updated.name).toEqual(testName("alchemy-mcn-onramp-v2"));
144 expect(updated.installRoutesInMagicWan).toBe(true);
145 expect(updated.description).toEqual("alchemy on-ramp test v2");
146
147 yield* stack.destroy();
148
149 yield* expectGone(accountId, onramp.onRampId);
150 }).pipe(logLevel),
151 {
152 tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"],
153 timeout: 120_000,
154 },
155 );
156
157 // Read-only list assertion that runs on every account. On an unentitled
158 // account `list()` catches the typed `FeatureNotEnabled` and returns a
159 // well-typed `[]`; on an entitled account it returns the account's on-ramps
160 // as the exact `read` Attributes shape (an array, possibly empty).
161 test.provider(
162 "list returns on-ramps or a typed [] when unentitled",
163 (stack) =>
164 Effect.gen(function* () {
165 const { accountId } = yield* yield* CloudflareEnvironment;
166
167 yield* stack.destroy();
168
169 const canList = yield* mcn.listOnRamps({ accountId }).pipe(
170 Effect.as(true),
171 Effect.catchTag("FeatureNotEnabled", () => Effect.succeed(false)),
172 );
173
174 const provider = yield* Provider.findProvider(Cloudflare.MagicCloudNetworking.OnRamp);
175 const all = yield* provider.list();
176
177 if (!canList) {
178 // Unentitled — FeatureNotEnabled makes the account non-listable.
179 expect(all).toEqual([]);
180 } else {
181 expect(Array.isArray(all)).toBe(true);
182 }
183
184 yield* stack.destroy();
185 }).pipe(logLevel),
186 { tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"] },
187 );
188
189 test.provider.skipIf(!entitled || !vpcId || !vpcRegion)(
190 "list enumerates the deployed on-ramp",
191 (stack) =>
192 Effect.gen(function* () {
193 const { accountId } = yield* yield* CloudflareEnvironment;
194
195 yield* stack.destroy();
196
197 const onramp = yield* stack.deploy(
198 Cloudflare.MagicCloudNetworking.OnRamp("ListRamp", {
199 name: testName("alchemy-mcn-list-onramp"),
200 cloudType: "AWS",
201 type: "OnrampTypeSingle",
202 region: vpcRegion!,
203 vpc: vpcId!,
204 dynamicRouting: false,
205 installRoutesInCloud: false,
206 installRoutesInMagicWan: false,
207 }),
208 );
209
210 const provider = yield* Provider.findProvider(Cloudflare.MagicCloudNetworking.OnRamp);
211 const all = yield* provider.list();
212
213 expect(all.some((x) => x.onRampId === onramp.onRampId)).toBe(true);
214 expect(all.some((x) => x.name === testName("alchemy-mcn-list-onramp"))).toBe(true);
215
216 yield* stack.destroy();
217
218 yield* expectGone(accountId, onramp.onRampId);
219 }).pipe(logLevel),
220 {
221 tags: ["provider:cloudflare", "provider:cloudflare:magiccloudnetworking", "live"],
222 timeout: 120_000,
223 },
224 );