-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-blame-tool.test.ts
More file actions
82 lines (69 loc) · 2.59 KB
/
git-blame-tool.test.ts
File metadata and controls
82 lines (69 loc) · 2.59 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
/**
* Integration tests for git_blame_tool.
*
* Tests create throwaway git repos via `git init` in OS temp dirs
* and exercise git blame on committed files.
*
* We test:
* 1. Happy path blame of a committed file (author, sha, content)
* 2. -L range narrows output to the requested lines only
* 3. Path-escape rejection (../../etc/passwd)
*/
import { afterEach, describe, expect, test } from "bun:test";
import { registerGitBlameTool } from "./git-blame-tool.js";
import { addCommit, captureTool, cleanupTmpPaths, makeRepo } from "./test-harness.js";
afterEach(cleanupTmpPaths);
describe("git_blame_tool", () => {
test("blame of a committed file returns author, sha, and content", async () => {
const repo = makeRepo();
addCommit(repo, "hello.txt", "line one\nline two\n", "feat: add hello");
const tool = captureTool(registerGitBlameTool);
const result = await tool({
workspaceRoot: repo,
path: "hello.txt",
format: "json",
});
const parsed = JSON.parse(result);
expect(parsed.path).toBe("hello.txt");
expect(Array.isArray(parsed.lines)).toBe(true);
expect(parsed.lines.length).toBe(2);
const first = parsed.lines[0];
expect(typeof first.sha).toBe("string");
expect(first.sha).toHaveLength(40);
expect(first.author).toBe("Test User");
expect(first.content).toBe("line one");
expect(first.line).toBe(1);
expect(typeof first.date).toBe("string");
expect(first.date).toMatch(/^\d{4}-\d{2}-\d{2}T/);
expect(first.summary).toBe("feat: add hello");
});
test("-L range narrows blame output to the specified lines", async () => {
const repo = makeRepo();
addCommit(repo, "multi.txt", "alpha\nbeta\ngamma\ndelta\n", "feat: add multi");
const tool = captureTool(registerGitBlameTool);
const result = await tool({
workspaceRoot: repo,
path: "multi.txt",
startLine: 2,
endLine: 3,
format: "json",
});
const parsed = JSON.parse(result);
expect(parsed.lines.length).toBe(2);
expect(parsed.lines[0].line).toBe(2);
expect(parsed.lines[0].content).toBe("beta");
expect(parsed.lines[1].line).toBe(3);
expect(parsed.lines[1].content).toBe("gamma");
});
test("path-escape attempt returns path_escapes_repo error", async () => {
const repo = makeRepo();
addCommit(repo, "file.txt", "content\n", "feat: add file");
const tool = captureTool(registerGitBlameTool);
const result = await tool({
workspaceRoot: repo,
path: "../../etc/passwd",
});
const parsed = JSON.parse(result);
expect(parsed.error).toBe("path_escapes_repo");
});
});