@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 86 lines 2.94 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-only");
19
20 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
21 status: number;
22 }> {}
23
24 const getTextReady = (url: string) =>
25 Effect.gen(function* () {
26 const client = yield* HttpClient.HttpClient;
27 const res = yield* client.get(url).pipe(
28 Effect.flatMap((res) =>
29 res.status === 200
30 ? Effect.succeed(res)
31 : Effect.fail(new WorkerNotReady({ status: res.status })),
32 ),
33 Effect.retry({
34 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
35 schedule: Schedule.max([
36 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
37 Schedule.recurs(10),
38 ]),
39 }),
40 );
41 return yield* res.text;
42 }).pipe(Effect.orDie);
43
44 /**
45 * An assets-only Worker (no `main`, no `script`) under `alchemy dev`: the
46 * local provider serves a stub that delegates every request to the ASSETS
47 * binding, so static files and the asset layer's 404 handling work without
48 * any entry module.
49 */
50 test.provider(
51 "assets-only worker serves static files locally",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const deployed = yield* stack.deploy(
57 Effect.gen(function* () {
58 const worker = yield* Cloudflare.Worker("assets-only-local", {
59 assets: { directory: fixtureDir, notFoundHandling: "404-page" },
60 });
61 return { worker };
62 }),
63 );
64
65 // The local provider serves from the dev proxy — proof no cloud call
66 // ran.
67 expect(deployed.worker.url).toMatch(/^http:\/\/localhost:\d+$/);
68
69 const body = yield* getTextReady(`${deployed.worker.url}`);
70 expect(body).toContain("alchemy-assets-only-index");
71
72 // The asset layer's custom 404 page is applied by the assets worker
73 // itself — the stub only delegates.
74 const client = yield* HttpClient.HttpClient;
75 const missing = yield* client.get(`${deployed.worker.url}/does-not-exist`).pipe(Effect.orDie);
76 expect(missing.status).toBe(404);
77 const missingBody = yield* missing.text.pipe(Effect.orDie);
78 expect(missingBody).toContain("alchemy-assets-only-404");
79
80 yield* stack.destroy();
81 }).pipe(logLevel),
82 {
83 tags: ["provider:cloudflare", "provider:cloudflare:worker", "local"],
84 timeout: 120_000,
85 },
86 );