-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathworktreeCleanup.test.ts
More file actions
167 lines (160 loc) · 4.87 KB
/
worktreeCleanup.test.ts
File metadata and controls
167 lines (160 loc) · 4.87 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { describe, expect, it } from "vitest";
import { collectMergedWorktreeCleanupCandidates } from "./worktreeCleanup";
describe("collectMergedWorktreeCleanupCandidates", () => {
it("includes only merged pull request worktrees and ignores root, blank, and branchless entries", () => {
const candidates = collectMergedWorktreeCleanupCandidates({
cwd: "/repo",
worktreeListStdout: [
"worktree /repo/",
"branch refs/heads/feature-root",
"",
"worktree /repo/worktrees/feature-one",
"branch refs/heads/feature-one",
"",
"worktree /repo/worktrees/feature-two",
"branch refs/heads/feature-two",
"prunable gitdir file points to non-existent location",
"",
"worktree /repo/worktrees/no-branch",
"HEAD abcdef1234567890",
"",
"worktree ",
"branch refs/heads/feature-blank-path",
"",
].join("\n"),
mergedPullRequests: [
{
number: 101,
title: "Root PR",
url: "https://example.com/pr/101",
headBranch: "feature-root",
mergedAt: "2026-03-01T00:00:00.000Z",
},
{
number: 102,
title: "Feature one",
url: "https://example.com/pr/102",
headBranch: "feature-one",
mergedAt: "2026-03-02T00:00:00.000Z",
},
{
number: 103,
title: "Feature two",
url: "https://example.com/pr/103",
headBranch: "feature-two",
mergedAt: "2026-03-03T00:00:00.000Z",
},
{
number: 104,
title: "Blank path",
url: "https://example.com/pr/104",
headBranch: "feature-blank-path",
mergedAt: "2026-03-04T00:00:00.000Z",
},
],
});
expect(candidates).toEqual([
{
path: "/repo/worktrees/feature-one",
branch: "feature-one",
prNumber: 102,
prTitle: "Feature one",
prUrl: "https://example.com/pr/102",
mergedAt: "2026-03-02T00:00:00.000Z",
pathExists: true,
prunable: false,
},
{
path: "/repo/worktrees/feature-two",
branch: "feature-two",
prNumber: 103,
prTitle: "Feature two",
prUrl: "https://example.com/pr/103",
mergedAt: "2026-03-03T00:00:00.000Z",
pathExists: false,
prunable: true,
},
]);
});
it("marks prunable entries as missing paths", () => {
const [candidate] = collectMergedWorktreeCleanupCandidates({
cwd: "/repo",
worktreeListStdout: [
"worktree /repo/worktrees/feature-missing",
"branch refs/heads/feature-missing",
"prunable gitdir file points to non-existent location",
"",
].join("\n"),
mergedPullRequests: [
{
number: 201,
title: "Missing worktree",
url: "https://example.com/pr/201",
headBranch: "feature-missing",
mergedAt: "2026-03-01T00:00:00.000Z",
},
],
});
expect(candidate).toMatchObject({
branch: "feature-missing",
pathExists: false,
prunable: true,
});
});
it("sorts by existing paths first, then mergedAt descending, then branch name", () => {
const candidates = collectMergedWorktreeCleanupCandidates({
cwd: "/repo",
worktreeListStdout: [
"worktree /repo/worktrees/feature-beta",
"branch refs/heads/feature-beta",
"",
"worktree /repo/worktrees/feature-missing",
"branch refs/heads/feature-missing",
"prunable gitdir file points to non-existent location",
"",
"worktree /repo/worktrees/feature-recent",
"branch refs/heads/feature-recent",
"",
"worktree /repo/worktrees/feature-alpha",
"branch refs/heads/feature-alpha",
"",
].join("\n"),
mergedPullRequests: [
{
number: 301,
title: "Feature beta",
url: "https://example.com/pr/301",
headBranch: "feature-beta",
mergedAt: "2026-03-01T12:00:00.000Z",
},
{
number: 302,
title: "Feature missing",
url: "https://example.com/pr/302",
headBranch: "feature-missing",
mergedAt: "2026-04-01T12:00:00.000Z",
},
{
number: 303,
title: "Feature recent",
url: "https://example.com/pr/303",
headBranch: "feature-recent",
mergedAt: "2026-03-02T12:00:00.000Z",
},
{
number: 304,
title: "Feature alpha",
url: "https://example.com/pr/304",
headBranch: "feature-alpha",
mergedAt: "2026-03-01T12:00:00.000Z",
},
],
});
expect(candidates.map((candidate) => candidate.branch)).toEqual([
"feature-recent",
"feature-alpha",
"feature-beta",
"feature-missing",
]);
});
});