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

main
ts 212 lines 8.62 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 contentScanning from "@distilled.cloud/cloudflare/content-scanning";
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 // Custom scan expressions require WAF Content Scanning (an Enterprise paid
18 // add-on) to be enabled on the zone. On the testing account's zone every
19 // payload call fails with "File Upload Scan not enabled", surfaced as the
20 // typed `ContentScanningNotEnabled` error. The full lifecycle test below is
21 // gated behind an entitled zone id supplied via env.
22 const entitledZoneId = process.env.CLOUDFLARE_TEST_CONTENT_SCANNING_ZONE_ID;
23
24 const resolveZoneId = Effect.gen(function* () {
25 const { accountId } = yield* yield* CloudflareEnvironment;
26 const zone = yield* findZoneByName({ accountId, name: zoneName });
27 if (!zone) {
28 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
29 }
30 return zone.id;
31 });
32
33 // Ride out eventual-consistency 403 blips on the test's own out-of-band
34 // calls by retrying the typed `Forbidden` error.
35 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
36
37 const listExpressions = (zoneId: string) =>
38 contentScanning.listPayloads({ zoneId }).pipe(
39 Effect.retry({
40 while: (e) => e._tag === "Forbidden",
41 schedule: forbiddenRetrySchedule,
42 times: 8,
43 }),
44 Effect.map((r) => r.result),
45 );
46
47 // Both cases enable the same zone-level content-scanning singleton as a prerequisite; run them serially so they don't fight over that singleton under the global concurrent test config.
48 describe.sequential(
49 "Expression",
50 { tags: ["provider:cloudflare", "provider:cloudflare:contentscanning", "live"] },
51 () => {
52 test.provider(
53 "surfaces the typed ContentScanningNotEnabled error when scanning is disabled",
54 (stack) =>
55 Effect.gen(function* () {
56 const zoneId = yield* resolveZoneId;
57
58 yield* stack.destroy();
59
60 // The standard testing zone has Content Scanning disabled (and lacks
61 // the add-on entirely) — payload calls must fail with the typed tag.
62 const error = yield* contentScanning.listPayloads({ zoneId }).pipe(
63 Effect.retry({
64 while: (e) => e._tag === "Forbidden",
65 schedule: forbiddenRetrySchedule,
66 times: 8,
67 }),
68 Effect.flip,
69 );
70 expect(error._tag).toEqual("ContentScanningNotEnabled");
71
72 yield* stack.destroy();
73 }).pipe(logLevel),
74 { tags: ["provider:cloudflare:zone"] },
75 );
76
77 // Requires WAF Content Scanning (Enterprise paid add-on) on the zone — without it payload
78 // calls fail with the typed ContentScanningNotEnabled. Unlock with CLOUDFLARE_TEST_CONTENT_SCANNING_ZONE_ID=<zone id>.
79 test.provider.skipIf(!entitledZoneId)(
80 "creates a custom expression, replaces on payload change, destroys cleanly",
81 (stack) =>
82 Effect.gen(function* () {
83 const zoneId = entitledZoneId!;
84
85 yield* stack.destroy();
86
87 const firstPayload = 'lookup_json_string(http.request.body.raw, "file")';
88 const secondPayload = 'lookup_json_string(http.request.body.raw, "document")';
89
90 // Scanning must be enabled for payload calls; the expression depends
91 // on the singleton through its zoneId output.
92 const first = yield* stack.deploy(
93 Effect.gen(function* () {
94 const scanning = yield* Cloudflare.ContentScanning.ContentScanning("UploadScanning", {
95 zoneId,
96 });
97 return yield* Cloudflare.ContentScanning.Expression("ScanField", {
98 zoneId: scanning.zoneId,
99 payload: firstPayload,
100 });
101 }),
102 );
103
104 expect(first.zoneId).toEqual(zoneId);
105 expect(first.payload).toEqual(firstPayload);
106 expect(first.expressionId).not.toEqual("");
107
108 // Out-of-band verification via the distilled API.
109 const live = yield* listExpressions(zoneId);
110 expect(live.some((e) => e.id === first.expressionId)).toBe(true);
111
112 // Changing the payload is a replacement — there is no update
113 // endpoint, so a new expression is created and the old one deleted.
114 const replaced = yield* stack.deploy(
115 Effect.gen(function* () {
116 const scanning = yield* Cloudflare.ContentScanning.ContentScanning("UploadScanning", {
117 zoneId,
118 });
119 return yield* Cloudflare.ContentScanning.Expression("ScanField", {
120 zoneId: scanning.zoneId,
121 payload: secondPayload,
122 });
123 }),
124 );
125
126 expect(replaced.payload).toEqual(secondPayload);
127 expect(replaced.expressionId).not.toEqual(first.expressionId);
128
129 const afterReplace = yield* listExpressions(zoneId);
130 expect(afterReplace.some((e) => e.id === replaced.expressionId)).toBe(true);
131 expect(afterReplace.some((e) => e.id === first.expressionId)).toBe(false);
132
133 yield* stack.destroy();
134
135 // The expression is gone; the singleton was restored to its
136 // pre-management status by its own destroy.
137 const status = yield* contentScanning.getContentScanning({ zoneId });
138 if (status.value === "enabled") {
139 // Zone was already enabled before the test — expressions must
140 // still be cleaned up.
141 const remaining = yield* listExpressions(zoneId);
142 expect(remaining.some((e) => e.id === replaced.expressionId)).toBe(false);
143 }
144 }).pipe(logLevel),
145 { timeout: 120_000 },
146 );
147
148 // list() fans out over every zone and skips zones without Content Scanning
149 // (typed ContentScanningNotEnabled/Forbidden) or deleted zones (InvalidRoute).
150 // The standing test zone has scanning disabled, so this returns a well-typed
151 // array (empty for un-entitled zones) without throwing — proving the
152 // pagination + typed-skip wiring end-to-end.
153 test.provider(
154 "list returns a well-typed array across all zones",
155 (stack) =>
156 Effect.gen(function* () {
157 yield* stack.destroy();
158
159 const provider = yield* Provider.findProvider(Cloudflare.ContentScanning.Expression);
160 const all = yield* provider.list();
161
162 expect(Array.isArray(all)).toBe(true);
163 for (const item of all) {
164 expect(typeof item.zoneId).toBe("string");
165 expect(typeof item.payload).toBe("string");
166 expect(typeof item.expressionId).toBe("string");
167 }
168
169 yield* stack.destroy();
170 }).pipe(logLevel),
171 { timeout: 120_000 },
172 );
173
174 // Requires WAF Content Scanning (Enterprise paid add-on) on the zone — without
175 // it payload calls fail with the typed ContentScanningNotEnabled. Unlock with
176 // CLOUDFLARE_TEST_CONTENT_SCANNING_ZONE_ID=<zone id>.
177 test.provider.skipIf(!entitledZoneId)(
178 "list enumerates the deployed expression",
179 (stack) =>
180 Effect.gen(function* () {
181 const zoneId = entitledZoneId!;
182
183 yield* stack.destroy();
184
185 const payload = 'lookup_json_string(http.request.body.raw, "file")';
186
187 const deployed = yield* stack.deploy(
188 Effect.gen(function* () {
189 const scanning = yield* Cloudflare.ContentScanning.ContentScanning("UploadScanning", {
190 zoneId,
191 });
192 return yield* Cloudflare.ContentScanning.Expression("ListField", {
193 zoneId: scanning.zoneId,
194 payload,
195 });
196 }),
197 );
198
199 const provider = yield* Provider.findProvider(Cloudflare.ContentScanning.Expression);
200 const all = yield* provider.list();
201
202 const found = all.find((e) => e.expressionId === deployed.expressionId);
203 expect(found).toBeDefined();
204 expect(found?.zoneId).toEqual(zoneId);
205 expect(found?.payload).toEqual(payload);
206
207 yield* stack.destroy();
208 }).pipe(logLevel),
209 { timeout: 120_000 },
210 );
211 },
212 );