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

worktreeCleanup.test.ts

109 lines3.8 KB

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 { makeThreadFixture } from "./test-fixtures";
6import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "./worktreeCleanup";
7​
8const localEnvironmentId = EnvironmentId.make("environment-local");
9​
10function makeThread(overrides: Partial<Thread> = {}): Thread {
11 return makeThreadFixture({
12 id: ThreadId.make("thread-1"),
13 environmentId: localEnvironmentId,
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 runtime: null,
23 messages: [],
24 proposedPlans: [],
25 createdAt: "2026-02-13T00:00:00.000Z",
26 updatedAt: "2026-02-13T00:00:00.000Z",
27 archivedAt: null,
28 deletedAt: null,
29 latestRun: null,
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([], ThreadId.make("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, ThreadId.make("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, ThreadId.make("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({
57 id: ThreadId.make("thread-1"),
58 worktreePath: "/tmp/repo/worktrees/feature-a",
59 }),
60 makeThread({
61 id: ThreadId.make("thread-2"),
62 worktreePath: "/tmp/repo/worktrees/feature-a",
63 }),
64 ];
65 const result = getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-1"));
66 expect(result).toBeNull();
67 });
68​
69 it("ignores threads linked to different worktrees", () => {
70 const threads = [
71 makeThread({
72 id: ThreadId.make("thread-1"),
73 worktreePath: "/tmp/repo/worktrees/feature-a",
74 }),
75 makeThread({
76 id: ThreadId.make("thread-2"),
77 worktreePath: "/tmp/repo/worktrees/feature-b",
78 }),
79 ];
80 const result = getOrphanedWorktreePathForThread(threads, ThreadId.make("thread-1"));
81 expect(result).toBe("/tmp/repo/worktrees/feature-a");
82 });
83});
84​
85describe("formatWorktreePathForDisplay", () => {
86 it("shows only the last path segment for unix-like paths", () => {
87 const result = formatWorktreePathForDisplay(
88 "/Users/julius/.t3/worktrees/t3code-mvp/t3code-4e609bb8",
89 );
90 expect(result).toBe("t3code-4e609bb8");
91 });
92​
93 it("normalizes windows separators before selecting the final segment", () => {
94 const result = formatWorktreePathForDisplay(
95 "C:\\Users\\julius\\.t3\\worktrees\\t3code-mvp\\t3code-4e609bb8",
96 );
97 expect(result).toBe("t3code-4e609bb8");
98 });
99​
100 it("uses the final segment even when outside ~/.t3/worktrees", () => {
101 const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree");
102 expect(result).toBe("my-worktree");
103 });
104​
105 it("ignores trailing slashes", () => {
106 const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree/");
107 expect(result).toBe("my-worktree");
108 });
109});