@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 183 lines 6.66 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 // Outgoing zone transfers are an Enterprise feature. On the testing
20 // account, `POST /zones/{id}/secondary_dns/outgoing` fails with HTTP
21 // 401 — result "Not authorized to setup outgoing zone transfers" —
22 // surfaced as the typed `OutgoingZoneTransfersNotAllowed` error. The
23 // full lifecycle test below is gated behind an entitled zone supplied
24 // via env.
25 const outgoingEntitled = !!process.env.CLOUDFLARE_TEST_OUTGOING_ZONE_TRANSFER;
26
27 const resolveZoneId = Effect.gen(function* () {
28 const { accountId } = yield* yield* CloudflareEnvironment;
29 const zone = yield* findZoneByName({ accountId, name: zoneName });
30 if (!zone) {
31 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
32 }
33 return zone.id;
34 });
35
36 const retryForbidden = <A, E extends { _tag: string }, R>(eff: Effect.Effect<A, E, R>) =>
37 eff.pipe(
38 Effect.retry({
39 while: (e) => e._tag === "Forbidden",
40 schedule: Schedule.exponential("500 millis"),
41 times: 8,
42 }),
43 );
44
45 test.provider.skipIf(outgoingEntitled)(
46 "surfaces the typed OutgoingZoneTransfersNotAllowed error on unentitled zones",
47 (stack) =>
48 Effect.gen(function* () {
49 const { accountId } = yield* yield* CloudflareEnvironment;
50 const zoneId = yield* resolveZoneId;
51
52 yield* stack.destroy();
53
54 // Need a peer to attempt the link with — peers themselves are
55 // available on this account.
56 const peer = yield* retryForbidden(
57 dns.createZoneTransferPeer({
58 accountId,
59 name: "alchemy-dnszt-outgoing-probe-peer",
60 }),
61 );
62
63 const error = yield* retryForbidden(
64 dns.createZoneTransferOutgoing({
65 zoneId,
66 name: `${zoneName}.`,
67 peers: [peer.id],
68 }),
69 ).pipe(
70 Effect.flip,
71 Effect.ensuring(
72 dns.deleteZoneTransferPeer({ accountId, peerId: peer.id }).pipe(Effect.ignore),
73 ),
74 );
75 expect(error._tag).toEqual("OutgoingZoneTransfersNotAllowed");
76 }).pipe(logLevel),
77 {
78 tags: ["provider:cloudflare", "provider:cloudflare:dns", "provider:cloudflare:zone", "live"],
79 timeout: 120_000,
80 },
81 );
82
83 test.provider.skipIf(!outgoingEntitled)(
84 "create, sync peers and enabled state, and delete the outgoing config",
85 (stack) =>
86 Effect.gen(function* () {
87 const zoneId = yield* resolveZoneId;
88
89 yield* stack.destroy();
90
91 const { outgoing, peer } = yield* stack.deploy(
92 Effect.gen(function* () {
93 const peer = yield* Cloudflare.DNS.ZoneTransferPeer("OutgoingPeer", {
94 name: testName("alchemy-dnszt-outgoing-peer"),
95 ip: "192.0.2.53",
96 port: 53,
97 });
98 const outgoing = yield* Cloudflare.DNS.ZoneTransferOutgoing("Outgoing", {
99 zoneId,
100 name: `${zoneName}.`,
101 peers: [peer.peerId],
102 });
103 return { outgoing, peer };
104 }),
105 );
106 expect(outgoing.zoneId).toEqual(zoneId);
107 expect(outgoing.peers).toEqual([peer.peerId]);
108 expect(outgoing.enabled).toEqual(true);
109
110 // Disable in place — toggled via the dedicated endpoint. Keep the
111 // peer deployed across the update (engine constraint: never drop a
112 // dependency in the same deploy that changes its dependent).
113 const disabled = yield* stack.deploy(
114 Effect.gen(function* () {
115 const peer = yield* Cloudflare.DNS.ZoneTransferPeer("OutgoingPeer", {
116 name: testName("alchemy-dnszt-outgoing-peer"),
117 ip: "192.0.2.53",
118 port: 53,
119 });
120 const outgoing = yield* Cloudflare.DNS.ZoneTransferOutgoing("Outgoing", {
121 zoneId,
122 name: `${zoneName}.`,
123 peers: [peer.peerId],
124 enabled: false,
125 });
126 return outgoing;
127 }),
128 );
129 expect(disabled.enabled).toEqual(false);
130
131 yield* stack.destroy();
132
133 // Once deleted, the GET reports the typed not-found tag.
134 const gone = yield* retryForbidden(dns.getZoneTransferOutgoing({ zoneId })).pipe(Effect.flip);
135 expect(gone._tag).toEqual("OutgoingZoneTransferNotFound");
136
137 // Re-running destroy is idempotent.
138 yield* stack.destroy();
139 }).pipe(logLevel),
140 {
141 tags: ["provider:cloudflare", "provider:cloudflare:dns", "provider:cloudflare:zone", "live"],
142 timeout: 300_000,
143 },
144 );
145
146 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
147 // API for the outgoing transfer config, so `list()` enumerates every zone
148 // via `listAllZones` and reads the singleton in each, skipping zones that
149 // have no config or lack the Secondary DNS entitlement (typed
150 // OutgoingZoneTransferNotFound / OutgoingZoneTransfersNotAllowed tags). The
151 // read-only assertion (well-typed Attributes[]) always runs; on the
152 // unentitled testing account no zone returns a config, so the deployed-
153 // presence assertion is gated behind an entitled zone.
154 test.provider(
155 "list enumerates the outgoing transfer config per zone",
156 (stack) =>
157 Effect.gen(function* () {
158 const zoneId = yield* resolveZoneId;
159
160 const provider = yield* Provider.findProvider(Cloudflare.DNS.ZoneTransferOutgoing);
161 const all = yield* provider.list();
162
163 // Always-on assertion: list() returns a well-typed Attributes[] and
164 // never throws even when every zone is unconfigured/unentitled.
165 expect(Array.isArray(all)).toBe(true);
166 for (const item of all) {
167 expect(typeof item.zoneId).toBe("string");
168 expect(Array.isArray(item.peers)).toBe(true);
169 expect(typeof item.enabled).toBe("boolean");
170 }
171
172 // Only an entitled+configured zone appears in the enumeration.
173 if (outgoingEntitled) {
174 expect(all.some((o) => o.zoneId === zoneId)).toBe(true);
175 }
176
177 yield* stack.destroy();
178 }).pipe(logLevel),
179 {
180 tags: ["provider:cloudflare", "provider:cloudflare:dns", "provider:cloudflare:zone", "live"],
181 timeout: 120_000,
182 },
183 );