Open History to see the last commit
1import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, type Thread } from "./types";
5import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "./worktreeCleanup";
6
7const localEnvironmentId = EnvironmentId.make("environment-local");
8
9function makeThread(overrides: Partial<Thread> = {}): Thread {
10 return {
11 id: ThreadId.make("thread-1"),
12 environmentId: localEnvironmentId,
13 projectId: ProjectId.make("project-1"),
14 title: "Thread",
15 modelSelection: {
16 instanceId: ProviderInstanceId.make("codex"),
17 model: "gpt-5.3-codex",
18 },
19 runtimeMode: DEFAULT_RUNTIME_MODE,
20 interactionMode: DEFAULT_INTERACTION_MODE,
21 session: null,
22 messages: [],
23 checkpoints: [],
24 pullRequests: [],
25 activities: [],
26 proposedPlans: [],
27 createdAt: "2026-02-13T00:00:00.000Z",
28 updatedAt: "2026-02-13T00:00:00.000Z",
29 archivedAt: null,
30 settledOverride: null,
31 settledAt: null,
32 deletedAt: null,
33 latestTurn: null,
34 branch: null,
35 worktreePath: null,
36 ...overrides,
37 };
38}
39
40describe("getOrphanedWorktreePathForThread", () => {
41 it("returns null when the target thread does not exist", () => {
42 const result = getOrphanedWorktreePathForThread([], ThreadId.make("missing-thread"));
43 expect(result).toBeNull();
44 });
45
46 it("returns null when the target thread has no worktree", () => {
47 const threads = [makeThread()];
48 const result = getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-1"));
49 expect(result).toBeNull();
50 });
51
52 it("returns the path when no other thread links to that worktree", () => {
53 const threads = [makeThread({ worktreePath: "/tmp/repo/worktrees/feature-a" })];
54 const result = getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-1"));
55 expect(result).toBe("/tmp/repo/worktrees/feature-a");
56 });
57
58 it("returns null when another thread links to the same worktree", () => {
59 const threads = [
60 makeThread({
61 id: ThreadId.make("thread-1"),
62 worktreePath: "/tmp/repo/worktrees/feature-a",
63 }),
64 makeThread({
65 id: ThreadId.make("thread-2"),
66 worktreePath: "/tmp/repo/worktrees/feature-a",
67 }),
68 ];
69 const result = getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-1"));
70 expect(result).toBeNull();
71 });
72
73 it("ignores threads linked to different worktrees", () => {
74 const threads = [
75 makeThread({
76 id: ThreadId.make("thread-1"),
77 worktreePath: "/tmp/repo/worktrees/feature-a",
78 }),
79 makeThread({
80 id: ThreadId.make("thread-2"),
81 worktreePath: "/tmp/repo/worktrees/feature-b",
82 }),
83 ];
84 const result = getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-1"));
85 expect(result).toBe("/tmp/repo/worktrees/feature-a");
86 });
87});
88
89describe("formatWorktreePathForDisplay", () => {
90 it("shows only the last path segment for unix-like paths", () => {
91 const result = formatWorktreePathForDisplay(
92 "/Users/julius/.t3/worktrees/t3code-mvp/t3code-4e609bb8",
93 );
94 expect(result).toBe("t3code-4e609bb8");
95 });
96
97 it("normalizes windows separators before selecting the final segment", () => {
98 const result = formatWorktreePathForDisplay(
99 "C:\\Users\\julius\\.t3\\worktrees\\t3code-mvp\\t3code-4e609bb8",
100 );
101 expect(result).toBe("t3code-4e609bb8");
102 });
103
104 it("uses the final segment even when outside ~/.t3/worktrees", () => {
105 const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree");
106 expect(result).toBe("my-worktree");
107 });
108
109 it("ignores trailing slashes", () => {
110 const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree/");
111 expect(result).toBe("my-worktree");
112 });
113});