1import * as AWS from "alchemy/AWS";
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// A fresh CloudFront distribution can serve transient 404/5xx responses
11// while it propagates. `Test.getWhenReady` fails on that cold-start
12// window and retries until the site serves a real response.
13const { getWhenReady } = Test;
14
15class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
16 body: string;
17}> {}
18
19// While the asset manifest and CloudFront edge caches are still
20// propagating, a 200 body can be stale — the status alone can't
21// distinguish "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: AWS.providers(),
46 state: AWS.state(),
47});
48
49// The first deploy runs the full Vite build AND creates a CloudFront
50// distribution (~5-10 minutes), so give the hook far more headroom than
51// the default 120s.
52const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
53 timeout: 1_200_000,
54});
55// Deleting the CloudFront distribution takes several minutes too.
56afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
57 timeout: 1_200_000,
58});
59
60const base = Effect.map(stack, ({ url }) => {
61 if (!url) throw new Error("expected the site to expose a CloudFront url");
62 return String(url).replace(/\/+$/, "");
63});
64
65// Resolve an asset href from index.html against the site base.
66const resolve = (baseUrl: string, href: string) =>
67 href.startsWith("http")
68 ? href
69 : `${baseUrl}${href.startsWith("/") ? "" : "/"}${href}`;
70
71test(
72 "deploys and exposes a url",
73 Effect.gen(function* () {
74 const { url } = yield* stack;
75 expect(url).toBeString();
76 }),
77 { timeout: 180_000 },
78);
79
80test(
81 "serves the index HTML",
82 Effect.gen(function* () {
83 const url = yield* base;
84 const html = yield* getBodyWhenReady(url, '<div id="root">');
85 expect(html).toContain("<title>Vite on AWS</title>");
86 }),
87 { timeout: 180_000 },
88);
89
90test(
91 "ships the React app in the client bundle",
92 Effect.gen(function* () {
93 const url = yield* base;
94 // Client-only SPA: the greeting and Card render in the browser, so
95 // assert on the built JS bundle Vite links from index.html.
96 const html = yield* getBodyWhenReady(url, '<script type="module"');
97 const script = html.match(/<script[^>]*type="module"[^>]*src="([^"]+)"/);
98 expect(script).not.toBeNull();
99 const js = yield* getBodyWhenReady(
100 resolve(url, script![1]!),
101 "Hello from Vite!",
102 );
103 expect(js).toContain("Hello from Vite!");
104 expect(js).toContain("Styled with Tailwind CSS");
105 }),
106 { timeout: 180_000 },
107);
108
109test(
110 "compiles tailwind from vite.config.ts",
111 Effect.gen(function* () {
112 const url = yield* base;
113 // Assert on the compiled stylesheet Vite links from index.html — the
114 // compiled rule for a utility used in src/main.tsx only exists if the
115 // @tailwindcss/vite plugin from the project's own vite.config.ts ran.
116 const html = yield* getBodyWhenReady(url, "stylesheet");
117 const link = html.match(
118 /<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/,
119 );
120 expect(link).not.toBeNull();
121 const css = yield* getBodyWhenReady(resolve(url, link![1]!), ".text-3xl");
122 expect(css).toContain(".text-3xl");
123 }),
124 { timeout: 180_000 },
125);
126
127test(
128 "answers unmatched paths with the index page (spa default)",
129 Effect.gen(function* () {
130 const url = yield* base;
131 // `spa` defaults on for AWS.Website.Vite: a miss serves the index
132 // page with a 200 so client-side routes deep-link correctly.
133 const html = yield* getBodyWhenReady(
134 `${url}/some/client/route`,
135 '<div id="root">',
136 );
137 expect(html).toContain("<title>Vite on AWS</title>");
138 }),
139 { timeout: 180_000 },
140);