@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 102 lines 3.52 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 AnonAccessWorker from "./fixtures/access-context/anon-worker.ts";
10 import AuthedAccessWorker from "./fixtures/access-context/authed-worker.ts";
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 /** GET a route, retrying until the freshly started workerd serves a 200. */
27 const getJsonReady = (url: string) =>
28 Effect.gen(function* () {
29 const client = yield* HttpClient.HttpClient;
30 const res = yield* client.get(url).pipe(
31 Effect.flatMap((res) =>
32 res.status === 200
33 ? Effect.succeed(res)
34 : res.text.pipe(
35 Effect.flatMap((body) =>
36 Effect.fail(new WorkerNotReady({ status: res.status, body })),
37 ),
38 ),
39 ),
40 Effect.retry({
41 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
42 schedule: Schedule.max([
43 Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
44 Schedule.recurs(10),
45 ]),
46 }),
47 );
48 return (yield* res.json) as Record<string, unknown>;
49 }).pipe(Effect.orDie);
50
51 /**
52 * Pins the `dev.access` simulation of `ctx.access` (the alchemy equivalent
53 * of wrangler's `access.dev` config): a worker with `dev.access` sees a
54 * defined `ctx.access` carrying the configured aud, and `getIdentity()`
55 * resolves the configured identity — while a worker without the config sees
56 * `ctx.access === undefined` (unauthenticated).
57 */
58 test.provider(
59 "dev.access simulates ctx.access locally",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const deployed = yield* stack.deploy(
65 Effect.gen(function* () {
66 const authed = yield* AuthedAccessWorker;
67 const anon = yield* AnonAccessWorker;
68 return { authed, anon };
69 }),
70 );
71
72 // Dev markers: both workers serve from the local dev proxy — no cloud
73 // deploy ran.
74 expect(deployed.authed.url).toMatch(/^http:\/\/localhost:\d+$/);
75 expect(deployed.anon.url).toMatch(/^http:\/\/localhost:\d+$/);
76 // No cloud script exists in dev — the local provider fabricates a
77 // `dev:`-marked identity (mode switches replace the resource, so the
78 // two identities never mix).
79 expect(deployed.authed.workerId).toMatch(/^dev:/);
80
81 const authed = yield* getJsonReady(deployed.authed.url!);
82 expect(authed).toEqual({
83 authenticated: true,
84 aud: "test-aud",
85 email: "dev@alchemy.test",
86 groups: [{ id: "g1", name: "devs" }],
87 });
88
89 const anon = yield* getJsonReady(deployed.anon.url!);
90 expect(anon).toEqual({ authenticated: false });
91
92 yield* stack.destroy();
93 }).pipe(logLevel),
94 {
95 tags: [
96 "provider:cloudflare",
97 "provider:cloudflare:access",
98 "provider:cloudflare:worker",
99 "local",
100 ],
101 },
102 );