@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, 04:45 UTC

main
ts 118 lines 4.08 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 can serve
20 // placeholder content with a 200 — the status alone can't distinguish "not
21 // 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 SolidStart (vite/nitro) build, so give the
47 // hook more 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 // SolidStart server-renders the index route in the Cloudflare Worker;
75 // the `GREETING` env value from alchemy.run.ts reaches it via
76 // `process.env` (populated by the `nodejs_compat` flag).
77 expect(html).toContain("Hello from SolidStart on Cloudflare!");
78 expect(html).toContain("Styled with Tailwind CSS");
79 }),
80 { timeout: 180_000 },
81 );
82
83 test(
84 "compiles tailwind from vite.config.ts",
85 Effect.gen(function* () {
86 const url = yield* base;
87 // The utility class in the markup proves the page shipped with Tailwind
88 // classes; wait until the deployed HTML includes it.
89 const html = yield* getBodyWhenReady(url, "text-3xl");
90
91 // The compiled rule for the utility must be served from one of the
92 // linked stylesheets — it only exists if the @tailwindcss/vite plugin
93 // from the project's own vite.config.ts ran during the build.
94 // SolidStart emits `href` before `rel`, so match attribute-order-
95 // agnostically and collect every stylesheet link.
96 const hrefs = [...html.matchAll(/<link\b[^>]*>/g)]
97 .map((m) => m[0])
98 .filter((tag) => /rel="stylesheet"/.test(tag))
99 .map((tag) => tag.match(/href="([^"]+)"/)?.[1])
100 .filter((href): href is string => !!href);
101 expect(hrefs.length).toBeGreaterThan(0);
102
103 const stylesheets = yield* Effect.all(
104 hrefs.map((href) =>
105 Effect.gen(function* () {
106 const cssUrl = href.startsWith("http")
107 ? href
108 : `${url}${href.startsWith("/") ? "" : "/"}${href}`;
109 const res = yield* getWhenReady(cssUrl);
110 expect(res.status).toBe(200);
111 return yield* res.text;
112 }),
113 ),
114 );
115 expect(stylesheets.some((css) => css.includes(".text-3xl"))).toBe(true);
116 }),
117 { timeout: 180_000 },
118 );