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

main
ts 98 lines 3.31 KB
Raw
1 import { expect } from "bun:test";
2 import * as Cloudflare from "@alchemy.run/cloudflare";
3 import * as Test from "@alchemy.run/test/Bun";
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 // Fresh `workers.dev` URLs transiently 404 while the route propagates.
11 // `Test.getWhenReady` fails on that cold-start window and retries until the
12 // worker serves a real response.
13 const { getWhenReady } = Test;
14
15 class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
16 body: string;
17 }> {}
18
19 // Retry until the served body actually contains the expected marker — a 200
20 // alone can't distinguish "still propagating" from "served".
21 const getBodyWhenReady = (url: string, expected: string) =>
22 Effect.gen(function* () {
23 const res = yield* getWhenReady(url);
24 expect(res.status).toBe(200);
25 const body = yield* res.text;
26 if (!body.includes(expected)) {
27 return yield* Effect.fail(new AssetNotReady({ body }));
28 }
29 return body;
30 }).pipe(
31 Effect.retry({
32 while: (error) => error instanceof AssetNotReady,
33 schedule: Schedule.max([
34 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
35 Schedule.recurs(20),
36 ]),
37 }),
38 );
39
40 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
41 providers: Cloudflare.providers(),
42 state: Cloudflare.state(),
43 });
44
45 // The first deploy runs the full Octane (vite) build, so give the hook more
46 // headroom than the default 120s.
47 const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
48 timeout: 600_000,
49 });
50 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
51
52 const base = Effect.map(stack, ({ url }) => {
53 if (!url) throw new Error("expected the site to expose a workers.dev url");
54 return url.replace(/\/+$/, "");
55 });
56
57 test(
58 "deploys and exposes a url",
59 Effect.gen(function* () {
60 const { url } = yield* stack;
61 expect(url).toBeString();
62 }),
63 { timeout: 180_000 },
64 );
65
66 test(
67 "serves the server-rendered home page",
68 Effect.gen(function* () {
69 const url = yield* base;
70 const html = yield* getBodyWhenReady(url, "Octane on Cloudflare Workers");
71 expect(html).toContain("Octane on Cloudflare Workers");
72 expect(html).toContain("Server-rendered by Octane, deployed by Alchemy.");
73 }),
74 { timeout: 180_000 },
75 );
76
77 test(
78 "compiles tailwind from vite.config.ts",
79 Effect.gen(function* () {
80 const url = yield* base;
81 // The utility class in the markup proves the page shipped with Tailwind
82 // classes; wait until the deployed HTML includes it.
83 const html = yield* getBodyWhenReady(url, "text-3xl");
84
85 // Octane's production server injects a per-route stylesheet link from the
86 // Vite client manifest. The compiled rule for the utility only exists if
87 // the @tailwindcss/vite plugin from the project's own vite.config.ts ran.
88 const link = html.match(/<link[^>]*rel="stylesheet"[^>]*href="([^"]+)"[^>]*>/);
89 expect(link).not.toBeNull();
90 const href = link![1]!;
91 const cssUrl = href.startsWith("http")
92 ? href
93 : `${url}${href.startsWith("/") ? "" : "/"}${href}`;
94 const css = yield* getBodyWhenReady(cssUrl, ".text-3xl");
95 expect(css).toContain(".text-3xl");
96 }),
97 { timeout: 180_000 },
98 );