Open History to see the last commit
1import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@v12/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 deletedAt: null,
30 latestTurn: null,
31 branch: null,
32 worktreePath: null,
33 ...overrides,
34 };
35}
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/.v12/worktrees/v12-mvp/v12-4e609bb8",
90 );
91 expect(result).toBe("v12-4e609bb8");
92 });
93
94 it("normalizes windows separators before selecting the final segment", () => {
95 const result = formatWorktreePathForDisplay(
96 "C:\\Users\\julius\\.v12\\worktrees\\v12-mvp\\v12-4e609bb8",
97 );
98 expect(result).toBe("v12-4e609bb8");
99 });
100
101 it("uses the final segment even when outside ~/.v12/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});