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

worktreeCleanup.test.ts

100 lines3.4 KB

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