@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 189 lines 7.33 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 acm from "@distilled.cloud/cloudflare/acm";
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 // Total TLS writes require the Advanced Certificate Manager add-on
18 // (~$10/mo per zone). The standard testing zone does not have it — every
19 // POST fails with the typed `AdvancedCertificateManagerRequired` (code
20 // 1450) error, while reads succeed and report `enabled: false`. The full
21 // enable/update/restore lifecycle is gated behind an entitled zone id
22 // supplied via env.
23 const entitledZoneId = process.env.CLOUDFLARE_TEST_ACM_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 // Freshly-minted scoped API tokens propagate eventually-consistently across
35 // Cloudflare's edge — ride out intermittent 403s via the typed `Forbidden`
36 // tag in each acm operation's error union.
37 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
38
39 const getTotalTls = (zoneId: string) =>
40 acm.getTotalTl({ zoneId }).pipe(
41 Effect.retry({
42 while: (e) => e._tag === "Forbidden",
43 schedule: forbiddenRetrySchedule,
44 times: 8,
45 }),
46 );
47
48 // Both cases mutate the same zone-level Total TLS singleton; run them serially so they don't corrupt each other's captured `initialEnabled` under the global concurrent test config.
49 describe.sequential(
50 "TotalTls",
51 { tags: ["provider:cloudflare", "provider:cloudflare:acm", "live"] },
52 () => {
53 test.provider(
54 "converges enabled:false as a no-op on a zone without the ACM entitlement",
55 (stack) =>
56 Effect.gen(function* () {
57 const zoneId = yield* resolveZoneId;
58
59 yield* stack.destroy();
60
61 // Total TLS defaults to disabled. Desiring `enabled: false` matches
62 // the observed state, so reconcile never POSTs — the deploy succeeds
63 // even though the zone lacks the ACM entitlement.
64 const setting = yield* stack.deploy(
65 Effect.gen(function* () {
66 return yield* Cloudflare.Acm.TotalTls("TotalTls", {
67 zoneId,
68 enabled: false,
69 });
70 }),
71 );
72
73 expect(setting.zoneId).toEqual(zoneId);
74 expect(setting.enabled).toEqual(false);
75 // The pre-management state was captured for restore-on-destroy.
76 expect(setting.initialEnabled).toEqual(false);
77
78 // Out-of-band verification via the distilled API.
79 const live = yield* getTotalTls(zoneId);
80 expect(live.enabled ?? false).toEqual(false);
81
82 // Destroy restores the (identical) baseline — also a no-op POST.
83 yield* stack.destroy();
84 const after = yield* getTotalTls(zoneId);
85 expect(after.enabled ?? false).toEqual(false);
86 }).pipe(logLevel),
87 { tags: ["provider:cloudflare:zone"] },
88 );
89
90 test.provider(
91 "surfaces the typed AdvancedCertificateManagerRequired error when enabling without the entitlement",
92 (stack) =>
93 Effect.gen(function* () {
94 const zoneId = yield* resolveZoneId;
95
96 yield* stack.destroy();
97
98 // Enabling Total TLS requires the ACM add-on; the distilled call
99 // must fail with the typed entitlement tag (code 1450).
100 const error = yield* acm.updateTotalTl({ zoneId, enabled: true }).pipe(
101 Effect.retry({
102 while: (e) => e._tag === "Forbidden",
103 schedule: forbiddenRetrySchedule,
104 times: 8,
105 }),
106 Effect.flip,
107 );
108 expect(error._tag).toEqual("AdvancedCertificateManagerRequired");
109
110 yield* stack.destroy();
111 }).pipe(logLevel),
112 { tags: ["provider:cloudflare:zone"] },
113 );
114
115 test.provider.skipIf(!entitledZoneId)(
116 "enables Total TLS, updates the CA in place, and restores the baseline on destroy",
117 (stack) =>
118 Effect.gen(function* () {
119 const zoneId = entitledZoneId!;
120
121 yield* stack.destroy();
122 // Known baseline: disabled.
123 const baseline = yield* getTotalTls(zoneId);
124
125 const setting = yield* stack.deploy(
126 Effect.gen(function* () {
127 return yield* Cloudflare.Acm.TotalTls("TotalTls", {
128 zoneId,
129 enabled: true,
130 });
131 }),
132 );
133
134 expect(setting.zoneId).toEqual(zoneId);
135 expect(setting.enabled).toEqual(true);
136 expect(setting.initialEnabled).toEqual(baseline.enabled ?? false);
137
138 // Out-of-band verification.
139 const live = yield* getTotalTls(zoneId);
140 expect(live.enabled).toEqual(true);
141
142 // Update in place — pin the issuing Certificate Authority. The
143 // singleton's identity (zoneId) is unchanged.
144 const updated = yield* stack.deploy(
145 Effect.gen(function* () {
146 return yield* Cloudflare.Acm.TotalTls("TotalTls", {
147 zoneId,
148 enabled: true,
149 certificateAuthority: "lets_encrypt",
150 });
151 }),
152 );
153 expect(updated.enabled).toEqual(true);
154 expect(updated.certificateAuthority).toEqual("lets_encrypt");
155 expect(updated.initialEnabled).toEqual(baseline.enabled ?? false);
156
157 // Destroy restores the pre-management state.
158 yield* stack.destroy();
159 const after = yield* getTotalTls(zoneId);
160 expect(after.enabled ?? false).toEqual(baseline.enabled ?? false);
161 }).pipe(logLevel),
162 { timeout: 120_000 },
163 );
164
165 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
166 // API for this per-zone setting, so `list()` enumerates every zone via
167 // `listAllZones` and reads the singleton in each (reads succeed without the
168 // ACM entitlement). Assert the result is non-empty and contains the standing
169 // test zone.
170 test.provider(
171 "list enumerates the setting across all zones",
172 (stack) =>
173 Effect.gen(function* () {
174 const zoneId = yield* resolveZoneId;
175
176 const provider = yield* Provider.findProvider(Cloudflare.Acm.TotalTls);
177 const all = yield* provider.list();
178
179 expect(all.length).toBeGreaterThan(0);
180 expect(all.some((s) => s.zoneId === zoneId)).toBe(true);
181
182 // `stack` is unused here (the singleton always exists on every zone),
183 // but keep the destroy bookend so the harness state stays clean.
184 yield* stack.destroy();
185 }).pipe(logLevel),
186 { tags: ["provider:cloudflare:zone"] },
187 );
188 },
189 );