| 1 | import * as Alchemy from "alchemy"; |
| 2 | import * as GCP from "alchemy/GCP"; |
| 3 | import * as Test from "alchemy/Test/Bun"; |
| 4 | import { describe, 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 { spawnSync } from "node:child_process"; |
| 10 | import Stack from "../alchemy.run.ts"; |
| 11 | |
| 12 | // A fresh Cloud Run revision can answer transient 404/5xx responses while |
| 13 | // its URL starts serving. `Test.getWhenReady` retries through that |
| 14 | // cold-start window until the site serves a real response. |
| 15 | const { getWhenReady } = Test; |
| 16 | |
| 17 | class AssetNotReady extends Data.TaggedError("AssetNotReady")<{ |
| 18 | body: string; |
| 19 | }> {} |
| 20 | |
| 21 | // Retry until the body matches — the status alone can't distinguish a |
| 22 | // cold start from a served page. |
| 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: GCP.providers(), |
| 47 | state: Alchemy.localState(), |
| 48 | }); |
| 49 | |
| 50 | // The image is built locally, so the whole suite needs Docker. |
| 51 | const dockerAvailable = |
| 52 | spawnSync("docker", ["info"], { stdio: "ignore", timeout: 15_000 }).status === |
| 53 | 0; |
| 54 | |
| 55 | describe.skipIf(!dockerAvailable)("gcp-website-nextjs", () => { |
| 56 | // The first deploy runs `next build`, builds and pushes the image |
| 57 | // (installing `next` into it), and rolls out Cloud Run. |
| 58 | const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), { |
| 59 | timeout: 1_200_000, |
| 60 | }); |
| 61 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { |
| 62 | timeout: 600_000, |
| 63 | }); |
| 64 | |
| 65 | const base = Effect.map(stack, ({ url }) => { |
| 66 | if (!url) throw new Error("expected the site to expose a Cloud Run url"); |
| 67 | return String(url).replace(/\/+$/, ""); |
| 68 | }); |
| 69 | |
| 70 | test( |
| 71 | "deploys and exposes a Cloud Run url", |
| 72 | Effect.gen(function* () { |
| 73 | const { url } = yield* stack; |
| 74 | expect(String(url)).toMatch(/^https:\/\/.+\.run\.app\/?$/); |
| 75 | }), |
| 76 | { timeout: 180_000 }, |
| 77 | ); |
| 78 | |
| 79 | test( |
| 80 | "serves the server-rendered home page", |
| 81 | Effect.gen(function* () { |
| 82 | const url = yield* base; |
| 83 | const html = yield* getBodyWhenReady(url, "Next.js on Cloud Run"); |
| 84 | // The `GREETING` env value from alchemy.run.ts, read via |
| 85 | // `process.env` in the force-dynamic page — proves the container |
| 86 | // rendered it at request time. |
| 87 | expect(html).toContain("Hello from Next.js on Cloud Run!"); |
| 88 | }), |
| 89 | { timeout: 180_000 }, |
| 90 | ); |
| 91 | |
| 92 | test( |
| 93 | "serves the dynamic API route", |
| 94 | Effect.gen(function* () { |
| 95 | const url = yield* base; |
| 96 | const res = yield* getWhenReady(`${url}/api/hello`); |
| 97 | expect(res.status).toBe(200); |
| 98 | const body = (yield* res.json) as { hello: string }; |
| 99 | expect(body.hello).toBe("world"); |
| 100 | }), |
| 101 | { timeout: 180_000 }, |
| 102 | ); |
| 103 | |
| 104 | test( |
| 105 | "compiles tailwind via postcss", |
| 106 | Effect.gen(function* () { |
| 107 | const url = yield* base; |
| 108 | // The page markup uses Tailwind utilities — proving the project's own |
| 109 | // postcss.config.mjs (@tailwindcss/postcss) ran inside `next build`. |
| 110 | const html = yield* getBodyWhenReady(url, "text-3xl"); |
| 111 | const match = html.match(/\/_next\/static\/[^"']+\.css/); |
| 112 | expect(match).not.toBeNull(); |
| 113 | const css = yield* getBodyWhenReady(`${url}${match![0]}`, ".text-3xl"); |
| 114 | expect(css).toContain(".font-bold"); |
| 115 | }), |
| 116 | { timeout: 180_000 }, |
| 117 | ); |
| 118 | |
| 119 | test( |
| 120 | "serves a static asset from public/", |
| 121 | Effect.gen(function* () { |
| 122 | const url = yield* base; |
| 123 | const body = yield* getBodyWhenReady( |
| 124 | `${url}/robots.txt`, |
| 125 | "User-agent: *", |
| 126 | ); |
| 127 | expect(body).toContain("User-agent: *"); |
| 128 | }), |
| 129 | { timeout: 180_000 }, |
| 130 | ); |
| 131 | }); |