-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.test.ts
More file actions
213 lines (187 loc) · 6.42 KB
/
git.test.ts
File metadata and controls
213 lines (187 loc) · 6.42 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
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import * as child_process from "child_process";
// Mock execFileSync before importing git module
vi.mock("child_process", () => ({
execFileSync: vi.fn(),
}));
vi.mock("../../src/lib/files.js", () => ({
PROJECT_DIR: "/fake/project",
}));
// Now import the module under test
import {
run,
getBranch,
getStatus,
getRecentCommits,
getLastCommit,
getLastCommitTime,
getDiffFiles,
getStagedFiles,
getDiffStat,
} from "../../src/lib/git.js";
const mockExecFileSync = vi.mocked(child_process.execFileSync);
beforeEach(() => {
vi.clearAllMocks();
});
describe("run()", () => {
it("accepts an array of args and returns trimmed stdout", () => {
mockExecFileSync.mockReturnValue(" result \n");
const result = run(["log", "--oneline", "-5"]);
expect(result).toBe("result");
expect(mockExecFileSync).toHaveBeenCalledWith(
"git",
["log", "--oneline", "-5"],
expect.objectContaining({ cwd: "/fake/project", encoding: "utf-8" })
);
});
it("splits a string command on whitespace", () => {
mockExecFileSync.mockReturnValue("ok\n");
run("status --short");
expect(mockExecFileSync).toHaveBeenCalledWith(
"git",
["status", "--short"],
expect.any(Object)
);
});
it("returns descriptive error on failure with stderr", () => {
const error = new Error("fail") as any;
error.stderr = "fatal: not a git repo\n";
error.stdout = "";
error.status = 128;
mockExecFileSync.mockImplementation(() => { throw error; });
const result = run(["status"]);
expect(result).toBe("fatal: not a git repo");
});
it("returns timeout message when process is killed", () => {
const error = new Error("killed") as any;
error.killed = true;
error.signal = "SIGTERM";
mockExecFileSync.mockImplementation(() => { throw error; });
const result = run(["log"], { timeout: 5000 });
expect(result).toBe("[timed out after 5000ms]");
});
it("returns git-not-found message on ENOENT", () => {
const error = new Error("spawn git ENOENT") as any;
error.code = "ENOENT";
mockExecFileSync.mockImplementation(() => { throw error; });
const result = run(["status"]);
expect(result).toBe("[git not found]");
});
it("returns command-failed with exit code when no stderr/stdout", () => {
const error = new Error("exit 1") as any;
error.status = 1;
error.stdout = "";
error.stderr = "";
mockExecFileSync.mockImplementation(() => { throw error; });
const result = run(["bad-command"]);
expect(result).toBe("[command failed: git bad-command (exit 1)]");
});
it("uses default 10s timeout", () => {
mockExecFileSync.mockReturnValue("");
run(["status"]);
expect(mockExecFileSync).toHaveBeenCalledWith(
"git",
["status"],
expect.objectContaining({ timeout: 10000 })
);
});
it("respects custom timeout", () => {
mockExecFileSync.mockReturnValue("");
run(["log"], { timeout: 30000 });
expect(mockExecFileSync).toHaveBeenCalledWith(
"git",
["log"],
expect.objectContaining({ timeout: 30000 })
);
});
});
describe("getBranch()", () => {
it("returns current branch name", () => {
mockExecFileSync.mockReturnValue("main\n");
expect(getBranch()).toBe("main");
expect(mockExecFileSync).toHaveBeenCalledWith(
"git",
["branch", "--show-current"],
expect.any(Object)
);
});
});
describe("getStatus()", () => {
it("returns short status", () => {
mockExecFileSync.mockReturnValue(" M src/index.ts\n?? new-file.ts\n");
expect(getStatus()).toBe("M src/index.ts\n?? new-file.ts");
});
});
describe("getRecentCommits()", () => {
it("defaults to 5 commits", () => {
mockExecFileSync.mockReturnValue("abc123 first\ndef456 second\n");
getRecentCommits();
expect(mockExecFileSync).toHaveBeenCalledWith(
"git",
["log", "--oneline", "-5"],
expect.any(Object)
);
});
it("accepts custom count", () => {
mockExecFileSync.mockReturnValue("");
getRecentCommits(10);
expect(mockExecFileSync).toHaveBeenCalledWith(
"git",
["log", "--oneline", "-10"],
expect.any(Object)
);
});
});
describe("getLastCommit()", () => {
it("returns last commit oneline", () => {
mockExecFileSync.mockReturnValue("abc123 fix: stuff\n");
expect(getLastCommit()).toBe("abc123 fix: stuff");
});
});
describe("getLastCommitTime()", () => {
it("returns last commit timestamp", () => {
mockExecFileSync.mockReturnValue("2026-03-04 21:00:00 -0700\n");
expect(getLastCommitTime()).toBe("2026-03-04 21:00:00 -0700");
});
});
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 when ref fails", () => {
mockExecFileSync
.mockImplementationOnce(() => { throw Object.assign(new Error("fail"), { status: 128, stdout: "", stderr: "" }); })
.mockReturnValueOnce("src/c.ts\n");
const result = getDiffFiles("HEAD~10");
expect(result).toBe("src/c.ts");
});
it("returns 'no commits' when both refs fail", () => {
const err = Object.assign(new Error("fail"), { status: 128, stdout: "", stderr: "" });
mockExecFileSync.mockImplementation(() => { throw err; });
expect(getDiffFiles()).toBe("no commits");
});
});
describe("getStagedFiles()", () => {
it("returns staged file names", () => {
mockExecFileSync.mockReturnValue("src/staged.ts\n");
expect(getStagedFiles()).toBe("src/staged.ts");
});
});
describe("getDiffStat()", () => {
it("returns diff stat for given ref", () => {
mockExecFileSync.mockReturnValue(" 2 files changed, 10 insertions(+)\n");
expect(getDiffStat("HEAD~3")).toBe("2 files changed, 10 insertions(+)");
});
it("falls back to HEAD~3 when ref fails", () => {
mockExecFileSync
.mockImplementationOnce(() => { throw Object.assign(new Error("fail"), { status: 128, stdout: "", stderr: "" }); })
.mockReturnValueOnce(" 1 file changed\n");
expect(getDiffStat("HEAD~10")).toBe("1 file changed");
});
it("returns fallback message when both refs fail", () => {
const err = Object.assign(new Error("fail"), { status: 128, stdout: "", stderr: "" });
mockExecFileSync.mockImplementation(() => { throw err; });
expect(getDiffStat()).toBe("no diff stats available");
});
});