1import * as Cloudflare from "alchemy/Cloudflare";
2import * as Test from "alchemy/Test/Bun";
3import { expect } from "bun:test";
4import * as Console from "effect/Console";
5import * as Data from "effect/Data";
6import * as Effect from "effect/Effect";
7import * as Schedule from "effect/Schedule";
8import 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.
13const { getWhenReady } = Test;
14
15class 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.
22const 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([
36 Schedule.exponential("500 millis"),
37 Schedule.spaced("3 seconds"),
38 ]),
39 Schedule.recurs(20),
40 ]),
41 }),
42 );
43
44const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
45 providers: Cloudflare.providers(),
46 state: Cloudflare.state(),
47});
48
49// The first deploy runs the full SolidStart (vite/nitro) build, so give the
50// hook more headroom than the default 120s.
51const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
52 timeout: 600_000,
53});
54afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
55
56const base = Effect.map(stack, ({ url }) => {
57 if (!url) throw new Error("expected the site to expose a workers.dev url");
58 return url.replace(/\/+$/, "");
59});
60
61test(
62 "deploys and exposes a url",
63 Effect.gen(function* () {
64 const { url } = yield* stack;
65 expect(url).toBeString();
66 }),
67 { timeout: 180_000 },
68);
69
70test(
71 "serves the server-rendered home page",
72 Effect.gen(function* () {
73 const url = yield* base;
74 const res = yield* getWhenReady(url);
75 expect(res.status).toBe(200);
76 const html = yield* res.text;
77 // SolidStart server-renders the index route in the Cloudflare Worker;
78 // the `GREETING` env value from alchemy.run.ts reaches it via
79 // `process.env` (populated by the `nodejs_compat` flag).
80 expect(html).toContain("Hello from SolidStart on Cloudflare!");
81 expect(html).toContain("Styled with Tailwind CSS");
82 }),
83 { timeout: 180_000 },
84);
85
86test(
87 "compiles tailwind from vite.config.ts",
88 Effect.gen(function* () {
89 const url = yield* base;
90 // The utility class in the markup proves the page shipped with Tailwind
91 // classes; wait until the deployed HTML includes it.
92 const html = yield* getBodyWhenReady(url, "text-3xl");
93
94 // The compiled rule for the utility must be served from one of the
95 // linked stylesheets — it only exists if the @tailwindcss/vite plugin
96 // from the project's own vite.config.ts ran during the build.
97 // SolidStart emits `href` before `rel`, so match attribute-order-
98 // agnostically and collect every stylesheet link.
99 const hrefs = [...html.matchAll(/<link\b[^>]*>/g)]
100 .map((m) => m[0])
101 .filter((tag) => /rel="stylesheet"/.test(tag))
102 .map((tag) => tag.match(/href="([^"]+)"/)?.[1])
103 .filter((href): href is string => !!href);
104 expect(hrefs.length).toBeGreaterThan(0);
105
106 const stylesheets = yield* Effect.all(
107 hrefs.map((href) =>
108 Effect.gen(function* () {
109 const cssUrl = href.startsWith("http")
110 ? href
111 : `${url}${href.startsWith("/") ? "" : "/"}${href}`;
112 const res = yield* getWhenReady(cssUrl);
113 expect(res.status).toBe(200);
114 return yield* res.text;
115 }),
116 ),
117 );
118 expect(stylesheets.some((css) => css.includes(".text-3xl"))).toBe(true);
119 }),
120 { timeout: 180_000 },
121);