@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 248 lines 8.19 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 * as FileSystem from "effect/FileSystem";
11 import * as Path from "effect/Path";
12 import { MinimumLogLevel } from "effect/References";
13 import * as Schedule from "effect/Schedule";
14 import * as Stream from "effect/Stream";
15 import { testName } from "../../names.ts";
16
17 const { test } = Test.make({ providers: Cloudflare.providers() });
18
19 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
20
21 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
22
23 // Deterministic per-test schema names.
24 const NAME_DEFAULT = testName("alch-userschema-default");
25 const NAME_REPLACE = testName("alch-userschema-replace");
26 const NAME_LIST = testName("alch-userschema-list");
27
28 const resolveZoneId = Effect.gen(function* () {
29 const { accountId } = yield* yield* CloudflareEnvironment;
30 const zone = yield* findZoneByName({ accountId, name: zoneName });
31 if (!zone) {
32 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
33 }
34 return zone.id;
35 });
36
37 const fixture = (file: string) =>
38 Effect.gen(function* () {
39 const fs = yield* FileSystem.FileSystem;
40 const path = yield* Path.Path;
41 return yield* fs.readFileString(path.join(import.meta.dirname, "fixtures", file));
42 });
43
44 // The scoped API token the test harness mints propagates eventually-
45 // consistently — a fresh token intermittently 403s. Ride out the blips on
46 // the test's own out-of-band calls by retrying the typed `Forbidden` error.
47 const forbiddenRetrySchedule = Schedule.exponential("500 millis");
48
49 // Read a schema out-of-band; `undefined` when gone.
50 const getSchema = (zoneId: string, schemaId: string) =>
51 apiGateway.getUserSchema({ zoneId, schemaId }).pipe(
52 Effect.map((schema): apiGateway.GetUserSchemaResponse | undefined => schema),
53 Effect.catchTag("SchemaNotFound", () => Effect.succeed(undefined)),
54 Effect.retry({
55 while: (e) => e._tag === "Forbidden",
56 schedule: forbiddenRetrySchedule,
57 times: 8,
58 }),
59 );
60
61 // Purge schemas left over from interrupted runs so each test starts clean.
62 const purgeSchemasNamed = (zoneId: string, name: string) =>
63 apiGateway.listUserSchemas.items({ zoneId, omitSource: true }).pipe(
64 Stream.runCollect,
65 Effect.flatMap((chunk) =>
66 Effect.forEach(
67 Array.from(chunk).filter((schema) => schema.name === name),
68 (schema) =>
69 apiGateway
70 .deleteUserSchema({ zoneId, schemaId: schema.schemaId })
71 .pipe(Effect.catchTag("SchemaNotFound", () => Effect.void)),
72 ),
73 ),
74 Effect.retry({
75 while: (e) => e._tag === "Forbidden",
76 schedule: forbiddenRetrySchedule,
77 times: 8,
78 }),
79 );
80
81 test.provider(
82 "create, enable validation in place, destroy a user schema",
83 (stack) =>
84 Effect.gen(function* () {
85 const zoneId = yield* resolveZoneId;
86 const source = yield* fixture("openapi-v1.json");
87
88 yield* stack.destroy();
89 yield* purgeSchemasNamed(zoneId, NAME_DEFAULT);
90
91 const schema = yield* stack.deploy(
92 Effect.gen(function* () {
93 return yield* Cloudflare.ApiShield.UserSchema("DefaultSchema", {
94 zoneId,
95 name: NAME_DEFAULT,
96 schema: source,
97 }).pipe(adopt(true));
98 }),
99 );
100
101 expect(schema.zoneId).toEqual(zoneId);
102 expect(schema.name).toEqual(NAME_DEFAULT);
103 expect(schema.kind).toEqual("openapi_v3");
104 expect(schema.source).toEqual(source);
105 expect(schema.validationEnabled).toEqual(false);
106 expect(schema.schemaId.length).toBeGreaterThan(0);
107
108 const live = yield* getSchema(zoneId, schema.schemaId);
109 expect(live?.name).toEqual(NAME_DEFAULT);
110 expect(live?.source).toEqual(source);
111
112 // Enable validation — same identity, patched in place.
113 const enabled = yield* stack.deploy(
114 Effect.gen(function* () {
115 return yield* Cloudflare.ApiShield.UserSchema("DefaultSchema", {
116 zoneId,
117 name: NAME_DEFAULT,
118 schema: source,
119 validationEnabled: true,
120 }).pipe(adopt(true));
121 }),
122 );
123 expect(enabled.schemaId).toEqual(schema.schemaId);
124 expect(enabled.validationEnabled).toEqual(true);
125
126 const patched = yield* getSchema(zoneId, schema.schemaId);
127 expect(patched?.validationEnabled).toEqual(true);
128
129 yield* stack.destroy();
130
131 const gone = yield* getSchema(zoneId, schema.schemaId);
132 expect(gone).toBeUndefined();
133 }).pipe(logLevel),
134 {
135 tags: [
136 "provider:cloudflare",
137 "provider:cloudflare:apishield",
138 "provider:cloudflare:zone",
139 "live",
140 ],
141 timeout: 120_000,
142 },
143 );
144
145 test.provider(
146 "changing the schema source triggers replacement",
147 (stack) =>
148 Effect.gen(function* () {
149 const zoneId = yield* resolveZoneId;
150 const sourceV1 = yield* fixture("openapi-v1.json");
151 const sourceV2 = yield* fixture("openapi-v2.json");
152
153 yield* stack.destroy();
154 yield* purgeSchemasNamed(zoneId, NAME_REPLACE);
155
156 const initial = yield* stack.deploy(
157 Effect.gen(function* () {
158 return yield* Cloudflare.ApiShield.UserSchema("ReplaceSchema", {
159 zoneId,
160 name: NAME_REPLACE,
161 schema: sourceV1,
162 }).pipe(adopt(true));
163 }),
164 );
165 expect(initial.source).toEqual(sourceV1);
166
167 const replaced = yield* stack.deploy(
168 Effect.gen(function* () {
169 return yield* Cloudflare.ApiShield.UserSchema("ReplaceSchema", {
170 zoneId,
171 name: NAME_REPLACE,
172 schema: sourceV2,
173 }).pipe(adopt(true));
174 }),
175 );
176
177 // The source is immutable — a new physical schema exists.
178 expect(replaced.schemaId).not.toEqual(initial.schemaId);
179 expect(replaced.source).toEqual(sourceV2);
180
181 // The old schema was deleted as part of the replacement.
182 const oldSchema = yield* getSchema(zoneId, initial.schemaId);
183 expect(oldSchema).toBeUndefined();
184
185 const live = yield* getSchema(zoneId, replaced.schemaId);
186 expect(live?.source).toEqual(sourceV2);
187
188 yield* stack.destroy();
189
190 const gone = yield* getSchema(zoneId, replaced.schemaId);
191 expect(gone).toBeUndefined();
192 }).pipe(logLevel),
193 {
194 tags: [
195 "provider:cloudflare",
196 "provider:cloudflare:apishield",
197 "provider:cloudflare:zone",
198 "live",
199 ],
200 timeout: 120_000,
201 },
202 );
203
204 // Canonical `list()` test (zone-scoped collection): `list()` fans out over
205 // every zone via `listAllZones` and exhaustively paginates each zone's
206 // schemas. Deploy a schema, then assert its id appears in the result.
207 test.provider(
208 "list enumerates the deployed user schema",
209 (stack) =>
210 Effect.gen(function* () {
211 const zoneId = yield* resolveZoneId;
212 const source = yield* fixture("openapi-v1.json");
213
214 yield* stack.destroy();
215 yield* purgeSchemasNamed(zoneId, NAME_LIST);
216
217 const schema = yield* stack.deploy(
218 Effect.gen(function* () {
219 return yield* Cloudflare.ApiShield.UserSchema("ListSchema", {
220 zoneId,
221 name: NAME_LIST,
222 schema: source,
223 }).pipe(adopt(true));
224 }),
225 );
226
227 const provider = yield* Provider.findProvider(Cloudflare.ApiShield.UserSchema);
228 const all = yield* provider.list();
229
230 const found = all.find((s) => s.schemaId === schema.schemaId);
231 expect(found).toBeDefined();
232 expect(found?.zoneId).toEqual(zoneId);
233 expect(found?.name).toEqual(NAME_LIST);
234 expect(found?.kind).toEqual("openapi_v3");
235 expect(found?.source).toEqual(source);
236
237 yield* stack.destroy();
238 }).pipe(logLevel),
239 {
240 tags: [
241 "provider:cloudflare",
242 "provider:cloudflare:apishield",
243 "provider:cloudflare:zone",
244 "live",
245 ],
246 timeout: 120_000,
247 },
248 );