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

main
ts 137 lines 4.5 KB
Raw
1 import { expect } from "bun:test";
2 import * as Railway from "@alchemy.run/railway";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as Alchemy from "alchemy";
5 import * as Console from "effect/Console";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import Stack from "../alchemy.run.ts";
10
11 // A fresh Railway deployment (and the Railway Service behind it) can serve
12 // transient 404/5xx responses while it propagates. `Test.getWhenReady`
13 // fails on that cold-start window and retries until the site serves a
14 // real response.
15 const { getWhenReady } = Test;
16
17 class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
18 body: string;
19 }> {}
20
21 // While the asset manifest and Railway edge caches are still
22 // propagating, a 200 body can be stale — the status alone can't
23 // distinguish "not yet" from "served", so retry until the body matches.
24 const getBodyWhenReady = (url: string, expected: string) =>
25 Effect.gen(function* () {
26 const res = yield* getWhenReady(url);
27 expect(res.status).toBe(200);
28 const body = yield* res.text;
29 if (!body.includes(expected)) {
30 return yield* Effect.fail(new AssetNotReady({ body }));
31 }
32 return body;
33 }).pipe(
34 Effect.retry({
35 while: (error) => error instanceof AssetNotReady,
36 schedule: Schedule.max([
37 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
38 Schedule.recurs(20),
39 ]),
40 }),
41 );
42
43 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
44 providers: Railway.providers(),
45 state: Alchemy.localState(),
46 profile: process.env.ALCHEMY_PROFILE,
47 });
48
49 // The first deploy runs the full Astro build AND creates a Railway
50 // distribution (~5-10 minutes), so give the hook far more headroom than
51 // the default 120s.
52 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
53 timeout: 600_000,
54 });
55 // Deleting the Railway deployment takes several minutes too.
56 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
57 timeout: 600_000,
58 });
59
60 const base = Effect.map(stack, ({ url }) => {
61 if (!url) throw new Error("expected the site to expose a url");
62 return String(url).replace(/\/+$/, "");
63 });
64
65 test(
66 "deploys and exposes a url",
67 Effect.gen(function* () {
68 const { url } = yield* stack;
69 expect(url).toBeString();
70 }),
71 { timeout: 180_000 },
72 );
73
74 test(
75 "serves the server-rendered home page",
76 Effect.gen(function* () {
77 const url = yield* base;
78 const res = yield* getWhenReady(url);
79 expect(res.status).toBe(200);
80 const html = yield* res.text;
81 // The `GREETING` env value from alchemy.run.ts, read via
82 // `process.env` in the page frontmatter — proves the Railway Service
83 // rendered it at request time.
84 expect(html).toContain("Hello from Astro on Railway!");
85 expect(html).toContain("server-rendered in a Railway Service");
86 }),
87 { timeout: 180_000 },
88 );
89
90 test(
91 "serves the prerendered about page",
92 Effect.gen(function* () {
93 const url = yield* base;
94 const res = yield* getWhenReady(`${url}/about/`);
95 expect(res.status).toBe(200);
96 }),
97 { timeout: 180_000 },
98 );
99
100 test(
101 "compiles tailwind from astro.config.ts",
102 Effect.gen(function* () {
103 const url = yield* base;
104 // The utility class in the markup proves the page shipped with Tailwind
105 // classes; wait until the deployed HTML includes it.
106 const html = yield* getBodyWhenReady(url, "text-3xl");
107
108 // Astro either links an external compiled stylesheet or inlines small
109 // ones as a <style> block — accept both, but the compiled rule for the
110 // utility must be served either way. That rule only exists if the
111 // @tailwindcss/vite plugin from the project's own astro.config.ts ran.
112 const link = html.match(/<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/);
113 if (link) {
114 const href = link[1]!;
115 const cssUrl = href.startsWith("http")
116 ? href
117 : `${url}${href.startsWith("/") ? "" : "/"}${href}`;
118 const css = yield* getBodyWhenReady(cssUrl, ".text-3xl");
119 expect(css).toContain(".text-3xl");
120 } else {
121 const style = html.match(/<style[^>]*>([\s\S]*?)<\/style>/);
122 expect(style).not.toBeNull();
123 expect(style![1]).toContain(".text-3xl");
124 }
125 }),
126 { timeout: 180_000 },
127 );
128
129 test(
130 "serves a static asset from public/",
131 Effect.gen(function* () {
132 const url = yield* base;
133 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
134 expect(body).toContain("User-agent: *");
135 }),
136 { timeout: 180_000 },
137 );