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

main
ts 330 lines 11.4 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 addressing from "@distilled.cloud/cloudflare/addressing";
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 * as Stream from "effect/Stream";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // A freshly minted scoped token propagates eventually-consistently across
17 // Cloudflare's edge — retry the typed `Forbidden` blips on out-of-band calls.
18 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
19
20 const retryForbidden = <A, E extends { _tag: string }, R>(effect: Effect.Effect<A, E, R>) =>
21 effect.pipe(
22 Effect.retry({
23 while: (e) => e._tag === "Forbidden",
24 schedule: forbiddenRetrySchedule,
25 times: 8,
26 }),
27 );
28
29 const resolveAccountId = Effect.gen(function* () {
30 const { accountId } = yield* yield* CloudflareEnvironment;
31 return accountId;
32 });
33
34 // BYOIP prefixes can only be onboarded through a Cloudflare contract process
35 // (LOA upload, IRR/RPKI validation, manual approval), so the full lifecycle
36 // tests are gated behind env vars pointing at a BYOIP-entitled account:
37 // CLOUDFLARE_TEST_BYOIP_CIDR — a CIDR onboardable by the account
38 // CLOUDFLARE_TEST_BYOIP_ASN — the ASN to advertise under
39 // CLOUDFLARE_TEST_BYOIP_PREFIX_ID — an existing approved prefix id
40 // CLOUDFLARE_TEST_BYOIP_DELEGATE_ACCOUNT_ID — second account for delegations
41 const byoipCidr = process.env.CLOUDFLARE_TEST_BYOIP_CIDR;
42 const byoipAsn = process.env.CLOUDFLARE_TEST_BYOIP_ASN;
43 const byoipPrefixId = process.env.CLOUDFLARE_TEST_BYOIP_PREFIX_ID;
44 const delegateAccountId = process.env.CLOUDFLARE_TEST_BYOIP_DELEGATE_ACCOUNT_ID;
45
46 // The read-only catalog endpoints are available regardless of the BYOIP
47 // entitlement — exercise the distilled wiring live on every run.
48 test.provider(
49 "lists the services catalog and prefixes (read-only)",
50 (stack) =>
51 Effect.gen(function* () {
52 const accountId = yield* resolveAccountId;
53
54 yield* stack.destroy();
55
56 const services = yield* retryForbidden(
57 addressing.listServices.items({ accountId }).pipe(
58 Stream.runCollect,
59 Effect.map((c) => Array.from(c)),
60 ),
61 );
62 expect(Array.isArray(services)).toBe(true);
63
64 const prefixes = yield* retryForbidden(
65 addressing.listPrefixes.items({ accountId }).pipe(
66 Stream.runCollect,
67 Effect.map((c) => Array.from(c)),
68 ),
69 );
70 expect(Array.isArray(prefixes)).toBe(true);
71
72 yield* stack.destroy();
73 }).pipe(logLevel),
74 { tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"] },
75 );
76
77 // `list()` enumerates account-scoped BYOIP prefixes via the catalog endpoint,
78 // which is available regardless of the BYOIP entitlement (it returns an empty
79 // array on accounts with no onboarded prefixes). The result is a well-typed
80 // `PrefixAttributes[]` — the exact shape `read` produces.
81 test.provider(
82 "list enumerates account prefixes (read-only)",
83 (stack) =>
84 Effect.gen(function* () {
85 yield* stack.destroy();
86
87 const provider = yield* Provider.findProvider(Cloudflare.Addressing.Prefix);
88 const all = yield* retryForbidden(provider.list());
89
90 expect(Array.isArray(all)).toBe(true);
91 for (const p of all) {
92 expect(typeof p.prefixId).toBe("string");
93 expect(typeof p.accountId).toBe("string");
94 expect(typeof p.cidr).toBe("string");
95 expect(typeof p.asn).toBe("number");
96 }
97
98 yield* stack.destroy();
99 }).pipe(logLevel),
100 { tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"] },
101 );
102
103 test.provider.skipIf(!byoipCidr || !byoipAsn)(
104 "prefix: create, patch description in place, destroy",
105 (stack) =>
106 Effect.gen(function* () {
107 const accountId = yield* resolveAccountId;
108 const cidr = byoipCidr!;
109 const asn = Number(byoipAsn!);
110
111 yield* stack.destroy();
112
113 const created = yield* stack.deploy(
114 Effect.gen(function* () {
115 return yield* Cloudflare.Addressing.Prefix("Byoip", {
116 cidr,
117 asn,
118 description: "alchemy v1",
119 delegateLoaCreation: true,
120 });
121 }),
122 );
123 expect(created.prefixId).toBeDefined();
124 expect(created.cidr).toEqual(cidr);
125 expect(created.asn).toEqual(asn);
126 expect(created.description).toEqual("alchemy v1");
127
128 // Out-of-band verification.
129 const live = yield* retryForbidden(
130 addressing.getPrefix({ accountId, prefixId: created.prefixId }),
131 );
132 expect(live.cidr).toEqual(cidr);
133 expect(live.description).toEqual("alchemy v1");
134
135 // Update the description in place — same physical prefix.
136 const updated = yield* stack.deploy(
137 Effect.gen(function* () {
138 return yield* Cloudflare.Addressing.Prefix("Byoip", {
139 cidr,
140 asn,
141 description: "alchemy v2",
142 delegateLoaCreation: true,
143 });
144 }),
145 );
146 expect(updated.prefixId).toEqual(created.prefixId);
147 expect(updated.description).toEqual("alchemy v2");
148
149 // Destroy and verify gone via the typed not-found read.
150 yield* stack.destroy();
151 const gone = yield* retryForbidden(
152 addressing.getPrefix({ accountId, prefixId: created.prefixId }),
153 ).pipe(Effect.catchTag("PrefixNotFound", () => Effect.succeed(undefined)));
154 expect(gone).toBeUndefined();
155 }).pipe(logLevel),
156 {
157 tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"],
158 timeout: 120_000,
159 },
160 );
161
162 test.provider.skipIf(!byoipPrefixId)(
163 "bgp prefix: adopt-or-create by cidr, toggle advertisement, withdraw on destroy",
164 (stack) =>
165 Effect.gen(function* () {
166 const accountId = yield* resolveAccountId;
167 const prefixId = byoipPrefixId!;
168
169 const prefix = yield* retryForbidden(addressing.getPrefix({ accountId, prefixId }));
170 const cidr = prefix.cidr!;
171
172 yield* stack.destroy();
173
174 // Cloudflare auto-creates BGP prefixes during onboarding — the
175 // resource adopts the one matching the CIDR rather than duplicating.
176 const created = yield* stack.deploy(
177 Effect.gen(function* () {
178 return yield* Cloudflare.Addressing.BgpPrefix("Bgp", {
179 prefixId,
180 cidr,
181 advertised: false,
182 });
183 }),
184 );
185 expect(created.bgpPrefixId).toBeDefined();
186 expect(created.cidr).toEqual(cidr);
187 expect(created.onDemand.advertised).toEqual(false);
188
189 // Advertise in place — same BGP prefix; propagation is eventually
190 // consistent so only the API acknowledgement is asserted.
191 const advertised = yield* stack.deploy(
192 Effect.gen(function* () {
193 return yield* Cloudflare.Addressing.BgpPrefix("Bgp", {
194 prefixId,
195 cidr,
196 advertised: true,
197 });
198 }),
199 );
200 expect(advertised.bgpPrefixId).toEqual(created.bgpPrefixId);
201 expect(advertised.onDemand.advertised).toEqual(true);
202
203 // Destroy withdraws the advertisement (no delete API exists).
204 yield* stack.destroy();
205 const after = yield* retryForbidden(
206 addressing.getPrefixBgpPrefix({
207 accountId,
208 prefixId,
209 bgpPrefixId: created.bgpPrefixId,
210 }),
211 );
212 expect(after.onDemand?.advertised ?? false).toEqual(false);
213 }).pipe(logLevel),
214 {
215 tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"],
216 timeout: 120_000,
217 },
218 );
219
220 test.provider.skipIf(!byoipPrefixId || !delegateAccountId)(
221 "prefix delegation: create and destroy (replace-only resource)",
222 (stack) =>
223 Effect.gen(function* () {
224 const accountId = yield* resolveAccountId;
225 const prefixId = byoipPrefixId!;
226
227 const prefix = yield* retryForbidden(addressing.getPrefix({ accountId, prefixId }));
228 const cidr = prefix.cidr!;
229
230 yield* stack.destroy();
231
232 const created = yield* stack.deploy(
233 Effect.gen(function* () {
234 return yield* Cloudflare.Addressing.PrefixDelegation("Share", {
235 prefixId,
236 cidr,
237 delegatedAccountId: delegateAccountId!,
238 });
239 }),
240 );
241 expect(created.delegationId).toBeDefined();
242 expect(created.cidr).toEqual(cidr);
243 expect(created.delegatedAccountId).toEqual(delegateAccountId);
244
245 // Out-of-band verification via the list endpoint (no get op).
246 const delegations = yield* retryForbidden(
247 addressing.listPrefixDelegations.items({ accountId, prefixId }).pipe(
248 Stream.runCollect,
249 Effect.map((c) => Array.from(c)),
250 ),
251 );
252 expect(delegations.some((d) => d.id === created.delegationId)).toBe(true);
253
254 yield* stack.destroy();
255 const after = yield* retryForbidden(
256 addressing.listPrefixDelegations.items({ accountId, prefixId }).pipe(
257 Stream.runCollect,
258 Effect.map((c) => Array.from(c)),
259 ),
260 );
261 expect(after.some((d) => d.id === created.delegationId)).toBe(false);
262 }).pipe(logLevel),
263 {
264 tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"],
265 timeout: 120_000,
266 },
267 );
268
269 test.provider.skipIf(!byoipPrefixId)(
270 "service binding: create against the CDN service and destroy",
271 (stack) =>
272 Effect.gen(function* () {
273 const accountId = yield* resolveAccountId;
274 const prefixId = byoipPrefixId!;
275
276 const prefix = yield* retryForbidden(addressing.getPrefix({ accountId, prefixId }));
277 const cidr = prefix.cidr!;
278
279 const services = yield* retryForbidden(
280 addressing.listServices.items({ accountId }).pipe(
281 Stream.runCollect,
282 Effect.map((c) => Array.from(c)),
283 ),
284 );
285 const cdn = services.find((s) => s.name === "CDN") ?? services[0];
286 expect(cdn?.id).toBeDefined();
287
288 yield* stack.destroy();
289
290 const created = yield* stack.deploy(
291 Effect.gen(function* () {
292 return yield* Cloudflare.Addressing.ServiceBinding("Cdn", {
293 prefixId,
294 cidr,
295 serviceId: cdn!.id!,
296 });
297 }),
298 );
299 expect(created.bindingId).toBeDefined();
300 expect(created.serviceId).toEqual(cdn!.id);
301 // Provisioning to the edge is asynchronous.
302 expect(["provisioning", "active"]).toContain(created.provisioning.state ?? "provisioning");
303
304 // Out-of-band verification.
305 const live = yield* retryForbidden(
306 addressing.getPrefixServiceBinding({
307 accountId,
308 prefixId,
309 bindingId: created.bindingId,
310 }),
311 );
312 expect(live.cidr).toEqual(cidr);
313
314 yield* stack.destroy();
315 const gone = yield* retryForbidden(
316 addressing.getPrefixServiceBinding({
317 accountId,
318 prefixId,
319 bindingId: created.bindingId,
320 }),
321 ).pipe(
322 Effect.catchTag(["BindingNotFound", "PrefixNotFound"], () => Effect.succeed(undefined)),
323 );
324 expect(gone).toBeUndefined();
325 }).pipe(logLevel),
326 {
327 tags: ["provider:cloudflare", "provider:cloudflare:addressing", "live"],
328 timeout: 120_000,
329 },
330 );