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