The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off -- Exercises the CLI's real stdout and stderr through a subprocess and an HTTP fixture. |
| 2 | import * as NodeHttp from "node:http"; |
| 3 | import * as NodeURL from "node:url"; |
| 4 | |
| 5 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | import { assert, it } from "@effect/vitest"; |
| 7 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
| 12 | import * as SqlClient from "effect/sql/SqlClient"; |
| 13 | |
| 14 | import { migrationManifest, runMigrations } from "./persistence/Migrations.ts"; |
| 15 | import { |
| 16 | makePersistedServerRuntimeState, |
| 17 | persistServerRuntimeState, |
| 18 | } from "./serverRuntimeState.ts"; |
| 19 | |
| 20 | it.live("keeps migration warnings out of JSON thread output", () => |
| 21 | Effect.gen(function* () { |
| 22 | const fs = yield* FileSystem.FileSystem; |
| 23 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-json-" }); |
| 24 | const stateDir = `${root}/userdata`; |
| 25 | yield* fs.makeDirectory(stateDir); |
| 26 | yield* Effect.gen(function* () { |
| 27 | const sql = yield* SqlClient.SqlClient; |
| 28 | yield* runMigrations(); |
| 29 | const futureId = Math.max(...migrationManifest.map(([id]) => id)) + 1; |
| 30 | yield* sql` |
| 31 | INSERT INTO effect_sql_migrations (migration_id, name) |
| 32 | VALUES (${futureId}, 'FutureMigration') |
| 33 | `; |
| 34 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: `${stateDir}/statev2.sqlite` }))); |
| 35 | |
| 36 | const server = yield* Effect.acquireRelease( |
| 37 | Effect.promise(async () => { |
| 38 | const server = NodeHttp.createServer(async (request, response) => { |
| 39 | let body = ""; |
| 40 | for await (const chunk of request) body += String(chunk); |
| 41 | if (request.method === "DELETE") { |
| 42 | response.writeHead(204).end(); |
| 43 | return; |
| 44 | } |
| 45 | const message = JSON.parse(body) as { |
| 46 | id?: string; |
| 47 | method: string; |
| 48 | params?: { name: string }; |
| 49 | }; |
| 50 | if (message.id === undefined) { |
| 51 | response.writeHead(202).end(); |
| 52 | return; |
| 53 | } |
| 54 | const result = |
| 55 | message.method === "initialize" |
| 56 | ? { protocolVersion: "2025-06-18" } |
| 57 | : { |
| 58 | structuredContent: |
| 59 | message.params?.name === "t3_project_list" |
| 60 | ? { |
| 61 | projects: [{ id: "project-1", title: "Test", workspaceRoot: root }], |
| 62 | nextCursor: null, |
| 63 | } |
| 64 | : { |
| 65 | projectId: "project-1", |
| 66 | currentThreadId: null, |
| 67 | threads: [], |
| 68 | nextCursor: null, |
| 69 | total: 0, |
| 70 | }, |
| 71 | }; |
| 72 | response.writeHead(200, { |
| 73 | "content-type": "application/json", |
| 74 | "mcp-session-id": "cli-json-test", |
| 75 | }); |
| 76 | response.end(JSON.stringify({ jsonrpc: "2.0", id: message.id, result })); |
| 77 | }); |
| 78 | await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 79 | return server; |
| 80 | }), |
| 81 | (server) => |
| 82 | Effect.promise(() => new Promise<void>((resolve) => server.close(() => resolve()))), |
| 83 | ); |
| 84 | const address = server.address(); |
| 85 | assert(address !== null && typeof address !== "string"); |
| 86 | yield* persistServerRuntimeState({ |
| 87 | path: `${stateDir}/server-runtime.json`, |
| 88 | state: yield* makePersistedServerRuntimeState({ |
| 89 | config: { host: "127.0.0.1", devUrl: undefined }, |
| 90 | port: address.port, |
| 91 | }), |
| 92 | }); |
| 93 | |
| 94 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 95 | const child = yield* spawner.spawn( |
| 96 | ChildProcess.make( |
| 97 | process.execPath, |
| 98 | [ |
| 99 | NodeURL.fileURLToPath(new URL("./bin.ts", import.meta.url)), |
| 100 | "thread", |
| 101 | "list", |
| 102 | "--project", |
| 103 | "project-1", |
| 104 | "--json", |
| 105 | "--base-dir", |
| 106 | root, |
| 107 | ], |
| 108 | { env: { T3CODE_HOME: root } }, |
| 109 | ), |
| 110 | ); |
| 111 | const { stdout, stderr, exitCode } = yield* Effect.all( |
| 112 | { |
| 113 | stdout: child.stdout.pipe(Stream.decodeText(), Stream.mkString), |
| 114 | stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString), |
| 115 | exitCode: child.exitCode, |
| 116 | }, |
| 117 | { concurrency: "unbounded" }, |
| 118 | ); |
| 119 | assert.strictEqual(Number(exitCode), 0, stderr); |
| 120 | assert.deepStrictEqual(JSON.parse(stdout), []); |
| 121 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 122 | ); |