@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 269 lines 8.54 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 apiGateway from "@distilled.cloud/cloudflare/api-gateway";
7 import { adopt } from "alchemy/AdoptPolicy";
8 import * as Provider from "alchemy/Provider";
9 import * as Effect from "effect/Effect";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Schedule from "effect/Schedule";
12 import { testName } from "../../names.ts";
13
14 const { test } = Test.make({ providers: Cloudflare.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
19
20 // Deterministic per-test label names (Cloudflare caps them at 24 chars).
21 const NAME_DEFAULT = testName("alch-as-default");
22 const NAME_RENAME_A = testName("alch-as-ren-a");
23 const NAME_RENAME_B = testName("alch-as-ren-b");
24 const NAME_LIST = testName("alch-as-list");
25
26 const resolveZoneId = Effect.gen(function* () {
27 const { accountId } = yield* yield* CloudflareEnvironment;
28 const zone = yield* findZoneByName({ accountId, name: zoneName });
29 if (!zone) {
30 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
31 }
32 return zone.id;
33 });
34
35 // The scoped API token the test harness mints propagates eventually-
36 // consistently — a fresh token intermittently 403s. Ride out the blips on
37 // the test's own out-of-band calls by retrying the typed `Forbidden` error.
38 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
39
40 // Read a label out-of-band; `undefined` when gone.
41 const getLabel = (zoneId: string, name: string) =>
42 apiGateway.getLabelUser({ zoneId, name }).pipe(
43 Effect.map((label): apiGateway.GetLabelUserResponse | undefined => label),
44 Effect.catchTag("LabelNotFound", () => Effect.succeed(undefined)),
45 Effect.retry({
46 while: (e) => e._tag === "Forbidden",
47 schedule: forbiddenRetrySchedule,
48 times: 8,
49 }),
50 );
51
52 // Purge a label left over from interrupted runs so each test starts clean.
53 const purgeLabel = (zoneId: string, name: string) =>
54 apiGateway.deleteLabelUser({ zoneId, name }).pipe(
55 Effect.catchTag("LabelNotFound", () => Effect.void),
56 Effect.retry({
57 while: (e) => e._tag === "Forbidden",
58 schedule: forbiddenRetrySchedule,
59 times: 8,
60 }),
61 );
62
63 test.provider(
64 "create, update description in place, destroy a label",
65 (stack) =>
66 Effect.gen(function* () {
67 const zoneId = yield* resolveZoneId;
68
69 yield* stack.destroy();
70 yield* purgeLabel(zoneId, NAME_DEFAULT);
71
72 const label = yield* stack.deploy(
73 Effect.gen(function* () {
74 return yield* Cloudflare.ApiShield.Label("DefaultLabel", {
75 zoneId,
76 name: NAME_DEFAULT,
77 description: "v1",
78 }).pipe(adopt(true));
79 }),
80 );
81
82 expect(label.zoneId).toEqual(zoneId);
83 expect(label.name).toEqual(NAME_DEFAULT);
84 expect(label.description).toEqual("v1");
85 expect(label.source).toEqual("user");
86
87 const live = yield* getLabel(zoneId, NAME_DEFAULT);
88 expect(live?.name).toEqual(NAME_DEFAULT);
89 expect(live?.description).toEqual("v1");
90
91 // Update the mutable description — same identity, patched in place.
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* Cloudflare.ApiShield.Label("DefaultLabel", {
95 zoneId,
96 name: NAME_DEFAULT,
97 description: "v2",
98 }).pipe(adopt(true));
99 }),
100 );
101 expect(updated.name).toEqual(NAME_DEFAULT);
102 expect(updated.createdAt).toEqual(label.createdAt);
103 expect(updated.description).toEqual("v2");
104
105 const patched = yield* getLabel(zoneId, NAME_DEFAULT);
106 expect(patched?.description).toEqual("v2");
107
108 yield* stack.destroy();
109
110 const gone = yield* getLabel(zoneId, NAME_DEFAULT);
111 expect(gone).toBeUndefined();
112 }).pipe(logLevel),
113 {
114 tags: [
115 "provider:cloudflare",
116 "provider:cloudflare:apishield",
117 "provider:cloudflare:zone",
118 "live",
119 ],
120 },
121 );
122
123 test.provider(
124 "renaming a label triggers replacement",
125 (stack) =>
126 Effect.gen(function* () {
127 const zoneId = yield* resolveZoneId;
128
129 yield* stack.destroy();
130 yield* purgeLabel(zoneId, NAME_RENAME_A);
131 yield* purgeLabel(zoneId, NAME_RENAME_B);
132
133 const initial = yield* stack.deploy(
134 Effect.gen(function* () {
135 return yield* Cloudflare.ApiShield.Label("RenameLabel", {
136 zoneId,
137 name: NAME_RENAME_A,
138 description: "before rename",
139 }).pipe(adopt(true));
140 }),
141 );
142 expect(initial.name).toEqual(NAME_RENAME_A);
143
144 const replaced = yield* stack.deploy(
145 Effect.gen(function* () {
146 return yield* Cloudflare.ApiShield.Label("RenameLabel", {
147 zoneId,
148 name: NAME_RENAME_B,
149 description: "after rename",
150 }).pipe(adopt(true));
151 }),
152 );
153
154 // The name is the label's identity — a new physical label exists.
155 expect(replaced.name).toEqual(NAME_RENAME_B);
156 expect(replaced.description).toEqual("after rename");
157
158 // The old label was deleted as part of the replacement.
159 const oldLabel = yield* getLabel(zoneId, NAME_RENAME_A);
160 expect(oldLabel).toBeUndefined();
161
162 const live = yield* getLabel(zoneId, NAME_RENAME_B);
163 expect(live?.name).toEqual(NAME_RENAME_B);
164
165 yield* stack.destroy();
166
167 const gone = yield* getLabel(zoneId, NAME_RENAME_B);
168 expect(gone).toBeUndefined();
169 }).pipe(logLevel),
170 {
171 tags: [
172 "provider:cloudflare",
173 "provider:cloudflare:apishield",
174 "provider:cloudflare:zone",
175 "live",
176 ],
177 },
178 );
179
180 test.provider(
181 "generated name respects Cloudflare's 24-character limit",
182 (stack) =>
183 Effect.gen(function* () {
184 const zoneId = yield* resolveZoneId;
185
186 yield* stack.destroy();
187
188 const label = yield* stack.deploy(
189 Effect.gen(function* () {
190 return yield* Cloudflare.ApiShield.Label("GeneratedNameLabel", { zoneId }).pipe(
191 adopt(true),
192 );
193 }),
194 );
195
196 expect(label.name.length).toBeGreaterThan(0);
197 expect(label.name.length).toBeLessThanOrEqual(24);
198 expect(label.description).toEqual("");
199
200 const live = yield* getLabel(zoneId, label.name);
201 expect(live?.name).toEqual(label.name);
202
203 yield* stack.destroy();
204
205 const gone = yield* getLabel(zoneId, label.name);
206 expect(gone).toBeUndefined();
207 }).pipe(logLevel),
208 {
209 tags: [
210 "provider:cloudflare",
211 "provider:cloudflare:apishield",
212 "provider:cloudflare:zone",
213 "live",
214 ],
215 },
216 );
217
218 test.provider(
219 "list enumerates the deployed label",
220 (stack) =>
221 Effect.gen(function* () {
222 const zoneId = yield* resolveZoneId;
223
224 yield* stack.destroy();
225 yield* purgeLabel(zoneId, NAME_LIST);
226
227 const deployed = yield* stack.deploy(
228 Effect.gen(function* () {
229 return yield* Cloudflare.ApiShield.Label("ListLabel", {
230 zoneId,
231 name: NAME_LIST,
232 description: "listed",
233 }).pipe(adopt(true));
234 }),
235 );
236
237 const provider = yield* Provider.findProvider(Cloudflare.ApiShield.Label);
238
239 // `list()` fans out over every zone and paginates each. Under a full
240 // concurrent run two things can blip: the freshly-minted scoped token
241 // 403s while it propagates (typed `Forbidden`), and a just-created label
242 // lags the zone list endpoint. Retry the whole enumeration on either, so
243 // the test rides out both instead of asserting on one snapshot.
244 const appears = (all: readonly { zoneId: string; name: string }[]) =>
245 all.some((label) => label.zoneId === zoneId && label.name === deployed.name);
246 const all = yield* provider.list().pipe(
247 Effect.flatMap((rows) =>
248 appears(rows) ? Effect.succeed(rows) : Effect.fail({ _tag: "LabelNotListed" as const }),
249 ),
250 Effect.retry({
251 while: (e) => e._tag === "Forbidden" || e._tag === "LabelNotListed",
252 schedule: Schedule.spaced("1 seconds"),
253 times: 15,
254 }),
255 );
256
257 expect(appears(all)).toBe(true);
258
259 yield* stack.destroy();
260 }).pipe(logLevel),
261 {
262 tags: [
263 "provider:cloudflare",
264 "provider:cloudflare:apishield",
265 "provider:cloudflare:zone",
266 "live",
267 ],
268 },
269 );