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