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

worktreeCleanup.test.ts

110 lines3.7 KB

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