-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMergeConflictShell.logic.test.ts
More file actions
266 lines (245 loc) · 8 KB
/
MergeConflictShell.logic.test.ts
File metadata and controls
266 lines (245 loc) · 8 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { describe, expect, it } from "vitest";
import {
buildConflictFeedbackPreview,
buildConflictRecommendation,
computeActiveStepIndex,
groupConflictCandidatesByFile,
humanizeConflictError,
pickRecommendedConflictCandidate,
pullRequestStateBadgeClassName,
workspaceModeLabel,
} from "./MergeConflictShell.logic";
describe("pickRecommendedConflictCandidate", () => {
it("prefers deterministic candidates", () => {
expect(
pickRecommendedConflictCandidate({
candidates: [
{
id: "src/demo.ts:review",
path: "src/demo.ts",
title: "Prefer current side",
description: "Review candidate",
confidence: "review",
previewPatch: "const review = true;\n",
},
{
id: "src/demo.ts:safe",
path: "src/demo.ts",
title: "Take theirs",
description: "Safe candidate",
confidence: "safe",
previewPatch: "const safe = true;\n",
},
],
})?.id,
).toBe("src/demo.ts:safe");
});
});
describe("groupConflictCandidatesByFile", () => {
it("keeps deterministic candidates first within a file group", () => {
const groups = groupConflictCandidatesByFile([
{
id: "src/b.ts:review",
path: "src/b.ts",
title: "Prefer incoming side",
description: "Review candidate",
confidence: "review",
previewPatch: "incoming\n",
},
{
id: "src/b.ts:safe",
path: "src/b.ts",
title: "Take ours",
description: "Safe candidate",
confidence: "safe",
previewPatch: "ours\n",
},
{
id: "src/a.ts:review",
path: "src/a.ts",
title: "Prefer current side",
description: "Review candidate",
confidence: "review",
previewPatch: "current\n",
},
]);
expect(groups.map((group) => group.path)).toEqual(["src/a.ts", "src/b.ts"]);
expect(groups[1]?.candidates.map((candidate) => candidate.id)).toEqual([
"src/b.ts:safe",
"src/b.ts:review",
]);
});
});
describe("buildConflictRecommendation", () => {
it("guides the user to prepare a local workspace when GitHub is conflicting but no candidates exist", () => {
expect(
buildConflictRecommendation({
analysis: {
status: "conflicted",
mergeableState: "CONFLICTING",
summary: "GitHub reports merge conflicts.",
candidates: [],
},
hasPreparedWorkspace: false,
}),
).toMatchObject({
tone: "warning",
title: "Prepare a local workspace to continue.",
recommendedAction: "prepare-worktree",
});
});
it("recommends reviewing the candidate when a safe resolution is ready", () => {
expect(
buildConflictRecommendation({
analysis: {
status: "conflicted",
mergeableState: "CONFLICTING",
summary: "GitHub reports merge conflicts.",
candidates: [
{
id: "src/auth.ts:safe",
path: "src/auth.ts",
title: "Take theirs",
description: "Safe candidate",
confidence: "safe",
previewPatch: "const safe = true;\n",
},
],
},
hasPreparedWorkspace: true,
}),
).toMatchObject({
tone: "success",
title: "Recommended resolution is ready.",
recommendedAction: "review-candidate",
});
});
it("recommends capturing a note when workspace is prepared but no candidates are available", () => {
expect(
buildConflictRecommendation({
analysis: {
status: "conflicted",
mergeableState: "CONFLICTING",
summary: "GitHub reports merge conflicts.",
candidates: [],
},
hasPreparedWorkspace: true,
}),
).toMatchObject({
tone: "warning",
title: "Manual merge work is still required.",
recommendedAction: "capture-note",
});
});
});
describe("humanizeConflictError", () => {
it("recognises the 'already checked out' error pattern", () => {
const result = humanizeConflictError(
"Error: Git manager failed in preparePullRequestThread: This PR branch is already checked out in the main repo.",
);
expect(result.summary).toBe("Branch already checked out");
expect(result.detail).toContain("worktree");
});
it("recognises the 'not a git repository' pattern", () => {
const result = humanizeConflictError(
"fatal: not a git repository (or any parent up to mount point /)",
);
expect(result.summary).toBe("Not a git repository");
});
it("falls back to stripping the prefix and using the first sentence", () => {
const result = humanizeConflictError(
"Git manager failed in preparePullRequestThread: Something unexpected happened. More details here.",
);
expect(result.summary).toBe("Something unexpected happened.");
expect(result.detail).toContain("Git manager failed");
});
});
describe("computeActiveStepIndex", () => {
it("returns the index of the first non-done step", () => {
expect(
computeActiveStepIndex([
{ status: "done" },
{ status: "done" },
{ status: "blocked" },
{ status: "todo" },
]),
).toBe(2);
});
it("returns steps.length when all steps are done", () => {
expect(computeActiveStepIndex([{ status: "done" }, { status: "done" }])).toBe(2);
});
it("returns 0 when no steps are done", () => {
expect(computeActiveStepIndex([{ status: "todo" }, { status: "todo" }])).toBe(0);
});
});
describe("buildConflictFeedbackPreview", () => {
it("builds a human-readable brief", () => {
expect(
buildConflictFeedbackPreview({
disposition: "review",
note: "Keep the API signature from the incoming branch.",
pullRequest: {
number: 42,
title: "Unify auth boundary",
url: "https://github.com/acme/app/pull/42",
baseBranch: "main",
headBranch: "feature/auth",
state: "open",
},
selectedCandidate: {
id: "src/auth.ts:theirs",
path: "src/auth.ts",
title: "Prefer incoming side",
description: "Review-required candidate using the incoming side.",
confidence: "review",
previewPatch: "export const auth = true;\n",
},
workspaceLabel: "Dedicated worktree",
}),
).toContain("Operator note: Keep the API signature from the incoming branch.");
});
});
describe("workspaceModeLabel", () => {
it('returns "Repo scan" when no workspace is prepared', () => {
expect(workspaceModeLabel(null)).toBe("Repo scan");
});
it('returns "Prepared in repo" for local mode', () => {
expect(
workspaceModeLabel({
branch: "feature/auth",
cwd: "/Users/val/project",
mode: "local",
worktreePath: null,
}),
).toBe("Prepared in repo");
});
it('returns "Dedicated worktree" for worktree mode', () => {
expect(
workspaceModeLabel({
branch: "feature/auth",
cwd: "/Users/val/.git/worktrees/auth",
mode: "worktree",
worktreePath: "/Users/val/.git/worktrees/auth",
}),
).toBe("Dedicated worktree");
});
});
describe("pullRequestStateBadgeClassName", () => {
it("returns emerald styling for open PRs", () => {
const result = pullRequestStateBadgeClassName("open");
expect(result).toContain("emerald");
expect(result).not.toContain("muted");
});
it("returns muted styling for merged PRs", () => {
const result = pullRequestStateBadgeClassName("merged");
expect(result).toContain("muted");
expect(result).not.toContain("emerald");
});
it("returns muted styling for closed PRs", () => {
const result = pullRequestStateBadgeClassName("closed");
expect(result).toContain("muted");
});
it("returns identical styling for merged and closed states", () => {
expect(pullRequestStateBadgeClassName("merged")).toBe(pullRequestStateBadgeClassName("closed"));
});
});