@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 237 lines 9.04 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as dnsFirewall from "@distilled.cloud/cloudflare/dns-firewall";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import { testName } from "../../names.ts";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // DNS Firewall is a paid add-on (Enterprise / contract). On the standard
17 // testing account `POST /accounts/{id}/dns_firewall` fails with Cloudflare
18 // error code 10101 — "You do not have access to this feature." — surfaced
19 // as the typed `DnsFirewallNotEntitled` error. The full lifecycle tests
20 // below are gated behind an entitled account supplied via env.
21 const entitled = !!process.env.CLOUDFLARE_TEST_DNS_FIREWALL;
22
23 // The scoped API token the test harness mints propagates eventually-
24 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
25 // declared in the distilled error union) on out-of-band verification calls.
26 const getCluster = (accountId: string, dnsFirewallId: string) =>
27 dnsFirewall.getDnsFirewall({ accountId, dnsFirewallId }).pipe(
28 Effect.retry({
29 while: (e) => e._tag === "Forbidden",
30 schedule: Schedule.exponential("500 millis"),
31 times: 8,
32 }),
33 );
34
35 // Poll until the cluster is gone — a missing cluster surfaces as the typed
36 // `DnsFirewallNotFound` (Cloudflare error code 11001).
37 const expectGone = (accountId: string, dnsFirewallId: string) =>
38 getCluster(accountId, dnsFirewallId).pipe(
39 Effect.flatMap(() => Effect.fail({ _tag: "ClusterNotDeleted" } as const)),
40 Effect.catchTag("DnsFirewallNotFound", () => Effect.void),
41 Effect.retry({
42 while: (e) => e._tag === "ClusterNotDeleted",
43 schedule: Schedule.exponential("500 millis"),
44 times: 10,
45 }),
46 );
47
48 test.provider.skipIf(entitled)(
49 "surfaces the typed DnsFirewallNotEntitled error on unentitled accounts",
50 (stack) =>
51 Effect.gen(function* () {
52 const { accountId } = yield* yield* CloudflareEnvironment;
53
54 yield* stack.destroy();
55
56 // The testing account has no DNS Firewall entitlement — the distilled
57 // create call must fail with the typed entitlement tag (Cloudflare
58 // error code 10101). Reads on the same account succeed (and show no
59 // clusters), proving the gate is on creation, not on the API token.
60 const error = yield* dnsFirewall
61 .createDnsFirewall({
62 accountId,
63 name: "alchemy-dnsfw-entitlement-probe",
64 upstreamIps: ["192.0.2.1"],
65 })
66 .pipe(Effect.flip);
67 expect(error._tag).toEqual("DnsFirewallNotEntitled");
68
69 const list = yield* dnsFirewall.listDnsFirewalls({ accountId }).pipe(
70 Effect.retry({
71 while: (e) => e._tag === "Forbidden",
72 schedule: Schedule.exponential("500 millis"),
73 times: 8,
74 }),
75 );
76 expect(list.result).toEqual([]);
77
78 yield* stack.destroy();
79 }).pipe(logLevel),
80 { tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"] },
81 );
82
83 test.provider.skipIf(!entitled)(
84 "create, verify out-of-band, destroy, and wait until gone",
85 (stack) =>
86 Effect.gen(function* () {
87 const { accountId } = yield* yield* CloudflareEnvironment;
88
89 yield* stack.destroy();
90
91 const cluster = yield* stack.deploy(
92 Cloudflare.DNS.Firewall("TestCluster", {
93 name: testName("alchemy-dnsfw-lifecycle"),
94 upstreamIps: ["192.0.2.1"],
95 }),
96 );
97
98 expect(cluster.dnsFirewallId).toBeTruthy();
99 expect(cluster.accountId).toEqual(accountId);
100 expect(cluster.name).toEqual(testName("alchemy-dnsfw-lifecycle"));
101 expect(cluster.upstreamIps).toEqual(["192.0.2.1"]);
102 // The key output — the assigned anycast IPs NS glue points at.
103 expect(cluster.dnsFirewallIps.length).toBeGreaterThan(0);
104 // Documented defaults.
105 expect(cluster.deprecateAnyRequests).toEqual(false);
106 expect(cluster.ecsFallback).toEqual(false);
107 expect(cluster.retries).toEqual(2);
108
109 const live = yield* getCluster(accountId, cluster.dnsFirewallId);
110 expect(live.name).toEqual(testName("alchemy-dnsfw-lifecycle"));
111 expect([...live.upstreamIps]).toEqual(["192.0.2.1"]);
112
113 yield* stack.destroy();
114
115 yield* expectGone(accountId, cluster.dnsFirewallId);
116 }).pipe(logLevel),
117 { tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"], timeout: 120_000 },
118 );
119
120 test.provider.skipIf(!entitled)(
121 "update mutable settings in place, replace on rename",
122 (stack) =>
123 Effect.gen(function* () {
124 const { accountId } = yield* yield* CloudflareEnvironment;
125
126 yield* stack.destroy();
127
128 const initial = yield* stack.deploy(
129 Cloudflare.DNS.Firewall("UpdateCluster", {
130 name: testName("alchemy-dnsfw-update"),
131 upstreamIps: ["192.0.2.1"],
132 }),
133 );
134
135 // In-place update: settings + an extra upstream IP, same id.
136 const updated = yield* stack.deploy(
137 Cloudflare.DNS.Firewall("UpdateCluster", {
138 name: testName("alchemy-dnsfw-update"),
139 upstreamIps: ["192.0.2.1", "192.0.2.2"],
140 ratelimit: 600,
141 minimumCacheTtl: 120,
142 attackMitigation: { enabled: true },
143 }),
144 );
145 expect(updated.dnsFirewallId).toEqual(initial.dnsFirewallId);
146 expect([...updated.upstreamIps].sort()).toEqual(["192.0.2.1", "192.0.2.2"]);
147 expect(updated.ratelimit).toEqual(600);
148 expect(updated.minimumCacheTtl).toEqual(120);
149 expect(updated.attackMitigation.enabled).toEqual(true);
150
151 const live = yield* getCluster(accountId, updated.dnsFirewallId);
152 expect(live.ratelimit).toEqual(600);
153 expect(live.minimumCacheTtl).toEqual(120);
154
155 // No-op redeploy keeps the same cluster.
156 const noop = yield* stack.deploy(
157 Cloudflare.DNS.Firewall("UpdateCluster", {
158 name: testName("alchemy-dnsfw-update"),
159 upstreamIps: ["192.0.2.1", "192.0.2.2"],
160 ratelimit: 600,
161 minimumCacheTtl: 120,
162 attackMitigation: { enabled: true },
163 }),
164 );
165 expect(noop.dnsFirewallId).toEqual(initial.dnsFirewallId);
166
167 // Rename — the name is the cold-state recovery identity, so a new
168 // cluster replaces the old one.
169 const renamed = yield* stack.deploy(
170 Cloudflare.DNS.Firewall("UpdateCluster", {
171 name: testName("alchemy-dnsfw-update-v2"),
172 upstreamIps: ["192.0.2.1", "192.0.2.2"],
173 ratelimit: 600,
174 minimumCacheTtl: 120,
175 attackMitigation: { enabled: true },
176 }),
177 );
178 expect(renamed.dnsFirewallId).not.toEqual(initial.dnsFirewallId);
179 expect(renamed.name).toEqual(testName("alchemy-dnsfw-update-v2"));
180
181 yield* expectGone(accountId, initial.dnsFirewallId);
182
183 yield* stack.destroy();
184
185 yield* expectGone(accountId, renamed.dnsFirewallId);
186 }).pipe(logLevel),
187 { tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"], timeout: 120_000 },
188 );
189
190 // Canonical `list()` test (account collection): the enumeration endpoint
191 // (`GET /accounts/{id}/dns_firewall`) is NOT entitlement-gated — only
192 // cluster *creation* is — so `list()` runs on the unentitled testing
193 // account and returns the (empty) cluster Attributes array. This read-only
194 // assertion proves `list()` exhaustively paginates and produces the exact
195 // `read` Attributes shape.
196 test.provider(
197 "list returns the DNS firewall cluster Attributes array",
198 (stack) =>
199 Effect.gen(function* () {
200 yield* stack.destroy();
201
202 const provider = yield* Provider.findProvider(Cloudflare.DNS.Firewall);
203 const all = yield* provider.list();
204 expect(Array.isArray(all)).toBe(true);
205
206 yield* stack.destroy();
207 }).pipe(logLevel),
208 { tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"] },
209 );
210
211 // Entitled accounts: deploy a real cluster and assert it appears in the
212 // exhaustively-paginated `list()` result with the exact `read` Attributes.
213 test.provider.skipIf(!entitled)(
214 "list enumerates the deployed DNS firewall cluster",
215 (stack) =>
216 Effect.gen(function* () {
217 yield* stack.destroy();
218
219 const deployed = yield* stack.deploy(
220 Cloudflare.DNS.Firewall("ListCluster", {
221 name: testName("alchemy-dnsfw-list"),
222 upstreamIps: ["192.0.2.1"],
223 }),
224 );
225
226 const provider = yield* Provider.findProvider(Cloudflare.DNS.Firewall);
227 const all = yield* provider.list();
228
229 const found = all.find((c) => c.dnsFirewallId === deployed.dnsFirewallId);
230 expect(found).toBeTruthy();
231 expect(found?.name).toEqual(testName("alchemy-dnsfw-list"));
232 expect(found?.accountId).toEqual(deployed.accountId);
233
234 yield* stack.destroy();
235 }).pipe(logLevel),
236 { tags: ["provider:cloudflare", "provider:cloudflare:dns", "live"], timeout: 120_000 },
237 );