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

main
ts 188 lines 6.59 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 logs from "@distilled.cloud/cloudflare/logs";
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 // Logpull (and its retention flag) is an Enterprise-only feature. On the
28 // standard testing account every `/logs/control/retention/flag` call fails
29 // with Cloudflare error code 10000 ("Unauthorized"), surfaced as the typed
30 // `LogsControlNotAuthorized` error. The full lifecycle test below is gated
31 // behind an entitled zone supplied via env.
32 const entitled = !!process.env.CLOUDFLARE_TEST_LOGS_CONTROL;
33
34 // The scoped API token the test harness mints propagates eventually-
35 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
36 // declared in the distilled error union via patches) on out-of-band calls.
37 const getFlag = (zoneId: string) =>
38 logs.getControlRetention({ zoneId }).pipe(
39 Effect.map((r) => r.flag ?? false),
40 Effect.retry({
41 while: (e) => e._tag === "Forbidden",
42 schedule: Schedule.exponential("500 millis"),
43 times: 8,
44 }),
45 );
46
47 const setBaseline = (zoneId: string, flag: boolean) =>
48 logs.createControlRetention({ zoneId, flag }).pipe(
49 Effect.retry({
50 while: (e) => e._tag === "Forbidden",
51 schedule: Schedule.exponential("500 millis"),
52 times: 8,
53 }),
54 );
55
56 test.provider.skipIf(entitled)(
57 "surfaces the typed LogsControlNotAuthorized error on unentitled zones",
58 (stack) =>
59 Effect.gen(function* () {
60 const zoneId = yield* resolveZoneId;
61
62 yield* stack.destroy();
63
64 // The testing zone has no Logpull entitlement — both reads and
65 // writes must fail with the typed authorization tag (Cloudflare
66 // error code 10000).
67 const readError = yield* logs.getControlRetention({ zoneId }).pipe(Effect.flip);
68 expect(readError._tag).toEqual("LogsControlNotAuthorized");
69
70 const writeError = yield* logs
71 .createControlRetention({ zoneId, flag: true })
72 .pipe(Effect.flip);
73 expect(writeError._tag).toEqual("LogsControlNotAuthorized");
74
75 yield* stack.destroy();
76 }).pipe(logLevel),
77 {
78 tags: [
79 "provider:cloudflare",
80 "provider:cloudflare:logscontrol",
81 "provider:cloudflare:zone",
82 "live",
83 ],
84 },
85 );
86
87 test.provider.skipIf(!entitled)(
88 "pins the retention flag, updates in place, and restores on destroy",
89 (stack) =>
90 Effect.gen(function* () {
91 const zoneId = yield* resolveZoneId;
92
93 yield* stack.destroy();
94 // Known baseline: retention disabled.
95 yield* setBaseline(zoneId, false);
96
97 const created = yield* stack.deploy(
98 Cloudflare.LogsControl.LogsRetentionFlag("Retention", { zoneId, flag: true }),
99 );
100 expect(created.zoneId).toEqual(zoneId);
101 expect(created.flag).toEqual(true);
102 // The pre-management value was captured for restore-on-destroy.
103 expect(created.initialFlag).toEqual(false);
104
105 const live = yield* getFlag(zoneId);
106 expect(live).toEqual(true);
107
108 // In-place update back to false — the captured initial value
109 // survives the update.
110 const updated = yield* stack.deploy(
111 Cloudflare.LogsControl.LogsRetentionFlag("Retention", { zoneId, flag: false }),
112 );
113 expect(updated.flag).toEqual(false);
114 expect(updated.initialFlag).toEqual(false);
115
116 const liveUpdated = yield* getFlag(zoneId);
117 expect(liveUpdated).toEqual(false);
118
119 // Flip it on again so destroy has something to restore.
120 yield* stack.deploy(
121 Cloudflare.LogsControl.LogsRetentionFlag("Retention", { zoneId, flag: true }),
122 );
123
124 yield* stack.destroy();
125
126 // Destroy restored the value the flag had before we managed it.
127 const restored = yield* getFlag(zoneId);
128 expect(restored).toEqual(false);
129 }).pipe(logLevel),
130 {
131 tags: [
132 "provider:cloudflare",
133 "provider:cloudflare:logscontrol",
134 "provider:cloudflare:zone",
135 "live",
136 ],
137 timeout: 120_000,
138 },
139 );
140
141 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
142 // API for this per-zone flag, so `list()` enumerates every zone via
143 // `listAllZones` and reads the singleton in each. Logpull is Enterprise-only,
144 // so on an unentitled account every per-zone read fails with the typed
145 // `LogsControlNotAuthorized` (code 10000) and `list()` skips it — yielding an
146 // array (typically empty) rather than throwing. This ungated case asserts that
147 // the typed-skip path keeps `list()` total; the entitled case below asserts
148 // the standing test zone is actually enumerated.
149 test.provider(
150 "list enumerates the retention flag across all zones",
151 (stack) =>
152 Effect.gen(function* () {
153 const provider = yield* Provider.findProvider(Cloudflare.LogsControl.LogsRetentionFlag);
154 const all = yield* provider.list();
155
156 expect(Array.isArray(all)).toBe(true);
157
158 // `stack` is unused here (the singleton always exists on every zone),
159 // but keep the destroy bookend so the harness state stays clean.
160 yield* stack.destroy();
161 }).pipe(logLevel),
162 { tags: ["provider:cloudflare", "provider:cloudflare:logscontrol", "live"] },
163 );
164
165 test.provider.skipIf(!entitled)(
166 "list contains the entitled test zone's retention flag",
167 (stack) =>
168 Effect.gen(function* () {
169 const zoneId = yield* resolveZoneId;
170
171 const provider = yield* Provider.findProvider(Cloudflare.LogsControl.LogsRetentionFlag);
172 const all = yield* provider.list();
173
174 expect(all.length).toBeGreaterThan(0);
175 expect(all.some((f) => f.zoneId === zoneId)).toBe(true);
176
177 yield* stack.destroy();
178 }).pipe(logLevel),
179 {
180 tags: [
181 "provider:cloudflare",
182 "provider:cloudflare:logscontrol",
183 "provider:cloudflare:zone",
184 "live",
185 ],
186 timeout: 120_000,
187 },
188 );