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

main
ts 218 lines 8.42 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 dns from "@distilled.cloud/cloudflare/dns";
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 const resolveZoneId = Effect.gen(function* () {
18 const { accountId } = yield* yield* CloudflareEnvironment;
19 const zone = yield* findZoneByName({ accountId, name: zoneName });
20 if (!zone) {
21 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
22 }
23 return zone.id;
24 });
25
26 // The scoped API token the test harness mints propagates eventually-
27 // consistently across Cloudflare's edge — a fresh token intermittently 403s.
28 // Ride out the blips on the test's own out-of-band calls by retrying the
29 // typed `Forbidden` error (part of each op's error union via distilled
30 // patches).
31 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
32
33 const getDnssec = (zoneId: string) =>
34 dns.getDnssec({ zoneId }).pipe(
35 Effect.retry({
36 while: (e) => e._tag === "Forbidden",
37 schedule: forbiddenRetrySchedule,
38 times: 8,
39 }),
40 );
41
42 /** Collapse the live five-state status into its desired-state family. */
43 const family = (status: string | null | undefined): "active" | "disabled" =>
44 status === "active" || status === "pending" ? "active" : "disabled";
45
46 // Normalize the zone to a known baseline (DNSSEC off) so each run starts
47 // from the same cloud state regardless of what a previous (possibly
48 // interrupted) run left behind. Only deactivates when needed so re-runs
49 // don't trip "already disabled" API validation.
50 const normalizeDisabled = (zoneId: string) =>
51 Effect.gen(function* () {
52 const observed = yield* getDnssec(zoneId);
53 if (family(observed.status) === "disabled") return;
54 // DELETE /dnssec only clears records after signing is already off —
55 // deactivation goes through PATCH status:"disabled".
56 yield* dns.patchDnssec({ zoneId, status: "disabled" }).pipe(
57 Effect.retry({
58 while: (e) => e._tag === "Forbidden",
59 schedule: forbiddenRetrySchedule,
60 times: 8,
61 }),
62 );
63 // Deactivation is eventually consistent — wait (bounded) for the zone
64 // to leave the active family before deploying on top of it.
65 yield* getDnssec(zoneId).pipe(
66 Effect.repeat({
67 schedule: Schedule.spaced("2 seconds"),
68 until: (o) => family(o.status) === "disabled",
69 times: 15,
70 }),
71 );
72 });
73
74 // Both cases mutate the same zone-level DNSSEC singleton with opposite desired states; run them serially so they don't corrupt each other's captured baseline under the global concurrent test config.
75 describe.sequential(
76 "Dnssec",
77 {
78 tags: ["provider:cloudflare", "provider:cloudflare:dns", "provider:cloudflare:zone", "live"],
79 },
80 () => {
81 test.provider(
82 "enables DNSSEC, captures the disabled baseline, destroy deactivates",
83 (stack) =>
84 Effect.gen(function* () {
85 const zoneId = yield* resolveZoneId;
86
87 yield* stack.destroy();
88 yield* normalizeDisabled(zoneId);
89
90 const dnssec = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* Cloudflare.DNS.Dnssec("ZoneDnssec", { zoneId });
93 }),
94 );
95
96 expect(dnssec.zoneId).toEqual(zoneId);
97 // No DS record at the registrar, so the zone sits at `pending` —
98 // that's the active family as far as Cloudflare's side goes.
99 expect(family(dnssec.status)).toEqual("active");
100 // The pre-management state was captured for restore-on-destroy.
101 expect(dnssec.initialStatus).toEqual("disabled");
102 // The DS record string is what users paste at their registrar.
103 expect(dnssec.ds).toBeTypeOf("string");
104 expect(dnssec.ds!.length).toBeGreaterThan(0);
105
106 // Out-of-band verify via the SDK.
107 const live = yield* getDnssec(zoneId);
108 expect(family(live.status)).toEqual("active");
109
110 yield* stack.destroy();
111
112 // The zone was disabled before we managed it, so destroy deactivates.
113 // Deactivation is eventually consistent — typed bounded wait.
114 const restored = yield* getDnssec(zoneId).pipe(
115 Effect.repeat({
116 schedule: Schedule.spaced("2 seconds"),
117 until: (o) => family(o.status) === "disabled",
118 times: 15,
119 }),
120 );
121 expect(family(restored.status)).toEqual("disabled");
122
123 // Re-running destroy is idempotent (DNSSEC already off).
124 yield* stack.destroy();
125 const still = yield* getDnssec(zoneId);
126 expect(family(still.status)).toEqual("disabled");
127 }).pipe(logLevel),
128 { timeout: 300_000 },
129 );
130
131 test.provider(
132 "updates status in place — same zone singleton, no replacement",
133 (stack) =>
134 Effect.gen(function* () {
135 const zoneId = yield* resolveZoneId;
136
137 yield* stack.destroy();
138 yield* normalizeDisabled(zoneId);
139
140 const enabled = yield* stack.deploy(
141 Effect.gen(function* () {
142 return yield* Cloudflare.DNS.Dnssec("ZoneDnssec", {
143 zoneId,
144 status: "active",
145 });
146 }),
147 );
148 expect(family(enabled.status)).toEqual("active");
149 expect(enabled.initialStatus).toEqual("disabled");
150
151 // Flip the desired status in place — `zoneId` (the identity) is
152 // unchanged, so this patches the same singleton rather than
153 // replacing it.
154 const disabled = yield* stack.deploy(
155 Effect.gen(function* () {
156 return yield* Cloudflare.DNS.Dnssec("ZoneDnssec", {
157 zoneId,
158 status: "disabled",
159 });
160 }),
161 );
162 expect(disabled.zoneId).toEqual(zoneId);
163 expect(family(disabled.status)).toEqual("disabled");
164 // The captured baseline survives the in-place update.
165 expect(disabled.initialStatus).toEqual("disabled");
166
167 const live = yield* getDnssec(zoneId);
168 expect(family(live.status)).toEqual("disabled");
169
170 // Destroy is a no-op here (baseline disabled, currently disabled).
171 yield* stack.destroy();
172 const after = yield* getDnssec(zoneId);
173 expect(family(after.status)).toEqual("disabled");
174 }).pipe(logLevel),
175 { timeout: 300_000 },
176 );
177
178 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
179 // API for per-zone DNSSEC, so `list()` enumerates every zone via
180 // `listAllZones` and reads each one's config, skipping disabled zones
181 // (which `read` treats as "not created"). Enable DNSSEC on the test zone
182 // so it appears in the result, assert it's present, then restore.
183 test.provider(
184 "list enumerates active DNSSEC across zones",
185 (stack) =>
186 Effect.gen(function* () {
187 const zoneId = yield* resolveZoneId;
188
189 yield* stack.destroy();
190 yield* normalizeDisabled(zoneId);
191
192 yield* stack.deploy(
193 Effect.gen(function* () {
194 return yield* Cloudflare.DNS.Dnssec("ZoneDnssec", { zoneId });
195 }),
196 );
197
198 const provider = yield* Provider.findProvider(Cloudflare.DNS.Dnssec);
199 const all = yield* provider.list();
200
201 expect(all.length).toBeGreaterThan(0);
202 expect(all.some((d) => d.zoneId === zoneId)).toBe(true);
203
204 yield* stack.destroy();
205 // Restore the disabled baseline (eventually consistent).
206 const restored = yield* getDnssec(zoneId).pipe(
207 Effect.repeat({
208 schedule: Schedule.spaced("2 seconds"),
209 until: (o) => family(o.status) === "disabled",
210 times: 15,
211 }),
212 );
213 expect(family(restored.status)).toEqual("disabled");
214 }).pipe(logLevel),
215 { timeout: 300_000 },
216 );
217 },
218 );