@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 200 lines 7.81 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 ssl from "@distilled.cloud/cloudflare/ssl";
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 // with "Unable to authenticate request". Ride out the blips on the test's
29 // own out-of-band calls by retrying the typed `Forbidden` error (part of
30 // each universal-setting operation's error union via distilled patches).
31 //
32 // The universal-settings PATCH endpoint is also rate-limited per zone
33 // ("Rate limit reached for the update operation. Please try again in a
34 // minute"), which surfaces as the typed `TooManyRequests` — back off and
35 // retry that too. The exponential schedule reaches the ~1 minute window
36 // within the bounded attempts.
37 const transientRetrySchedule = Schedule.exponential("500 millis");
38
39 const getUniversal = (zoneId: string) =>
40 ssl.getUniversalSetting({ zoneId }).pipe(
41 Effect.retry({
42 while: (e) => e._tag === "Forbidden" || e._tag === "TooManyRequests",
43 schedule: transientRetrySchedule,
44 times: 8,
45 }),
46 );
47
48 // Normalize the setting to a known baseline so each run starts from the
49 // same cloud state regardless of what a previous (possibly interrupted)
50 // run left behind. Cloudflare's default for Universal SSL is enabled.
51 const setBaseline = (zoneId: string, enabled: boolean) =>
52 ssl.patchUniversalSetting({ zoneId, enabled }).pipe(
53 Effect.retry({
54 while: (e) => e._tag === "Forbidden" || e._tag === "TooManyRequests",
55 schedule: transientRetrySchedule,
56 times: 8,
57 }),
58 );
59
60 // Toggling Universal SSL off DELETES the standing zone's universal edge
61 // certificate; Cloudflare re-issues it minutes later on re-enable — and
62 // sometimes not at all (`enabled: true` with zero certificate packs), which
63 // breaks every TLS-dependent test on the shared zone (worker routes, R2
64 // domains, Hyperdrive origins) until someone re-orders the cert by toggling
65 // the setting off→on. The toggle lifecycle is therefore opt-in; the
66 // read-only `list` test below always runs.
67 const destructive = !!process.env.CLOUDFLARE_TEST_UNIVERSAL_SSL;
68
69 describe.sequential(
70 "UniversalSsl",
71 { tags: ["provider:cloudflare", "provider:cloudflare:ssl", "provider:cloudflare:zone", "live"] },
72 () => {
73 test.provider.skipIf(!destructive)(
74 "disables Universal SSL and restores the original value on destroy",
75 (stack) =>
76 Effect.gen(function* () {
77 const zoneId = yield* resolveZoneId;
78
79 yield* stack.destroy();
80 // Known baseline: Universal SSL defaults to enabled.
81 yield* setBaseline(zoneId, true);
82
83 const setting = yield* stack.deploy(
84 Effect.gen(function* () {
85 return yield* Cloudflare.Ssl.UniversalSsl("UniversalSsl", { zoneId, enabled: false });
86 }),
87 );
88
89 expect(setting.zoneId).toEqual(zoneId);
90 expect(setting.enabled).toEqual(false);
91 // The pre-management value was captured for restore-on-destroy.
92 expect(setting.initialEnabled).toEqual(true);
93
94 const live = yield* getUniversal(zoneId);
95 expect(live.enabled).toEqual(false);
96
97 yield* stack.destroy();
98
99 // Destroy restored the value the setting had before we managed it.
100 const restored = yield* getUniversal(zoneId);
101 expect(restored.enabled).toEqual(true);
102 }).pipe(logLevel),
103 { timeout: 180_000 },
104 );
105
106 test.provider.skipIf(!destructive)(
107 "updates enabled in place and keeps the captured initial value",
108 (stack) =>
109 Effect.gen(function* () {
110 const zoneId = yield* resolveZoneId;
111
112 yield* stack.destroy();
113 yield* setBaseline(zoneId, true);
114
115 const initial = yield* stack.deploy(
116 Effect.gen(function* () {
117 return yield* Cloudflare.Ssl.UniversalSsl("UniversalSsl", { zoneId, enabled: false });
118 }),
119 );
120
121 expect(initial.enabled).toEqual(false);
122 expect(initial.initialEnabled).toEqual(true);
123
124 const updated = yield* stack.deploy(
125 Effect.gen(function* () {
126 return yield* Cloudflare.Ssl.UniversalSsl("UniversalSsl", { zoneId, enabled: true });
127 }),
128 );
129
130 // Same singleton patched in place; the original value survives the
131 // update so destroy still restores the pre-management state.
132 expect(updated.enabled).toEqual(true);
133 expect(updated.initialEnabled).toEqual(true);
134
135 const live = yield* getUniversal(zoneId);
136 expect(live.enabled).toEqual(true);
137
138 yield* stack.destroy();
139
140 const restored = yield* getUniversal(zoneId);
141 expect(restored.enabled).toEqual(true);
142 }).pipe(logLevel),
143 { timeout: 180_000 },
144 );
145
146 test.provider.skipIf(!destructive)(
147 "destroy restores a disabled baseline when managing from a disabled zone",
148 (stack) =>
149 Effect.gen(function* () {
150 const zoneId = yield* resolveZoneId;
151
152 yield* stack.destroy();
153 // Pre-management state is disabled — the capture-and-restore must
154 // bring the zone back to disabled, not to Cloudflare's default.
155 yield* setBaseline(zoneId, false);
156
157 const setting = yield* stack.deploy(
158 Effect.gen(function* () {
159 return yield* Cloudflare.Ssl.UniversalSsl("UniversalSsl", { zoneId, enabled: true });
160 }),
161 );
162
163 expect(setting.enabled).toEqual(true);
164 expect(setting.initialEnabled).toEqual(false);
165
166 const live = yield* getUniversal(zoneId);
167 expect(live.enabled).toEqual(true);
168
169 yield* stack.destroy();
170
171 const restored = yield* getUniversal(zoneId);
172 expect(restored.enabled).toEqual(false);
173
174 // Leave the zone in its default state for other suites.
175 yield* setBaseline(zoneId, true);
176 }).pipe(logLevel),
177 { timeout: 180_000 },
178 );
179
180 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
181 // API for this per-zone setting, so `list()` enumerates every zone via
182 // `listAllZones` and reads the singleton in each. Assert the result is
183 // non-empty and contains the standing test zone.
184 test.provider("list enumerates the setting across all zones", (stack) =>
185 Effect.gen(function* () {
186 const zoneId = yield* resolveZoneId;
187
188 const provider = yield* Provider.findProvider(Cloudflare.Ssl.UniversalSsl);
189 const all = yield* provider.list();
190
191 expect(all.length).toBeGreaterThan(0);
192 expect(all.some((s) => s.zoneId === zoneId)).toBe(true);
193
194 // `stack` is unused here (the singleton always exists on every zone),
195 // but keep the destroy bookend so the harness state stays clean.
196 yield* stack.destroy();
197 }).pipe(logLevel),
198 );
199 },
200 );