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

main
ts 173 lines 6.16 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 zones from "@distilled.cloud/cloudflare/zones";
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 // Zone holds are Enterprise-only. On the testing account's zone every
19 // createHold/patchHold fails with Cloudflare code 1005 ("Zone holds are only
20 // available on Enterprise zones (1005)"), surfaced as the typed
21 // `ZoneHoldsRequireEnterprise` error. The full lifecycle test below is gated
22 // behind an Enterprise zone id supplied via env.
23 const enterpriseZoneId = process.env.CLOUDFLARE_TEST_ENTERPRISE_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 across Cloudflare's edge — ride out 403 blips on the test's
36 // own out-of-band calls by retrying the typed `Forbidden` error.
37 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
38
39 const getHold = (zoneId: string) =>
40 zones.getHold({ zoneId }).pipe(
41 Effect.retry({
42 while: (e) => e._tag === "Forbidden",
43 schedule: forbiddenRetrySchedule,
44 times: 8,
45 }),
46 );
47
48 // Normalize to a no-hold baseline so each run starts from the same cloud
49 // state regardless of what a previous (possibly interrupted) run left behind.
50 // deleteHold is naturally idempotent — it succeeds even when no hold exists.
51 const removeHoldBaseline = (zoneId: string) =>
52 zones.deleteHold({ zoneId }).pipe(
53 Effect.retry({
54 while: (e) => e._tag === "Forbidden",
55 schedule: forbiddenRetrySchedule,
56 times: 8,
57 }),
58 );
59
60 const isHeld = (hold: zones.GetHoldResponse): boolean => hold.hold === true;
61
62 const includesSubdomains = (hold: zones.GetHoldResponse): boolean =>
63 hold.includeSubdomains === true || hold.includeSubdomains === "true";
64
65 test.provider(
66 "surfaces the typed ZoneHoldsRequireEnterprise error on non-Enterprise zones",
67 (stack) =>
68 Effect.gen(function* () {
69 const zoneId = yield* resolveZoneId;
70
71 yield* stack.destroy();
72
73 // getHold works on every plan and reports the zone as not held.
74 const observed = yield* getHold(zoneId);
75 expect(isHeld(observed)).toBe(false);
76
77 // deleteHold is idempotent — removing a non-existent hold succeeds.
78 yield* removeHoldBaseline(zoneId);
79
80 // Placing a hold on a non-Enterprise zone fails with the typed
81 // entitlement tag.
82 const error = yield* zones.createHold({ zoneId }).pipe(
83 Effect.retry({
84 while: (e) => e._tag === "Forbidden",
85 schedule: forbiddenRetrySchedule,
86 times: 8,
87 }),
88 Effect.flip,
89 );
90 expect(error._tag).toEqual("ZoneHoldsRequireEnterprise");
91
92 yield* stack.destroy();
93 }).pipe(logLevel),
94 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
95 );
96
97 // Canonical `list()` test (zone-scoped singleton): a hold is a per-zone
98 // singleton — `getHold` returns a record for every zone — and there is no
99 // account-wide enumeration API, so `list()` enumerates every zone via
100 // `listAllZones` and reads the hold state in each. Assert the result is
101 // non-empty and contains the standing test zone. This works on any plan
102 // (reading the hold state needs no Enterprise entitlement).
103 test.provider(
104 "list enumerates the hold state across all zones",
105 (stack) =>
106 Effect.gen(function* () {
107 const zoneId = yield* resolveZoneId;
108
109 yield* stack.destroy();
110
111 const provider = yield* Provider.findProvider(Cloudflare.Zone.Hold);
112 const all = yield* provider.list();
113
114 expect(all.length).toBeGreaterThan(0);
115 expect(all.some((h) => h.zoneId === zoneId)).toBe(true);
116
117 yield* stack.destroy();
118 }).pipe(logLevel),
119 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
120 );
121
122 test.provider.skipIf(!enterpriseZoneId)(
123 "places a hold, updates includeSubdomains in place, and removes it on destroy",
124 (stack) =>
125 Effect.gen(function* () {
126 const zoneId = enterpriseZoneId!;
127
128 yield* stack.destroy();
129 // Known baseline: no hold on the zone.
130 yield* removeHoldBaseline(zoneId);
131
132 const hold = yield* stack.deploy(
133 Effect.gen(function* () {
134 return yield* Cloudflare.Zone.Hold("Hold", {
135 zoneId,
136 });
137 }),
138 );
139
140 expect(hold.zoneId).toEqual(zoneId);
141 expect(hold.hold).toBe(true);
142 expect(hold.includeSubdomains).toBe(false);
143
144 // Out-of-band verification via the distilled API.
145 const live = yield* getHold(zoneId);
146 expect(isHeld(live)).toBe(true);
147 expect(includesSubdomains(live)).toBe(false);
148
149 // Update in place — extend the hold to subdomains via patchHold.
150 const updated = yield* stack.deploy(
151 Effect.gen(function* () {
152 return yield* Cloudflare.Zone.Hold("Hold", {
153 zoneId,
154 includeSubdomains: true,
155 });
156 }),
157 );
158
159 expect(updated.hold).toBe(true);
160 expect(updated.includeSubdomains).toBe(true);
161
162 const liveUpdated = yield* getHold(zoneId);
163 expect(isHeld(liveUpdated)).toBe(true);
164 expect(includesSubdomains(liveUpdated)).toBe(true);
165
166 yield* stack.destroy();
167
168 // Destroy removed the hold.
169 const removed = yield* getHold(zoneId);
170 expect(isHeld(removed)).toBe(false);
171 }).pipe(logLevel),
172 { tags: ["provider:cloudflare", "provider:cloudflare:zone", "live"] },
173 );