|
| 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("never starts the history worker after an interrupted import", () => |
| 102 | + Effect.gen(function* () { |
| 103 | + const history = yield* runCli(["__claude-history", "getSessionMessages", "session"]).pipe( |
| 104 | + Effect.forkChild, |
| 105 | + ); |
| 106 | + yield* Effect.promise(() => hooks.historyImportStarted.promise); |
| 107 | + yield* Fiber.interrupt(history); |
| 108 | + hooks.releaseHistoryImport.resolve(); |
| 109 | + yield* Effect.promise(() => import("../claudeHistoryWorker.ts")); |
| 110 | + assert.equal(hooks.runClaudeHistoryWorker.mock.calls.length, 0); |
| 111 | + |
| 112 | + yield* runCli(["__claude-history", "getSessionMessages", "session"]); |
| 113 | + yield* runCli(["__claude-history", "forkSession", "session", '{"dir":"/repo"}']); |
| 114 | + assert.deepEqual(hooks.runClaudeHistoryWorker.mock.calls, [ |
| 115 | + ["getSessionMessages", "session", undefined], |
| 116 | + ["forkSession", "session", '{"dir":"/repo"}'], |
| 117 | + ]); |
| 118 | + }), |
| 119 | + ); |
| 120 | +}); |
0 commit comments