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 branch: null,
33 worktreePath: null,
34 ...overrides,
35 };
36}
37
38describe("getOrphanedWorktreePathForThread", () => {
39 it("returns null when the target thread does not exist", () => {
40 const result = getOrphanedWorktreePathForThread([], ThreadId.makeUnsafe("missing-thread"));
41 expect(result).toBeNull();
42 });
43
44 it("returns null when the target thread has no worktree", () => {
45 const threads = [makeThread()];
46 const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
47 expect(result).toBeNull();
48 });
49
50 it("returns the path when no other thread links to that worktree", () => {
51 const threads = [makeThread({ worktreePath: "/tmp/repo/worktrees/feature-a" })];
52 const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
53 expect(result).toBe("/tmp/repo/worktrees/feature-a");
54 });
55
56 it("returns null when another thread links to the same worktree", () => {
57 const threads = [
58 makeThread({
59 id: ThreadId.makeUnsafe("thread-1"),
60 worktreePath: "/tmp/repo/worktrees/feature-a",
61 }),
62 makeThread({
63 id: ThreadId.makeUnsafe("thread-2"),
64 worktreePath: "/tmp/repo/worktrees/feature-a",
65 }),
66 ];
67 const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
68 expect(result).toBeNull();
69 });
70
71 it("ignores threads linked to different worktrees", () => {
72 const threads = [
73 makeThread({
74 id: ThreadId.makeUnsafe("thread-1"),
75 worktreePath: "/tmp/repo/worktrees/feature-a",
76 }),
77 makeThread({
78 id: ThreadId.makeUnsafe("thread-2"),
79 worktreePath: "/tmp/repo/worktrees/feature-b",
80 }),
81 ];
82 const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
83 expect(result).toBe("/tmp/repo/worktrees/feature-a");
84 });
85});
86
87describe("formatWorktreePathForDisplay", () => {
88 it("shows only the last path segment for unix-like paths", () => {
89 const result = formatWorktreePathForDisplay(
90 "/Users/julius/.t3/worktrees/codething-mvp/codething-4e609bb8",
91 );
92 expect(result).toBe("codething-4e609bb8");
93 });
94
95 it("normalizes windows separators before selecting the final segment", () => {
96 const result = formatWorktreePathForDisplay(
97 "C:\\Users\\julius\\.t3\\worktrees\\codething-mvp\\codething-4e609bb8",
98 );
99 expect(result).toBe("codething-4e609bb8");
100 });
101
102 it("uses the final segment even when outside ~/.t3/worktrees", () => {
103 const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree");
104 expect(result).toBe("my-worktree");
105 });
106
107 it("ignores trailing slashes", () => {
108 const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree/");
109 expect(result).toBe("my-worktree");
110 });
111});