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

main
ts 153 lines 5.7 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 cni from "@distilled.cloud/cloudflare/network-interconnects";
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
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // The scoped API token the test harness mints propagates eventually-
16 // consistently across Cloudflare's edge — a fresh token intermittently 403s.
17 // Ride out the blips by retrying the typed `Forbidden` error (part of each
18 // CNI setting operation's error union via distilled patches).
19 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
20
21 const getSetting = (accountId: string) =>
22 cni.getSetting({ accountId }).pipe(
23 Effect.retry({
24 while: (e) => e._tag === "Forbidden",
25 schedule: forbiddenRetrySchedule,
26 times: 8,
27 }),
28 );
29
30 // Normalize the singleton to a known baseline so each run starts from the
31 // same cloud state regardless of what a previous (possibly interrupted)
32 // run left behind. 13335 is Cloudflare's own ASN — the account default.
33 const baselineAsn = 13335;
34
35 const setBaseline = (accountId: string) =>
36 cni.putSetting({ accountId, defaultAsn: baselineAsn }).pipe(
37 Effect.retry({
38 while: (e) => e._tag === "Forbidden",
39 schedule: forbiddenRetrySchedule,
40 times: 8,
41 }),
42 );
43
44 test.provider(
45 "pins the default ASN, updates in place, and restores the original on destroy",
46 (stack) =>
47 Effect.gen(function* () {
48 const { accountId } = yield* yield* CloudflareEnvironment;
49
50 yield* stack.destroy();
51
52 // CNI is entitlement-gated — on accounts without the Network
53 // Interconnect feature every call fails with the typed `Forbidden`
54 // error even after the token-propagation retries. Skip the
55 // lifecycle assertions on such accounts.
56 const probe = yield* getSetting(accountId).pipe(
57 Effect.catchTag("Forbidden", () => Effect.succeed(undefined)),
58 );
59 if (probe === undefined) {
60 yield* Effect.logWarning(
61 "Skipping: account lacks the Cloudflare Network Interconnect entitlement (Forbidden).",
62 );
63 return;
64 }
65
66 yield* setBaseline(accountId);
67
68 const settings = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* Cloudflare.NetworkInterconnects.NetworkInterconnectSettings("CniSettings", {
71 defaultAsn: 65000,
72 });
73 }),
74 );
75
76 expect(settings.accountId).toEqual(accountId);
77 expect(settings.defaultAsn).toEqual(65000);
78 // The pre-management value was captured for restore-on-destroy.
79 expect(settings.initialDefaultAsn).toEqual(baselineAsn);
80
81 // Out-of-band verification via the distilled API.
82 const live = yield* getSetting(accountId);
83 expect(live.defaultAsn).toEqual(65000);
84
85 // Update in place — same singleton, initialDefaultAsn survives.
86 const updated = yield* stack.deploy(
87 Effect.gen(function* () {
88 return yield* Cloudflare.NetworkInterconnects.NetworkInterconnectSettings("CniSettings", {
89 defaultAsn: 64999,
90 });
91 }),
92 );
93 expect(updated.defaultAsn).toEqual(64999);
94 expect(updated.initialDefaultAsn).toEqual(baselineAsn);
95
96 const liveUpdated = yield* getSetting(accountId);
97 expect(liveUpdated.defaultAsn).toEqual(64999);
98
99 yield* stack.destroy();
100
101 // Destroy restored the value the singleton had before we managed it.
102 const restored = yield* getSetting(accountId);
103 expect(restored.defaultAsn).toEqual(baselineAsn);
104 }).pipe(logLevel),
105 {
106 tags: ["provider:cloudflare", "provider:cloudflare:networkinterconnects", "live"],
107 timeout: 120_000,
108 },
109 );
110
111 // Canonical `list()` test (account singleton): there is no enumeration API
112 // for the CNI settings object, so `list()` reads the single
113 // `/accounts/{account_id}/cni/settings` and returns a one-element array.
114 // CNI is enterprise-gated — on accounts without the entitlement the route
115 // rejects with the typed `Forbidden` error, which `list()` maps to `[]`
116 // ("unset"). The read-only assertions below hold in both cases; the
117 // entitled-account content assertion is gated on a non-empty result.
118 test.provider(
119 "list enumerates the CNI settings singleton",
120 (stack) =>
121 Effect.gen(function* () {
122 const { accountId } = yield* yield* CloudflareEnvironment;
123
124 yield* stack.destroy();
125
126 const provider = yield* Provider.findProvider(
127 Cloudflare.NetworkInterconnects.NetworkInterconnectSettings,
128 );
129 const all = yield* provider.list();
130
131 // Always a well-typed array (empty when the account lacks the
132 // entitlement, one element when CNI settings are present).
133 expect(Array.isArray(all)).toBe(true);
134 expect(all.length).toBeLessThanOrEqual(1);
135
136 if (all.length === 0) {
137 yield* Effect.logWarning(
138 "list() returned []: account lacks the Cloudflare Network Interconnect entitlement (Forbidden).",
139 );
140 } else {
141 const [settings] = all;
142 expect(settings.accountId).toEqual(accountId);
143 expect(typeof settings.defaultAsn).toBe("number");
144 expect(settings.initialDefaultAsn).toEqual(settings.defaultAsn);
145 }
146
147 yield* stack.destroy();
148 }).pipe(logLevel),
149 {
150 tags: ["provider:cloudflare", "provider:cloudflare:networkinterconnects", "live"],
151 timeout: 120_000,
152 },
153 );