@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 318 lines 11.9 KB
Raw
1 import { expect, it } from "@alchemy.run/test";
2 import * as Core from "@alchemy.run/test/Core";
3 import * as Effect from "effect/Effect";
4 import * as Exit from "effect/Exit";
5 import * as Fiber from "effect/Fiber";
6 import * as Layer from "effect/Layer";
7 import * as Schedule from "effect/Schedule";
8 import * as Scope from "effect/Scope";
9 import { RpcProviderProxy } from "@/Local/RpcProviderProxy";
10 import { Stack, type StackSpec } from "@/Stack";
11 import type { EchoClient } from "./fixtures/rpc-echo.ts";
12
13 const options = { providers: Layer.empty, dev: true, sidecar: true };
14 const COMMAND_LOCAL = import.meta.resolve("../../src/Command/Local.ts", import.meta.url);
15 const ECHO = new URL("./fixtures/rpc-server-entry.ts", import.meta.url).href;
16 const BLOCKED_ECHO = new URL("./fixtures/rpc-blocked-provider.ts", import.meta.url).href;
17 type StackShape = Omit<StackSpec, "output">;
18 type Handle = NonNullable<ReturnType<typeof Core.makeSidecarHandle>>;
19 const dummyStack = (name: string): StackShape => ({
20 name,
21 stage: "test",
22 resources: {},
23 bindings: {},
24 actions: {},
25 });
26
27 const runAsFile = <A, E, R>(handle: Handle, stackName: string, body: Effect.Effect<A, E, R>) =>
28 Effect.suspend(() => {
29 const sharedScope = Scope.makeUnsafe("sequential");
30 return Core.toEffect(
31 body.pipe(Effect.provideService(Stack, dummyStack(stackName))) as Core.TestEffect<A>,
32 options,
33 sharedScope,
34 handle,
35 ).pipe(Effect.ensuring(Scope.close(sharedScope, Exit.void)));
36 });
37
38 const reserve = (profile?: string) =>
39 Effect.acquireRelease(
40 Effect.sync(() => Core.makeSidecarHandle({ ...options, profile })!),
41 (handle) => handle.close,
42 );
43
44 const echo = (handle: Handle, stackName: string) =>
45 runAsFile(
46 handle,
47 stackName,
48 Effect.gen(function* () {
49 const proxy = yield* RpcProviderProxy;
50 return (yield* proxy.get(ECHO, "Test.Echo")) as unknown as EchoClient;
51 }),
52 );
53
54 it.live(
55 "sidecar POST still returns a ws URL after the first file's scope closes",
56 () =>
57 Effect.gen(function* () {
58 const handle = yield* reserve();
59 for (const stackName of ["sidecar-lifetime-a", "sidecar-lifetime-b"]) {
60 yield* runAsFile(
61 handle,
62 stackName,
63 Effect.gen(function* () {
64 const proxy = yield* RpcProviderProxy;
65 expect(yield* proxy.get(COMMAND_LOCAL, "Command.Dev")).toBeDefined();
66 }),
67 );
68 }
69 }).pipe(Effect.scoped),
70 { tags: ["local"], timeout: 60_000, exclusive: true },
71 );
72
73 it.live(
74 "unused reservations and closed handles never start a sidecar",
75 () =>
76 Effect.gen(function* () {
77 const baseline = yield* Core.sidecarDiagnostics;
78 const handle = yield* reserve("sidecar-unused-reservation");
79 const reserved = yield* Core.sidecarDiagnostics;
80 expect(reserved.reservations).toBe(baseline.reservations + 1);
81 expect(reserved.started).toBe(baseline.started);
82 const server = yield* runAsFile(
83 handle,
84 "unused-handle",
85 Effect.gen(function* () {
86 return yield* (yield* RpcProviderProxy).serverDiagnostics;
87 }),
88 );
89 expect(server).toBeUndefined();
90 expect((yield* Core.sidecarDiagnostics).started).toBe(baseline.started);
91 yield* handle.close;
92 yield* handle.close;
93 expect(yield* Core.sidecarDiagnostics).toEqual(baseline);
94 const closed = yield* echo(handle, "closed-handle").pipe(Effect.exit);
95 expect(closed._tag).toBe("Failure");
96 expect(yield* Core.sidecarDiagnostics).toEqual(baseline);
97 }).pipe(Effect.scoped),
98 { tags: ["local"], timeout: 30_000, exclusive: true },
99 );
100
101 it.live(
102 "last session owner finalizes its context while queued files retain the singleton",
103 () =>
104 Effect.gen(function* () {
105 const profile = "sidecar-session-owners";
106 const baseline = yield* Core.sidecarDiagnostics;
107 const first = yield* reserve(profile);
108 const second = yield* reserve(profile);
109 const queued = yield* reserve(profile);
110 const a = yield* echo(first, "shared-owner-stack");
111 const b = yield* echo(second, "shared-owner-stack");
112 const before = yield* a.stats();
113 expect(before.active).toBe(1);
114 expect(before.built).toBe(1);
115 expect((yield* Core.sidecarDiagnostics).sessions).toBe(baseline.sessions + 1);
116 expect((yield* Core.sidecarDiagnostics).owners).toBe(baseline.owners + 2);
117 yield* first.close;
118 expect(yield* b.echo("still owned")).toBe("echo:still owned");
119 expect((yield* b.stats()).finalized).toBe(0);
120 yield* second.close;
121 const released = yield* Core.sidecarDiagnostics;
122 expect(released.sessions).toBe(baseline.sessions);
123 expect(released.owners).toBe(baseline.owners);
124 expect(released.connections).toBe(baseline.connections);
125 expect(released.started).toBe(baseline.started + 1);
126
127 const later = yield* echo(queued, "queued-owner-stack");
128 expect(yield* later.identity()).toEqual({
129 name: "queued-owner-stack",
130 stage: "test",
131 });
132 expect(yield* later.stats()).toEqual({
133 active: 1,
134 built: 2,
135 finalized: 1,
136 });
137 yield* queued.close;
138 expect(yield* Core.sidecarDiagnostics).toEqual(baseline);
139 }).pipe(Effect.scoped),
140 { tags: ["local"], timeout: 60_000, exclusive: true },
141 );
142
143 it.live(
144 "cancelled and rejected lookups preserve another owner's live session",
145 () =>
146 Effect.gen(function* () {
147 const first = yield* reserve();
148 const second = yield* reserve();
149 const name = "shared-lookup-cancellation";
150 const provider = yield* echo(first, name);
151 yield* provider.retainArtifact();
152 const initial = yield* provider.stats();
153 const inspect = runAsFile(
154 first,
155 name,
156 Effect.gen(function* () {
157 return yield* (yield* RpcProviderProxy).serverDiagnostics;
158 }),
159 );
160 const baseline = yield* inspect;
161 expect(baseline).toBeDefined();
162 const pending = yield* runAsFile(
163 second,
164 name,
165 Effect.gen(function* () {
166 return yield* (yield* RpcProviderProxy).get(BLOCKED_ECHO, "Test.Echo");
167 }),
168 ).pipe(Effect.forkChild);
169 const building = yield* inspect.pipe(
170 Effect.repeat({
171 schedule: Schedule.spaced("100 millis"),
172 times: 10,
173 until: (counts) => counts?.building === baseline!.building + 1,
174 }),
175 );
176 expect(building?.building).toBe(baseline!.building + 1);
177 yield* Fiber.interrupt(pending);
178 expect(yield* provider.echo("after cancellation")).toBe("echo:after cancellation");
179 expect(yield* provider.artifactCount()).toBe(1);
180 expect((yield* provider.stats()).finalized).toBe(initial.finalized);
181
182 const rejected = yield* runAsFile(
183 second,
184 name,
185 Effect.gen(function* () {
186 return yield* (yield* RpcProviderProxy).get(ECHO, "Test.Missing");
187 }),
188 ).pipe(Effect.exit);
189 expect(rejected._tag).toBe("Failure");
190 yield* second.close;
191 expect(yield* provider.echo("after rejected lookup")).toBe("echo:after rejected lookup");
192 expect(yield* provider.artifactCount()).toBe(1);
193 expect((yield* provider.stats()).finalized).toBe(initial.finalized);
194 expect((yield* inspect)?.contexts).toBe(baseline!.contexts);
195 yield* first.close;
196 }).pipe(Effect.scoped),
197 { tags: ["local"], timeout: 60_000, exclusive: true },
198 );
199
200 it.live(
201 "a dead sidecar transport reconnects without reviving the old generation",
202 () =>
203 Effect.gen(function* () {
204 const handle = yield* reserve("sidecar-transport-recovery");
205 const name = "transport-recovery";
206 const original = yield* echo(handle, name);
207 const pid = yield* original.processId();
208 yield* Effect.sync(() => process.kill(pid, "SIGKILL"));
209 expect((yield* original.echo("dead transport").pipe(Effect.exit))._tag).toBe("Failure");
210 const recovered = yield* echo(handle, name).pipe(
211 Effect.exit,
212 Effect.repeat({
213 schedule: Schedule.spaced("100 millis"),
214 times: 10,
215 until: Exit.isSuccess,
216 }),
217 );
218 expect(recovered._tag).toBe("Success");
219 if (Exit.isFailure(recovered)) return yield* Effect.failCause(recovered.cause);
220 expect(yield* recovered.value.echo("reconnected")).toBe("echo:reconnected");
221 expect(yield* recovered.value.processId()).not.toBe(pid);
222 yield* handle.close;
223 }).pipe(Effect.scoped),
224 { tags: ["local"], timeout: 60_000, exclusive: true },
225 );
226
227 it.live(
228 "closing a handle interrupts a pending RPC build without reviving its generation",
229 () =>
230 Effect.gen(function* () {
231 const monitor = yield* reserve();
232 const observer = yield* echo(monitor, "pending-build-observer");
233 const initial = yield* observer.stats();
234 const inspect = runAsFile(
235 monitor,
236 "pending-build-observer",
237 Effect.gen(function* () {
238 return yield* (yield* RpcProviderProxy).serverDiagnostics;
239 }),
240 );
241 const baseline = yield* inspect;
242 expect(baseline).toBeDefined();
243 const first = yield* reserve();
244 const pending = yield* runAsFile(
245 first,
246 "pending-build-stack",
247 Effect.gen(function* () {
248 return yield* (yield* RpcProviderProxy).get(BLOCKED_ECHO, "Test.Echo");
249 }),
250 ).pipe(Effect.forkChild);
251 const building = yield* inspect.pipe(
252 Effect.repeat({
253 schedule: Schedule.spaced("100 millis"),
254 times: 10,
255 until: (counts) => counts?.building === baseline!.building + 1,
256 }),
257 );
258 expect(building?.building).toBe(baseline!.building + 1);
259 const acquired = yield* observer.stats().pipe(
260 Effect.repeat({
261 schedule: Schedule.spaced("100 millis"),
262 times: 10,
263 until: (stats) => stats.active === initial.active + 1,
264 }),
265 );
266 expect(acquired.active).toBe(initial.active + 1);
267 yield* first.close;
268 expect((yield* Fiber.await(pending))._tag).toBe("Failure");
269 expect(yield* inspect).toEqual(baseline);
270 expect((yield* observer.stats()).finalized).toBe(initial.finalized + 1);
271
272 const successor = yield* reserve();
273 const next = yield* echo(successor, "pending-build-stack");
274 expect(yield* next.echo("new generation")).toBe("echo:new generation");
275 yield* first.close;
276 expect(yield* next.echo("still active")).toBe("echo:still active");
277 yield* successor.close;
278 expect(yield* inspect).toEqual(baseline);
279 }).pipe(Effect.scoped),
280 { tags: ["local"], timeout: 60_000, exclusive: true },
281 );
282
283 it.live(
284 "released files return session and provider counts to baseline without retaining artifacts",
285 () =>
286 Effect.gen(function* () {
287 const monitor = yield* reserve();
288 const observer = yield* echo(monitor, "session-count-observer");
289 const baseline = yield* Core.sidecarDiagnostics;
290 const inspect = runAsFile(
291 monitor,
292 "session-count-observer",
293 Effect.gen(function* () {
294 return yield* (yield* RpcProviderProxy).serverDiagnostics;
295 }),
296 );
297 const serverBaseline = yield* inspect;
298 const before = yield* observer.stats();
299 for (let index = 0; index < 6; index++) {
300 const handle = yield* reserve();
301 const name = `session-release-${index}`;
302 const provider = yield* echo(handle, name);
303 expect(yield* provider.identity()).toEqual({ name, stage: "test" });
304 yield* provider.retainArtifact();
305 expect(yield* provider.artifactCount()).toBe(1);
306 expect(yield* observer.artifactCount()).toBe(0);
307 yield* handle.close;
308 expect(yield* Core.sidecarDiagnostics).toEqual(baseline);
309 expect(yield* inspect).toEqual(serverBaseline);
310 expect(yield* observer.stats()).toEqual({
311 active: before.active,
312 built: before.built + index + 1,
313 finalized: before.finalized + index + 1,
314 });
315 }
316 }).pipe(Effect.scoped),
317 { tags: ["local"], timeout: 90_000, exclusive: true },
318 );