@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 134 lines 4.46 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-astro", () => {
47 // The first deploy runs the Astro build, builds and pushes the image, and
48 // rolls out Cloud Run, so give the hook far more headroom than the default.
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: 1_200_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 url",
63 Effect.gen(function* () {
64 const { url } = yield* stack;
65 expect(url).toBeString();
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 res = yield* getWhenReady(url);
75 expect(res.status).toBe(200);
76 const html = yield* res.text;
77 // The `GREETING` env value from alchemy.run.ts, read via
78 // `process.env` in the page frontmatter — proves the container
79 // rendered it at request time.
80 expect(html).toContain("Hello from Astro on Cloud Run!");
81 expect(html).toContain("server-rendered on Cloud Run");
82 }),
83 { timeout: 180_000 },
84 );
85
86 test(
87 "serves the prerendered about page",
88 Effect.gen(function* () {
89 const url = yield* base;
90 const res = yield* getWhenReady(`${url}/about/`);
91 expect(res.status).toBe(200);
92 }),
93 { timeout: 180_000 },
94 );
95
96 test(
97 "compiles tailwind from astro.config.ts",
98 Effect.gen(function* () {
99 const url = yield* base;
100 // The utility class in the markup proves the page shipped with Tailwind
101 // classes; wait until the deployed HTML includes it.
102 const html = yield* getBodyWhenReady(url, "text-3xl");
103
104 // Astro either links an external compiled stylesheet or inlines small
105 // ones as a <style> block — accept both, but the compiled rule for the
106 // utility must be served either way. That rule only exists if the
107 // @tailwindcss/vite plugin from the project's own astro.config.ts ran.
108 const link = html.match(/<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/);
109 if (link) {
110 const href = link[1]!;
111 const cssUrl = href.startsWith("http")
112 ? href
113 : `${url}${href.startsWith("/") ? "" : "/"}${href}`;
114 const css = yield* getBodyWhenReady(cssUrl, ".text-3xl");
115 expect(css).toContain(".text-3xl");
116 } else {
117 const style = html.match(/<style[^>]*>([\s\S]*?)<\/style>/);
118 expect(style).not.toBeNull();
119 expect(style![1]).toContain(".text-3xl");
120 }
121 }),
122 { timeout: 180_000 },
123 );
124
125 test(
126 "serves a static asset from public/",
127 Effect.gen(function* () {
128 const url = yield* base;
129 const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
130 expect(body).toContain("User-agent: *");
131 }),
132 { timeout: 180_000 },
133 );
134 });