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

main
ts 128 lines 5.41 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as pathe from "pathe";
8 import { cloneFixture } from "../Utils/Fixture.ts";
9 import { expectUrlContains } from "../Utils/Http.ts";
10 import { expectWorkerExists, waitForWorkerToBeDeleted } from "../Utils/Worker.ts";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "octane-spa-app");
17
18 // Keep the temp clone under the alchemy package (same convention as the
19 // Vite tests) so the project root stays representable relative to cwd —
20 // and so `octane`/`@octanejs/vite-plugin` resolve from the workspace's
21 // hoisted node_modules when the fixture's own tree has none.
22 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
23
24 const fixtureEntries = [
25 ".gitignore",
26 "package.json",
27 "vite.config.ts",
28 "index.html",
29 "src",
30 "public",
31 ];
32
33 const memoInclude = ["src/**", "public/**", "index.html", "vite.config.ts", "package.json"];
34
35 /** The shell marker baked into the fixture's `index.html` head. */
36 const SPA_SHELL_MARKER = "octane-spa-shell";
37
38 // ─────────────────────────────────────────────────────────────────────
39 // Octane SPA via `Website.Vite` — the documented path for route-less,
40 // client-only Octane apps (no `octane.config.ts`): the `octane()`
41 // compiler plugin in the app's own `vite.config.ts` composes with the
42 // injected Cloudflare Vite plugin, the project builds as a plain Vite
43 // SPA (assets only), and `notFoundHandling: "single-page-application"`
44 // makes deep links serve the app shell so the client router/app boots.
45 // Fullstack Octane apps (routes + SSR) are covered by Octane.test.ts.
46 // ─────────────────────────────────────────────────────────────────────
47 describe.concurrent(
48 "OctaneSpa",
49 {
50 tags: [
51 "provider:cloudflare",
52 "provider:cloudflare:website",
53 "provider:cloudflare:worker",
54 "live",
55 ],
56 },
57 () => {
58 test.provider(
59 "Vite: client-only Octane app deploys as a SPA with shell fallback",
60 (stack) =>
61 Effect.gen(function* () {
62 const { accountId } = yield* yield* CloudflareEnvironment;
63
64 yield* stack.destroy();
65
66 const rootDir = yield* cloneFixture(fixtureDir, {
67 prefix: "alchemy-octane-spa-",
68 tempRoot,
69 entries: fixtureEntries,
70 });
71
72 const site = yield* stack.deploy(
73 Effect.gen(function* () {
74 return yield* Cloudflare.Website.Vite("OctaneSpaSite", {
75 rootDir,
76 workersDev: { enabled: true, previewsEnabled: true },
77 memo: { include: memoInclude },
78 assets: { notFoundHandling: "single-page-application" },
79 });
80 }),
81 );
82
83 expect(site.url).toBeDefined();
84 expect(site.hash?.input).toBeDefined();
85 yield* expectWorkerExists(site.workerName, accountId);
86
87 // (a) `/` serves the app shell (identified by the head marker).
88 // The page markup renders only in the browser, so its marker must
89 // be absent from the raw HTML.
90 const shellBody = yield* expectUrlContains(`${site.url!}/`, SPA_SHELL_MARKER, {
91 timeout: "120 seconds",
92 label: "SPA shell at /",
93 });
94 expect(shellBody).not.toContain("OCTANE_SPA_PAGE_MARKER");
95
96 // (b) A hard GET to an unregistered deep route serves the shell
97 // with a 200 (`expectUrlContains` requires `res.ok`) — the SPA
98 // fallback that lets the client-side app boot on any URL.
99 const deepBody = yield* expectUrlContains(`${site.url!}/widgets/42`, SPA_SHELL_MARKER, {
100 timeout: "60 seconds",
101 label: "deep link serves SPA shell",
102 });
103 expect(deepBody).not.toContain("OCTANE_SPA_PAGE_MARKER");
104
105 // (c) The hydrating client bundle serves and carries the content
106 // the browser renders: extract the hashed module the shell loads
107 // and assert the page marker is inside it.
108 const scriptSrc = shellBody.match(/\/assets\/[^"']+\.js/)?.[0];
109 expect(scriptSrc).toBeDefined();
110 yield* expectUrlContains(`${site.url!}${scriptSrc!}`, "OCTANE_SPA_PAGE_MARKER", {
111 timeout: "60 seconds",
112 label: "client bundle carries the page content",
113 });
114
115 // (d) A `public/` asset serves its own bytes.
116 const robots = yield* expectUrlContains(`${site.url!}/robots.txt`, "User-agent", {
117 timeout: "60 seconds",
118 label: "static asset with SPA handling",
119 });
120 expect(robots).not.toContain(SPA_SHELL_MARKER);
121
122 yield* stack.destroy();
123 yield* waitForWorkerToBeDeleted(site.workerName, accountId);
124 }).pipe(logLevel),
125 { timeout: 360_000 },
126 );
127 },
128 );