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