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

main
ts 244 lines 10.2 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 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
18
19 // Deterministic per-test names (never derived from Date.now() or
20 // randomness). Each test owns disjoint identifiers so reruns never collide.
21 const PROJECT_CRUD = testName("alchemy-e3-pages-dom-crud");
22 const PROJECT_REPLACE = testName("alchemy-e3-pages-dom-repl");
23 const PROJECT_LIST = testName("alchemy-e3-pages-dom-list");
24 const DOMAIN_LIST = `${testName("alchemy-pages-list")}.${zoneName}`;
25 const DOMAIN_CRUD = `${testName("alchemy-pages-crud")}.${zoneName}`;
26 const DOMAIN_REPLACE_A = `${testName("alchemy-pages-replace-a")}.${zoneName}`;
27 const DOMAIN_REPLACE_B = `${testName("alchemy-pages-replace-b")}.${zoneName}`;
28
29 // The scoped API token the test harness mints propagates eventually-
30 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
31 // declared in the distilled error union) on the test's own out-of-band
32 // verification calls.
33 const forbiddenRetry = {
34 while: (e: { _tag: string }) => e._tag === "Forbidden",
35 schedule: Schedule.exponential("500 millis"),
36 times: 8,
37 } as const;
38
39 const getDomain = (accountId: string, projectName: string, domainName: string) =>
40 pages.getProjectDomain({ accountId, projectName, domainName }).pipe(Effect.retry(forbiddenRetry));
41
42 // Attaching a domain is asynchronous: the attachment, its zone tag and
43 // validation/verification blocks propagate across Cloudflare's edge a beat
44 // after `create` returns. Poll `getProjectDomain` with a bounded schedule,
45 // riding out the brief `PagesDomainNotFound` window, until the domain is
46 // observable with a status. We assert reachability — NOT a terminal `active`
47 // status, since certificate issuance needs a real CNAME and stays pending.
48 const waitForDomain = (accountId: string, projectName: string, domainName: string) =>
49 pages.getProjectDomain({ accountId, projectName, domainName }).pipe(
50 Effect.retry({
51 while: (e) => e._tag === "Forbidden" || e._tag === "PagesDomainNotFound",
52 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
53 }),
54 );
55
56 const expectDomainGone = (accountId: string, projectName: string, domainName: string) =>
57 getDomain(accountId, projectName, domainName).pipe(
58 Effect.flatMap(() => Effect.fail({ _tag: "DomainNotDeleted" } as const)),
59 // A detached domain surfaces as `PagesDomainNotFound` (code 8000021);
60 // if the whole project is gone the API reports `ProjectNotFound`
61 // (code 8000007). Both mean the attachment no longer exists.
62 Effect.catchTag("PagesDomainNotFound", () => Effect.void),
63 Effect.catchTag("ProjectNotFound", () => Effect.void),
64 Effect.retry({
65 while: (e) => e._tag === "DomainNotDeleted",
66 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
67 }),
68 );
69
70 // Purge leftovers from interrupted runs so deterministically-named tests
71 // start from a clean slate (deleting the project detaches its domains).
72 const purgeProject = (accountId: string, projectName: string) =>
73 pages.deleteProject({ accountId, projectName }).pipe(
74 Effect.catchTag("ProjectNotFound", () => Effect.void),
75 Effect.retry(forbiddenRetry),
76 );
77
78 test.provider(
79 "attach and detach a custom domain",
80 (stack) =>
81 Effect.gen(function* () {
82 const { accountId } = yield* yield* CloudflareEnvironment;
83
84 yield* stack.destroy();
85 yield* purgeProject(accountId, PROJECT_CRUD);
86
87 const { project, domain } = yield* stack.deploy(
88 Effect.gen(function* () {
89 const project = yield* Cloudflare.Pages.Project("DomainCrudProject", {
90 name: PROJECT_CRUD,
91 }).pipe(adopt(true));
92 const domain = yield* Cloudflare.Pages.Domain("CrudDomain", {
93 projectName: project.name,
94 name: DOMAIN_CRUD,
95 }).pipe(adopt(true));
96 return { project, domain };
97 }),
98 );
99
100 expect(domain.domainId).toBeDefined();
101 expect(domain.accountId).toEqual(accountId);
102 expect(domain.projectName).toEqual(project.name);
103 expect(domain.name).toEqual(DOMAIN_CRUD);
104 // Certificate issuance is asynchronous — the domain legitimately stays
105 // `initializing`/`pending` (the cert authority, validation/verification
106 // blocks and zone tag fill in over time). This test exercises CRUD, not
107 // certificate issuance, so only assert the attachment exists with a
108 // status, never a terminal `active`.
109 expect(domain.status).toBeTruthy();
110 expect(domain.createdOn).toBeTruthy();
111
112 // The attachment propagates a beat after `create` returns — poll until
113 // it is observable rather than asserting it is immediately readable.
114 const live = yield* waitForDomain(accountId, project.name, DOMAIN_CRUD);
115 expect(live.domainId).toEqual(domain.domainId);
116 expect(live.name).toEqual(DOMAIN_CRUD);
117 expect(live.status).toBeTruthy();
118
119 // Redeploying identical props is a no-op (same attachment).
120 const noop = yield* stack.deploy(
121 Effect.gen(function* () {
122 const project = yield* Cloudflare.Pages.Project("DomainCrudProject", {
123 name: PROJECT_CRUD,
124 }).pipe(adopt(true));
125 const domain = yield* Cloudflare.Pages.Domain("CrudDomain", {
126 projectName: project.name,
127 name: DOMAIN_CRUD,
128 }).pipe(adopt(true));
129 return { project, domain };
130 }),
131 );
132 expect(noop.domain.domainId).toEqual(domain.domainId);
133
134 yield* stack.destroy();
135
136 yield* expectDomainGone(accountId, PROJECT_CRUD, DOMAIN_CRUD);
137 }).pipe(
138 logLevel,
139 // Guarantee teardown even if an assertion throws mid-test, so a failed
140 // run never leaves a dangling Pages.Domain / Pages.Project behind.
141 // `stack.destroy()` tears down everything in the scratch stack (domain
142 // then project); the next run's start-of-test purge is the backstop.
143 Effect.ensuring(stack.destroy().pipe(Effect.orDie).pipe(Effect.ignore)),
144 ),
145 { tags: ["provider:cloudflare", "provider:cloudflare:pages", "live"] },
146 );
147
148 test.provider(
149 "changing the domain name triggers replacement",
150 (stack) =>
151 Effect.gen(function* () {
152 const { accountId } = yield* yield* CloudflareEnvironment;
153
154 yield* stack.destroy();
155 yield* purgeProject(accountId, PROJECT_REPLACE);
156
157 const initial = yield* stack.deploy(
158 Effect.gen(function* () {
159 const project = yield* Cloudflare.Pages.Project("DomainReplProject", {
160 name: PROJECT_REPLACE,
161 }).pipe(adopt(true));
162 const domain = yield* Cloudflare.Pages.Domain("ReplaceDomain", {
163 projectName: project.name,
164 name: DOMAIN_REPLACE_A,
165 }).pipe(adopt(true));
166 return domain;
167 }),
168 );
169
170 expect(initial.name).toEqual(DOMAIN_REPLACE_A);
171
172 const replaced = yield* stack.deploy(
173 Effect.gen(function* () {
174 const project = yield* Cloudflare.Pages.Project("DomainReplProject", {
175 name: PROJECT_REPLACE,
176 }).pipe(adopt(true));
177 const domain = yield* Cloudflare.Pages.Domain("ReplaceDomain", {
178 projectName: project.name,
179 name: DOMAIN_REPLACE_B,
180 }).pipe(adopt(true));
181 return domain;
182 }),
183 );
184
185 // The domain name is the attachment's identity — a new physical
186 // attachment exists.
187 expect(replaced.domainId).not.toEqual(initial.domainId);
188 expect(replaced.name).toEqual(DOMAIN_REPLACE_B);
189
190 // The old domain was detached as part of the replacement.
191 yield* expectDomainGone(accountId, PROJECT_REPLACE, DOMAIN_REPLACE_A);
192
193 const live = yield* getDomain(accountId, PROJECT_REPLACE, DOMAIN_REPLACE_B);
194 expect(live.domainId).toEqual(replaced.domainId);
195
196 yield* stack.destroy();
197
198 yield* expectDomainGone(accountId, PROJECT_REPLACE, DOMAIN_REPLACE_B);
199 }).pipe(logLevel),
200 { tags: ["provider:cloudflare", "provider:cloudflare:pages", "live"] },
201 );
202
203 // Canonical `list()` test (parent fan-out): domains have no account-wide
204 // enumeration API, so `list()` enumerates every Pages project and lists each
205 // project's domains. Deploy a project + domain, then assert the attachment
206 // appears in the exhaustively-paginated result with its full `read`
207 // Attributes shape.
208 test.provider(
209 "list enumerates domains across all Pages projects",
210 (stack) =>
211 Effect.gen(function* () {
212 const { accountId } = yield* yield* CloudflareEnvironment;
213
214 yield* stack.destroy();
215 yield* purgeProject(accountId, PROJECT_LIST);
216
217 const { domain } = yield* stack.deploy(
218 Effect.gen(function* () {
219 const project = yield* Cloudflare.Pages.Project("DomainListProject", {
220 name: PROJECT_LIST,
221 }).pipe(adopt(true));
222 const domain = yield* Cloudflare.Pages.Domain("ListDomain", {
223 projectName: project.name,
224 name: DOMAIN_LIST,
225 }).pipe(adopt(true));
226 return { domain };
227 }),
228 );
229
230 const provider = yield* Provider.findProvider(Cloudflare.Pages.Domain);
231 const all = yield* provider.list();
232
233 const found = all.find((d) => d.domainId === domain.domainId);
234 expect(found).toBeDefined();
235 expect(found?.name).toEqual(DOMAIN_LIST);
236 expect(found?.projectName).toEqual(PROJECT_LIST);
237 expect(found?.accountId).toEqual(accountId);
238
239 yield* stack.destroy();
240
241 yield* expectDomainGone(accountId, PROJECT_LIST, DOMAIN_LIST);
242 }).pipe(logLevel),
243 { tags: ["provider:cloudflare", "provider:cloudflare:pages", "live"] },
244 );