Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import {
4 DEFAULT_SERVER_SETTINGS,
5 ProviderDriverKind,
6 type ProviderInstanceConfig,
7 ProviderInstanceId,
8 type ServerSettings,
9} from "@t3tools/contracts";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Path from "effect/Path";
13
14import { resolveUsageTranscriptDirs } from "./usageTranscriptDirs.ts";
15
16const CLAUDE = ProviderDriverKind.make("claudeAgent");
17const CODEX = ProviderDriverKind.make("codex");
18
19/**
20 * Legacy homes are pinned to paths that do not exist so the Claude nested
21 * `.claude/projects` probe resolves the same way on every machine.
22 */
23const makeSettings = (
24 root: string,
25 providerInstances: Record<string, ProviderInstanceConfig> = {},
26): ServerSettings => ({
27 ...DEFAULT_SERVER_SETTINGS,
28 providers: {
29 ...DEFAULT_SERVER_SETTINGS.providers,
30 claudeAgent: { ...DEFAULT_SERVER_SETTINGS.providers.claudeAgent, homePath: `${root}/claude` },
31 codex: { ...DEFAULT_SERVER_SETTINGS.providers.codex, homePath: `${root}/codex` },
32 },
33 providerInstances: providerInstances as ServerSettings["providerInstances"],
34});
35
36it.layer(NodeServices.layer)("usageTranscriptDirs", (it) => {
37 describe("resolveUsageTranscriptDirs", () => {
38 it.effect("scans the legacy Claude and Codex homes when no instances are configured", () =>
39 Effect.gen(function* () {
40 const path = yield* Path.Path;
41 const fileSystem = yield* FileSystem.FileSystem;
42 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
43
44 const dirs = yield* resolveUsageTranscriptDirs(makeSettings(root));
45
46 expect(dirs).toEqual([
47 { provider: "codex", dir: path.resolve(root, "codex", "sessions") },
48 { provider: "claude", dir: path.resolve(root, "claude", "projects") },
49 ]);
50 }),
51 );
52
53 it.effect("adds a transcript directory for every extra provider instance", () =>
54 Effect.gen(function* () {
55 const path = yield* Path.Path;
56 const fileSystem = yield* FileSystem.FileSystem;
57 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
58
59 const dirs = yield* resolveUsageTranscriptDirs(
60 makeSettings(root, {
61 [ProviderInstanceId.make("claude_work")]: {
62 driver: CLAUDE,
63 config: { homePath: `${root}/claude-work` },
64 },
65 [ProviderInstanceId.make("codex_personal")]: {
66 driver: CODEX,
67 config: { homePath: `${root}/codex-personal` },
68 },
69 }),
70 );
71
72 expect(dirs).toEqual([
73 { provider: "claude", dir: path.resolve(root, "claude-work", "projects") },
74 { provider: "codex", dir: path.resolve(root, "codex-personal", "sessions") },
75 { provider: "codex", dir: path.resolve(root, "codex", "sessions") },
76 { provider: "claude", dir: path.resolve(root, "claude", "projects") },
77 ]);
78 }),
79 );
80
81 it.effect("prefers a nested .claude/projects directory when the home has one", () =>
82 Effect.gen(function* () {
83 const path = yield* Path.Path;
84 const fileSystem = yield* FileSystem.FileSystem;
85 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
86 const nested = path.join(root, "claude", ".claude", "projects");
87 yield* fileSystem.makeDirectory(nested, { recursive: true });
88
89 const dirs = yield* resolveUsageTranscriptDirs(makeSettings(root));
90
91 expect(dirs.find((entry) => entry.provider === "claude")?.dir).toBe(path.resolve(nested));
92 }),
93 );
94
95 it.effect("collapses instances that share a home so nothing is counted twice", () =>
96 Effect.gen(function* () {
97 const fileSystem = yield* FileSystem.FileSystem;
98 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
99
100 const dirs = yield* resolveUsageTranscriptDirs(
101 makeSettings(root, {
102 [ProviderInstanceId.make("claude_chrome")]: {
103 driver: CLAUDE,
104 config: { homePath: `${root}/claude`, launchArgs: "--chrome" },
105 },
106 }),
107 );
108
109 expect(dirs.filter((entry) => entry.provider === "claude")).toHaveLength(1);
110 }),
111 );
112
113 it.effect("skips drivers without transcripts and configs that fail to decode", () =>
114 Effect.gen(function* () {
115 const fileSystem = yield* FileSystem.FileSystem;
116 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
117
118 const dirs = yield* resolveUsageTranscriptDirs(
119 makeSettings(root, {
120 [ProviderInstanceId.make("cursor_main")]: {
121 driver: ProviderDriverKind.make("cursor"),
122 config: {},
123 },
124 [ProviderInstanceId.make("claude_broken")]: {
125 driver: CLAUDE,
126 config: { homePath: 42 },
127 },
128 }),
129 );
130
131 expect(dirs).toHaveLength(2);
132 }),
133 );
134 });
135});