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
21// "not 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 Vite build, so give the hook more headroom
50// 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
61// Resolve an asset href from index.html against the site base.
62const resolve = (baseUrl: string, href: string) =>
63 href.startsWith("http")
64 ? href
65 : `${baseUrl}${href.startsWith("/") ? "" : "/"}${href}`;
66
67test(
68 "deploys and exposes a url",
69 Effect.gen(function* () {
70 const { url } = yield* stack;
71 expect(url).toBeString();
72 }),
73 { timeout: 180_000 },
74);
75
76test(
77 "serves the index HTML",
78 Effect.gen(function* () {
79 const url = yield* base;
80 const html = yield* getBodyWhenReady(url, '<div id="root">');
81 expect(html).toContain("<title>Foldkit on Cloudflare</title>");
82 }),
83 { timeout: 180_000 },
84);
85
86test(
87 "ships the Foldkit app in the client bundle",
88 Effect.gen(function* () {
89 const url = yield* base;
90 // Client-only SPA: the greeting and card view render in the browser, so
91 // assert on the built JS bundle Vite links from index.html.
92 const html = yield* getBodyWhenReady(url, '<script type="module"');
93 const script = html.match(/<script[^>]*type="module"[^>]*src="([^"]+)"/);
94 expect(script).not.toBeNull();
95 const js = yield* getBodyWhenReady(
96 resolve(url, script![1]!),
97 "Hello from Foldkit!",
98 );
99 expect(js).toContain("Hello from Foldkit!");
100 expect(js).toContain("Styled with Tailwind CSS");
101 }),
102 { timeout: 180_000 },
103);
104
105test(
106 "compiles tailwind from vite.config.ts",
107 Effect.gen(function* () {
108 const url = yield* base;
109 // Assert on the compiled stylesheet Vite links from index.html — the
110 // compiled rule for a utility used in src/main.ts only exists if the
111 // @tailwindcss/vite plugin from the project's own vite.config.ts ran.
112 const html = yield* getBodyWhenReady(url, "stylesheet");
113 const link = html.match(
114 /<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/,
115 );
116 expect(link).not.toBeNull();
117 const css = yield* getBodyWhenReady(resolve(url, link![1]!), ".text-3xl");
118 expect(css).toContain(".text-3xl");
119 }),
120 { timeout: 180_000 },
121);