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

main
ts 160 lines 5.97 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 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 import { testName } from "../../names.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
18
19 // Incoming zone transfers require a SECONDARY zone, and secondary zone
20 // signup is not allowed on the testing account (`POST /zones` with
21 // `type: "secondary"` fails with "Secondary zone signup not allowed").
22 // On a regular (full) zone the incoming POST superficially succeeds but
23 // the configuration does not persist — the follow-up GET reports the
24 // typed `IncomingZoneTransferNotFound`. The full lifecycle test below
25 // is gated behind a real secondary zone supplied via env.
26 const secondaryZoneId = process.env.CLOUDFLARE_TEST_SECONDARY_ZONE_ID;
27
28 const resolveZoneId = Effect.gen(function* () {
29 const { accountId } = yield* yield* CloudflareEnvironment;
30 const zone = yield* findZoneByName({ accountId, name: zoneName });
31 if (!zone) {
32 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
33 }
34 return zone.id;
35 });
36
37 const retryForbidden = <A, E extends { _tag: string }, R>(eff: Effect.Effect<A, E, R>) =>
38 eff.pipe(
39 Effect.retry({
40 while: (e) => e._tag === "Forbidden",
41 schedule: Schedule.exponential("500 millis"),
42 times: 8,
43 }),
44 );
45
46 // Canonical `list()` test (zone-scoped singleton): there is no
47 // account-wide API for this per-zone config, so `list()` enumerates every
48 // zone via `listAllZones` and reads the incoming transfer config in each,
49 // skipping zones that have none (`IncomingZoneTransferNotFound`). The
50 // read-only assertion always runs and proves the result is a well-typed
51 // `Attributes[]`; if a real secondary zone is supplied, assert it appears.
52 test.provider(
53 "list enumerates incoming configs across all zones",
54 (stack) =>
55 Effect.gen(function* () {
56 const provider = yield* Provider.findProvider(Cloudflare.DNS.ZoneTransferIncoming);
57 const all = yield* provider.list();
58
59 expect(Array.isArray(all)).toBe(true);
60 for (const row of all) {
61 expect(typeof row.zoneId).toBe("string");
62 expect(Array.isArray(row.peers)).toBe(true);
63 }
64 if (secondaryZoneId) {
65 expect(all.some((c) => c.zoneId === secondaryZoneId)).toBe(true);
66 }
67
68 // `stack` is unused (list is a read-only account enumeration); keep the
69 // destroy bookend so the harness state stays clean.
70 yield* stack.destroy();
71 }).pipe(logLevel),
72 {
73 tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"],
74 timeout: 120_000,
75 },
76 );
77
78 test.provider.skipIf(!!secondaryZoneId)(
79 "incoming config does not persist on non-secondary zones (typed not-found)",
80 (stack) =>
81 Effect.gen(function* () {
82 const zoneId = yield* resolveZoneId;
83
84 yield* stack.destroy();
85
86 // The GET on a zone that was never linked reports the typed tag.
87 const error = yield* retryForbidden(dns.getZoneTransferIncoming({ zoneId })).pipe(
88 Effect.flip,
89 );
90 expect(error._tag).toEqual("IncomingZoneTransferNotFound");
91 }).pipe(logLevel),
92 {
93 tags: ["provider:cloudflare", "provider:cloudflare:dns", "provider:cloudflare:zone", "live"],
94 timeout: 120_000,
95 },
96 );
97
98 test.provider.skipIf(!secondaryZoneId)(
99 "create, update autoRefreshSeconds in place, and delete the incoming config",
100 (stack) =>
101 Effect.gen(function* () {
102 const zoneId = secondaryZoneId!;
103
104 yield* stack.destroy();
105
106 const { incoming, peer } = yield* stack.deploy(
107 Effect.gen(function* () {
108 const peer = yield* Cloudflare.DNS.ZoneTransferPeer("IncomingPeer", {
109 name: testName("alchemy-dnszt-incoming-peer"),
110 ip: "192.0.2.53",
111 port: 53,
112 });
113 const incoming = yield* Cloudflare.DNS.ZoneTransferIncoming("Incoming", {
114 zoneId,
115 name: `${zoneName}.`,
116 peers: [peer.peerId],
117 autoRefreshSeconds: 86400,
118 });
119 return { incoming, peer };
120 }),
121 );
122 expect(incoming.zoneId).toEqual(zoneId);
123 expect(incoming.peers).toEqual([peer.peerId]);
124 expect(incoming.autoRefreshSeconds).toEqual(86400);
125
126 // Update the refresh interval in place. Keep the peer deployed
127 // across the update (engine constraint: never drop a dependency
128 // in the same deploy that changes its dependent).
129 const updated = yield* stack.deploy(
130 Effect.gen(function* () {
131 const peer = yield* Cloudflare.DNS.ZoneTransferPeer("IncomingPeer", {
132 name: testName("alchemy-dnszt-incoming-peer"),
133 ip: "192.0.2.53",
134 port: 53,
135 });
136 const incoming = yield* Cloudflare.DNS.ZoneTransferIncoming("Incoming", {
137 zoneId,
138 name: `${zoneName}.`,
139 peers: [peer.peerId],
140 autoRefreshSeconds: 43200,
141 });
142 return incoming;
143 }),
144 );
145 expect(updated.autoRefreshSeconds).toEqual(43200);
146
147 yield* stack.destroy();
148
149 // Once deleted, the GET reports the typed not-found tag.
150 const gone = yield* retryForbidden(dns.getZoneTransferIncoming({ zoneId })).pipe(Effect.flip);
151 expect(gone._tag).toEqual("IncomingZoneTransferNotFound");
152
153 // Re-running destroy is idempotent.
154 yield* stack.destroy();
155 }).pipe(logLevel),
156 {
157 tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"],
158 timeout: 300_000,
159 },
160 );