Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ClaudeHome.test.ts

123 lines5.0 KB

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";
8​
9import {
10 makeClaudeCapabilitiesCacheKey,
11 makeClaudeContinuationGroupKey,
12 makeClaudeEnvironment,
13 mergeClaudeProcessEnvironment,
14 resolveClaudeHomePath,
15} from "./ClaudeHome.ts";
16​
17it.layer(NodeServices.layer)("ClaudeHome", (it) => {
18 describe("Claude home resolution", () => {
19 it.effect("resolves configured Claude HOME and stamps continuation/cache keys with it", () =>
20 Effect.gen(function* () {
21 const path = yield* Path.Path;
22 const homePath = "~/.claude-work";
23 const resolved = path.resolve(NodeOS.homedir(), ".claude-work");
24​
25 expect(yield* resolveClaudeHomePath({ homePath })).toBe(resolved);
26 expect((yield* makeClaudeEnvironment({ homePath })).CLAUDE_CONFIG_DIR).toBe(resolved);
27 expect(yield* makeClaudeContinuationGroupKey({ homePath })).toBe(`claude:home:${resolved}`);
28 expect(yield* makeClaudeCapabilitiesCacheKey({ binaryPath: "claude", homePath })).toBe(
29 `claude\0${resolved}\0`,
30 );
31 }),
32 );
33​
34 it.effect("uses inherited CLAUDE_CONFIG_DIR when homePath is empty", () =>
35 Effect.gen(function* () {
36 const path = yield* Path.Path;
37 const inherited = path.resolve("/tmp/claude-inherited");
38 const environment = { CLAUDE_CONFIG_DIR: inherited };
39​
40 expect(yield* resolveClaudeHomePath({ homePath: "" }, environment)).toBe(inherited);
41 expect(yield* makeClaudeContinuationGroupKey({ homePath: "" }, environment)).toBe(
42 `claude:home:${inherited}`,
43 );
44​
45 const explicit = path.resolve(NodeOS.homedir(), ".claude-work");
46 expect(yield* resolveClaudeHomePath({ homePath: "~/.claude-work" }, environment)).toBe(
47 explicit,
48 );
49 }),
50 );
51​
52 it.effect("gives a symlinked Claude home the continuation key of its target", () =>
53 Effect.gen(function* () {
54 const fileSystem = yield* FileSystem.FileSystem;
55 const path = yield* Path.Path;
56 const root = yield* fileSystem.realPath(yield* fileSystem.makeTempDirectoryScoped());
57 const target = path.join(root, ".claude");
58 const link = path.join(root, ".claude-second-account");
59 yield* fileSystem.makeDirectory(target);
60 yield* fileSystem.symlink(target, link);
61​
62 const direct = yield* makeClaudeContinuationGroupKey({ homePath: target });
63 expect(yield* makeClaudeContinuationGroupKey({ homePath: link })).toBe(direct);
64 expect(
65 yield* makeClaudeContinuationGroupKey({ homePath: "" }, { CLAUDE_CONFIG_DIR: link }),
66 ).toBe(direct);
67 // The spawned process keeps the literal path, because Claude keys its login by that path.
68 expect((yield* makeClaudeEnvironment({ homePath: link })).CLAUDE_CONFIG_DIR).toBe(link);
69 }).pipe(Effect.scoped),
70 );
71​
72 it.effect("separates capability probes by cwd", () =>
73 Effect.gen(function* () {
74 const config = { binaryPath: "claude", homePath: "" };
75 const first = yield* makeClaudeCapabilitiesCacheKey(config, "/repo-a");
76 const second = yield* makeClaudeCapabilitiesCacheKey(config, "/repo-b");
77 expect(first).not.toBe(second);
78 }),
79 );
80 });
81​
82 it.effect(
83 "drops the launching Claude session and Electron's run-as-Node flag, and labels processes t3-code unless overridden",
84 () =>
85 Effect.gen(function* () {
86 const fileSystem = yield* FileSystem.FileSystem;
87 const path = yield* Path.Path;
88 const homePath = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-home-" });
89 const host = {
90 CLAUDE_CODE_ENTRYPOINT: "claude-desktop",
91 CLAUDECODE: "1",
92 CLAUDE_CODE_SESSION_ID: "host-session",
93 CLAUDE_CODE_MESSAGING_SOCKET: "/tmp/host-claude.sock",
94 CLAUDE_EFFORT: "high",
95 ELECTRON_RUN_AS_NODE: "1",
96 KEEP: "1",
97 };
98 const instance = [
99 { name: "CLAUDE_CODE_ENTRYPOINT", value: "instance", sensitive: false },
100 { name: "CLAUDE_EFFORT", value: "low", sensitive: false },
101 ];
102​
103 expect(yield* mergeClaudeProcessEnvironment({ homePath }, undefined, host)).toEqual({
104 CLAUDE_CODE_ENTRYPOINT: "t3-code",
105 KEEP: "1",
106 });
107 expect(yield* mergeClaudeProcessEnvironment({ homePath }, instance, host)).toEqual({
108 CLAUDE_CODE_ENTRYPOINT: "instance",
109 CLAUDE_EFFORT: "low",
110 KEEP: "1",
111 });
112​
113 yield* fileSystem.writeFileString(
114 path.join(homePath, "settings.json"),
115 JSON.stringify({ env: { CLAUDE_CODE_ENTRYPOINT: "custom" } }),
116 );
117 expect(
118 (yield* mergeClaudeProcessEnvironment({ homePath }, instance, host))
119 .CLAUDE_CODE_ENTRYPOINT,
120 ).toBe("custom");
121 }),
122 );
123});