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

main
ts 506 lines 18.3 KB
Raw
1 import { resolve4, resolveNs, Resolver } from "node:dns/promises";
2 /**
3 * `domain.dns` on Railway websites, deployed for real: a StaticSite whose
4 * custom hostname is published through a DNS adapter (Cloudflare, and a
5 * Route 53 zone delegated from the Cloudflare test zone). Asserts the
6 * records land in the DNS host exactly as `Railway.CustomDomain.dnsRecords`
7 * reports them, Railway verifies the hostname and serves HTTPS on it, and
8 * destroy removes both the records and the Railway custom domain.
9 *
10 * `test/Railway` is excluded from the default `pnpm test` set; run it
11 * explicitly.
12 */
13 import * as AWS from "@alchemy.run/aws";
14 import * as Cloudflare from "@alchemy.run/cloudflare";
15 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
16 import { findZoneByName } from "@alchemy.run/cloudflare/Zone/lookup";
17 import * as Railway from "@alchemy.run/railway";
18 import type { CustomDomainDnsRecord } from "@alchemy.run/railway/CustomDomain";
19 import { describe, expect } from "@alchemy.run/test";
20 import * as Test from "@alchemy.run/test/Alchemy";
21 import * as route53 from "@distilled.cloud/aws/route-53";
22 import * as dns from "@distilled.cloud/cloudflare/dns";
23 import { Query } from "@distilled.cloud/core/query";
24 import { Railway as RailwayApi } from "@distilled.cloud/railway";
25 import * as Output from "alchemy/Output";
26 import { isResourceState, State } from "alchemy/State";
27 import * as Data from "effect/Data";
28 import * as Effect from "effect/Effect";
29 import * as HttpClient from "effect/http/HttpClient";
30 import * as HttpClientError from "effect/http/HttpClientError";
31 import * as Layer from "effect/Layer";
32 import { MinimumLogLevel } from "effect/References";
33 import * as Schedule from "effect/Schedule";
34 import * as Stream from "effect/Stream";
35 import * as pathe from "pathe";
36 import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts";
37 import { suitePartition } from "../suiteProject.ts";
38
39 const { test } = Test.make({
40 providers: Layer.mergeAll(Railway.providers(), Cloudflare.providers(), AWS.providers()),
41 });
42
43 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
44
45 const ZONE = "alchemy-test-2.us";
46 const HOSTNAME = `rw-dns.${ZONE}`;
47 const R53_ZONE = `rw-dns-r53.${ZONE}`;
48 const R53_HOSTNAME = `www.${R53_ZONE}`;
49 const MARKER = "StaticSite fixture v1";
50
51 const fixtureDir = pathe.resolve(
52 import.meta.dirname,
53 "../../Cloudflare/Website/staticsite-fixture",
54 );
55 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
56
57 const tags = [
58 "provider:railway",
59 "provider:railway:customdomain",
60 "provider:railway:website",
61 "provider:cloudflare",
62 "provider:cloudflare:dns",
63 "live",
64 ];
65
66 class RecordNotPublished extends Data.TaggedError("RecordNotPublished")<{
67 readonly server: string;
68 readonly name: string;
69 readonly type: string;
70 }> {}
71
72 class DomainNotVerified extends Data.TaggedError("DomainNotVerified")<{
73 readonly verified: boolean | null | undefined;
74 readonly certificateStatus: string | undefined;
75 }> {}
76
77 class SiteNotServing extends Data.TaggedError("SiteNotServing")<{
78 readonly status: number;
79 }> {}
80
81 interface PublishedRecord {
82 name: string;
83 type: string;
84 value: string;
85 }
86
87 const trimDot = (value: string) => value.replace(/\.$/, "").toLowerCase();
88 const unquote = (value: string) => value.replace(/^"|"$/g, "");
89
90 const normalize = (records: ReadonlyArray<PublishedRecord>) =>
91 records
92 .map((record) => ({
93 name: trimDot(record.name),
94 type: record.type,
95 value: record.type === "TXT" ? unquote(record.value) : trimDot(record.value),
96 }))
97 .sort((a, b) =>
98 `${a.name}|${a.type}|${a.value}`.localeCompare(`${b.name}|${b.type}|${b.value}`),
99 );
100
101 const underHostname = (name: string, hostname: string) =>
102 trimDot(name) === hostname || trimDot(name).endsWith(`.${hostname}`);
103
104 const resolveZone = Effect.gen(function* () {
105 const { accountId } = yield* yield* CloudflareEnvironment;
106 const zone = yield* findZoneByName({ accountId, name: ZONE });
107 if (!zone) {
108 return yield* Effect.die(new Error(`zone "${ZONE}" not found`));
109 }
110 const nameServers = yield* Effect.tryPromise(() => resolveNs(ZONE)).pipe(Effect.orDie);
111 return { id: zone.id, nameServers };
112 });
113
114 /** Every Cloudflare record at or below `hostname` (DNS-only records only). */
115 const cloudflareRecordsUnder = (zoneId: string, hostname: string) =>
116 dns.listRecords.items({ zoneId, name: { endswith: hostname } }).pipe(
117 Stream.runCollect,
118 Effect.map((chunk) =>
119 Array.from(chunk).filter((record) => underHostname(record.name, hostname)),
120 ),
121 );
122
123 /** Every Route 53 record at or below `hostname`, excluding the zone's NS/SOA. */
124 const route53RecordsUnder = (hostedZoneId: string, hostname: string) =>
125 route53.listResourceRecordSets({ HostedZoneId: hostedZoneId }).pipe(
126 Effect.map((result) =>
127 (result.ResourceRecordSets ?? [])
128 .filter(
129 (set) => set.Type !== "NS" && set.Type !== "SOA" && underHostname(set.Name, hostname),
130 )
131 .flatMap((set) =>
132 (set.ResourceRecords ?? []).map((record) => ({
133 name: set.Name,
134 type: set.Type,
135 value: record.Value,
136 })),
137 ),
138 ),
139 );
140
141 /**
142 * The persisted Attributes of the website's `Railway.CustomDomain`
143 * (`{siteId}/Domain`), i.e. what the resource reported on deploy.
144 */
145 const customDomainAttrs = (
146 stack: Test.ScratchStack,
147 siteId: string,
148 ): Effect.Effect<Railway.CustomDomain["Attributes"]> =>
149 Effect.gen(function* () {
150 const state = yield* yield* State;
151 const row = yield* state.get({
152 stack: stack.name,
153 stage: stack.stage,
154 fqn: `${siteId}/Domain`,
155 });
156 if (!row || !isResourceState(row) || row.attr === undefined) {
157 return yield* Effect.die(new Error(`no ${siteId}/Domain state row`));
158 }
159 return row.attr as Railway.CustomDomain["Attributes"];
160 }).pipe(Effect.provide(stack.state), Effect.orDie);
161
162 const liveDomain = Query.fn((id: string, projectId: string) => {
163 const domain = RailwayApi.customDomain({ id, projectId });
164 return {
165 id: domain.id,
166 domain: domain.domain,
167 deletedAt: domain.deletedAt,
168 syncStatus: domain.syncStatus,
169 status: {
170 verified: domain.status.verified,
171 certificateStatus: domain.status.certificateStatus,
172 verificationDnsHost: domain.status.verificationDnsHost,
173 verificationToken: domain.status.verificationToken,
174 dnsRecords: domain.status.dnsRecords.pipe(
175 Query.map((record) => ({
176 fqdn: record.fqdn,
177 zone: record.zone,
178 recordType: record.recordType,
179 requiredValue: record.requiredValue,
180 })),
181 ),
182 },
183 };
184 });
185
186 /**
187 * Railway's raw `status.dnsRecords` (plus its ownership TXT) must each be
188 * published: the mapping in `Railway.CustomDomain` qualifies zone-relative
189 * `fqdn`s, so compare either form.
190 */
191 const expectRailwayRecordsPublished = (
192 live: Effect.Success<ReturnType<typeof liveDomain>>,
193 published: ReadonlyArray<PublishedRecord>,
194 ) => {
195 const records = normalize(published);
196 const has = (type: string, host: string, zone: string, value: string) =>
197 records.some(
198 (record) =>
199 record.type === type &&
200 record.value === (type === "TXT" ? unquote(value) : trimDot(value)) &&
201 (record.name === trimDot(host) || record.name === `${trimDot(host)}.${trimDot(zone)}`),
202 );
203 const required = live.status.dnsRecords.filter((record) =>
204 ["DNS_RECORD_TYPE_CNAME", "DNS_RECORD_TYPE_A", "DNS_RECORD_TYPE_TXT"].includes(
205 record.recordType,
206 ),
207 );
208 expect(required.length).toBeGreaterThan(0);
209 for (const record of required) {
210 expect(
211 has(
212 record.recordType.replace("DNS_RECORD_TYPE_", ""),
213 record.fqdn,
214 record.zone,
215 record.requiredValue,
216 ),
217 ).toBe(true);
218 }
219 const host = live.status.verificationDnsHost;
220 const token = live.status.verificationToken;
221 if (host && token) {
222 expect(has("TXT", host, required[0]?.zone ?? ZONE, token)).toBe(true);
223 }
224 };
225
226 /**
227 * Wait until every authoritative nameserver answers each record (local
228 * resolvers negative-cache the hostname).
229 */
230 const waitForAuthoritative = (
231 nameServers: ReadonlyArray<string>,
232 records: ReadonlyArray<CustomDomainDnsRecord>,
233 ) =>
234 Effect.gen(function* () {
235 const servers = (yield* Effect.forEach(nameServers, (ns) =>
236 Effect.tryPromise(() => resolve4(trimDot(ns))).pipe(Effect.orDie),
237 )).flat();
238 yield* Effect.forEach(servers, (server) =>
239 Effect.forEach(records, (record) =>
240 Effect.gen(function* () {
241 const resolver = yield* Effect.sync(() => {
242 const r = new Resolver();
243 r.setServers([server]);
244 return r;
245 });
246 const answers = yield* Effect.tryPromise(() =>
247 record.type === "TXT"
248 ? resolver
249 .resolveTxt(record.name)
250 .then((rows) => rows.map((chunks) => chunks.join("")))
251 : record.type === "CNAME"
252 ? resolver.resolveCname(record.name)
253 : resolver.resolve4(record.name),
254 ).pipe(Effect.orElseSucceed(() => [] as string[]));
255 if (
256 !answers.some(
257 (answer) => trimDot(answer) === trimDot(record.value) || answer === record.value,
258 )
259 ) {
260 return yield* Effect.fail(
261 new RecordNotPublished({
262 server,
263 name: record.name,
264 type: record.type,
265 }),
266 );
267 }
268 }).pipe(Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 20 })),
269 ),
270 );
271 });
272
273 /** Railway verifies ownership and issues the certificate. */
274 const waitUntilVerified = (customDomainId: string, projectId: string) =>
275 liveDomain(customDomainId, projectId).pipe(
276 Effect.flatMap((domain) =>
277 domain.status.verified === true
278 ? Effect.succeed(domain)
279 : Effect.fail(
280 new DomainNotVerified({
281 verified: domain.status.verified,
282 certificateStatus: domain.status.certificateStatus ?? undefined,
283 }),
284 ),
285 ),
286 Effect.tapError((error) => Effect.logInfo("Railway domain pending", error)),
287 Effect.retry({
288 while: (error) => error._tag === "DomainNotVerified",
289 schedule: Schedule.spaced("10 seconds"),
290 times: 30,
291 }),
292 );
293
294 const waitUntilServing = (hostname: string) =>
295 Effect.gen(function* () {
296 const client = yield* HttpClient.HttpClient;
297 return yield* client.get(`https://${hostname}/`).pipe(
298 Effect.flatMap(
299 (res): Effect.Effect<string, HttpClientError.HttpClientError | SiteNotServing> =>
300 res.status === 200 ? res.text : Effect.fail(new SiteNotServing({ status: res.status })),
301 ),
302 Effect.timeout("10 seconds"),
303 Effect.tapError((error) =>
304 Effect.logInfo(
305 `https://${hostname} not serving yet: ${error._tag === "SiteNotServing" ? `HTTP ${error.status}` : error.message}`,
306 ),
307 ),
308 Effect.retry({ schedule: Schedule.spaced("10 seconds"), times: 30 }),
309 );
310 });
311
312 const readDomainDeletion = Query.fn((id: string, projectId: string) => {
313 const domain = RailwayApi.customDomain({ id, projectId });
314 return { deletedAt: domain.deletedAt, syncStatus: domain.syncStatus };
315 });
316
317 const waitUntilDomainGone = (customDomainId: string, projectId: string) =>
318 readDomainDeletion(customDomainId, projectId).pipe(
319 Effect.map((domain) =>
320 domain.deletedAt != null || domain.syncStatus === "DELETED"
321 ? ("gone" as const)
322 : ("found" as const),
323 ),
324 Effect.catchTag("RailwayNotFound", () => Effect.succeed("gone" as const)),
325 Effect.repeat({
326 schedule: Schedule.spaced("2 seconds"),
327 until: (status) => status === "gone",
328 times: 15,
329 }),
330 );
331
332 const cloneSite = cloneFixture(fixtureDir, {
333 prefix: "alchemy-railway-dns-",
334 tempRoot,
335 entries: ["src", "build.sh"],
336 });
337
338 // Sequential: concurrent Railway builds in the shared suite project can
339 // outlast the Service's bounded deploy wait.
340 describe.sequential("Railway.Website domain.dns (live)", () => {
341 test.provider(
342 "Cloudflare DNS publishes Railway's records; the hostname verifies and serves HTTPS",
343 (stack) =>
344 Effect.gen(function* () {
345 yield* stack.destroy();
346 const zone = yield* resolveZone;
347 const cwd = yield* cloneSite;
348
349 const deployed = yield* stack.deploy(
350 Effect.gen(function* () {
351 const { project, environment } = yield* suitePartition;
352 const site = yield* Railway.Website.StaticSite("Site", {
353 project,
354 environment,
355 cwd,
356 command: "bash build.sh",
357 shell: true,
358 outdir: "dist",
359 domain: { name: HOSTNAME, dns: Cloudflare.DNS.Adapter() },
360 });
361 return { site, environment };
362 }),
363 );
364 expect(deployed.site.url).toBe(`https://${HOSTNAME}`);
365
366 const attrs = yield* customDomainAttrs(stack, "Site");
367 expect(attrs.domain).toBe(HOSTNAME);
368 expect(attrs.serviceId).toBe(deployed.site.service!.serviceId);
369 expect(attrs.dnsRecords.length).toBeGreaterThan(0);
370 expect(
371 attrs.dnsRecords.some(
372 (record) => record.type === "CNAME" && trimDot(record.name) === HOSTNAME,
373 ),
374 ).toBe(true);
375 expect(attrs.dnsRecords.some((record) => record.type === "TXT")).toBe(true);
376
377 // Cloudflare holds exactly the records the CustomDomain reports, DNS-only.
378 const published = yield* cloudflareRecordsUnder(zone.id, HOSTNAME);
379 expect(
380 normalize(
381 published.map((record) => ({
382 name: record.name,
383 type: record.type,
384 value: record.content ?? "",
385 })),
386 ),
387 ).toEqual(normalize(attrs.dnsRecords));
388 expect(published.every((record) => record.proxied !== true)).toBe(true);
389
390 // ...and those are the records Railway's live API requires.
391 const live = yield* liveDomain(attrs.customDomainId, attrs.projectId);
392 expectRailwayRecordsPublished(
393 live,
394 published.map((record) => ({
395 name: record.name,
396 type: record.type,
397 value: record.content ?? "",
398 })),
399 );
400
401 yield* waitForAuthoritative(zone.nameServers, attrs.dnsRecords);
402 const verified = yield* waitUntilVerified(attrs.customDomainId, attrs.projectId);
403 expect(verified.status.verified).toBe(true);
404 expect(yield* waitUntilServing(HOSTNAME)).toContain(MARKER);
405
406 yield* stack.destroy();
407
408 expect(yield* cloudflareRecordsUnder(zone.id, HOSTNAME)).toEqual([]);
409 expect(yield* waitUntilDomainGone(attrs.customDomainId, attrs.projectId)).toBe("gone");
410 }).pipe(logLevel),
411 { tags, timeout: 900_000 },
412 );
413
414 test.provider(
415 "Route 53 (delegated from Cloudflare) publishes Railway's records; the hostname verifies and serves HTTPS",
416 (stack) =>
417 Effect.gen(function* () {
418 yield* stack.destroy();
419 const zone = yield* resolveZone;
420 const cwd = yield* cloneSite;
421
422 const deployed = yield* stack.deploy(
423 Effect.gen(function* () {
424 const hostedZone = yield* AWS.Route53.HostedZone("Zone", {
425 name: R53_ZONE,
426 forceDestroy: true,
427 });
428 // Delegate the sub-zone from the Cloudflare test zone.
429 yield* Effect.forEach([0, 1, 2, 3], (index) =>
430 Cloudflare.DNS.Record(`Delegation${index}`, {
431 zoneId: zone.id,
432 name: R53_ZONE,
433 type: "NS",
434 content: Output.map(
435 hostedZone.nameServers,
436 (servers) => servers[index]!,
437 ) as unknown as string,
438 ttl: 300,
439 }),
440 );
441 const { project, environment } = yield* suitePartition;
442 const site = yield* Railway.Website.StaticSite("Site", {
443 project,
444 environment,
445 cwd,
446 command: "bash build.sh",
447 shell: true,
448 outdir: "dist",
449 domain: {
450 name: R53_HOSTNAME,
451 dns: AWS.Route53.Adapter({ hostedZoneId: hostedZone.id }),
452 },
453 });
454 return { site, hostedZone };
455 }),
456 );
457 expect(deployed.site.url).toBe(`https://${R53_HOSTNAME}`);
458 const hostedZoneId = deployed.hostedZone.id;
459 const nameServers = deployed.hostedZone.nameServers;
460
461 const attrs = yield* customDomainAttrs(stack, "Site");
462 expect(attrs.domain).toBe(R53_HOSTNAME);
463
464 // Route 53 holds exactly the records the CustomDomain reports.
465 const published = yield* route53RecordsUnder(hostedZoneId, R53_HOSTNAME);
466 expect(normalize(published)).toEqual(normalize(attrs.dnsRecords));
467 expectRailwayRecordsPublished(
468 yield* liveDomain(attrs.customDomainId, attrs.projectId),
469 published,
470 );
471
472 // Cloudflare delegates the sub-zone to exactly its Route 53 nameservers.
473 const delegation = yield* dns.listRecords
474 .items({ zoneId: zone.id, name: { exact: R53_ZONE }, type: "NS" })
475 .pipe(Stream.runCollect);
476 expect(
477 Array.from(delegation)
478 .map((record) => trimDot(record.content ?? ""))
479 .sort(),
480 ).toEqual(nameServers.map(trimDot).sort());
481
482 yield* waitForAuthoritative(nameServers, attrs.dnsRecords);
483 const verified = yield* waitUntilVerified(attrs.customDomainId, attrs.projectId);
484 expect(verified.status.verified).toBe(true);
485 expect(yield* waitUntilServing(R53_HOSTNAME)).toContain(MARKER);
486
487 yield* stack.destroy();
488
489 expect(
490 yield* route53.getHostedZone({ Id: hostedZoneId }).pipe(
491 Effect.as("found" as const),
492 Effect.catchTag("NoSuchHostedZone", () => Effect.succeed("gone" as const)),
493 ),
494 ).toBe("gone");
495 const remaining = yield* dns.listRecords
496 .items({ zoneId: zone.id, name: { exact: R53_ZONE }, type: "NS" })
497 .pipe(Stream.runCollect);
498 expect(Array.from(remaining)).toEqual([]);
499 expect(yield* waitUntilDomainGone(attrs.customDomainId, attrs.projectId)).toBe("gone");
500 }).pipe(logLevel),
501 {
502 tags: [...tags, "provider:aws", "provider:aws:route53"],
503 timeout: 900_000,
504 },
505 );
506 });