Open History to see the last commit
1import * as NodeOS from "node:os";
2
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { describe, expect, it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8import * as HostProcess from "@t3tools/shared/HostProcess";
9
10import {
11 claudeLocalSettingsDirectory,
12 makeClaudeCapabilitiesCacheKey,
13 makeClaudeContinuationGroupKey,
14 makeClaudeEnvironment,
15 mergeClaudeProcessEnvironment,
16 resolveClaudeHomePath,
17} from "./ClaudeHome.ts";
18
19it.layer(NodeServices.layer)("ClaudeHome", (it) => {
20 it.effect("finds the main checkout from a worktree whose name holds a line separator", () =>
21 Effect.gen(function* () {
22 const fileSystem = yield* FileSystem.FileSystem;
23 const path = yield* Path.Path;
24 const repo = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-local-" });
25 const worktree = path.join(repo, ".claude", "worktrees", "feature\u2028tools");
26 const worktreeGitDir = path.join(repo, ".git", "worktrees", "feature\u2028tools");
27 yield* fileSystem.makeDirectory(worktree, { recursive: true });
28 yield* fileSystem.makeDirectory(worktreeGitDir, { recursive: true });
29 yield* fileSystem.writeFileString(path.join(worktree, ".git"), `gitdir: ${worktreeGitDir}\n`);
30 yield* fileSystem.writeFileString(path.join(worktreeGitDir, "commondir"), "../..\n");
31
32 expect(yield* claudeLocalSettingsDirectory(worktree)).toBe(repo);
33 }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
34 );
35
36 describe("Claude home resolution", () => {
37 it.effect("resolves configured Claude HOME and stamps continuation/cache keys with it", () =>
38 Effect.gen(function* () {
39 const path = yield* Path.Path;
40 const homePath = "~/.claude-work";
41 const resolved = path.resolve(NodeOS.homedir(), ".claude-work");
42
43 expect(yield* resolveClaudeHomePath({ homePath })).toBe(resolved);
44 expect((yield* makeClaudeEnvironment({ homePath })).CLAUDE_CONFIG_DIR).toBe(resolved);
45 expect(yield* makeClaudeContinuationGroupKey({ homePath })).toBe(`claude:home:${resolved}`);
46 expect(yield* makeClaudeCapabilitiesCacheKey({ binaryPath: "claude", homePath })).toBe(
47 `claude\0${resolved}\0`,
48 );
49 }),
50 );
51
52 it.effect("uses inherited CLAUDE_CONFIG_DIR when homePath is empty", () =>
53 Effect.gen(function* () {
54 const path = yield* Path.Path;
55 const inherited = path.resolve("/tmp/claude-inherited");
56 const environment = { CLAUDE_CONFIG_DIR: inherited };
57
58 expect(yield* resolveClaudeHomePath({ homePath: "" }, environment)).toBe(inherited);
59 expect(yield* makeClaudeContinuationGroupKey({ homePath: "" }, environment)).toBe(
60 `claude:home:${inherited}`,
61 );
62
63 const explicit = path.resolve(NodeOS.homedir(), ".claude-work");
64 expect(yield* resolveClaudeHomePath({ homePath: "~/.claude-work" }, environment)).toBe(
65 explicit,
66 );
67 }),
68 );
69
70 it.effect("gives a symlinked Claude home the continuation key of its target", () =>
71 Effect.gen(function* () {
72 const fileSystem = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const root = yield* fileSystem.realPath(yield* fileSystem.makeTempDirectoryScoped());
75 const target = path.join(root, ".claude");
76 const link = path.join(root, ".claude-second-account");
77 yield* fileSystem.makeDirectory(target);
78 yield* fileSystem.symlink(target, link);
79
80 const direct = yield* makeClaudeContinuationGroupKey({ homePath: target });
81 expect(yield* makeClaudeContinuationGroupKey({ homePath: link })).toBe(direct);
82 expect(
83 yield* makeClaudeContinuationGroupKey({ homePath: "" }, { CLAUDE_CONFIG_DIR: link }),
84 ).toBe(direct);
85 // The spawned process keeps the literal path, because Claude keys its login by that path.
86 expect((yield* makeClaudeEnvironment({ homePath: link })).CLAUDE_CONFIG_DIR).toBe(link);
87 }).pipe(Effect.scoped),
88 );
89
90 it.effect("separates capability probes by cwd", () =>
91 Effect.gen(function* () {
92 const config = { binaryPath: "claude", homePath: "" };
93 const first = yield* makeClaudeCapabilitiesCacheKey(config, "/repo-a");
94 const second = yield* makeClaudeCapabilitiesCacheKey(config, "/repo-b");
95 expect(first).not.toBe(second);
96 }),
97 );
98 });
99
100 it.effect(
101 "drops the launching Claude session and Electron's run-as-Node flag, and labels processes t3-code unless overridden, without reading settings while turned off",
102 () =>
103 Effect.gen(function* () {
104 const fileSystem = yield* FileSystem.FileSystem;
105 const path = yield* Path.Path;
106 const homePath = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-home-" });
107 const host = {
108 CLAUDE_CODE_ENTRYPOINT: "claude-desktop",
109 CLAUDECODE: "1",
110 CLAUDE_CODE_SESSION_ID: "host-session",
111 CLAUDE_CODE_MESSAGING_SOCKET: "/tmp/host-claude.sock",
112 CLAUDE_EFFORT: "high",
113 ELECTRON_RUN_AS_NODE: "1",
114 KEEP: "1",
115 };
116 const instance = [
117 { name: "CLAUDE_CODE_ENTRYPOINT", value: "instance", sensitive: false },
118 { name: "CLAUDE_EFFORT", value: "low", sensitive: false },
119 ];
120
121 expect(
122 yield* mergeClaudeProcessEnvironment({ homePath, enabled: true }, undefined, host),
123 ).toEqual({
124 CLAUDE_CODE_ENTRYPOINT: "t3-code",
125 KEEP: "1",
126 });
127 expect(
128 yield* mergeClaudeProcessEnvironment({ homePath, enabled: true }, instance, host),
129 ).toEqual({
130 CLAUDE_CODE_ENTRYPOINT: "instance",
131 CLAUDE_EFFORT: "low",
132 KEEP: "1",
133 });
134
135 yield* fileSystem.writeFileString(
136 path.join(homePath, "settings.json"),
137 JSON.stringify({ env: { CLAUDE_CODE_ENTRYPOINT: "custom" } }),
138 );
139 expect(
140 (yield* mergeClaudeProcessEnvironment({ homePath, enabled: true }, instance, host))
141 .CLAUDE_CODE_ENTRYPOINT,
142 ).toBe("custom");
143 expect(
144 (yield* mergeClaudeProcessEnvironment({ homePath, enabled: false }, instance, host))
145 .CLAUDE_CODE_ENTRYPOINT,
146 ).toBe("instance");
147 }),
148 );
149});