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 (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.
15const { getWhenReady } = Test;
16
17class 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.
24const 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([
38 Schedule.exponential("500 millis"),
39 Schedule.spaced("3 seconds"),
40 ]),
41 Schedule.recurs(20),
42 ]),
43 }),
44 );
45
46const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
47 providers: Railway.providers(),
48 state: Alchemy.localState(),
49 profile: process.env.ALCHEMY_PROFILE,
50});
51
52// The first deploy runs the full Nuxt build AND creates a Railway
53// distribution (~5-10 minutes), so give the hook far more headroom than
54// the default 120s.
55const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
56 timeout: 600_000,
57});
58// Deleting the Railway deployment takes several minutes too.
59afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
60 timeout: 600_000,
61});
62
63const base = Effect.map(stack, ({ url }) => {
64 if (!url) throw new Error("expected the site to expose a url");
65 return String(url).replace(/\/+$/, "");
66});
67
68test(
69 "deploys and exposes a url",
70 Effect.gen(function* () {
71 const { url } = yield* stack;
72 expect(url).toBeString();
73 }),
74 { timeout: 180_000 },
75);
76
77test(
78 "serves the server-rendered home page",
79 Effect.gen(function* () {
80 const url = yield* base;
81 const res = yield* getWhenReady(url);
82 expect(res.status).toBe(200);
83 const html = yield* res.text;
84 expect(html).toContain("Nuxt on Railway");
85 // The `GREETING` env value from alchemy.run.ts, read via
86 // `process.env` during SSR — proves the Railway Service rendered it at
87 // request time.
88 expect(html).toContain("Hello from Nuxt on Railway!");
89 }),
90 { timeout: 180_000 },
91);
92
93test(
94 "serves the api route with the environment value",
95 Effect.gen(function* () {
96 const url = yield* base;
97 const body = yield* getBodyWhenReady(
98 `${url}/api/hello`,
99 "Hello from Nuxt on Railway!",
100 );
101 expect(JSON.parse(body)).toEqual({
102 greeting: "Hello from Nuxt on Railway!",
103 });
104 }),
105 { timeout: 180_000 },
106);
107
108test(
109 "serves the prerendered about page",
110 Effect.gen(function* () {
111 const url = yield* base;
112 const res = yield* getWhenReady(`${url}/about`);
113 expect(res.status).toBe(200);
114 }),
115 { timeout: 180_000 },
116);
117
118test(
119 "compiles tailwind from nuxt.config.ts",
120 Effect.gen(function* () {
121 const url = yield* base;
122 // The utility classes on the home page prove the markup made it through
123 // the build; the compiled rule proves the @tailwindcss/vite plugin from
124 // the project's own nuxt.config.ts ran during it.
125 const html = yield* getBodyWhenReady(url, "text-3xl");
126 expect(html).toContain("text-3xl");
127
128 // Nuxt either links the compiled stylesheet (/_nuxt/*.css) or inlines it
129 // into a <style> tag depending on its inlineStyles feature — accept both.
130 const links = [...html.matchAll(/href="([^"]+\.css)"/g)].map((m) => m[1]!);
131 let css = [...html.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/g)]
132 .map((m) => m[1]!)
133 .join("\n");
134 for (const link of links) {
135 const href = link.startsWith("http") ? link : `${url}${link}`;
136 css += yield* getBodyWhenReady(href, "{");
137 }
138 expect(css).toContain(".text-3xl");
139 expect(css).toContain("font-bold");
140 }),
141 { timeout: 180_000 },
142);
143
144test(
145 "serves a static asset from public/",
146 Effect.gen(function* () {
147 const url = yield* base;
148 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
149 expect(body).toContain("User-agent: *");
150 }),
151 { timeout: 180_000 },
152);