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