1import * as Alchemy from "alchemy";
2import * as Railway from "alchemy/Railway";
3import * as Test from "alchemy/Test/Bun";
4import { expect } from "bun:test";
5import * as Console from "effect/Console";
6import * as Data from "effect/Data";
7import * as Effect from "effect/Effect";
8import * as Schedule from "effect/Schedule";
9import Stack from "../alchemy.run.ts";
10
11// A fresh Railway deployment can serve transient 404/5xx responses
12// while it propagates. `Test.getWhenReady` fails on that cold-start
13// window and retries until the site serves a real response.
14const { getWhenReady } = Test;
15
16class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
17 body: string;
18}> {}
19
20// While the asset manifest and Railway edge caches are still
21// propagating, a 200 body can be stale — the status alone can't
22// distinguish "not yet" from "served", so retry until the body matches.
23const getBodyWhenReady = (url: string, expected: string) =>
24 Effect.gen(function* () {
25 const res = yield* getWhenReady(url);
26 expect(res.status).toBe(200);
27 const body = yield* res.text;
28 if (!body.includes(expected)) {
29 return yield* Effect.fail(new AssetNotReady({ body }));
30 }
31 return body;
32 }).pipe(
33 Effect.retry({
34 while: (error) => error instanceof AssetNotReady,
35 schedule: Schedule.max([
36 Schedule.min([
37 Schedule.exponential("500 millis"),
38 Schedule.spaced("3 seconds"),
39 ]),
40 Schedule.recurs(20),
41 ]),
42 }),
43 );
44
45const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
46 providers: Railway.providers(),
47 state: Alchemy.localState(),
48 profile: process.env.ALCHEMY_PROFILE,
49});
50
51// The first deploy runs the full Vite build AND creates a Railway
52// distribution (~5-10 minutes), so give the hook far more headroom than
53// the default 120s.
54const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
55 timeout: 600_000,
56});
57// Deleting the Railway deployment takes several minutes too.
58afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
59 timeout: 600_000,
60});
61
62const base = Effect.map(stack, ({ url }) => {
63 if (!url) throw new Error("expected the site to expose a url");
64 return String(url).replace(/\/+$/, "");
65});
66
67// Resolve an asset href from index.html against the site base.
68const resolve = (baseUrl: string, href: string) =>
69 href.startsWith("http")
70 ? href
71 : `${baseUrl}${href.startsWith("/") ? "" : "/"}${href}`;
72
73test(
74 "deploys and exposes a url",
75 Effect.gen(function* () {
76 const { url } = yield* stack;
77 expect(url).toBeString();
78 }),
79 { timeout: 180_000 },
80);
81
82test(
83 "serves the index HTML",
84 Effect.gen(function* () {
85 const url = yield* base;
86 const html = yield* getBodyWhenReady(url, '<div id="root">');
87 expect(html).toContain("<title>Foldkit on Railway</title>");
88 }),
89 { timeout: 180_000 },
90);
91
92test(
93 "ships the Foldkit app in the client bundle",
94 Effect.gen(function* () {
95 const url = yield* base;
96 // Client-only SPA: the greeting and card view render in the browser, so
97 // assert on the built JS bundle Vite links from index.html.
98 const html = yield* getBodyWhenReady(url, '<script type="module"');
99 const script = html.match(/<script[^>]*type="module"[^>]*src="([^"]+)"/);
100 expect(script).not.toBeNull();
101 const js = yield* getBodyWhenReady(
102 resolve(url, script![1]!),
103 "Hello from Foldkit!",
104 );
105 expect(js).toContain("Hello from Foldkit!");
106 expect(js).toContain("Styled with Tailwind CSS");
107 }),
108 { timeout: 180_000 },
109);
110
111test(
112 "compiles tailwind from vite.config.ts",
113 Effect.gen(function* () {
114 const url = yield* base;
115 // Assert on the compiled stylesheet Vite links from index.html — the
116 // compiled rule for a utility used in src/main.ts only exists if the
117 // @tailwindcss/vite plugin from the project's own vite.config.ts ran.
118 const html = yield* getBodyWhenReady(url, "stylesheet");
119 const link = html.match(
120 /<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/,
121 );
122 expect(link).not.toBeNull();
123 const css = yield* getBodyWhenReady(resolve(url, link![1]!), ".text-3xl");
124 expect(css).toContain(".text-3xl");
125 }),
126 { timeout: 180_000 },
127);