@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 120 lines 4.87 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 Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import * as pathe from "pathe";
10
11 const { test } = Test.make({
12 providers: Cloudflare.providers(),
13 dev: true,
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures/assets-routing");
19 const main = pathe.resolve(fixtureDir, "worker.ts");
20 const assetsDir = pathe.resolve(fixtureDir, "assets");
21
22 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
23 status: number;
24 }> {}
25
26 const getTextReady = (url: string) =>
27 Effect.gen(function* () {
28 const client = yield* HttpClient.HttpClient;
29 const res = yield* client.get(url).pipe(
30 Effect.flatMap((res) =>
31 res.status === 200
32 ? Effect.succeed(res)
33 : Effect.fail(new WorkerNotReady({ status: res.status })),
34 ),
35 Effect.retry({
36 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
37 schedule: Schedule.max([
38 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
39 Schedule.recurs(10),
40 ]),
41 }),
42 );
43 return yield* res.text;
44 }).pipe(Effect.orDie);
45
46 /**
47 * Pins `runWorkerFirst` routing through the local dev pipeline
48 * (LocalWorkerProvider → cloudflare-runtime), matching deployed behavior:
49 *
50 * - omitted → assets-first: a request matching an asset never invokes the
51 * worker (the #1121 dev default fix)
52 * - a glob array → only matching paths go worker-first, shadowing even a
53 * matching asset; everything else stays assets-first (a boolean coercion
54 * of the array anywhere along the pass-through fails one direction or
55 * the other)
56 *
57 * The fixture makes the answering layer observable: the worker echoes
58 * `assets-routing-worker:<path>` for every request, and the assets
59 * directory contains `index.html` plus `api/hello.txt` — a path that both
60 * layers can answer.
61 */
62 test.provider(
63 "dev runWorkerFirst routing: assets-first default and selective glob arrays",
64 (stack) =>
65 Effect.gen(function* () {
66 yield* stack.destroy();
67
68 const deployed = yield* stack.deploy(
69 Effect.gen(function* () {
70 const globs = yield* Cloudflare.Worker("assets-routing-globs", {
71 main,
72 assets: {
73 directory: assetsDir,
74 runWorkerFirst: ["/api/*"],
75 },
76 });
77 const defaults = yield* Cloudflare.Worker("assets-routing-default", {
78 main,
79 assets: { directory: assetsDir },
80 });
81 return { globs, defaults };
82 }),
83 );
84
85 // Both serve from the local dev proxy — proof no cloud call ran.
86 expect(deployed.globs.url).toMatch(/^http:\/\/localhost:\d+$/);
87 expect(deployed.defaults.url).toMatch(/^http:\/\/localhost:\d+$/);
88
89 // ── glob array ────────────────────────────────────────────────────
90 // `/` is outside the globs: the asset answers. If the array were
91 // coerced to blanket worker-first, the worker's echo would win.
92 const globsIndex = yield* getTextReady(`${deployed.globs.url}/`);
93 expect(globsIndex).toContain("alchemy-assets-routing-index");
94
95 // `/api/hello.txt` matches both an asset and the `/api/*` glob: the
96 // worker must answer. If the array were dropped (treated as false),
97 // the asset would serve instead.
98 const globsApi = yield* getTextReady(`${deployed.globs.url}/api/hello.txt`);
99 expect(globsApi).toBe("assets-routing-worker:/api/hello.txt");
100
101 // A path neither layer claims still falls through to the worker.
102 const globsMiss = yield* getTextReady(`${deployed.globs.url}/nothing`);
103 expect(globsMiss).toBe("assets-routing-worker:/nothing");
104
105 // ── omitted (assets-first default) ────────────────────────────────
106 // The same both-layers path now serves the asset: the worker is only
107 // invoked for requests with no matching asset — deployed behavior.
108 const defaultApi = yield* getTextReady(`${deployed.defaults.url}/api/hello.txt`);
109 expect(defaultApi).toContain("alchemy-assets-routing-api-asset");
110
111 const defaultMiss = yield* getTextReady(`${deployed.defaults.url}/nothing`);
112 expect(defaultMiss).toBe("assets-routing-worker:/nothing");
113
114 yield* stack.destroy();
115 }).pipe(logLevel),
116 {
117 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
118 timeout: 120_000,
119 },
120 );