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

main
ts 167 lines 6.28 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 apiGateway from "@distilled.cloud/cloudflare/api-gateway";
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 // The API Shield configuration (session identifiers) requires an API Shield
19 // entitlement (Enterprise). On the standard testing zone every call fails
20 // with the typed `NotEntitled` error (Cloudflare code 10403):
21 // "You are not entitled for this service"
22 // The full lifecycle test is gated behind an entitled zone id from env.
23 const entitledZoneId = process.env.CLOUDFLARE_TEST_API_SHIELD_ZONE_ID;
24
25 const resolveZoneId = Effect.gen(function* () {
26 const { accountId } = yield* yield* CloudflareEnvironment;
27 const zone = yield* findZoneByName({ accountId, name: zoneName });
28 if (!zone) {
29 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
30 }
31 return zone.id;
32 });
33
34 // The scoped API token the test harness mints propagates eventually-
35 // consistently — a fresh token intermittently 403s. Ride out the blips on
36 // the test's own out-of-band calls by retrying the typed `Forbidden` error.
37 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
38
39 const getConfiguration = (zoneId: string) =>
40 apiGateway.getConfiguration({ zoneId }).pipe(
41 Effect.retry({
42 while: (e) => e._tag === "Forbidden",
43 schedule: forbiddenRetrySchedule,
44 times: 8,
45 }),
46 );
47
48 const setBaseline = (
49 zoneId: string,
50 authIdCharacteristics: { name: string; type: "header" | "cookie" }[],
51 ) =>
52 apiGateway.putConfiguration({ zoneId, authIdCharacteristics }).pipe(
53 Effect.retry({
54 while: (e) => e._tag === "Forbidden",
55 schedule: forbiddenRetrySchedule,
56 times: 8,
57 }),
58 );
59
60 test.provider(
61 "surfaces the typed NotEntitled error on unentitled zones",
62 (stack) =>
63 Effect.gen(function* () {
64 const zoneId = yield* resolveZoneId;
65
66 yield* stack.destroy();
67
68 // The standard testing zone lacks the API Shield entitlement — the
69 // distilled call must fail with the typed entitlement tag.
70 const error = yield* apiGateway.getConfiguration({ zoneId }).pipe(
71 Effect.retry({
72 while: (e) => e._tag === "Forbidden",
73 schedule: forbiddenRetrySchedule,
74 times: 8,
75 }),
76 Effect.flip,
77 );
78 expect(error._tag).toEqual("NotEntitled");
79
80 yield* stack.destroy();
81 }).pipe(logLevel),
82 {
83 tags: [
84 "provider:cloudflare",
85 "provider:cloudflare:apishield",
86 "provider:cloudflare:zone",
87 "live",
88 ],
89 },
90 );
91
92 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
93 // API for this per-zone configuration, so `list()` enumerates every zone via
94 // `listAllZones` and reads the singleton in each, skipping zones that reject
95 // with the typed `NotEntitled` / `InvalidObjectIdentifier` / `Forbidden`
96 // tags. On the unentitled testing account every zone is skipped, so the
97 // result is an empty array — the assertion is that `list()` resolves to an
98 // array (proving the typed skip path) rather than throwing. Presence of the
99 // standing test zone is asserted only on an entitled account (env-gated).
100 test.provider(
101 "list enumerates the configuration across all zones",
102 (stack) =>
103 Effect.gen(function* () {
104 const provider = yield* Provider.findProvider(Cloudflare.ApiShield.Configuration);
105 const all = yield* provider.list();
106
107 expect(Array.isArray(all)).toBe(true);
108
109 if (entitledZoneId) {
110 expect(all.some((c) => c.zoneId === entitledZoneId)).toBe(true);
111 }
112
113 // `stack` is unused (the singleton always exists on every entitled zone),
114 // but keep the destroy bookends so the harness state stays clean.
115 yield* stack.destroy();
116 }).pipe(logLevel),
117 { tags: ["provider:cloudflare", "provider:cloudflare:apishield", "live"] },
118 );
119
120 test.provider.skipIf(!entitledZoneId)(
121 "sets session identifiers and restores the original value on destroy",
122 (stack) =>
123 Effect.gen(function* () {
124 const zoneId = entitledZoneId!;
125
126 yield* stack.destroy();
127 // Known baseline: no session identifiers configured.
128 yield* setBaseline(zoneId, []);
129
130 const config = yield* stack.deploy(
131 Effect.gen(function* () {
132 return yield* Cloudflare.ApiShield.Configuration("SessionIds", {
133 zoneId,
134 authIdCharacteristics: [{ name: "authorization", type: "header" }],
135 });
136 }),
137 );
138
139 expect(config.zoneId).toEqual(zoneId);
140 expect(config.authIdCharacteristics).toEqual([{ name: "authorization", type: "header" }]);
141 // The pre-management value was captured for restore-on-destroy.
142 expect(config.initialAuthIdCharacteristics).toEqual([]);
143
144 // Out-of-band verification via the distilled API.
145 const live = yield* getConfiguration(zoneId);
146 expect(live.authIdCharacteristics).toEqual([{ name: "authorization", type: "header" }]);
147
148 // Update in place — same singleton, the captured baseline survives.
149 const updated = yield* stack.deploy(
150 Effect.gen(function* () {
151 return yield* Cloudflare.ApiShield.Configuration("SessionIds", {
152 zoneId,
153 authIdCharacteristics: [{ name: "session_id", type: "cookie" }],
154 });
155 }),
156 );
157 expect(updated.authIdCharacteristics).toEqual([{ name: "session_id", type: "cookie" }]);
158 expect(updated.initialAuthIdCharacteristics).toEqual([]);
159
160 // Destroy restores the captured baseline.
161 yield* stack.destroy();
162
163 const restored = yield* getConfiguration(zoneId);
164 expect(restored.authIdCharacteristics).toEqual([]);
165 }).pipe(logLevel),
166 { tags: ["provider:cloudflare", "provider:cloudflare:apishield", "live"], timeout: 120_000 },
167 );