@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 106 lines 3.46 KB
Raw
1 // Test fixture: boots an RpcSpawner, starts the dev sidecar, reconciles a
2 // real DevServer through its Command.Dev provider group, prints the
3 // dev-server pid, then idles until the test harness kills this parent process.
4 // Relative imports (not `@/` alias) so this file runs under both Bun and Node
5 // without a paths-aware loader.
6 import { newWebSocketRpcSession } from "capnweb";
7 import * as Deferred from "effect/Deferred";
8 import * as Effect from "effect/Effect";
9 import * as FileSystem from "effect/FileSystem";
10 import * as FetchHttpClient from "effect/http/FetchHttpClient";
11 import * as HttpBody from "effect/http/HttpBody";
12 import * as HttpClient from "effect/http/HttpClient";
13 import * as Layer from "effect/Layer";
14 import * as Schedule from "effect/Schedule";
15 import { unwrapRpcHandlers } from "../../../src/Local/RpcSerialization.ts";
16 import type { RpcProxyApi } from "../../../src/Local/RpcServer.ts";
17 import {
18 encodeSessionEnvironment,
19 SESSION_ENV_PARAM,
20 } from "../../../src/Local/RpcServerEnvironment.ts";
21 import { layerServer, RpcSpawner } from "../../../src/Local/RpcSpawner.ts";
22 import { PlatformServices } from "../../../src/Util/PlatformServices.ts";
23
24 const sidecarEntry = process.argv[2];
25 const providersUrl = process.argv[3];
26 const command = process.argv[4];
27 const pidFile = process.argv[5];
28
29 if (!sidecarEntry || !providersUrl || !command || !pidFile) {
30 console.error(
31 "usage: rpc-spawner-devserver-parent.ts <sidecar-entry-url> <providers-url> <command> <pid-file>",
32 );
33 process.exit(2);
34 }
35
36 const program = Effect.gen(function* () {
37 const sp = yield* RpcSpawner;
38 const http = yield* HttpClient.HttpClient;
39 const wsUrl = yield* http
40 .post(sp.url, {
41 body: yield* HttpBody.json({ serverEntryUrl: sidecarEntry }),
42 })
43 .pipe(Effect.flatMap((res) => res.text));
44
45 // Sessions carry the stack environment (children are shared across stacks).
46 const sessionUrl = new URL(wsUrl);
47 sessionUrl.searchParams.set(
48 SESSION_ENV_PARAM,
49 encodeSessionEnvironment({
50 alchemyContext: {
51 dotAlchemy: "/tmp/.alchemy",
52 updateStateStore: false,
53 dev: true,
54 adopt: false,
55 },
56 stack: { name: "test", stage: "dev" },
57 }),
58 );
59 const session = newWebSocketRpcSession<RpcProxyApi>(sessionUrl.toString());
60 const wrapped = yield* Effect.promise(
61 () =>
62 session.getProvider("Command.Dev", providersUrl) as ReturnType<RpcProxyApi["getProvider"]>,
63 );
64 const provider = unwrapRpcHandlers(wrapped, []);
65
66 yield* provider.reconcile({
67 id: "Dev",
68 fqn: "Dev",
69 instanceId: "Dev",
70 news: {
71 command,
72 env: { PID_FILE: pidFile, MARKER: "rpc-devserver" },
73 },
74 olds: undefined,
75 output: undefined,
76 session: {} as never,
77 bindings: [],
78 });
79
80 const pid = yield* Effect.gen(function* () {
81 const fs = yield* FileSystem.FileSystem;
82 const content = yield* fs.readFileString(pidFile);
83 return (JSON.parse(content) as { pid: number }).pid;
84 }).pipe(Effect.retry({ schedule: Schedule.spaced("100 millis"), times: 50 }));
85
86 console.log(`PARENT_PID=${process.pid}\n`);
87 console.log(`DEVSERVER_PID=${pid}\n`);
88
89 const stop = yield* Deferred.make<void>();
90 yield* Deferred.await(stop);
91 });
92
93 program
94 .pipe(
95 Effect.provide([
96 Layer.provide(layerServer({ profile: undefined, envFile: undefined }), PlatformServices),
97 PlatformServices,
98 FetchHttpClient.layer,
99 ]),
100 Effect.scoped,
101 Effect.runPromise,
102 )
103 .catch((e) => {
104 console.error(e);
105 process.exit(1);
106 });