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