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

main
ts 259 lines 9.51 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as pages from "@distilled.cloud/cloudflare/pages";
6 import { adopt } from "alchemy/AdoptPolicy";
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 import { testName } from "../../names.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // Deterministic per-test project names (never derived from Date.now() or
18 // randomness). Project names form globally-unique *.pages.dev subdomains,
19 // so they carry an alchemy-e3 prefix to avoid collisions.
20 const NAME_UPDATE = testName("alchemy-e3-pages-update");
21 const NAME_REPLACE_A = testName("alchemy-e3-pages-replace-a");
22 const NAME_REPLACE_B = testName("alchemy-e3-pages-replace-b");
23 const NAME_LIST = testName("alchemy-e3-pages-list");
24
25 // The scoped API token the test harness mints propagates eventually-
26 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
27 // declared in the distilled error union) on the test's own out-of-band
28 // verification calls.
29 const getProject = (accountId: string, projectName: string) =>
30 pages.getProject({ accountId, projectName }).pipe(
31 Effect.retry({
32 while: (e) => e._tag === "Forbidden",
33 schedule: Schedule.exponential("500 millis"),
34 times: 8,
35 }),
36 );
37
38 const expectGone = (accountId: string, projectName: string) =>
39 getProject(accountId, projectName).pipe(
40 Effect.flatMap(() => Effect.fail({ _tag: "ProjectNotDeleted" } as const)),
41 // A missing project surfaces as `ProjectNotFound` (Cloudflare error
42 // code 8000007) — that's the success condition here.
43 Effect.catchTag("ProjectNotFound", () => Effect.void),
44 Effect.retry({
45 while: (e) => e._tag === "ProjectNotDeleted",
46 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
47 }),
48 );
49
50 // Purge leftovers from interrupted runs so deterministically-named tests
51 // start from a clean slate.
52 const purgeProject = (accountId: string, projectName: string) =>
53 pages.deleteProject({ accountId, projectName }).pipe(
54 Effect.catchTag("ProjectNotFound", () => Effect.void),
55 Effect.retry({
56 while: (e) => e._tag === "Forbidden",
57 schedule: Schedule.exponential("500 millis"),
58 times: 8,
59 }),
60 );
61
62 test.provider(
63 "create and delete a project with generated name",
64 (stack) =>
65 Effect.gen(function* () {
66 const { accountId } = yield* yield* CloudflareEnvironment;
67
68 yield* stack.destroy();
69
70 const project = yield* stack.deploy(Cloudflare.Pages.Project("DefaultProject", {}));
71
72 expect(project.projectId).toBeDefined();
73 expect(project.accountId).toEqual(accountId);
74 expect(project.name).toBeTruthy();
75 expect(project.subdomain).toEqual(`${project.name}.pages.dev`);
76 expect(project.productionBranch).toEqual("main");
77 expect(project.createdOn).toBeTruthy();
78
79 const live = yield* getProject(accountId, project.name);
80 expect(live.id).toEqual(project.projectId);
81 expect(live.name).toEqual(project.name);
82 expect(live.productionBranch).toEqual("main");
83
84 yield* stack.destroy();
85
86 yield* expectGone(accountId, project.name);
87 }).pipe(logLevel),
88 { tags: ["provider:cloudflare", "provider:cloudflare:pages", "live"] },
89 );
90
91 test.provider(
92 "update mutable props in place (same project id)",
93 (stack) =>
94 Effect.gen(function* () {
95 const { accountId } = yield* yield* CloudflareEnvironment;
96
97 yield* stack.destroy();
98 yield* purgeProject(accountId, NAME_UPDATE);
99
100 const initial = yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* Cloudflare.Pages.Project("UpdateProject", {
103 name: NAME_UPDATE,
104 productionBranch: "main",
105 buildConfig: { buildCommand: "npm run build", destinationDir: "dist" },
106 deploymentConfigs: {
107 production: {
108 compatibilityDate: "2025-01-01",
109 envVars: { FOO: { value: "foo-v1" }, DROP_ME: { value: "going-away" } },
110 },
111 },
112 }).pipe(adopt(true));
113 }),
114 );
115
116 expect(initial.name).toEqual(NAME_UPDATE);
117 expect(initial.productionBranch).toEqual("main");
118
119 const observed = yield* getProject(accountId, NAME_UPDATE);
120 expect(observed.buildConfig?.buildCommand).toEqual("npm run build");
121 expect(observed.deploymentConfigs.production?.envVars).toMatchObject({
122 FOO: { value: "foo-v1" },
123 DROP_ME: { value: "going-away" },
124 });
125
126 const updated = yield* stack.deploy(
127 Effect.gen(function* () {
128 return yield* Cloudflare.Pages.Project("UpdateProject", {
129 name: NAME_UPDATE,
130 productionBranch: "develop",
131 buildConfig: { buildCommand: "npm run build:v2", destinationDir: "out" },
132 deploymentConfigs: {
133 production: {
134 compatibilityDate: "2025-06-01",
135 envVars: { FOO: { value: "foo-v2" }, BAR: { value: "bar-v1" } },
136 },
137 },
138 }).pipe(adopt(true));
139 }),
140 );
141
142 // Same project mutated in place — not a replacement.
143 expect(updated.projectId).toEqual(initial.projectId);
144 expect(updated.productionBranch).toEqual("develop");
145
146 const live = yield* getProject(accountId, NAME_UPDATE);
147 expect(live.productionBranch).toEqual("develop");
148 expect(live.buildConfig?.buildCommand).toEqual("npm run build:v2");
149 expect(live.buildConfig?.destinationDir).toEqual("out");
150 expect(live.deploymentConfigs.production?.compatibilityDate).toEqual("2025-06-01");
151 expect(live.deploymentConfigs.production?.envVars).toMatchObject({
152 FOO: { value: "foo-v2" },
153 BAR: { value: "bar-v1" },
154 });
155 // PATCH deep-merges — the reconciler must null out removed env vars.
156 expect(live.deploymentConfigs.production?.envVars).not.toHaveProperty("DROP_ME");
157
158 // Redeploying identical props is a no-op (still the same project).
159 const noop = yield* stack.deploy(
160 Effect.gen(function* () {
161 return yield* Cloudflare.Pages.Project("UpdateProject", {
162 name: NAME_UPDATE,
163 productionBranch: "develop",
164 buildConfig: { buildCommand: "npm run build:v2", destinationDir: "out" },
165 deploymentConfigs: {
166 production: {
167 compatibilityDate: "2025-06-01",
168 envVars: { FOO: { value: "foo-v2" }, BAR: { value: "bar-v1" } },
169 },
170 },
171 }).pipe(adopt(true));
172 }),
173 );
174 expect(noop.projectId).toEqual(initial.projectId);
175
176 yield* stack.destroy();
177
178 yield* expectGone(accountId, NAME_UPDATE);
179 }).pipe(logLevel),
180 { tags: ["provider:cloudflare", "provider:cloudflare:pages", "live"] },
181 );
182
183 test.provider(
184 "list enumerates the deployed project",
185 (stack) =>
186 Effect.gen(function* () {
187 const { accountId } = yield* yield* CloudflareEnvironment;
188
189 yield* stack.destroy();
190 yield* purgeProject(accountId, NAME_LIST);
191
192 const deployed = yield* stack.deploy(
193 Effect.gen(function* () {
194 return yield* Cloudflare.Pages.Project("ListProject", { name: NAME_LIST }).pipe(
195 adopt(true),
196 );
197 }),
198 );
199
200 const provider = yield* Provider.findProvider(Cloudflare.Pages.Project);
201 const all = yield* provider.list();
202
203 const match = all.find((p) => p.projectId === deployed.projectId);
204 expect(match).toBeDefined();
205 expect(match?.name).toEqual(NAME_LIST);
206 expect(match?.accountId).toEqual(accountId);
207 expect(match?.subdomain).toEqual(`${NAME_LIST}.pages.dev`);
208
209 yield* stack.destroy();
210
211 yield* expectGone(accountId, NAME_LIST);
212 }).pipe(logLevel),
213 { tags: ["provider:cloudflare", "provider:cloudflare:pages", "live"] },
214 );
215
216 test.provider(
217 "changing the name triggers replacement",
218 (stack) =>
219 Effect.gen(function* () {
220 const { accountId } = yield* yield* CloudflareEnvironment;
221
222 yield* stack.destroy();
223 yield* purgeProject(accountId, NAME_REPLACE_A);
224 yield* purgeProject(accountId, NAME_REPLACE_B);
225
226 const initial = yield* stack.deploy(
227 Effect.gen(function* () {
228 return yield* Cloudflare.Pages.Project("ReplaceProject", { name: NAME_REPLACE_A }).pipe(
229 adopt(true),
230 );
231 }),
232 );
233
234 expect(initial.name).toEqual(NAME_REPLACE_A);
235
236 const replaced = yield* stack.deploy(
237 Effect.gen(function* () {
238 return yield* Cloudflare.Pages.Project("ReplaceProject", { name: NAME_REPLACE_B }).pipe(
239 adopt(true),
240 );
241 }),
242 );
243
244 // The name is the project's identity — a new physical project exists.
245 expect(replaced.projectId).not.toEqual(initial.projectId);
246 expect(replaced.name).toEqual(NAME_REPLACE_B);
247
248 // The old project was deleted as part of the replacement.
249 yield* expectGone(accountId, NAME_REPLACE_A);
250
251 const live = yield* getProject(accountId, NAME_REPLACE_B);
252 expect(live.id).toEqual(replaced.projectId);
253
254 yield* stack.destroy();
255
256 yield* expectGone(accountId, NAME_REPLACE_B);
257 }).pipe(logLevel),
258 { tags: ["provider:cloudflare", "provider:cloudflare:pages", "live"] },
259 );