-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-diff-tool.test.ts
More file actions
255 lines (211 loc) · 9.29 KB
/
git-diff-tool.test.ts
File metadata and controls
255 lines (211 loc) · 9.29 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
/**
* Tests for git_diff tool.
*
* These tests verify that the tool correctly builds git diff arguments
* and generates appropriate labels for various diff scenarios.
*/
import { afterEach, describe, expect, test } from "bun:test";
import { appendFileSync } from "node:fs";
import { join } from "node:path";
import { registerGitDiffTool } from "./git-diff-tool.js";
import {
addCommit,
captureTool,
cleanupTmpPaths,
gitCmd,
makeRepoWithSeed,
mkTmpDir,
} from "./test-harness.js";
afterEach(cleanupTmpPaths);
// Test parameter validation and arg building
describe("git_diff tool parameter handling", () => {
test("builds args for unstaged changes (no params)", () => {
// When no parameters provided, git diff with no args shows unstaged changes
const args = ["diff"];
expect(args).toContain("diff");
expect(args.length).toBe(1);
});
test("builds args for staged changes", () => {
// When staged: true, git diff --staged
const args = ["diff", "--staged"];
expect(args).toContain("--staged");
});
test("builds args for range diff base..head", () => {
// When base and head provided, git diff base..head
const args = ["diff", "main..feature"];
expect(args).toContain("main..feature");
});
test("builds args for single ref (only base)", () => {
// When only base provided, still generates base..HEAD
const args = ["diff", "main..HEAD"];
expect(args).toContain("main..HEAD");
});
test("builds args with path scoping", () => {
// When path provided, appends -- path
const args = ["diff", "--", "src/main.ts"];
expect(args).toContain("--");
expect(args).toContain("src/main.ts");
});
test("builds args for staged + path", () => {
// When staged: true and path provided
const args = ["diff", "--staged", "--", "src/main.ts"];
expect(args).toContain("--staged");
expect(args).toContain("src/main.ts");
});
test("builds args for range + path", () => {
// When base/head and path provided
const args = ["diff", "main..feature", "--", "src/main.ts"];
expect(args).toContain("main..feature");
expect(args).toContain("src/main.ts");
});
test("validates unsafe range tokens are rejected", () => {
// isSafeGitUpstreamToken checks for known injection patterns
// Ranges with newlines, semicolons, pipes should be rejected
const unsafeTokens = ["main\nsemantically", "main;rm -rf", "main|cat"];
for (const token of unsafeTokens) {
// These should fail validation in the actual tool
const hasShellMeta = /[\n\r;|&`$<>]/.test(token);
expect(hasShellMeta).toBe(true);
}
});
test("accepts safe range tokens", () => {
const safeTokens = ["main", "feature", "v1.2.3", "release/1.0", "HEAD~3", "HEAD~3..main"];
for (const token of safeTokens) {
// Basic sanity: they don't contain obvious injection chars
const hasShellMeta = /[\n\r;|&`$<>]/.test(token);
expect(hasShellMeta).toBe(false);
}
});
});
describe("git_diff tool range labels", () => {
test("labels unstaged changes correctly", () => {
const label = "unstaged changes";
expect(label).toContain("unstaged");
});
test("labels staged changes correctly", () => {
const label = "staged changes";
expect(label).toContain("staged");
});
test("labels range changes correctly", () => {
const label = "main..feature";
expect(label).toMatch(/^[a-zA-Z0-9.~/-]+\.\.[a-zA-Z0-9.~/-]+$/);
});
test("labels path-scoped changes correctly", () => {
const label = "unstaged changes (src/main.ts)";
expect(label).toContain("(src/main.ts)");
});
test("labels range + path changes correctly", () => {
const label = "main..feature (src/main.ts)";
expect(label).toContain("main..feature");
expect(label).toContain("(src/main.ts)");
});
});
describe("git_diff execute handler", () => {
test("returns unstaged diff in json format", async () => {
const repo = makeRepoWithSeed("mcp-git-diff-test-");
appendFileSync(join(repo, "seed.txt"), "changed\n");
const run = captureTool(registerGitDiffTool);
const text = await run({ workspaceRoot: repo, format: "json" });
const parsed = JSON.parse(text) as { range: string; diff: string };
expect(parsed.range).toBe("unstaged changes");
expect(parsed.diff).toContain("+changed");
});
test("returns staged path-scoped diff in markdown format", async () => {
const repo = makeRepoWithSeed("mcp-git-diff-test-");
appendFileSync(join(repo, "seed.txt"), "staged\n");
gitCmd(repo, "add", "seed.txt");
const run = captureTool(registerGitDiffTool);
const text = await run({ workspaceRoot: repo, staged: true, path: "seed.txt" });
expect(text).toContain("# Diff: staged changes (seed.txt)");
expect(text).toContain("```diff");
expect(text).toContain("+staged");
});
test("returns range diff with implicit HEAD", async () => {
const repo = makeRepoWithSeed("mcp-git-diff-test-");
const base = gitCmd(repo, "rev-parse", "HEAD").trim();
addCommit(repo, "later.txt", "later\n", "chore: later");
const run = captureTool(registerGitDiffTool);
const text = await run({ workspaceRoot: repo, base, format: "json" });
const parsed = JSON.parse(text) as { range: string; diff: string };
expect(parsed.range).toBe(`${base}..HEAD`);
expect(parsed.diff).toContain("later.txt");
});
test("returns no changes message for clean unstaged markdown diff", async () => {
const repo = makeRepoWithSeed("mcp-git-diff-test-");
const run = captureTool(registerGitDiffTool);
const text = await run({ workspaceRoot: repo });
expect(text).toContain("# Diff: unstaged changes");
expect(text).toContain("_(no changes)_");
});
test("rejects unsafe range tokens", async () => {
const repo = makeRepoWithSeed("mcp-git-diff-test-");
const run = captureTool(registerGitDiffTool);
const text = await run({ workspaceRoot: repo, base: "main;rm", format: "json" });
const parsed = JSON.parse(text) as { error: string };
expect(parsed).toEqual({ error: "unsafe_range_token" });
});
test("returns not_a_git_repository for invalid workspaceRoot", async () => {
const plainDir = mkTmpDir("mcp-git-diff-plain-");
const run = captureTool(registerGitDiffTool);
const text = await run({ workspaceRoot: plainDir, format: "json" });
const parsed = JSON.parse(text) as { error: string };
expect(parsed.error).toBe("not_a_git_repository");
});
test("multi-path diff scopes output to both specified files", async () => {
const repo = makeRepoWithSeed("mcp-git-diff-test-");
// Create two additional files and commit them
addCommit(repo, "alpha.txt", "alpha\n", "chore: alpha");
addCommit(repo, "beta.txt", "beta\n", "chore: beta");
// Record the SHA before making unstaged changes
appendFileSync(join(repo, "alpha.txt"), "alpha-changed\n");
appendFileSync(join(repo, "beta.txt"), "beta-changed\n");
appendFileSync(join(repo, "seed.txt"), "seed-changed\n");
const run = captureTool(registerGitDiffTool);
const text = await run({
workspaceRoot: repo,
paths: ["alpha.txt", "beta.txt"],
format: "json",
});
const parsed = JSON.parse(text) as { range: string; diff: string };
// Range label should list both paths
expect(parsed.range).toBe("unstaged changes (alpha.txt, beta.txt)");
// Diff includes changes to alpha and beta but NOT seed
expect(parsed.diff).toContain("alpha-changed");
expect(parsed.diff).toContain("beta-changed");
expect(parsed.diff).not.toContain("seed-changed");
});
test("unified: 0 suppresses context lines around change", async () => {
const repo = makeRepoWithSeed("mcp-git-diff-test-");
// Create a multi-line file where neighbors are clearly identifiable
const lines = ["line1", "line2", "line3", "CHANGE_ME", "line5", "line6", "line7"].join("\n");
addCommit(repo, "multi.txt", `${lines}\n`, "chore: multi");
// Replace only the middle line
const changed = ["line1", "line2", "line3", "CHANGED", "line5", "line6", "line7"].join("\n");
const { writeFileSync } = await import("node:fs");
writeFileSync(join(repo, "multi.txt"), `${changed}\n`);
const run = captureTool(registerGitDiffTool);
const text = await run({ workspaceRoot: repo, unified: 0, format: "json" });
const parsed = JSON.parse(text) as { range: string; diff: string };
// The change must appear
expect(parsed.diff).toContain("CHANGED");
// With unified=0, neighboring lines must not appear as context lines
// (git context lines start with a literal space character followed by the line content)
const contextLines = parsed.diff
.split("\n")
.filter((l: string) => l.startsWith(" ") && !l.startsWith(" @"));
expect(contextLines.some((l: string) => l.includes("line3"))).toBe(false);
expect(contextLines.some((l: string) => l.includes("line5"))).toBe(false);
});
test("path escape via paths array returns path_escapes_repo", async () => {
const repo = makeRepoWithSeed("mcp-git-diff-test-");
const run = captureTool(registerGitDiffTool);
const text = await run({
workspaceRoot: repo,
paths: ["../../etc/passwd"],
format: "json",
});
const parsed = JSON.parse(text) as { error: string; path: string };
expect(parsed.error).toBe("path_escapes_repo");
expect(parsed.path).toBe("../../etc/passwd");
});
});