@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 147 lines 5.29 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Alchemy from "alchemy";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import * as pathe from "pathe";
11
12 // `dev: true` runs local providers behind the RPC sidecar proxy by default,
13 // matching the process topology of the real `alchemy dev` command.
14 const { test } = Test.make({
15 providers: Cloudflare.providers(),
16 dev: true,
17 });
18
19 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
20
21 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
22 status: number;
23 body: string;
24 }> {}
25
26 /**
27 * Fetch a URL, retrying until the worker serves a 200 (fresh dev workers can
28 * briefly 503 while workerd boots; the first browser request may also launch
29 * Chrome). Non-200s carry the body so a persistent fixture error surfaces.
30 */
31 const getReady = (url: string) =>
32 Effect.gen(function* () {
33 const client = yield* HttpClient.HttpClient;
34 return yield* client.get(url).pipe(
35 Effect.flatMap((res) =>
36 res.status === 200
37 ? Effect.succeed(res)
38 : res.text.pipe(
39 Effect.flatMap((body) =>
40 Effect.fail(new WorkerNotReady({ status: res.status, body })),
41 ),
42 ),
43 ),
44 Effect.retry({
45 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
46 schedule: Schedule.max([
47 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
48 Schedule.recurs(10),
49 ]),
50 }),
51 );
52 }).pipe(Effect.orDie);
53
54 const fixtureMain = pathe.resolve(import.meta.dirname, "fixtures/browser-local-worker.ts");
55
56 /**
57 * Under `alchemy dev` the `browser` binding is emulated by cloudflare-runtime:
58 * a real headless Chrome is launched on this machine (downloaded on first use
59 * into the shared wrangler cache) and the Browser Rendering session protocol
60 * is proxied to its CDP endpoint. `@cloudflare/puppeteer` drives it exactly
61 * like the real service — no Cloudflare credentials or preview session are
62 * involved for the binding.
63 */
64 test.provider(
65 "browser binding round-trips against the local Chrome simulator",
66 (stack) =>
67 Effect.gen(function* () {
68 yield* stack.destroy();
69
70 const deployed = yield* stack.deploy(
71 Effect.gen(function* () {
72 const worker = yield* Cloudflare.Worker("browser-local-worker", {
73 main: fixtureMain,
74 compatibility: { flags: ["nodejs_compat"] },
75 env: { BROWSER: Cloudflare.Browser("BROWSER") },
76 });
77 return { url: worker.url.as<string>() };
78 }),
79 );
80
81 // Served from the local dev proxy — proof the worker runs locally.
82 expect(deployed.url).toMatch(/^http:\/\/localhost:\d+$/);
83
84 // Render an inline page through puppeteer over the emulated binding.
85 // The first request may download Chrome, so it gets a long retry rope.
86 const titleRes = yield* getReady(`${deployed.url}/title`);
87 const body = (yield* titleRes.json) as {
88 title: string;
89 heading: string;
90 };
91 expect(body.title).toBe("Local Browser Test");
92 expect(body.heading).toBe("Hello from the local browser");
93
94 // A screenshot of the same page is a real PNG (magic bytes).
95 const shotRes = yield* getReady(`${deployed.url}/screenshot`);
96 const bytes = new Uint8Array(yield* shotRes.arrayBuffer);
97 expect(bytes.length).toBeGreaterThan(1000);
98 expect(Array.from(bytes.slice(0, 4))).toEqual([0x89, 0x50, 0x4e, 0x47]);
99
100 yield* stack.destroy();
101 }).pipe(logLevel),
102 {
103 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
104 timeout: 180_000,
105 },
106 );
107
108 /**
109 * `Alchemy.remote()` opts the binding OUT of local emulation: the
110 * locally-served worker drives the real Browser Rendering service through
111 * the remote-bindings preview session. The live suite
112 * (test/Cloudflare/Browser/Browser.test.ts) covers deployed-binding
113 * behavior; this leg pins the dev-mode remote lowering path.
114 */
115 test.provider(
116 "Alchemy.remote() proxies the binding to the real Browser Rendering service",
117 (stack) =>
118 Effect.gen(function* () {
119 yield* stack.destroy();
120
121 const deployed = yield* stack.deploy(
122 Effect.gen(function* () {
123 const worker = yield* Cloudflare.Worker("browser-remote-worker", {
124 main: fixtureMain,
125 compatibility: { flags: ["nodejs_compat"] },
126 env: {
127 BROWSER: Cloudflare.Browser("BROWSER").pipe(Alchemy.remote()),
128 },
129 });
130 return { url: worker.url.as<string>() };
131 }),
132 );
133
134 // Still served locally — only the binding is remote.
135 expect(deployed.url).toMatch(/^http:\/\/localhost:\d+$/);
136
137 const titleRes = yield* getReady(`${deployed.url}/title`);
138 const body = (yield* titleRes.json) as { title: string };
139 expect(body.title).toBe("Local Browser Test");
140
141 yield* stack.destroy();
142 }).pipe(logLevel),
143 {
144 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
145 timeout: 180_000,
146 },
147 );