-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.test.ts
More file actions
202 lines (174 loc) · 7.11 KB
/
git.test.ts
File metadata and controls
202 lines (174 loc) · 7.11 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
import { describe, it, expect, vi, beforeEach } from "vitest";
import * as child_process from "child_process";
// Mock execFileSync before importing the module
vi.mock("child_process", () => ({
execFileSync: vi.fn(),
}));
// Mock files module to avoid PROJECT_DIR issues
vi.mock("../../src/lib/files.js", () => ({
PROJECT_DIR: "/tmp/test-project",
}));
// Import after mocks are set up
const { run, getBranch, getStatus, getRecentCommits, getLastCommit, getLastCommitTime, getDiffFiles, getStagedFiles, getDiffStat } = await import("../../src/lib/git.js");
const mockExecFileSync = vi.mocked(child_process.execFileSync);
describe("lib/git", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("run()", () => {
it("accepts an array of args", () => {
mockExecFileSync.mockReturnValue("ok\n");
const result = run(["status", "--short"]);
expect(result).toBe("ok");
expect(mockExecFileSync).toHaveBeenCalledWith("git", ["status", "--short"], expect.objectContaining({
cwd: "/tmp/test-project",
encoding: "utf-8",
}));
});
it("splits string args on whitespace", () => {
mockExecFileSync.mockReturnValue("ok\n");
run("log --oneline -5");
expect(mockExecFileSync).toHaveBeenCalledWith("git", ["log", "--oneline", "-5"], expect.any(Object));
});
it("trims output", () => {
mockExecFileSync.mockReturnValue(" hello world \n");
expect(run(["status"])).toBe("hello world");
});
it("returns timeout message when process is killed", () => {
const err: any = new Error("killed");
err.killed = true;
mockExecFileSync.mockImplementation(() => { throw err; });
expect(run(["log"])).toBe("[timed out after 10000ms]");
});
it("returns timeout message with custom timeout", () => {
const err: any = new Error("killed");
err.signal = "SIGTERM";
mockExecFileSync.mockImplementation(() => { throw err; });
expect(run(["log"], { timeout: 5000 })).toBe("[timed out after 5000ms]");
});
it("returns stderr on command failure", () => {
const err: any = new Error("fail");
err.stderr = "fatal: not a git repo\n";
err.stdout = "";
mockExecFileSync.mockImplementation(() => { throw err; });
expect(run(["status"])).toBe("fatal: not a git repo");
});
it("returns stdout on failure if stderr empty", () => {
const err: any = new Error("fail");
err.stderr = "";
err.stdout = "partial output\n";
mockExecFileSync.mockImplementation(() => { throw err; });
expect(run(["diff"])).toBe("partial output");
});
it("returns ENOENT message when git not found", () => {
const err: any = new Error("not found");
err.code = "ENOENT";
err.stdout = "";
err.stderr = "";
mockExecFileSync.mockImplementation(() => { throw err; });
expect(run(["status"])).toBe("[git not found]");
});
it("returns generic failure message when no output available", () => {
const err: any = new Error("fail");
err.status = 128;
err.stdout = "";
err.stderr = "";
mockExecFileSync.mockImplementation(() => { throw err; });
expect(run(["push"])).toBe("[command failed: git push (exit 128)]");
});
it("respects custom timeout option", () => {
mockExecFileSync.mockReturnValue("ok");
run(["status"], { timeout: 5000 });
expect(mockExecFileSync).toHaveBeenCalledWith("git", ["status"], expect.objectContaining({
timeout: 5000,
}));
});
});
describe("convenience functions", () => {
it("getBranch calls branch --show-current", () => {
mockExecFileSync.mockReturnValue("main\n");
expect(getBranch()).toBe("main");
expect(mockExecFileSync).toHaveBeenCalledWith("git", ["branch", "--show-current"], expect.any(Object));
});
it("getStatus calls status --short", () => {
mockExecFileSync.mockReturnValue("M src/index.ts\n");
expect(getStatus()).toBe("M src/index.ts");
});
it("getRecentCommits defaults to 5", () => {
mockExecFileSync.mockReturnValue("abc123 commit msg\n");
getRecentCommits();
expect(mockExecFileSync).toHaveBeenCalledWith("git", ["log", "--oneline", "-5"], expect.any(Object));
});
it("getRecentCommits accepts custom count", () => {
mockExecFileSync.mockReturnValue("abc123 commit msg\n");
getRecentCommits(10);
expect(mockExecFileSync).toHaveBeenCalledWith("git", ["log", "--oneline", "-10"], expect.any(Object));
});
it("getLastCommit returns single oneline commit", () => {
mockExecFileSync.mockReturnValue("abc123 fix bug\n");
expect(getLastCommit()).toBe("abc123 fix bug");
});
it("getLastCommitTime returns formatted time", () => {
mockExecFileSync.mockReturnValue("2026-03-09 08:00:00 -0700\n");
expect(getLastCommitTime()).toBe("2026-03-09 08:00:00 -0700");
});
it("getStagedFiles calls diff --staged --name-only", () => {
mockExecFileSync.mockReturnValue("src/index.ts\n");
expect(getStagedFiles()).toBe("src/index.ts");
});
});
describe("getDiffFiles()", () => {
it("returns diff files for given ref", () => {
mockExecFileSync.mockReturnValue("src/a.ts\nsrc/b.ts\n");
expect(getDiffFiles("HEAD~5")).toBe("src/a.ts\nsrc/b.ts");
});
it("falls back to HEAD~1 on error", () => {
mockExecFileSync
.mockReturnValueOnce("[command failed: git diff (exit 128)]" as any)
.mockReturnValueOnce("src/c.ts\n");
// First call fails (starts with "["), second succeeds
// But run() catches errors internally, so we need to mock differently
const err: any = new Error("fail");
err.status = 128;
err.stdout = "";
err.stderr = "";
mockExecFileSync.mockReset();
mockExecFileSync
.mockImplementationOnce(() => { throw err; })
.mockReturnValueOnce("src/fallback.ts\n");
expect(getDiffFiles("HEAD~3")).toBe("src/fallback.ts");
});
it("returns 'no commits' when both attempts fail", () => {
const err: any = new Error("fail");
err.status = 128;
err.stdout = "";
err.stderr = "";
mockExecFileSync.mockImplementation(() => { throw err; });
expect(getDiffFiles()).toBe("no commits");
});
});
describe("getDiffStat()", () => {
it("returns diff stat for given ref", () => {
mockExecFileSync.mockReturnValue(" 2 files changed, 10 insertions(+)\n");
expect(getDiffStat("HEAD~2")).toBe("2 files changed, 10 insertions(+)");
});
it("falls back to HEAD~3 on error", () => {
const err: any = new Error("fail");
err.status = 128;
err.stdout = "";
err.stderr = "";
mockExecFileSync
.mockImplementationOnce(() => { throw err; })
.mockReturnValueOnce(" 1 file changed\n");
expect(getDiffStat()).toBe("1 file changed");
});
it("returns fallback message when both fail", () => {
const err: any = new Error("fail");
err.status = 128;
err.stdout = "";
err.stderr = "";
mockExecFileSync.mockImplementation(() => { throw err; });
expect(getDiffStat()).toBe("no diff stats available");
});
});
});