@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 199 lines 7.12 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 Alchemy from "alchemy";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as Path from "effect/Path";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Schedule from "effect/Schedule";
12 import * as pathe from "pathe";
13 import { cloneFixture } from "../Utils/Fixture.ts";
14 import { expectUrlContains } from "../Utils/Http.ts";
15 import { waitForWorkerToBeDeleted } from "../Utils/Worker.ts";
16
17 const { test } = Test.make({ providers: Cloudflare.providers(), dev: true });
18
19 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
20
21 const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "octane-app");
22 const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
23
24 const fixtureEntries = [
25 ".gitignore",
26 "package.json",
27 "octane.config.ts",
28 "vite.config.ts",
29 "index.html",
30 "src",
31 "public",
32 ];
33
34 const memoInclude = [
35 "src/**",
36 "public/**",
37 "index.html",
38 "octane.config.ts",
39 "vite.config.ts",
40 "package.json",
41 ];
42
43 // Tests are independent (per-test scratch stacks, private fixture clones),
44 // so run them concurrently; suites are sequential by default.
45 describe.concurrent(
46 "Octane dev",
47 { tags: ["provider:cloudflare", "provider:cloudflare:website"] },
48 () => {
49 test.provider(
50 "Octane dev: local dev server renders SSR and picks up edits",
51 (stack) =>
52 Effect.gen(function* () {
53 const fs = yield* FileSystem.FileSystem;
54 const path = yield* Path.Path;
55
56 yield* stack.destroy();
57
58 const rootDir = yield* cloneFixture(fixtureDir, {
59 prefix: "alchemy-octane-dev-",
60 tempRoot,
61 entries: fixtureEntries,
62 });
63
64 const site = yield* stack.deploy(
65 Cloudflare.Website.Octane("OctaneLocal", {
66 rootDir,
67 dev: { port: 0 },
68 memo: { include: memoInclude },
69 }),
70 );
71
72 // Local identity: the url points at the alchemy dev proxy — no
73 // cloud Worker exists.
74 expect(site.url).toBeDefined();
75 expect(site.url).toMatch(/^http:\/\/localhost:\d+/);
76
77 // SSR page rendered by Octane's dev middleware behind the proxy.
78 yield* expectUrlContains(`${site.url!}/`, "OCTANE_PAGE_MARKER", {
79 timeout: "120 seconds",
80 label: "octane dev SSR home page",
81 });
82
83 // Server route through the same middleware. Octane's dev server
84 // supplies no `context.platform` (upstream limitation), so the
85 // handler degrades to a null binding — the route itself must work.
86 const hello = yield* fetchJsonReady<{
87 marker: string;
88 binding: string | null;
89 hasWaitUntil: boolean;
90 }>(`${site.url!}/api/hello`);
91 expect(hello.marker).toBe("api-route-ok");
92 expect(hello.binding).toBeNull();
93 expect(hello.hasWaitUntil).toBe(false);
94
95 // ── HMR: edit the page in place. The stack is NOT re-applied — the
96 // dev server must pick the change up and serve it through the same
97 // alchemy proxy ──────────────────────────────────────────────────────
98 const appPath = path.join(rootDir, "src/App.tsx");
99 const app = yield* fs.readFileString(appPath);
100 yield* fs.writeFileString(
101 appPath,
102 app.replace("OCTANE_PAGE_MARKER", "OCTANE_PAGE_MARKER_V2"),
103 );
104
105 yield* expectUrlContains(`${site.url!}/`, "OCTANE_PAGE_MARKER_V2", {
106 timeout: "90 seconds",
107 label: "octane dev SSR page after edit",
108 });
109
110 yield* stack.destroy();
111 }).pipe(logLevel),
112 { tags: ["local"], timeout: 300_000 },
113 );
114
115 /**
116 * `Alchemy.remote()` opts the whole site OUT of local emulation: even under
117 * `alchemy dev` the Worker builds and deploys to real Cloudflare (pinning
118 * the stamped-mode delete path on destroy). This is the supported way to
119 * exercise bindings during dev while Octane's dev middleware cannot serve
120 * `context.platform`.
121 */
122 test.provider(
123 "Octane dev: Alchemy.remote() deploys the real Worker",
124 (stack) =>
125 Effect.gen(function* () {
126 const { accountId } = yield* yield* CloudflareEnvironment;
127
128 yield* stack.destroy();
129
130 const rootDir = yield* cloneFixture(fixtureDir, {
131 prefix: "alchemy-octane-dev-remote-",
132 tempRoot,
133 entries: fixtureEntries,
134 });
135
136 const bindingMarker = "octane-dev-remote-marker";
137
138 const site = yield* stack.deploy(
139 Cloudflare.Website.Octane("OctaneRemoteSite", {
140 rootDir,
141 workersDev: { enabled: true, previewsEnabled: true },
142 memo: { include: memoInclude },
143 env: {
144 TEST_BINDING: bindingMarker,
145 },
146 }).pipe(Alchemy.remote()),
147 );
148
149 // A real deployment: workers.dev URL, not the local proxy.
150 expect(site.url).toBeDefined();
151 expect(site.url).not.toMatch(/^http:\/\/localhost/);
152
153 yield* expectUrlContains(`${site.url!}/`, "OCTANE_PAGE_MARKER", {
154 timeout: "120 seconds",
155 label: "remote() SSR home page in dev mode",
156 });
157
158 // The deployed worker carries the real binding through
159 // `context.platform.env`.
160 const hello = yield* fetchJsonReady<{
161 marker: string;
162 binding: string | null;
163 hasWaitUntil: boolean;
164 }>(`${site.url!}/api/hello`);
165 expect(hello.marker).toBe("api-route-ok");
166 expect(hello.binding).toBe(bindingMarker);
167 expect(hello.hasWaitUntil).toBe(true);
168
169 yield* stack.destroy();
170
171 // The stamped-live row deletes the real Worker even in a dev run.
172 yield* waitForWorkerToBeDeleted(site.workerName, accountId);
173 }).pipe(logLevel),
174 { tags: ["provider:cloudflare:worker", "live"], timeout: 600_000 },
175 );
176 },
177 );
178
179 /** GET `url` until it answers 200 with a JSON body. */
180 const fetchJsonReady = <T>(url: string) =>
181 Effect.gen(function* () {
182 const client = yield* HttpClient.HttpClient;
183 return yield* client.get(url).pipe(
184 Effect.flatMap((res) =>
185 res.status === 200
186 ? Effect.flatMap(res.text, (body) =>
187 Effect.try({
188 try: () => JSON.parse(body) as T,
189 catch: () => new Error(`non-json body: ${body}`),
190 }),
191 )
192 : Effect.fail(new Error(`Worker not ready: ${res.status}`)),
193 ),
194 Effect.retry({
195 schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
196 times: 10,
197 }),
198 );
199 });