The best way to control Claude Code, Codex and more from any computer or phone

main
ts 190 lines 6.5 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 schemaValidation from "@distilled.cloud/cloudflare/schema-validation";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9 import * as FileSystem from "effect/FileSystem";
10 import * as Path from "effect/Path";
11 import { MinimumLogLevel } from "effect/References";
12 import * as Schedule from "effect/Schedule";
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 const resolveZoneId = Effect.gen(function* () {
21 const { accountId } = yield* yield* CloudflareEnvironment;
22 const zone = yield* findZoneByName({ accountId, name: zoneName });
23 if (!zone) {
24 return yield* Effect.die(new Error(`zone "${zoneName}" not found in account`));
25 }
26 return zone.id;
27 });
28
29 const readFixture = (name: string) =>
30 Effect.gen(function* () {
31 const fs = yield* FileSystem.FileSystem;
32 const path = yield* Path.Path;
33 return yield* fs.readFileString(path.join(import.meta.dirname, "fixtures", name));
34 });
35
36 // The scoped API token the test harness mints propagates eventually-
37 // consistently across Cloudflare's edge — ride out 403 blips on the test's
38 // own out-of-band calls by retrying the typed `Forbidden` error.
39 const getSchemaOob = (zoneId: string, schemaId: string) =>
40 schemaValidation.getSchema({ zoneId, schemaId }).pipe(
41 Effect.retry({
42 while: (e) => e._tag === "Forbidden",
43 schedule: Schedule.exponential("500 millis"),
44 times: 8,
45 }),
46 );
47
48 test.provider(
49 "uploads a schema, enables in place, replaces on disable and source change, destroys",
50 (stack) =>
51 Effect.gen(function* () {
52 const zoneId = yield* resolveZoneId;
53 const v1 = yield* readFixture("openapi-v1.json");
54 const v2 = yield* readFixture("openapi-v2.json");
55
56 yield* stack.destroy();
57
58 const created = yield* stack.deploy(
59 Effect.gen(function* () {
60 return yield* Cloudflare.SchemaValidation.SchemaValidationSchema("TestSchema", {
61 zoneId,
62 source: v1,
63 validationEnabled: false,
64 });
65 }),
66 );
67
68 expect(created.schemaId).toBeTruthy();
69 expect(created.zoneId).toEqual(zoneId);
70 expect(created.kind).toEqual("openapi_v3");
71 expect(created.validationEnabled).toEqual(false);
72
73 const live = yield* getSchemaOob(zoneId, created.schemaId);
74 expect(live.name).toEqual(created.name);
75 expect(live.source).toEqual(v1);
76 expect(live.validationEnabled ?? false).toEqual(false);
77
78 // Enabling (false → true) is the one in-place update.
79 const enabled = yield* stack.deploy(
80 Effect.gen(function* () {
81 return yield* Cloudflare.SchemaValidation.SchemaValidationSchema("TestSchema", {
82 zoneId,
83 source: v1,
84 validationEnabled: true,
85 });
86 }),
87 );
88 expect(enabled.schemaId).toEqual(created.schemaId);
89 expect(enabled.validationEnabled).toEqual(true);
90 expect(enabled.source).toEqual(v1);
91
92 const enabledLive = yield* getSchemaOob(zoneId, enabled.schemaId);
93 expect(enabledLive.validationEnabled).toEqual(true);
94
95 // Cloudflare rejects disabling in place — true → false replaces the
96 // schema (new id, old one deleted).
97 const disabled = yield* stack.deploy(
98 Effect.gen(function* () {
99 return yield* Cloudflare.SchemaValidation.SchemaValidationSchema("TestSchema", {
100 zoneId,
101 source: v1,
102 validationEnabled: false,
103 });
104 }),
105 );
106 expect(disabled.schemaId).not.toEqual(created.schemaId);
107 expect(disabled.validationEnabled).toEqual(false);
108
109 const firstGone = yield* schemaValidation
110 .getSchema({ zoneId, schemaId: created.schemaId })
111 .pipe(Effect.flip);
112 expect(firstGone._tag).toEqual("SchemaNotFound");
113
114 // The schema body is immutable — changing the source uploads a new
115 // schema (new id) and deletes the old one.
116 const replaced = yield* stack.deploy(
117 Effect.gen(function* () {
118 return yield* Cloudflare.SchemaValidation.SchemaValidationSchema("TestSchema", {
119 zoneId,
120 source: v2,
121 });
122 }),
123 );
124 expect(replaced.schemaId).not.toEqual(disabled.schemaId);
125 expect(replaced.source).toEqual(v2);
126
127 const oldGone = yield* schemaValidation
128 .getSchema({ zoneId, schemaId: disabled.schemaId })
129 .pipe(Effect.flip);
130 expect(oldGone._tag).toEqual("SchemaNotFound");
131
132 yield* stack.destroy();
133
134 const gone = yield* schemaValidation
135 .getSchema({ zoneId, schemaId: replaced.schemaId })
136 .pipe(Effect.flip);
137 expect(gone._tag).toEqual("SchemaNotFound");
138 }).pipe(logLevel),
139 {
140 tags: [
141 "provider:cloudflare",
142 "provider:cloudflare:schemavalidation",
143 "provider:cloudflare:zone",
144 "live",
145 ],
146 timeout: 120_000,
147 },
148 );
149
150 // Canonical `list()` test (zone-scoped collection): `list()` enumerates every
151 // zone in the account via `listAllZones` and exhaustively paginates each
152 // zone's schemas. Deploy a real schema and assert it appears in the result.
153 test.provider(
154 "list enumerates the deployed schema across all zones",
155 (stack) =>
156 Effect.gen(function* () {
157 const zoneId = yield* resolveZoneId;
158 const v1 = yield* readFixture("openapi-v1.json");
159
160 yield* stack.destroy();
161
162 const deployed = yield* stack.deploy(
163 Effect.gen(function* () {
164 return yield* Cloudflare.SchemaValidation.SchemaValidationSchema("ListSchema", {
165 zoneId,
166 source: v1,
167 validationEnabled: false,
168 });
169 }),
170 );
171
172 const provider = yield* Provider.findProvider(
173 Cloudflare.SchemaValidation.SchemaValidationSchema,
174 );
175 const all = yield* provider.list();
176
177 expect(all.some((s) => s.schemaId === deployed.schemaId && s.zoneId === zoneId)).toBe(true);
178
179 yield* stack.destroy();
180 }).pipe(logLevel),
181 {
182 tags: [
183 "provider:cloudflare",
184 "provider:cloudflare:schemavalidation",
185 "provider:cloudflare:zone",
186 "live",
187 ],
188 timeout: 120_000,
189 },
190 );