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

main
ts 226 lines 8.35 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 { describe, expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as cache from "@distilled.cloud/cloudflare/cache";
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 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
16
17 const resolveZoneId = Effect.gen(function* () {
18 const { accountId } = yield* yield* CloudflareEnvironment;
19 const zone = yield* findZoneByName({ accountId, name: zoneName });
20 if (!zone) {
21 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
22 }
23 return zone.id;
24 });
25
26 // The scoped API token the test harness mints propagates eventually-
27 // consistently across Cloudflare's edge — a fresh token intermittently rejects
28 // requests with `403 Forbidden` OR `401 Unauthorized` until it has propagated.
29 // Ride out the blips on the test's own out-of-band calls by retrying both typed
30 // auth errors (part of each cache operation's error union via distilled patches).
31 const tokenPropagationRetrySchedule = Schedule.exponential("500 millis");
32 const isTokenPropagationError = (e: { _tag: string }) =>
33 e._tag === "Forbidden" || e._tag === "Unauthorized";
34
35 /**
36 * Observe the live setting: the configured value, or `undefined` when the
37 * setting does not exist (typed `VariantsNotConfigured`).
38 */
39 const getVariants = (zoneId: string) =>
40 cache.getVariant({ zoneId }).pipe(
41 Effect.catchTag("VariantsNotConfigured", () => Effect.succeed(undefined)),
42 Effect.retry({
43 while: isTokenPropagationError,
44 schedule: tokenPropagationRetrySchedule,
45 times: 8,
46 }),
47 );
48
49 // Normalize to a known baseline: the setting deleted (zone default).
50 const resetBaseline = (zoneId: string) =>
51 cache.deleteVariant({ zoneId }).pipe(
52 Effect.catchTag("VariantsNotConfigured", () => Effect.succeed(undefined)),
53 Effect.retry({
54 while: isTokenPropagationError,
55 schedule: tokenPropagationRetrySchedule,
56 times: 8,
57 }),
58 );
59
60 describe.sequential(
61 "Variants",
62 {
63 tags: ["provider:cloudflare", "provider:cloudflare:cache", "provider:cloudflare:zone", "live"],
64 },
65 () => {
66 test.provider("creates, updates in place, and deletes the variants setting", (stack) =>
67 Effect.gen(function* () {
68 const zoneId = yield* resolveZoneId;
69
70 yield* stack.destroy();
71 // Known baseline: the setting does not exist.
72 yield* resetBaseline(zoneId);
73
74 const created = yield* stack.deploy(
75 Effect.gen(function* () {
76 return yield* Cloudflare.Cache.Variants("ImageVariants", {
77 zoneId,
78 jpeg: ["image/webp"],
79 });
80 }),
81 );
82
83 expect(created.zoneId).toEqual(zoneId);
84 expect(created.value).toEqual({ jpeg: ["image/webp"] });
85 expect(created.editable).toEqual(true);
86
87 // Out-of-band verification via the distilled API.
88 const live = yield* getVariants(zoneId);
89 expect(live).toBeDefined();
90 expect(live!.value.jpeg).toEqual(["image/webp"]);
91
92 // Update in place — PATCH replaces the full value object.
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 return yield* Cloudflare.Cache.Variants("ImageVariants", {
96 zoneId,
97 jpeg: ["image/webp", "image/avif"],
98 png: ["image/webp"],
99 });
100 }),
101 );
102
103 expect(updated.value).toEqual({ jpeg: ["image/webp", "image/avif"], png: ["image/webp"] });
104
105 const liveUpdated = yield* getVariants(zoneId);
106 expect(liveUpdated!.value.jpeg).toEqual(["image/webp", "image/avif"]);
107 expect(liveUpdated!.value.png).toEqual(["image/webp"]);
108
109 // Removing an extension from props unsets it (full replace).
110 const narrowed = yield* stack.deploy(
111 Effect.gen(function* () {
112 return yield* Cloudflare.Cache.Variants("ImageVariants", {
113 zoneId,
114 png: ["image/avif"],
115 });
116 }),
117 );
118
119 expect(narrowed.value).toEqual({ png: ["image/avif"] });
120 const liveNarrowed = yield* getVariants(zoneId);
121 expect(liveNarrowed!.value.jpeg ?? undefined).toBeUndefined();
122 expect(liveNarrowed!.value.png).toEqual(["image/avif"]);
123
124 yield* stack.destroy();
125
126 // Destroy removed the setting entirely — observed via the typed
127 // `VariantsNotConfigured` error (mapped to `undefined`).
128 const gone = yield* getVariants(zoneId);
129 expect(gone).toBeUndefined();
130 }).pipe(logLevel),
131 );
132
133 test.provider("deploy is idempotent and converges out-of-band drift", (stack) =>
134 Effect.gen(function* () {
135 const zoneId = yield* resolveZoneId;
136
137 yield* stack.destroy();
138 yield* resetBaseline(zoneId);
139
140 const created = yield* stack.deploy(
141 Effect.gen(function* () {
142 return yield* Cloudflare.Cache.Variants("ImageVariants", {
143 zoneId,
144 webp: ["image/avif"],
145 });
146 }),
147 );
148 expect(created.value).toEqual({ webp: ["image/avif"] });
149
150 // Drift the setting out-of-band. The next reconcile (triggered by a
151 // prop change) observes the live cloud state and replaces the full
152 // value — the drifted `gif` entry must not survive.
153 yield* cache.patchVariant({ zoneId, value: { gif: ["image/webp"] } }).pipe(
154 Effect.retry({
155 while: isTokenPropagationError,
156 schedule: tokenPropagationRetrySchedule,
157 times: 8,
158 }),
159 );
160
161 const converged = yield* stack.deploy(
162 Effect.gen(function* () {
163 return yield* Cloudflare.Cache.Variants("ImageVariants", {
164 zoneId,
165 webp: ["image/avif"],
166 tiff: ["image/webp"],
167 });
168 }),
169 );
170 expect(converged.value).toEqual({ tiff: ["image/webp"], webp: ["image/avif"] });
171
172 const live = yield* getVariants(zoneId);
173 expect(live!.value.webp).toEqual(["image/avif"]);
174 expect(live!.value.tiff).toEqual(["image/webp"]);
175 expect(live!.value.gif ?? undefined).toBeUndefined();
176
177 yield* stack.destroy();
178
179 const gone = yield* getVariants(zoneId);
180 expect(gone).toBeUndefined();
181 }).pipe(logLevel),
182 );
183
184 // Canonical `list()` test (zone-scoped singleton with create/delete
185 // semantics): there is no account-wide API, so `list()` enumerates every
186 // zone via `listAllZones` and reads the setting in each, skipping zones
187 // where it was never configured. Deploy the setting on the standing test
188 // zone first so it appears in the enumeration, then assert it is present.
189 test.provider("list enumerates the configured variants settings", (stack) =>
190 Effect.gen(function* () {
191 const zoneId = yield* resolveZoneId;
192
193 yield* stack.destroy();
194 yield* resetBaseline(zoneId);
195
196 yield* stack.deploy(
197 Effect.gen(function* () {
198 return yield* Cloudflare.Cache.Variants("ImageVariants", {
199 zoneId,
200 jpeg: ["image/webp"],
201 });
202 }),
203 );
204
205 const provider = yield* Provider.findProvider(Cloudflare.Cache.Variants);
206 // `list()` enumerates every zone with the freshly-minted scoped token,
207 // which propagates eventually-consistently and intermittently returns
208 // `401 Unauthorized` / `403 Forbidden` — ride those blips out too.
209 const all = yield* provider.list().pipe(
210 Effect.retry({
211 while: isTokenPropagationError,
212 schedule: tokenPropagationRetrySchedule,
213 times: 8,
214 }),
215 );
216
217 expect(all.length).toBeGreaterThan(0);
218 const entry = all.find((v) => v.zoneId === zoneId);
219 expect(entry).toBeDefined();
220 expect(entry!.value.jpeg).toEqual(["image/webp"]);
221
222 yield* stack.destroy();
223 }).pipe(logLevel),
224 );
225 },
226 );