| 1 | import * as AWS from "alchemy/AWS"; |
| 2 | import * as Test from "alchemy/Test/Bun"; |
| 3 | import { expect } from "bun:test"; |
| 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 | // 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. |
| 13 | const { getWhenReady } = Test; |
| 14 | |
| 15 | class 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. |
| 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([ |
| 36 | Schedule.exponential("500 millis"), |
| 37 | Schedule.spaced("3 seconds"), |
| 38 | ]), |
| 39 | Schedule.recurs(20), |
| 40 | ]), |
| 41 | }), |
| 42 | ); |
| 43 | |
| 44 | const { 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. |
| 52 | const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), { |
| 53 | timeout: 1_200_000, |
| 54 | }); |
| 55 | // Deleting the CloudFront distribution takes several minutes too. |
| 56 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { |
| 57 | timeout: 1_200_000, |
| 58 | }); |
| 59 | |
| 60 | const 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. |
| 66 | const resolve = (baseUrl: string, href: string) => |
| 67 | href.startsWith("http") |
| 68 | ? href |
| 69 | : `${baseUrl}${href.startsWith("/") ? "" : "/"}${href}`; |
| 70 | |
| 71 | test( |
| 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 | |
| 80 | test( |
| 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>Foldkit on AWS</title>"); |
| 86 | }), |
| 87 | { timeout: 180_000 }, |
| 88 | ); |
| 89 | |
| 90 | test( |
| 91 | "ships the Foldkit app in the client bundle", |
| 92 | Effect.gen(function* () { |
| 93 | const url = yield* base; |
| 94 | // Client-only SPA: the greeting and card view 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 Foldkit!", |
| 102 | ); |
| 103 | expect(js).toContain("Hello from Foldkit!"); |
| 104 | expect(js).toContain("Styled with Tailwind CSS"); |
| 105 | }), |
| 106 | { timeout: 180_000 }, |
| 107 | ); |
| 108 | |
| 109 | test( |
| 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.ts 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 | ); |