Open History to see the last commit
1import { ProjectId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vitest";
3
4import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, type Thread } from "./types";
5import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "./worktreeCleanup";
6
7function makeThread(overrides: Partial<Thread> = {}): Thread {
8 return {
9 id: ThreadId.makeUnsafe("thread-1"),
10 codexThreadId: null,
11 projectId: ProjectId.makeUnsafe("project-1"),
12 title: "Thread",
13 modelSelection: {
14 provider: "codex",
15 model: "gpt-5.3-codex",
16 },
17 runtimeMode: DEFAULT_RUNTIME_MODE,
18 interactionMode: DEFAULT_INTERACTION_MODE,
19 session: null,
20 messages: [],
21 turnDiffSummaries: [],
22 activities: [],
23 proposedPlans: [],
24 error: null,
25 createdAt: "2026-02-13T00:00:00.000Z",
26 archivedAt: null,
27 latestTurn: null,
28 branch: null,
29 worktreePath: null,
30 ...overrides,
31 };
32}
33
34describe("getOrphanedWorktreePathForThread", () => {
35 it("returns null when the target thread does not exist", () => {
36 const result = getOrphanedWorktreePathForThread([], ThreadId.makeUnsafe("missing-thread"));
37 expect(result).toBeNull();
38 });
39
40 it("returns null when the target thread has no worktree", () => {
41 const threads = [makeThread()];
42 const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
43 expect(result).toBeNull();
44 });
45
46 it("returns the path when no other thread links to that worktree", () => {
47 const threads = [makeThread({ worktreePath: "/tmp/repo/worktrees/feature-a" })];
48 const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
49 expect(result).toBe("/tmp/repo/worktrees/feature-a");
50 });
51
52 it("returns null when another thread links to the same worktree", () => {
53 const threads = [
54 makeThread({
55 id: ThreadId.makeUnsafe("thread-1"),
56 worktreePath: "/tmp/repo/worktrees/feature-a",
57 }),
58 makeThread({
59 id: ThreadId.makeUnsafe("thread-2"),
60 worktreePath: "/tmp/repo/worktrees/feature-a",
61 }),
62 ];
63 const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
64 expect(result).toBeNull();
65 });
66
67 it("ignores threads linked to different worktrees", () => {
68 const threads = [
69 makeThread({
70 id: ThreadId.makeUnsafe("thread-1"),
71 worktreePath: "/tmp/repo/worktrees/feature-a",
72 }),
73 makeThread({
74 id: ThreadId.makeUnsafe("thread-2"),
75 worktreePath: "/tmp/repo/worktrees/feature-b",
76 }),
77 ];
78 const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
79 expect(result).toBe("/tmp/repo/worktrees/feature-a");
80 });
81});
82
83describe("formatWorktreePathForDisplay", () => {
84 it("shows only the last path segment for unix-like paths", () => {
85 const result = formatWorktreePathForDisplay(
86 "/Users/julius/.t3/worktrees/t3code-mvp/t3code-4e609bb8",
87 );
88 expect(result).toBe("t3code-4e609bb8");
89 });
90
91 it("normalizes windows separators before selecting the final segment", () => {
92 const result = formatWorktreePathForDisplay(
93 "C:\\Users\\julius\\.t3\\worktrees\\t3code-mvp\\t3code-4e609bb8",
94 );
95 expect(result).toBe("t3code-4e609bb8");
96 });
97
98 it("uses the final segment even when outside ~/.t3/worktrees", () => {
99 const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree");
100 expect(result).toBe("my-worktree");
101 });
102
103 it("ignores trailing slashes", () => {
104 const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree/");
105 expect(result).toBe("my-worktree");
106 });
107});