@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 265 lines 8.92 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 securityTxt from "@distilled.cloud/cloudflare/security-txt";
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 security-txt operation's error union via distilled patches).
31 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
32
33 const getSecurityTxt = (zoneId: string) =>
34 securityTxt.getSecurityTxt({ zoneId }).pipe(
35 Effect.retry({
36 while: (e) => e._tag === "Forbidden",
37 schedule: forbiddenRetrySchedule,
38 times: 8,
39 }),
40 );
41
42 // Normalize the zone to a known baseline (no security.txt configured) so
43 // each run starts from the same cloud state regardless of what a previous
44 // (possibly interrupted) run left behind. DELETE on an absent file
45 // succeeds, so this is safe to run unconditionally.
46 const clearBaseline = (zoneId: string) =>
47 securityTxt.deleteSecurityTxt({ zoneId }).pipe(
48 Effect.retry({
49 while: (e) => e._tag === "Forbidden",
50 schedule: forbiddenRetrySchedule,
51 times: 8,
52 }),
53 );
54
55 const contact = ["mailto:security@alchemy.run"];
56 const expires = "2030-01-01T00:00:00Z";
57
58 describe.sequential(
59 "SecurityTxt",
60 {
61 tags: [
62 "provider:cloudflare",
63 "provider:cloudflare:securitytxt",
64 "provider:cloudflare:zone",
65 "live",
66 ],
67 },
68 () => {
69 test.provider("creates a security.txt, verifies out-of-band, and deletes on destroy", (stack) =>
70 Effect.gen(function* () {
71 const zoneId = yield* resolveZoneId;
72
73 yield* stack.destroy();
74 yield* clearBaseline(zoneId);
75
76 const created = yield* stack.deploy(
77 Effect.gen(function* () {
78 return yield* Cloudflare.SecurityTxt.SecurityTxt("SecurityTxt", {
79 zoneId,
80 contact,
81 expires,
82 });
83 }),
84 );
85
86 expect(created.zoneId).toEqual(zoneId);
87 expect(created.enabled).toEqual(true);
88 expect(created.contact).toEqual(contact);
89 expect(created.expires).toEqual(expires);
90 expect(created.policy).toBeUndefined();
91 expect(created.preferredLanguages).toBeUndefined();
92
93 // Out-of-band: the file exists with the configured fields.
94 const live = yield* getSecurityTxt(zoneId);
95 expect(typeof live).not.toEqual("string");
96 if (typeof live !== "string") {
97 expect(live.enabled).toEqual(true);
98 expect(live.contact).toEqual(contact);
99 expect(live.expires).toEqual(expires);
100 }
101
102 yield* stack.destroy();
103
104 // Destroy removed the file — Cloudflare reports the unconfigured
105 // state as an empty-string sentinel.
106 const gone = yield* getSecurityTxt(zoneId);
107 expect(gone).toEqual("");
108
109 // Destroying again is a no-op (idempotent delete).
110 yield* stack.destroy();
111 }).pipe(logLevel),
112 );
113
114 test.provider("updates mutable fields in place (full-replace PUT)", (stack) =>
115 Effect.gen(function* () {
116 const zoneId = yield* resolveZoneId;
117
118 yield* stack.destroy();
119 yield* clearBaseline(zoneId);
120
121 const created = yield* stack.deploy(
122 Effect.gen(function* () {
123 return yield* Cloudflare.SecurityTxt.SecurityTxt("SecurityTxt", {
124 zoneId,
125 contact,
126 expires,
127 });
128 }),
129 );
130 expect(created.policy).toBeUndefined();
131
132 const updated = yield* stack.deploy(
133 Effect.gen(function* () {
134 return yield* Cloudflare.SecurityTxt.SecurityTxt("SecurityTxt", {
135 zoneId,
136 contact,
137 expires,
138 policy: ["https://alchemy.run/security-policy"],
139 preferredLanguages: "en, es",
140 });
141 }),
142 );
143
144 // Same singleton replaced in place — zone identity is unchanged.
145 expect(updated.zoneId).toEqual(zoneId);
146 expect(updated.policy).toEqual(["https://alchemy.run/security-policy"]);
147 expect(updated.preferredLanguages).toEqual("en, es");
148
149 const live = yield* getSecurityTxt(zoneId);
150 expect(typeof live).not.toEqual("string");
151 if (typeof live !== "string") {
152 expect(live.policy).toEqual(["https://alchemy.run/security-policy"]);
153 expect(live.preferredLanguages).toEqual("en, es");
154 }
155
156 // Dropping the optional fields converges back to the minimal file.
157 const reverted = yield* stack.deploy(
158 Effect.gen(function* () {
159 return yield* Cloudflare.SecurityTxt.SecurityTxt("SecurityTxt", {
160 zoneId,
161 contact,
162 expires,
163 });
164 }),
165 );
166 expect(reverted.policy).toBeUndefined();
167 expect(reverted.preferredLanguages).toBeUndefined();
168
169 yield* stack.destroy();
170
171 const gone = yield* getSecurityTxt(zoneId);
172 expect(gone).toEqual("");
173 }).pipe(logLevel),
174 );
175
176 test.provider("disables the file without deleting it, then destroy removes it", (stack) =>
177 Effect.gen(function* () {
178 const zoneId = yield* resolveZoneId;
179
180 yield* stack.destroy();
181 yield* clearBaseline(zoneId);
182
183 yield* stack.deploy(
184 Effect.gen(function* () {
185 return yield* Cloudflare.SecurityTxt.SecurityTxt("SecurityTxt", {
186 zoneId,
187 contact,
188 expires,
189 });
190 }),
191 );
192
193 const disabled = yield* stack.deploy(
194 Effect.gen(function* () {
195 return yield* Cloudflare.SecurityTxt.SecurityTxt("SecurityTxt", {
196 zoneId,
197 enabled: false,
198 contact,
199 expires,
200 });
201 }),
202 );
203
204 // The configuration survives but the file is no longer served.
205 expect(disabled.enabled).toEqual(false);
206 expect(disabled.contact).toEqual(contact);
207
208 const live = yield* getSecurityTxt(zoneId);
209 expect(typeof live).not.toEqual("string");
210 if (typeof live !== "string") {
211 expect(live.enabled).toEqual(false);
212 }
213
214 yield* stack.destroy();
215
216 const gone = yield* getSecurityTxt(zoneId);
217 expect(gone).toEqual("");
218 }).pipe(logLevel),
219 );
220
221 // Canonical `list()` test (zone-scoped singleton): there is no account-wide
222 // API for this per-zone file, so `list()` enumerates every zone via
223 // `listAllZones` and reads each. Only configured zones are emitted, so deploy
224 // a security.txt on the standing test zone and assert it appears.
225 test.provider("list enumerates configured security.txt files", (stack) =>
226 Effect.gen(function* () {
227 const zoneId = yield* resolveZoneId;
228
229 yield* stack.destroy();
230 yield* clearBaseline(zoneId);
231
232 const deployed = yield* stack.deploy(
233 Effect.gen(function* () {
234 return yield* Cloudflare.SecurityTxt.SecurityTxt("SecurityTxt", {
235 zoneId,
236 contact,
237 expires,
238 });
239 }),
240 );
241
242 const provider = yield* Provider.findProvider(Cloudflare.SecurityTxt.SecurityTxt);
243 // Ride out token eventual-consistency 403s on the per-zone reads.
244 const all = yield* provider.list().pipe(
245 Effect.retry({
246 while: (e) => e._tag === "Forbidden",
247 schedule: forbiddenRetrySchedule,
248 times: 8,
249 }),
250 );
251
252 expect(all.length).toBeGreaterThan(0);
253 const entry = all.find((s) => s.zoneId === deployed.zoneId);
254 expect(entry).toBeDefined();
255 expect(entry?.contact).toEqual(contact);
256 expect(entry?.expires).toEqual(expires);
257
258 yield* stack.destroy();
259
260 const gone = yield* getSecurityTxt(zoneId);
261 expect(gone).toEqual("");
262 }).pipe(logLevel),
263 );
264 },
265 );