The best way to control Claude Code, Codex and more from any computer or phone

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