Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

worktreeCleanup.test.ts

97 lines3.5 KB

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