-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-merge-tool.test.ts
More file actions
400 lines (358 loc) · 13.5 KB
/
git-merge-tool.test.ts
File metadata and controls
400 lines (358 loc) · 13.5 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
* Integration tests for git_merge.
*
* Uses throwaway on-disk repos so merge/rebase semantics are exercised end-to-end.
* No network, no real upstream — every branch lives locally.
*/
import { afterEach, describe, expect, test } from "bun:test";
import { existsSync, readdirSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { registerGitMergeTool } from "./git-merge-tool.js";
import {
addCommit,
captureTool,
cleanupTmpPaths,
gitCmd,
makeRepoWithSeed,
mkTmpDir,
trackTmpPath,
} from "./test-harness.js";
afterEach(cleanupTmpPaths);
// ---------------------------------------------------------------------------
// Repo helpers (shared via test-harness.ts)
// ---------------------------------------------------------------------------
function makeRepo(): string {
return makeRepoWithSeed("mcp-git-merge-test-");
}
function createBranchAhead(dir: string, branch: string, files: Record<string, string>): void {
gitCmd(dir, "checkout", "-b", branch);
for (const [path, body] of Object.entries(files)) {
writeFileSync(join(dir, path), body);
gitCmd(dir, "add", path);
}
gitCmd(dir, "commit", "-m", `feat: ${branch}`);
gitCmd(dir, "checkout", "main");
}
// ---------------------------------------------------------------------------
// Fast-forward path (most common: agent worktree ahead of main)
// ---------------------------------------------------------------------------
describe("git_merge fast-forward", () => {
test("ahead-only source fast-forwards under auto strategy", async () => {
const dir = makeRepo();
createBranchAhead(dir, "feature/a", { "a.txt": "A\n" });
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
applied: number;
results: Array<{ source: string; ok: boolean; outcome: string }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.applied).toBe(1);
expect(parsed.results[0]?.outcome).toBe("fast_forward");
// main now contains a.txt
expect(existsSync(join(dir, "a.txt"))).toBe(true);
});
test("multiple ahead-only sources apply in order", async () => {
const dir = makeRepo();
createBranchAhead(dir, "feature/a", { "a.txt": "A\n" });
// second branch starts from main (before a is merged) and adds b.txt
createBranchAhead(dir, "feature/b", { "b.txt": "B\n" });
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a", "feature/b"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
applied: number;
results: Array<{ outcome: string }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.applied).toBe(2);
// First is FF, second is either FF (if merge-base still main tip) or rebase_then_ff.
// After FFing feature/a, main has new commit; feature/b's base is still the original.
// So feature/b has diverged and under auto will rebase_then_ff.
expect(["fast_forward", "rebase_then_ff"]).toContain(parsed.results[0]?.outcome as string);
expect(["fast_forward", "rebase_then_ff"]).toContain(parsed.results[1]?.outcome as string);
});
test("already up-to-date source is reported but not re-applied", async () => {
const dir = makeRepo();
// feature/a points at main, then main advances past it.
gitCmd(dir, "branch", "feature/a", "HEAD");
addCommit(dir, "extra.txt", "extra\n", "chore: advance main");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ outcome: string }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.results[0]?.outcome).toBe("up_to_date");
});
});
// ---------------------------------------------------------------------------
// Strategy matrix
// ---------------------------------------------------------------------------
describe("git_merge strategy", () => {
test("ff-only on diverged branches returns cannot_fast_forward", async () => {
const dir = makeRepo();
createBranchAhead(dir, "feature/a", { "a.txt": "A\n" });
// advance main so feature/a is behind
addCommit(dir, "m.txt", "M\n", "chore: main advance");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
strategy: "ff-only",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ error: string }>;
};
expect(parsed.ok).toBe(false);
expect(parsed.results[0]?.error).toBe("cannot_fast_forward");
});
test("auto on diverged branches rebases then fast-forwards when clean", async () => {
const dir = makeRepo();
createBranchAhead(dir, "feature/a", { "a.txt": "A\n" });
addCommit(dir, "m.txt", "M\n", "chore: main advance");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ outcome: string }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.results[0]?.outcome).toBe("rebase_then_ff");
});
test("merge strategy always creates a merge commit", async () => {
const dir = makeRepo();
createBranchAhead(dir, "feature/a", { "a.txt": "A\n" });
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
strategy: "merge",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ outcome: string }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.results[0]?.outcome).toBe("merge_commit");
// Confirm there's a merge commit at HEAD (two parents).
const parents = gitCmd(dir, "rev-list", "--parents", "-1", "HEAD").trim().split(" ");
expect(parents.length).toBe(3); // commit + 2 parents
});
test("auto falls back to merge commit when rebase conflicts", async () => {
const dir = makeRepo();
// Both branches touch the same file with incompatible content.
gitCmd(dir, "checkout", "-b", "feature/a");
writeFileSync(join(dir, "shared.txt"), "alpha\n");
gitCmd(dir, "add", "shared.txt");
gitCmd(dir, "commit", "-m", "feat: alpha");
gitCmd(dir, "checkout", "main");
addCommit(dir, "shared.txt", "beta\n", "chore: beta on main");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ ok: boolean; outcome?: string; error?: string; conflictStage?: string }>;
};
// Rebase conflicts, then merge-commit also conflicts on shared.txt — final outcome is conflict at merge stage.
expect(parsed.ok).toBe(false);
expect(parsed.results[0]?.ok).toBe(false);
expect(parsed.results[0]?.conflictStage).toBe("merge");
});
test("rebase strategy surfaces rebase conflict without merge fallback", async () => {
const dir = makeRepo();
gitCmd(dir, "checkout", "-b", "feature/a");
writeFileSync(join(dir, "shared.txt"), "alpha\n");
gitCmd(dir, "add", "shared.txt");
gitCmd(dir, "commit", "-m", "feat: alpha");
gitCmd(dir, "checkout", "main");
addCommit(dir, "shared.txt", "beta\n", "chore: beta");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
strategy: "rebase",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ error: string; conflictStage: string; conflictPaths: string[] }>;
};
expect(parsed.ok).toBe(false);
expect(parsed.results[0]?.conflictStage).toBe("rebase");
expect(parsed.results[0]?.error).toBe("rebase_conflicts");
// No rebase artifacts left behind.
const hasRebaseDir = readdirSync(join(dir, ".git")).some((n: string) =>
n.startsWith("rebase-"),
);
expect(hasRebaseDir).toBe(false);
});
});
// ---------------------------------------------------------------------------
// Cleanup flags
// ---------------------------------------------------------------------------
describe("git_merge cleanup", () => {
test("deleteMergedBranches deletes non-protected source after FF", async () => {
const dir = makeRepo();
createBranchAhead(dir, "feature/a", { "a.txt": "A\n" });
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
deleteMergedBranches: true,
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ branchDeleted?: boolean }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.results[0]?.branchDeleted).toBe(true);
// Branch no longer exists.
const branches = gitCmd(dir, "branch").trim();
expect(branches).not.toContain("feature/a");
});
test("deleteMergedBranches skips protected names", async () => {
const dir = makeRepo();
// Create a branch called `dev` (protected) that is ahead of main.
gitCmd(dir, "checkout", "-b", "dev");
writeFileSync(join(dir, "d.txt"), "d\n");
gitCmd(dir, "add", "d.txt");
gitCmd(dir, "commit", "-m", "feat: dev");
gitCmd(dir, "checkout", "main");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["dev"],
deleteMergedBranches: true,
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ branchDeleted?: boolean }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.results[0]?.branchDeleted).toBeUndefined();
// dev still exists.
const branches = gitCmd(dir, "branch").trim();
expect(branches).toContain("dev");
});
test("simple ff-merge (markdown)", async () => {
const dir = makeRepo();
createBranchAhead(dir, "feature/a", { "a.txt": "A\n" });
gitCmd(dir, "checkout", "main");
const run = captureTool(registerGitMergeTool);
const text = await run({ workspaceRoot: dir, sources: ["feature/a"] });
expect(text).toContain("# Merge into `main`");
expect(text).toContain("feature/a");
expect(text).toMatch(/✓|✔/);
});
test("deleteMergedWorktrees removes a worktree attached to merged source", async () => {
const dir = makeRepo();
// Create branch + worktree for it.
gitCmd(dir, "branch", "feature/w", "HEAD");
const wtPath = trackTmpPath(join(tmpdir(), `mcp-wt-${Date.now()}`));
gitCmd(dir, "worktree", "add", wtPath, "feature/w");
// Add a commit in the worktree so it's ahead.
writeFileSync(join(wtPath, "w.txt"), "W\n");
gitCmd(wtPath, "add", "w.txt");
gitCmd(wtPath, "commit", "-m", "feat: w");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/w"],
deleteMergedWorktrees: true,
deleteMergedBranches: true,
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ branchDeleted?: boolean; worktreeRemoved?: string }>;
};
expect(parsed.ok).toBe(true);
expect(parsed.results[0]?.worktreeRemoved).toBe(wtPath);
expect(parsed.results[0]?.branchDeleted).toBe(true);
expect(existsSync(wtPath)).toBe(false);
});
});
// ---------------------------------------------------------------------------
// Guardrails
// ---------------------------------------------------------------------------
describe("git_merge guardrails", () => {
test("working_tree_dirty refuses when tree has unstaged changes", async () => {
const dir = makeRepo();
createBranchAhead(dir, "feature/a", { "a.txt": "A\n" });
writeFileSync(join(dir, "seed.txt"), "mutated\n");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["feature/a"],
});
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("working_tree_dirty");
});
test("unknown source returns source_not_found", async () => {
const dir = makeRepo();
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["does-not-exist"],
});
const parsed = JSON.parse(text) as {
ok: boolean;
results: Array<{ error: string }>;
};
expect(parsed.ok).toBe(false);
expect(parsed.results[0]?.error).toBe("source_not_found");
});
test("unsafe ref token rejected", async () => {
const dir = makeRepo();
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: dir,
format: "json",
sources: ["; rm -rf /"],
});
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("unsafe_ref_token");
});
test("non-git workspaceRoot returns not_a_git_repository", async () => {
const plain = mkTmpDir("mcp-plain-");
const run = captureTool(registerGitMergeTool);
const text = await run({
workspaceRoot: plain,
format: "json",
sources: ["anything"],
});
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("not_a_git_repository");
});
});