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

main
ts 211 lines 6.83 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 pageShield from "@distilled.cloud/cloudflare/page-shield";
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 // Fresh scoped tokens propagate eventually-consistently across Cloudflare's
28 // edge — retry the typed `Forbidden` error on out-of-band calls.
29 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
30
31 const getSettings = (zoneId: string) =>
32 pageShield.getPageShield({ zoneId }).pipe(
33 Effect.retry({
34 while: (e) => e._tag === "Forbidden",
35 schedule: forbiddenRetrySchedule,
36 times: 8,
37 }),
38 );
39
40 // Known baseline for the singleton: Page Shield off, API defaults for the
41 // reporting flags. Tests start from here so `initial*` capture is
42 // deterministic, and destroy must put the zone back here.
43 const setBaseline = (zoneId: string) =>
44 pageShield
45 .putPageShield({
46 zoneId,
47 enabled: false,
48 useCloudflareReportingEndpoint: true,
49 useConnectionUrlPath: false,
50 })
51 .pipe(
52 Effect.retry({
53 while: (e) => e._tag === "Forbidden",
54 schedule: forbiddenRetrySchedule,
55 times: 8,
56 }),
57 );
58
59 test.provider(
60 "enables Page Shield, updates in place, and restores the baseline on destroy",
61 (stack) =>
62 Effect.gen(function* () {
63 const zoneId = yield* resolveZoneId;
64
65 yield* stack.destroy();
66 yield* setBaseline(zoneId);
67
68 // Create — enable Page Shield with defaults.
69 const settings = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* Cloudflare.PageShield.Settings("PageShield", {
72 zoneId,
73 });
74 }),
75 );
76
77 expect(settings.zoneId).toEqual(zoneId);
78 expect(settings.enabled).toEqual(true);
79 expect(settings.useCloudflareReportingEndpoint).toEqual(true);
80 expect(settings.useConnectionUrlPath).toEqual(false);
81 // The pre-management state was captured for restore-on-destroy.
82 expect(settings.initialEnabled).toEqual(false);
83 expect(settings.initialUseCloudflareReportingEndpoint).toEqual(true);
84 expect(settings.initialUseConnectionUrlPath).toEqual(false);
85
86 // Out-of-band verification via the distilled API.
87 const live = yield* getSettings(zoneId);
88 expect(live.enabled).toEqual(true);
89
90 // Update in place — flip `enabled` (the only flag writable without
91 // extra entitlements on the test zone); same singleton, initial*
92 // survive.
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 return yield* Cloudflare.PageShield.Settings("PageShield", {
96 zoneId,
97 enabled: false,
98 });
99 }),
100 );
101 expect(updated.enabled).toEqual(false);
102 expect(updated.initialEnabled).toEqual(false);
103
104 const liveDisabled = yield* getSettings(zoneId);
105 expect(liveDisabled.enabled).toEqual(false);
106
107 // And back on — the last managed state differs from the baseline,
108 // so the restore below is observable.
109 const reenabled = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* Cloudflare.PageShield.Settings("PageShield", {
112 zoneId,
113 enabled: true,
114 });
115 }),
116 );
117 expect(reenabled.enabled).toEqual(true);
118 expect(reenabled.initialEnabled).toEqual(false);
119
120 const liveReenabled = yield* getSettings(zoneId);
121 expect(liveReenabled.enabled).toEqual(true);
122
123 yield* stack.destroy();
124
125 // Destroy restored the configuration the zone had before us.
126 const restored = yield* getSettings(zoneId);
127 expect(restored.enabled).toEqual(false);
128 expect(restored.useCloudflareReportingEndpoint).toEqual(true);
129 expect(restored.useConnectionUrlPath).toEqual(false);
130 }).pipe(logLevel),
131 {
132 tags: [
133 "provider:cloudflare",
134 "provider:cloudflare:pageshield",
135 "provider:cloudflare:zone",
136 "live",
137 ],
138 timeout: 120_000,
139 },
140 );
141
142 test.provider(
143 "surfaces the typed NotEntitled error for plan-gated flags",
144 (stack) =>
145 Effect.gen(function* () {
146 const zoneId = yield* resolveZoneId;
147
148 yield* stack.destroy();
149
150 // The test zone lacks the connection-monitor entitlement — the
151 // distilled call must fail with the typed entitlement tag.
152 const error = yield* pageShield
153 .putPageShield({
154 zoneId,
155 enabled: true,
156 useCloudflareReportingEndpoint: true,
157 useConnectionUrlPath: true,
158 })
159 .pipe(
160 Effect.retry({
161 while: (e) => e._tag === "Forbidden",
162 schedule: forbiddenRetrySchedule,
163 times: 8,
164 }),
165 Effect.flip,
166 );
167 expect(error._tag).toEqual("NotEntitled");
168
169 yield* stack.destroy();
170 }).pipe(logLevel),
171 {
172 tags: [
173 "provider:cloudflare",
174 "provider:cloudflare:pageshield",
175 "provider:cloudflare:zone",
176 "live",
177 ],
178 timeout: 120_000,
179 },
180 );
181
182 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
183 // API for this per-zone configuration, so `list()` enumerates every zone via
184 // `listAllZones` and reads the singleton in each. Assert the result is
185 // non-empty and contains the standing test zone.
186 test.provider(
187 "list enumerates Page Shield settings across all zones",
188 (stack) =>
189 Effect.gen(function* () {
190 const zoneId = yield* resolveZoneId;
191
192 const provider = yield* Provider.findProvider(Cloudflare.PageShield.Settings);
193 const all = yield* provider.list();
194
195 expect(all.length).toBeGreaterThan(0);
196 expect(all.some((s) => s.zoneId === zoneId)).toBe(true);
197
198 // `stack` is unused here (the singleton always exists on every zone),
199 // but keep the destroy bookend so the harness state stays clean.
200 yield* stack.destroy();
201 }).pipe(logLevel),
202 {
203 tags: [
204 "provider:cloudflare",
205 "provider:cloudflare:pageshield",
206 "provider:cloudflare:zone",
207 "live",
208 ],
209 timeout: 120_000,
210 },
211 );