| 1 | import * as Alchemy from "alchemy"; |
| 2 | import * as Hetzner from "alchemy/Hetzner"; |
| 3 | import * as Test from "alchemy/Test/Bun"; |
| 4 | import { expect } from "bun:test"; |
| 5 | import * as Console from "effect/Console"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import Stack from "../alchemy.run.ts"; |
| 10 | |
| 11 | // A fresh Hetzner deployment (and the Hetzner Server behind it) can serve |
| 12 | // transient 404/5xx responses while it propagates. `Test.getWhenReady` |
| 13 | // fails on that cold-start window and retries until the site serves a |
| 14 | // real response. |
| 15 | const { getWhenReady } = Test; |
| 16 | |
| 17 | class AssetNotReady extends Data.TaggedError("AssetNotReady")<{ |
| 18 | body: string; |
| 19 | }> {} |
| 20 | |
| 21 | // While the asset manifest and Hetzner edge caches are still |
| 22 | // propagating, a 200 body can be stale — the status alone can't |
| 23 | // distinguish "not yet" from "served", so retry until the body matches. |
| 24 | const getBodyWhenReady = (url: string, expected: string) => |
| 25 | Effect.gen(function* () { |
| 26 | const res = yield* getWhenReady(url); |
| 27 | expect(res.status).toBe(200); |
| 28 | const body = yield* res.text; |
| 29 | if (!body.includes(expected)) { |
| 30 | return yield* Effect.fail(new AssetNotReady({ body })); |
| 31 | } |
| 32 | return body; |
| 33 | }).pipe( |
| 34 | Effect.retry({ |
| 35 | while: (error) => error instanceof AssetNotReady, |
| 36 | schedule: Schedule.max([ |
| 37 | Schedule.min([ |
| 38 | Schedule.exponential("500 millis"), |
| 39 | Schedule.spaced("3 seconds"), |
| 40 | ]), |
| 41 | Schedule.recurs(20), |
| 42 | ]), |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | const hasCreds = !!process.env.HCLOUD_TOKEN; |
| 47 | |
| 48 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 49 | providers: Hetzner.providers(), |
| 50 | state: Alchemy.localState(), |
| 51 | profile: process.env.ALCHEMY_PROFILE, |
| 52 | }); |
| 53 | |
| 54 | // The first deploy runs the full Next.js + Node build AND creates a |
| 55 | // Hetzner deployment (~5-10 minutes), so give the hook far more |
| 56 | // headroom than the default 120s. |
| 57 | if (!hasCreds) { |
| 58 | test.skip("skipped without HCLOUD_TOKEN", Effect.void); |
| 59 | } else { |
| 60 | const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), { |
| 61 | timeout: 600_000, |
| 62 | }); |
| 63 | // Deleting the Hetzner deployment takes several minutes too. |
| 64 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { |
| 65 | timeout: 600_000, |
| 66 | }); |
| 67 | |
| 68 | const base = Effect.map(stack, ({ url }) => { |
| 69 | if (!url) throw new Error("expected the site to expose a url"); |
| 70 | return String(url).replace(/\/+$/, ""); |
| 71 | }); |
| 72 | |
| 73 | test( |
| 74 | "deploys and exposes a url", |
| 75 | Effect.gen(function* () { |
| 76 | const { url } = yield* stack; |
| 77 | expect(url).toBeString(); |
| 78 | }), |
| 79 | { timeout: 180_000 }, |
| 80 | ); |
| 81 | |
| 82 | // In-repo ONLY: bun's isolated-install store breaks Node's traced |
| 83 | // node_modules when the Hetzner Server zip flattens the `next` symlink (see the |
| 84 | // README) — Hetzner Server-served routes 500 with a missing @swc/helpers module. |
| 85 | // A standalone copy of this example passes all five tests. Set |
| 86 | // AWS_WEBSITE_NEXTJS_LAMBDA=1 to run them here (e.g. once the |
| 87 | // symlink-preserving bundle lands). |
| 88 | test( |
| 89 | "serves the server-rendered home page", |
| 90 | Effect.gen(function* () { |
| 91 | const url = yield* base; |
| 92 | const res = yield* getWhenReady(url); |
| 93 | expect(res.status).toBe(200); |
| 94 | const html = yield* res.text; |
| 95 | expect(html).toContain("Next.js on Hetzner"); |
| 96 | // The `GREETING` env value from alchemy.run.ts, read via |
| 97 | // `process.env` in the force-dynamic page — proves the Hetzner Server |
| 98 | // rendered it at request time. |
| 99 | expect(html).toContain("Hello from Next.js on Hetzner!"); |
| 100 | }), |
| 101 | { timeout: 180_000 }, |
| 102 | ); |
| 103 | |
| 104 | test( |
| 105 | "serves the dynamic API route", |
| 106 | Effect.gen(function* () { |
| 107 | const url = yield* base; |
| 108 | const res = yield* getWhenReady(`${url}/api/hello`); |
| 109 | expect(res.status).toBe(200); |
| 110 | const body = (yield* res.json) as { hello: string }; |
| 111 | expect(body.hello).toBe("world"); |
| 112 | }), |
| 113 | { timeout: 180_000 }, |
| 114 | ); |
| 115 | |
| 116 | test( |
| 117 | "compiles tailwind via postcss", |
| 118 | Effect.gen(function* () { |
| 119 | const url = yield* base; |
| 120 | // The page markup uses Tailwind utilities — proving the project's own |
| 121 | // postcss.config.mjs (@tailwindcss/postcss) ran inside `next build`. |
| 122 | const html = yield* getBodyWhenReady(url, "text-3xl"); |
| 123 | expect(html).toContain("text-3xl"); |
| 124 | |
| 125 | // Next.js links the compiled stylesheet from under /_next/static/ |
| 126 | // (chunks/*.css as of Next 16). |
| 127 | const match = html.match(/\/_next\/static\/[^"']+\.css/); |
| 128 | expect(match).not.toBeNull(); |
| 129 | |
| 130 | // The stylesheet must contain the compiled Tailwind rule, not just the |
| 131 | // class name in markup. |
| 132 | const css = yield* getBodyWhenReady(`${url}${match![0]}`, ".text-3xl"); |
| 133 | expect(css).toContain(".text-3xl"); |
| 134 | expect(css).toContain(".font-bold"); |
| 135 | }), |
| 136 | { timeout: 180_000 }, |
| 137 | ); |
| 138 | |
| 139 | test( |
| 140 | "serves a static asset from public/", |
| 141 | Effect.gen(function* () { |
| 142 | const url = yield* base; |
| 143 | const body = yield* getBodyWhenReady( |
| 144 | `${url}/robots.txt`, |
| 145 | "User-agent: *", |
| 146 | ); |
| 147 | expect(body).toContain("User-agent: *"); |
| 148 | }), |
| 149 | { timeout: 180_000 }, |
| 150 | ); |
| 151 | } |