@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 137 lines 4.44 KB
Raw
1 import { expect } from "bun:test";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as Console from "effect/Console";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import Stack from "../alchemy.run.ts";
9
10 // Fresh `workers.dev` URLs transiently 404 while the route propagates.
11 // `Test.getWhenReady` fails on that cold-start window and retries until the
12 // worker serves a real response.
13 const { getWhenReady } = Test;
14
15 class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
16 body: string;
17 }> {}
18
19 // While the static-asset manifest is still propagating, Cloudflare serves a
20 // managed "content signals" robots.txt with a 200 — the status alone can't
21 // distinguish "not yet" from "served", so retry until the body matches.
22 const getBodyWhenReady = (url: string, expected: string) =>
23 Effect.gen(function* () {
24 const res = yield* getWhenReady(url);
25 expect(res.status).toBe(200);
26 const body = yield* res.text;
27 if (!body.includes(expected)) {
28 return yield* Effect.fail(new AssetNotReady({ body }));
29 }
30 return body;
31 }).pipe(
32 Effect.retry({
33 while: (error) => error instanceof AssetNotReady,
34 schedule: Schedule.max([
35 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
36 Schedule.recurs(20),
37 ]),
38 }),
39 );
40
41 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
42 providers: Cloudflare.providers(),
43 state: Cloudflare.state(),
44 });
45
46 // The first deploy runs the full Nuxt build, so give the hook more
47 // headroom than the default 120s.
48 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
49 timeout: 600_000,
50 });
51 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
52
53 const base = Effect.map(stack, ({ url }) => {
54 if (!url) throw new Error("expected the site to expose a workers.dev url");
55 return url.replace(/\/+$/, "");
56 });
57
58 test(
59 "deploys and exposes a url",
60 Effect.gen(function* () {
61 const { url } = yield* stack;
62 expect(url).toBeString();
63 }),
64 { timeout: 180_000 },
65 );
66
67 test(
68 "serves the server-rendered home page",
69 Effect.gen(function* () {
70 const url = yield* base;
71 const res = yield* getWhenReady(url);
72 expect(res.status).toBe(200);
73 const html = yield* res.text;
74 expect(html).toContain("Nuxt on Cloudflare");
75 // The `GREETING` env value from alchemy.run.ts, read via
76 // `event.context.cloudflare.env` during SSR — proves the Worker
77 // rendered it at request time.
78 expect(html).toContain("Hello from Nuxt on Cloudflare!");
79 }),
80 { timeout: 180_000 },
81 );
82
83 test(
84 "serves the api route with the binding",
85 Effect.gen(function* () {
86 const url = yield* base;
87 const body = yield* getBodyWhenReady(`${url}/api/hello`, "Hello from Nuxt on Cloudflare!");
88 expect(JSON.parse(body)).toEqual({
89 greeting: "Hello from Nuxt on Cloudflare!",
90 });
91 }),
92 { timeout: 180_000 },
93 );
94
95 test(
96 "serves the prerendered about page",
97 Effect.gen(function* () {
98 const url = yield* base;
99 const res = yield* getWhenReady(`${url}/about`);
100 expect(res.status).toBe(200);
101 }),
102 { timeout: 180_000 },
103 );
104
105 test(
106 "compiles tailwind from nuxt.config.ts",
107 Effect.gen(function* () {
108 const url = yield* base;
109 // The utility classes on the home page prove the markup made it through
110 // the build; the compiled rule proves the @tailwindcss/vite plugin from
111 // the project's own nuxt.config.ts ran during it.
112 const html = yield* getBodyWhenReady(url, "text-3xl");
113 expect(html).toContain("text-3xl");
114
115 // Nuxt either links the compiled stylesheet (/_nuxt/*.css) or inlines it
116 // into a <style> tag depending on its inlineStyles feature — accept both.
117 const links = [...html.matchAll(/href="([^"]+\.css)"/g)].map((m) => m[1]!);
118 let css = [...html.matchAll(/<style[^>]*>([\s\S]*?)<\/style>/g)].map((m) => m[1]!).join("\n");
119 for (const link of links) {
120 const href = link.startsWith("http") ? link : `${url}${link}`;
121 css += yield* getBodyWhenReady(href, "{");
122 }
123 expect(css).toContain(".text-3xl");
124 expect(css).toContain("font-bold");
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 );