-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-show-tool.test.ts
More file actions
227 lines (189 loc) · 6.86 KB
/
git-show-tool.test.ts
File metadata and controls
227 lines (189 loc) · 6.86 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
/**
* Integration tests for git_show_tool.
*
* Tests create throwaway git repos via `git init` in OS temp dirs
* and exercise git show for commits and file inspection.
*
* We test:
* 1. git show on a commit ref returns message + diff
* 2. git show with a path returns file content at that ref
* 3. commit message is correctly extracted from git show output
* 4. not_a_git_repository error for non-git path
* 5. invalid ref error handling
* 6. JSON format output
*/
import { afterEach, describe, expect, test } from "bun:test";
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { registerGitShowTool } from "./git-show-tool.js";
import { addCommit, captureTool, cleanupTmpPaths, gitCmd, makeRepo } from "./test-harness.js";
afterEach(cleanupTmpPaths);
describe("git_show_tool", () => {
test("git show on a commit returns message + diff", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "content\n", "feat: add file");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD",
format: "markdown",
});
// Result should contain commit message and diff info
expect(result).toContain("feat: add file");
expect(result).toContain("git show HEAD");
});
test("git show with path shows file content at ref", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "first content\n", "feat: add file");
addCommit(repo, "file.txt", "second content\n", "fix: update file");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD~1",
path: "file.txt",
format: "markdown",
});
// Result should contain the file path and content from the previous commit
expect(result).toContain("file.txt");
expect(result).toContain("first content");
});
test("git show returns JSON format", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "content\n", "feat: add file");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD",
format: "json",
});
const parsed = JSON.parse(result);
expect(parsed.ref).toBe("HEAD");
expect(parsed.message).toContain("feat: add file");
expect(typeof parsed.diff).toBe("string");
});
test("git show not_a_git_repository error for invalid path", async () => {
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: "/nonexistent/path",
ref: "HEAD",
});
expect(result).toContain("not_a_git_repository");
});
test("git show invalid ref returns error", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "content\n", "feat: add file");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "invalid-ref-xyz",
});
expect(result).toContain("git_show_failed");
});
test("git show with path includes path in JSON", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "content\n", "feat: add file");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD",
path: "file.txt",
format: "json",
});
const parsed = JSON.parse(result);
expect(parsed.path).toBe("file.txt");
expect(parsed.ref).toBe("HEAD");
});
test("git show rejects leading-dash ref injection", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "content\n", "feat: add file");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "--output=/tmp/x",
});
expect(result).toContain("unsafe_ref_token");
});
test("git show rejects path that escapes repo", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "content\n", "feat: add file");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD",
path: "../../etc/passwd",
});
expect(result).toContain("path_escapes_repo");
});
test("git show commit message with multiline content", async () => {
const repo = makeRepo();
writeFileSync(join(repo, "file.txt"), "content\n");
gitCmd(repo, "add", "file.txt");
gitCmd(
repo,
"commit",
"-m",
"feat: add file\n\nThis is a detailed description\nof the feature.",
);
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD",
format: "json",
});
const parsed = JSON.parse(result);
expect(parsed.message).toContain("feat: add file");
expect(parsed.message).toContain("detailed description");
});
test("git show stat:true returns diffstat not full patch", async () => {
const repo = makeRepo();
addCommit(repo, "alpha.ts", "const x = 1;\n", "feat: add alpha");
addCommit(repo, "beta.ts", "const y = 2;\n", "feat: add beta");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD",
stat: true,
format: "json",
});
const parsed = JSON.parse(result);
expect(parsed.stat).toBe(true);
expect(parsed.message).toContain("feat: add beta");
// statOutput should be present (contains the diffstat summary line)
expect(typeof parsed.statOutput).toBe("string");
expect(parsed.statOutput).toContain("changed");
// Full patch content should NOT appear in statOutput
expect(parsed.statOutput ?? "").not.toContain("diff --git");
expect(parsed.diff).toBeUndefined();
});
test("git show paths[] filters diff to specified files", async () => {
const repo = makeRepo();
// Commit two files in one commit
writeFileSync(join(repo, "a.txt"), "aaa\n");
writeFileSync(join(repo, "b.txt"), "bbb\n");
gitCmd(repo, "add", "a.txt", "b.txt");
gitCmd(repo, "commit", "-m", "feat: add a and b");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD",
paths: ["a.txt"],
format: "json",
});
const parsed = JSON.parse(result);
expect(parsed.paths).toEqual(["a.txt"]);
// Diff should mention a.txt but NOT b.txt
expect(parsed.diff).toContain("a.txt");
expect(parsed.diff ?? "").not.toContain("b.txt");
});
test("git show rejects path in paths[] that escapes repo", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "content\n", "feat: add file");
const tool = captureTool(registerGitShowTool);
const result = await tool({
workspaceRoot: repo,
ref: "HEAD",
paths: ["safe.txt", "../../etc/shadow"],
});
expect(result).toContain("path_escapes_repo");
});
});