| 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 (and the Lambda behind it) can serve |
| 11 | // transient 404/5xx responses while it propagates. `Test.getWhenReady` |
| 12 | // fails on that cold-start window and retries until the site serves a |
| 13 | // real response. |
| 14 | const { getWhenReady } = Test; |
| 15 | |
| 16 | class AssetNotReady extends Data.TaggedError("AssetNotReady")<{ |
| 17 | body: string; |
| 18 | }> {} |
| 19 | |
| 20 | // While the asset manifest and CloudFront edge caches are still |
| 21 | // propagating, a 200 body can be stale — the status alone can't |
| 22 | // distinguish "not yet" from "served", so retry until the body matches. |
| 23 | const getBodyWhenReady = (url: string, expected: string) => |
| 24 | Effect.gen(function* () { |
| 25 | const res = yield* getWhenReady(url); |
| 26 | expect(res.status).toBe(200); |
| 27 | const body = yield* res.text; |
| 28 | if (!body.includes(expected)) { |
| 29 | return yield* Effect.fail(new AssetNotReady({ body })); |
| 30 | } |
| 31 | return body; |
| 32 | }).pipe( |
| 33 | Effect.retry({ |
| 34 | while: (error) => error instanceof AssetNotReady, |
| 35 | schedule: Schedule.max([ |
| 36 | Schedule.min([ |
| 37 | Schedule.exponential("500 millis"), |
| 38 | Schedule.spaced("3 seconds"), |
| 39 | ]), |
| 40 | Schedule.recurs(20), |
| 41 | ]), |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 46 | providers: AWS.providers(), |
| 47 | state: AWS.state(), |
| 48 | }); |
| 49 | |
| 50 | // The first deploy runs the full SolidStart (vite/nitro) build AND |
| 51 | // creates a CloudFront distribution (~5-10 minutes), so give the hook |
| 52 | // far more headroom than the default 120s. |
| 53 | const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), { |
| 54 | timeout: 1_200_000, |
| 55 | }); |
| 56 | // Deleting the CloudFront distribution takes several minutes too. |
| 57 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { |
| 58 | timeout: 1_200_000, |
| 59 | }); |
| 60 | |
| 61 | const base = Effect.map(stack, ({ url }) => { |
| 62 | if (!url) throw new Error("expected the site to expose a CloudFront url"); |
| 63 | return String(url).replace(/\/+$/, ""); |
| 64 | }); |
| 65 | |
| 66 | test( |
| 67 | "deploys and exposes a url", |
| 68 | Effect.gen(function* () { |
| 69 | const { url } = yield* stack; |
| 70 | expect(url).toBeString(); |
| 71 | }), |
| 72 | { timeout: 180_000 }, |
| 73 | ); |
| 74 | |
| 75 | test( |
| 76 | "serves the server-rendered home page", |
| 77 | Effect.gen(function* () { |
| 78 | const url = yield* base; |
| 79 | const res = yield* getWhenReady(url); |
| 80 | expect(res.status).toBe(200); |
| 81 | const html = yield* res.text; |
| 82 | // The `GREETING` env value from alchemy.run.ts, read via |
| 83 | // `process.env` in the index route — proves the Lambda rendered it |
| 84 | // at request time. |
| 85 | expect(html).toContain("Hello from SolidStart on AWS!"); |
| 86 | expect(html).toContain("Styled with Tailwind CSS"); |
| 87 | }), |
| 88 | { timeout: 180_000 }, |
| 89 | ); |
| 90 | |
| 91 | test( |
| 92 | "compiles tailwind from vite.config.ts", |
| 93 | Effect.gen(function* () { |
| 94 | const url = yield* base; |
| 95 | // The utility class in the markup proves the page shipped with Tailwind |
| 96 | // classes; wait until the deployed HTML includes it. |
| 97 | const html = yield* getBodyWhenReady(url, "text-3xl"); |
| 98 | |
| 99 | // The compiled rule for the utility must be served from one of the |
| 100 | // linked stylesheets — it only exists if the @tailwindcss/vite plugin |
| 101 | // from the project's own vite.config.ts ran during the build. |
| 102 | // SolidStart emits `href` before `rel`, so match attribute-order- |
| 103 | // agnostically and collect every stylesheet link. |
| 104 | const hrefs = [...html.matchAll(/<link\b[^>]*>/g)] |
| 105 | .map((m) => m[0]) |
| 106 | .filter((tag) => /rel="stylesheet"/.test(tag)) |
| 107 | .map((tag) => tag.match(/href="([^"]+)"/)?.[1]) |
| 108 | .filter((href): href is string => !!href); |
| 109 | expect(hrefs.length).toBeGreaterThan(0); |
| 110 | |
| 111 | const stylesheets = yield* Effect.all( |
| 112 | hrefs.map((href) => |
| 113 | Effect.gen(function* () { |
| 114 | const cssUrl = href.startsWith("http") |
| 115 | ? href |
| 116 | : `${url}${href.startsWith("/") ? "" : "/"}${href}`; |
| 117 | const res = yield* getWhenReady(cssUrl); |
| 118 | expect(res.status).toBe(200); |
| 119 | return yield* res.text; |
| 120 | }), |
| 121 | ), |
| 122 | ); |
| 123 | expect(stylesheets.some((css) => css.includes(".text-3xl"))).toBe(true); |
| 124 | }), |
| 125 | { timeout: 180_000 }, |
| 126 | ); |
| 127 | |
| 128 | test( |
| 129 | "serves a static asset from public/", |
| 130 | Effect.gen(function* () { |
| 131 | const url = yield* base; |
| 132 | const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *"); |
| 133 | expect(body).toContain("User-agent: *"); |
| 134 | }), |
| 135 | { timeout: 180_000 }, |
| 136 | ); |