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

main
ts 244 lines 8.79 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as zones from "@distilled.cloud/cloudflare/zones";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
17
18 const resolveZoneId = Effect.gen(function* () {
19 const { accountId } = yield* yield* CloudflareEnvironment;
20 const zone = yield* findZoneByName({ accountId, name: zoneName });
21 if (!zone) {
22 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
23 }
24 return zone.id;
25 });
26
27 // The scoped API token the test harness mints propagates eventually-
28 // consistently across Cloudflare's edge — a fresh token intermittently 403s
29 // with "Unable to authenticate request". Ride out the blips on the test's
30 // own out-of-band calls by retrying the typed `Forbidden` error (part of
31 // each setting operation's error union via distilled patches).
32 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
33
34 const getSetting = (zoneId: string, settingId: string) =>
35 zones.getSetting({ zoneId, settingId }).pipe(
36 Effect.retry({
37 while: (e) => e._tag === "Forbidden",
38 schedule: forbiddenRetrySchedule,
39 times: 8,
40 }),
41 );
42
43 // Normalize a setting to a known baseline so each run starts from the same
44 // cloud state regardless of what a previous (possibly interrupted) run left
45 // behind.
46 const setBaseline = (zoneId: string, settingId: string, value: unknown) =>
47 zones.patchSetting({ zoneId, settingId, value }).pipe(
48 Effect.retry({
49 while: (e) => e._tag === "Forbidden",
50 schedule: forbiddenRetrySchedule,
51 times: 8,
52 }),
53 );
54
55 const valueOf = (setting: zones.GetSettingResponse): unknown =>
56 (setting as { value?: unknown }).value;
57
58 test.provider(
59 "pins a toggle setting and restores the original value on destroy",
60 (stack) =>
61 Effect.gen(function* () {
62 const zoneId = yield* resolveZoneId;
63
64 yield* stack.destroy();
65 // Known baseline: always_online defaults to "on".
66 yield* setBaseline(zoneId, "always_online", "on");
67
68 const setting = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* Cloudflare.Zone.Setting("AlwaysOnline", {
71 zoneId,
72 settingId: "always_online",
73 value: "off",
74 });
75 }),
76 );
77
78 expect(setting.zoneId).toEqual(zoneId);
79 expect(setting.settingId).toEqual("always_online");
80 expect(setting.value).toEqual("off");
81 // The pre-management value was captured for restore-on-destroy.
82 expect(setting.initialValue).toEqual("on");
83
84 const live = yield* getSetting(zoneId, "always_online");
85 expect(valueOf(live)).toEqual("off");
86
87 yield* stack.destroy();
88
89 // Destroy restored the value the setting had before we managed it.
90 const restored = yield* getSetting(zoneId, "always_online");
91 expect(valueOf(restored)).toEqual("on");
92 }).pipe(logLevel),
93 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
94 );
95
96 test.provider(
97 "updates a numeric setting in place and keeps the captured initial value",
98 (stack) =>
99 Effect.gen(function* () {
100 const zoneId = yield* resolveZoneId;
101
102 yield* stack.destroy();
103 // Known baseline: browser_cache_ttl defaults to 14400 (4 hours).
104 yield* setBaseline(zoneId, "browser_cache_ttl", 14400);
105
106 const initial = yield* stack.deploy(
107 Effect.gen(function* () {
108 return yield* Cloudflare.Zone.Setting("BrowserCacheTtl", {
109 zoneId,
110 settingId: "browser_cache_ttl",
111 value: 1800,
112 });
113 }),
114 );
115
116 expect(initial.value).toEqual(1800);
117 expect(initial.initialValue).toEqual(14400);
118
119 const updated = yield* stack.deploy(
120 Effect.gen(function* () {
121 return yield* Cloudflare.Zone.Setting("BrowserCacheTtl", {
122 zoneId,
123 settingId: "browser_cache_ttl",
124 value: 3600,
125 });
126 }),
127 );
128
129 // Same singleton patched in place; the original value survives the
130 // update so destroy still restores the pre-management state.
131 expect(updated.value).toEqual(3600);
132 expect(updated.initialValue).toEqual(14400);
133
134 const live = yield* getSetting(zoneId, "browser_cache_ttl");
135 expect(valueOf(live)).toEqual(3600);
136
137 yield* stack.destroy();
138
139 const restored = yield* getSetting(zoneId, "browser_cache_ttl");
140 expect(valueOf(restored)).toEqual(14400);
141 }).pipe(logLevel),
142 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
143 );
144
145 test.provider(
146 "changing settingId replaces — old setting restored, new setting pinned",
147 (stack) =>
148 Effect.gen(function* () {
149 const zoneId = yield* resolveZoneId;
150
151 yield* stack.destroy();
152 // Known baselines: browser_check and email_obfuscation default to "on".
153 yield* setBaseline(zoneId, "browser_check", "on");
154 yield* setBaseline(zoneId, "email_obfuscation", "on");
155
156 const first = yield* stack.deploy(
157 Effect.gen(function* () {
158 return yield* Cloudflare.Zone.Setting("ReplaceSetting", {
159 zoneId,
160 settingId: "browser_check",
161 value: "off",
162 });
163 }),
164 );
165
166 expect(first.settingId).toEqual("browser_check");
167 const firstLive = yield* getSetting(zoneId, "browser_check");
168 expect(valueOf(firstLive)).toEqual("off");
169
170 // settingId is the resource's identity — switching it is a
171 // replacement: the new setting gets pinned and the old setting is
172 // restored to its pre-management value as the old instance deletes.
173 const replaced = yield* stack.deploy(
174 Effect.gen(function* () {
175 return yield* Cloudflare.Zone.Setting("ReplaceSetting", {
176 zoneId,
177 settingId: "email_obfuscation",
178 value: "off",
179 });
180 }),
181 );
182
183 expect(replaced.settingId).toEqual("email_obfuscation");
184 expect(replaced.initialValue).toEqual("on");
185
186 const newLive = yield* getSetting(zoneId, "email_obfuscation");
187 expect(valueOf(newLive)).toEqual("off");
188
189 const oldRestored = yield* getSetting(zoneId, "browser_check");
190 expect(valueOf(oldRestored)).toEqual("on");
191
192 yield* stack.destroy();
193
194 const finalRestored = yield* getSetting(zoneId, "email_obfuscation");
195 expect(valueOf(finalRestored)).toEqual("on");
196 }).pipe(logLevel),
197 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
198 );
199
200 // Canonical `list()` test. Zone settings are keyed by (zoneId, settingId):
201 // `list()` enumerates every zone via `listAllZones` and reads each known
202 // setting, emitting one Attributes per (zone, setting) — the same shape
203 // `read` produces. Deploy a deterministic setting on the standing test zone
204 // and assert that exact (zone, setting) entry shows up in the listing.
205 test.provider(
206 "list enumerates the deployed (zone, setting) pair",
207 (stack) =>
208 Effect.gen(function* () {
209 const zoneId = yield* resolveZoneId;
210
211 yield* stack.destroy();
212 // Known baseline so the value we assert against is deterministic.
213 yield* setBaseline(zoneId, "always_use_https", "off");
214
215 const deployed = yield* stack.deploy(
216 Effect.gen(function* () {
217 return yield* Cloudflare.Zone.Setting("AlwaysUseHttps", {
218 zoneId,
219 settingId: "always_use_https",
220 value: "on",
221 });
222 }),
223 );
224 expect(deployed.settingId).toEqual("always_use_https");
225 expect(deployed.value).toEqual("on");
226
227 const provider = yield* Provider.findProvider(Cloudflare.Zone.Setting);
228 const all = yield* provider.list();
229
230 // The deployed (zone, setting) pair is present, hydrated into the exact
231 // `read`/`Attributes` shape (one row per (zoneId, settingId)).
232 expect(all.length).toBeGreaterThan(0);
233 const entry = all.find((s) => s.zoneId === zoneId && s.settingId === "always_use_https");
234 expect(entry).toBeDefined();
235 expect(entry?.value).toEqual("on");
236
237 yield* stack.destroy();
238
239 // Capture-and-restore: destroy put the setting back to its baseline.
240 const restored = yield* getSetting(zoneId, "always_use_https");
241 expect(valueOf(restored)).toEqual("off");
242 }).pipe(logLevel),
243 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
244 );