@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 171 lines 5.85 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 * as Schedule from "effect/Schedule";
8 import Stack from "./fixtures/dynamic-worker-loader/stack.ts";
9
10 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
11 providers: Cloudflare.providers(),
12 });
13
14 class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
15 status: number;
16 body: string;
17 }> {}
18
19 // Fresh workers.dev URLs serve Cloudflare's placeholder page for a few seconds
20 // after deploy. Retry until the worker (and its dynamically-loaded child)
21 // answer 200, surfacing the body if not so a real failure isn't masked.
22 const readJson = (url: string) =>
23 HttpClient.HttpClient.pipe(
24 Effect.flatMap((client) => client.get(url)),
25 Effect.flatMap((res) =>
26 res.status === 200
27 ? res.json
28 : res.text.pipe(
29 Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))),
30 ),
31 ),
32 Effect.retry({
33 while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
34 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(20)]),
35 }),
36 );
37
38 // The dynamic worker's hit counter lives in module scope, so an isolate id
39 // reused across test attempts (the runner retries) or runs keeps counting.
40 // Mint a fresh id per attempt so `hits` always starts at 1.
41 const freshId = (prefix: string) =>
42 Effect.sync(() => `${prefix}-${crypto.randomUUID().slice(0, 8)}`);
43
44 const stack = beforeAll(deploy(Stack));
45 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
46
47 test(
48 "async worker loads and proxies to a dynamic worker via env binding",
49 Effect.gen(function* () {
50 const { asyncWorkerUrl } = yield* stack;
51 const body = yield* readJson(asyncWorkerUrl);
52 expect(body).toMatchObject({ mode: "async", ok: true });
53 }),
54 {
55 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
56 timeout: 180_000,
57 },
58 );
59
60 test(
61 "effect worker loads and proxies to a dynamic worker via yield* loader",
62 Effect.gen(function* () {
63 const { effectWorkerUrl } = yield* stack;
64 const body = yield* readJson(effectWorkerUrl);
65 expect(body).toMatchObject({ mode: "effect", ok: true });
66 }),
67 {
68 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
69 timeout: 180_000,
70 },
71 );
72
73 test(
74 "effect worker reaches a named dynamic worker via loader.get",
75 Effect.gen(function* () {
76 const { effectWorkerUrl } = yield* stack;
77 const body = yield* readJson(`${effectWorkerUrl}/get`);
78 expect(body).toMatchObject({ mode: "get", ok: true });
79 }),
80 {
81 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
82 timeout: 180_000,
83 },
84 );
85
86 test(
87 "effect worker get() fetch proxies to a cached dynamic worker",
88 Effect.gen(function* () {
89 const { getWorkerUrl } = yield* stack;
90 const id = yield* freshId("stub");
91 const body = yield* readJson(`${getWorkerUrl}/?id=${id}`);
92 expect(body).toMatchObject({ id, hits: 1 });
93 }),
94 {
95 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
96 timeout: 180_000,
97 },
98 );
99
100 test(
101 "effect worker get() reuses the isolate for the same id",
102 Effect.gen(function* () {
103 const { getWorkerUrl } = yield* stack;
104 const id = yield* freshId("reuse");
105 const first = yield* readJson(`${getWorkerUrl}/?id=${id}`);
106 expect(first).toMatchObject({ id, hits: 1 });
107 // `get()` only reuses an isolate within the same colo/process, so two
108 // back-to-back requests can occasionally be served by two fresh
109 // isolates (both reporting `hits: 1`). Any response with `hits >= 2`
110 // proves module state survived across requests, i.e. the isolate was
111 // reused — poll for that instead of asserting the second request exactly.
112 const reused = yield* readJson(`${getWorkerUrl}/?id=${id}`).pipe(
113 Effect.repeat({
114 until: (body) => (body as { hits: number }).hits >= 2,
115 schedule: Schedule.spaced("500 millis"),
116 times: 10,
117 }),
118 );
119 expect(reused).toMatchObject({ id });
120 expect((reused as { hits: number }).hits).toBeGreaterThanOrEqual(2);
121 }),
122 {
123 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
124 timeout: 180_000,
125 },
126 );
127
128 test(
129 "effect worker get() getEntrypoint() fetch works",
130 Effect.gen(function* () {
131 const { getWorkerUrl } = yield* stack;
132 const id = yield* freshId("named");
133 const body = yield* readJson(`${getWorkerUrl}/?id=${id}&entrypoint=1`);
134 expect(body).toMatchObject({ id, hits: 1 });
135 }),
136 {
137 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
138 timeout: 180_000,
139 },
140 );
141
142 // `globalOutbound: null` must reach the runtime as `null` — coercing it to
143 // `undefined` (the old `?.raw` behavior) silently restores default outbound
144 // access for workers meant to be sandboxed (#746). The fixture's dynamic
145 // worker attempts an outbound fetch and reports whether the runtime allowed
146 // it.
147 test(
148 "dynamic worker loaded with globalOutbound: null cannot reach the network",
149 Effect.gen(function* () {
150 const { effectWorkerUrl } = yield* stack;
151 const body = yield* readJson(`${effectWorkerUrl}/outbound/sandboxed`);
152 expect(body).toMatchObject({ outbound: "blocked" });
153 }),
154 {
155 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
156 timeout: 180_000,
157 },
158 );
159
160 test(
161 "dynamic worker loaded without globalOutbound has default network access",
162 Effect.gen(function* () {
163 const { effectWorkerUrl } = yield* stack;
164 const body = yield* readJson(`${effectWorkerUrl}/outbound/open`);
165 expect(body).toMatchObject({ outbound: "allowed", status: 200 });
166 }),
167 {
168 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
169 timeout: 180_000,
170 },
171 );