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, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - CLI integration uses temporary Node paths. |
| 2 | import * as NodePath from "node:path"; |
| 3 | |
| 4 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 5 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | import { assert, it } from "@effect/vitest"; |
| 7 | import * as NetService from "@t3tools/shared/Net"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Exit from "effect/Exit"; |
| 10 | import * as Fiber from "effect/Fiber"; |
| 11 | import * as FileSystem from "effect/FileSystem"; |
| 12 | import * as Layer from "effect/Layer"; |
| 13 | import { Command } from "effect/cli"; |
| 14 | import { FetchHttpClient } from "effect/http"; |
| 15 | import { vi } from "vite-plus/test"; |
| 16 | |
| 17 | import { cli } from "../binCli.ts"; |
| 18 | import { |
| 19 | makePersistedServerRuntimeState, |
| 20 | persistServerRuntimeState, |
| 21 | } from "../serverRuntimeState.ts"; |
| 22 | |
| 23 | const hooks = vi.hoisted(() => ({ |
| 24 | importsAttempted: [] as string[], |
| 25 | historyImportStarted: Promise.withResolvers<void>(), |
| 26 | releaseHistoryImport: Promise.withResolvers<void>(), |
| 27 | runClaudeHistoryWorker: vi.fn(async () => {}), |
| 28 | })); |
| 29 | |
| 30 | vi.mock("../server.ts", () => { |
| 31 | hooks.importsAttempted.push("server"); |
| 32 | throw new Error("server import failed"); |
| 33 | }); |
| 34 | vi.mock("../orchestration-v2/runtimeLayer.ts", () => { |
| 35 | hooks.importsAttempted.push("runtimeLayer"); |
| 36 | throw new Error("runtime import failed"); |
| 37 | }); |
| 38 | vi.mock("../claudeHistoryWorker.ts", async () => { |
| 39 | hooks.importsAttempted.push("claudeHistoryWorker"); |
| 40 | hooks.historyImportStarted.resolve(); |
| 41 | await hooks.releaseHistoryImport.promise; |
| 42 | return { runClaudeHistoryWorker: hooks.runClaudeHistoryWorker }; |
| 43 | }); |
| 44 | |
| 45 | const layerCliRuntime = Layer.mergeAll(NodeServices.layer, NetService.layer); |
| 46 | const runCli = (args: ReadonlyArray<string>) => |
| 47 | Command.runWith(cli, { version: "0.0.0" })(args).pipe(Effect.provide(layerCliRuntime)); |
| 48 | |
| 49 | it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("CLI deferred imports", (it) => { |
| 50 | it.effect("runs commands that do not need them without loading them", () => |
| 51 | Effect.gen(function* () { |
| 52 | hooks.importsAttempted.length = 0; |
| 53 | for (const args of [["--version"], ["--help"], ["project", "--help"], ["thread", "--help"]]) { |
| 54 | yield* runCli(args); |
| 55 | } |
| 56 | assert.deepEqual(hooks.importsAttempted, []); |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | it.effect("fails a server start on a broken import before resolving config", () => |
| 61 | Effect.gen(function* () { |
| 62 | const fs = yield* FileSystem.FileSystem; |
| 63 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-deferred-server-" }); |
| 64 | const baseDir = NodePath.join(root, "state"); |
| 65 | |
| 66 | for (const command of ["serve", "start"]) { |
| 67 | const exit = yield* runCli([command, "--base-dir", baseDir]).pipe(Effect.exit); |
| 68 | assert.isTrue(Exit.hasDies(exit)); |
| 69 | } |
| 70 | |
| 71 | assert.isFalse(yield* fs.exists(baseDir)); |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | it.effect("keeps server state when the offline project runtime fails to import", () => |
| 76 | Effect.gen(function* () { |
| 77 | const fs = yield* FileSystem.FileSystem; |
| 78 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-deferred-project-" }); |
| 79 | const baseDir = NodePath.join(root, "state"); |
| 80 | const runtimeStatePath = NodePath.join(baseDir, "userdata", "server-runtime.json"); |
| 81 | yield* persistServerRuntimeState({ |
| 82 | path: runtimeStatePath, |
| 83 | // An owner id skips the legacy liveness probe, so only the free lock decides. |
| 84 | state: { |
| 85 | ...(yield* makePersistedServerRuntimeState({ |
| 86 | config: { host: "127.0.0.1", devUrl: undefined }, |
| 87 | port: 3_773, |
| 88 | })), |
| 89 | ownerId: "dead-server", |
| 90 | }, |
| 91 | }); |
| 92 | const unreachable: typeof globalThis.fetch = () => |
| 93 | Promise.reject( |
| 94 | new TypeError("fetch failed", { |
| 95 | cause: Object.assign(new Error("connect ECONNREFUSED"), { code: "ECONNREFUSED" }), |
| 96 | }), |
| 97 | ); |
| 98 | |
| 99 | const exit = yield* runCli(["project", "add", root, "--base-dir", baseDir]).pipe( |
| 100 | Effect.provideService(FetchHttpClient.Fetch, unreachable), |
| 101 | Effect.exit, |
| 102 | ); |
| 103 | |
| 104 | assert.isTrue(Exit.hasDies(exit)); |
| 105 | assert.isTrue(yield* fs.exists(runtimeStatePath)); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | it.effect("forwards live project mutations without importing the offline runtime", () => |
| 110 | Effect.gen(function* () { |
| 111 | const fs = yield* FileSystem.FileSystem; |
| 112 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-deferred-live-project-" }); |
| 113 | const baseDir = NodePath.join(root, "state"); |
| 114 | const workspaceRoot = NodePath.join(root, "workspace"); |
| 115 | yield* fs.makeDirectory(workspaceRoot); |
| 116 | yield* persistServerRuntimeState({ |
| 117 | path: NodePath.join(baseDir, "userdata", "server-runtime.json"), |
| 118 | state: yield* makePersistedServerRuntimeState({ |
| 119 | config: { host: "127.0.0.1", devUrl: undefined }, |
| 120 | port: 3_773, |
| 121 | }), |
| 122 | }); |
| 123 | |
| 124 | hooks.importsAttempted.length = 0; |
| 125 | let mutations = 0; |
| 126 | const fetch: typeof globalThis.fetch = async (_input, init) => { |
| 127 | const isMutation = init?.method === "POST"; |
| 128 | if (isMutation) mutations++; |
| 129 | return Response.json( |
| 130 | isMutation |
| 131 | ? { |
| 132 | id: "live-project", |
| 133 | title: NodePath.basename(workspaceRoot), |
| 134 | workspaceRoot, |
| 135 | defaultModelSelection: null, |
| 136 | scripts: [], |
| 137 | createdAt: "2026-01-01T00:00:00.000Z", |
| 138 | updatedAt: "2026-01-01T00:00:00.000Z", |
| 139 | deletedAt: null, |
| 140 | } |
| 141 | : { projects: [], updatedAt: "2026-01-01T00:00:00.000Z" }, |
| 142 | ); |
| 143 | }; |
| 144 | |
| 145 | const exit = yield* runCli(["project", "add", workspaceRoot, "--base-dir", baseDir]).pipe( |
| 146 | Effect.provideService(FetchHttpClient.Fetch, fetch), |
| 147 | Effect.exit, |
| 148 | ); |
| 149 | |
| 150 | assert.isTrue(Exit.isSuccess(exit)); |
| 151 | assert.equal(mutations, 1); |
| 152 | assert.deepEqual(hooks.importsAttempted, []); |
| 153 | }), |
| 154 | ); |
| 155 | |
| 156 | it.effect("never starts the history worker after an interrupted import", () => |
| 157 | Effect.gen(function* () { |
| 158 | const history = yield* runCli(["__claude-history", "getSessionMessages", "session"]).pipe( |
| 159 | Effect.forkChild, |
| 160 | ); |
| 161 | yield* Effect.promise(() => hooks.historyImportStarted.promise); |
| 162 | yield* Fiber.interrupt(history); |
| 163 | hooks.releaseHistoryImport.resolve(); |
| 164 | yield* Effect.promise(() => import("../claudeHistoryWorker.ts")); |
| 165 | assert.equal(hooks.runClaudeHistoryWorker.mock.calls.length, 0); |
| 166 | |
| 167 | yield* runCli(["__claude-history", "getSessionMessages", "session"]); |
| 168 | yield* runCli(["__claude-history", "forkSession", "session", '{"dir":"/repo"}']); |
| 169 | assert.deepEqual(hooks.runClaudeHistoryWorker.mock.calls, [ |
| 170 | ["getSessionMessages", "session", undefined], |
| 171 | ["forkSession", "session", '{"dir":"/repo"}'], |
| 172 | ]); |
| 173 | }), |
| 174 | ); |
| 175 | }); |