-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathworktreeCleanup.test.ts
More file actions
139 lines (120 loc) · 4.63 KB
/
Copy pathworktreeCleanup.test.ts
File metadata and controls
139 lines (120 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { ProjectId, ThreadId } from "@okcode/contracts";
import { describe, expect, it } from "vitest";
import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, type Thread } from "./types";
import {
formatBranchAge,
formatWorktreePathForDisplay,
getOrphanedWorktreePathForThread,
} from "./worktreeCleanup";
function makeThread(overrides: Partial<Thread> = {}): Thread {
return {
id: ThreadId.makeUnsafe("thread-1"),
codexThreadId: null,
projectId: ProjectId.makeUnsafe("project-1"),
title: "Thread",
model: "gpt-5.3-codex",
runtimeMode: DEFAULT_RUNTIME_MODE,
interactionMode: DEFAULT_INTERACTION_MODE,
session: null,
messages: [],
turnDiffSummaries: [],
activities: [],
proposedPlans: [],
error: null,
createdAt: "2026-02-13T00:00:00.000Z",
latestTurn: null,
branch: null,
worktreePath: null,
...overrides,
};
}
describe("getOrphanedWorktreePathForThread", () => {
it("returns null when the target thread does not exist", () => {
const result = getOrphanedWorktreePathForThread([], ThreadId.makeUnsafe("missing-thread"));
expect(result).toBeNull();
});
it("returns null when the target thread has no worktree", () => {
const threads = [makeThread()];
const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
expect(result).toBeNull();
});
it("returns the path when no other thread links to that worktree", () => {
const threads = [makeThread({ worktreePath: "/tmp/repo/worktrees/feature-a" })];
const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
expect(result).toBe("/tmp/repo/worktrees/feature-a");
});
it("returns null when another thread links to the same worktree", () => {
const threads = [
makeThread({
id: ThreadId.makeUnsafe("thread-1"),
worktreePath: "/tmp/repo/worktrees/feature-a",
}),
makeThread({
id: ThreadId.makeUnsafe("thread-2"),
worktreePath: "/tmp/repo/worktrees/feature-a",
}),
];
const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
expect(result).toBeNull();
});
it("ignores threads linked to different worktrees", () => {
const threads = [
makeThread({
id: ThreadId.makeUnsafe("thread-1"),
worktreePath: "/tmp/repo/worktrees/feature-a",
}),
makeThread({
id: ThreadId.makeUnsafe("thread-2"),
worktreePath: "/tmp/repo/worktrees/feature-b",
}),
];
const result = getOrphanedWorktreePathForThread(threads, ThreadId.makeUnsafe("thread-1"));
expect(result).toBe("/tmp/repo/worktrees/feature-a");
});
});
describe("formatBranchAge", () => {
const now = new Date("2026-04-06T12:00:00.000Z");
it("returns 'just now' for timestamps less than a minute ago", () => {
expect(formatBranchAge("2026-04-06T11:59:30.000Z", now)).toBe("just now");
});
it("returns minutes for recent merges", () => {
expect(formatBranchAge("2026-04-06T11:45:00.000Z", now)).toBe("15m ago");
});
it("returns hours for same-day merges", () => {
expect(formatBranchAge("2026-04-06T05:00:00.000Z", now)).toBe("7h ago");
});
it("returns days for merges within a week", () => {
expect(formatBranchAge("2026-04-03T12:00:00.000Z", now)).toBe("3d ago");
});
it("returns weeks for merges within a month", () => {
expect(formatBranchAge("2026-03-16T12:00:00.000Z", now)).toBe("3w ago");
});
it("returns months for older merges", () => {
expect(formatBranchAge("2026-01-06T12:00:00.000Z", now)).toBe("3mo ago");
});
it("returns 'just now' for future timestamps", () => {
expect(formatBranchAge("2026-04-07T00:00:00.000Z", now)).toBe("just now");
});
});
describe("formatWorktreePathForDisplay", () => {
it("shows only the last path segment for unix-like paths", () => {
const result = formatWorktreePathForDisplay(
"/Users/julius/.okcode/worktrees/okcode-mvp/okcode-4e609bb8",
);
expect(result).toBe("okcode-4e609bb8");
});
it("normalizes windows separators before selecting the final segment", () => {
const result = formatWorktreePathForDisplay(
"C:\\Users\\julius\\.okcode\\worktrees\\okcode-mvp\\okcode-4e609bb8",
);
expect(result).toBe("okcode-4e609bb8");
});
it("uses the final segment even when outside ~/.okcode/worktrees", () => {
const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree");
expect(result).toBe("my-worktree");
});
it("ignores trailing slashes", () => {
const result = formatWorktreePathForDisplay("/tmp/custom-worktrees/my-worktree/");
expect(result).toBe("my-worktree");
});
});