@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 144 lines 4.66 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 Nuxt 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 expect(html).toContain("Nuxt on Railway");
82 // The `GREETING` env value from alchemy.run.ts, read via
83 // `process.env` during SSR — proves the Railway Service rendered it at
84 // request time.
85 expect(html).toContain("Hello from Nuxt on Railway!");
86 }),
87 { timeout: 180_000 },
88 );
89
90 test(
91 "serves the api route with the environment value",
92 Effect.gen(function* () {
93 const url = yield* base;
94 const body = yield* getBodyWhenReady(`${url}/api/hello`, "Hello from Nuxt on Railway!");
95 expect(JSON.parse(body)).toEqual({
96 greeting: "Hello from Nuxt on Railway!",
97 });
98 }),
99 { timeout: 180_000 },
100 );
101
102 test(
103 "serves the prerendered about page",
104 Effect.gen(function* () {
105 const url = yield* base;
106 const res = yield* getWhenReady(`${url}/about`);
107 expect(res.status).toBe(200);
108 }),
109 { timeout: 180_000 },
110 );
111
112 test(
113 "compiles tailwind from nuxt.config.ts",
114 Effect.gen(function* () {
115 const url = yield* base;
116 // The utility classes on the home page prove the markup made it through
117 // the build; the compiled rule proves the @tailwindcss/vite plugin from
118 // the project's own nuxt.config.ts ran during it.
119 const html = yield* getBodyWhenReady(url, "text-3xl");
120 expect(html).toContain("text-3xl");
121
122 // Nuxt either links the compiled stylesheet (/_nuxt/*.css) or inlines it
123 // into a <style> tag depending on its inlineStyles feature — accept both.
124 const links = [...html.matchAll(/href="([^"]+\.css)"/g)].map((m) => m[1]!);
125 let css = [...html.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/g)].map((m) => m[1]!).join("\n");
126 for (const link of links) {
127 const href = link.startsWith("http") ? link : `${url}${link}`;
128 css += yield* getBodyWhenReady(href, "{");
129 }
130 expect(css).toContain(".text-3xl");
131 expect(css).toContain("font-bold");
132 }),
133 { timeout: 180_000 },
134 );
135
136 test(
137 "serves a static asset from public/",
138 Effect.gen(function* () {
139 const url = yield* base;
140 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
141 expect(body).toContain("User-agent: *");
142 }),
143 { timeout: 180_000 },
144 );