-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-ingest.test.ts
More file actions
138 lines (118 loc) · 4.7 KB
/
Copy pathcommit-ingest.test.ts
File metadata and controls
138 lines (118 loc) · 4.7 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
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { CommitIngester } from "../src/history/commit-ingest.js";
import { EventStore } from "../src/history/event-store.js";
import { execFileSync } from "child_process";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
vi.mock("child_process", () => ({
execFileSync: vi.fn()
}));
describe("CommitIngester", () => {
const testDir = path.join(os.tmpdir(), "refiner-commit-test-" + Date.now());
beforeEach(() => {
process.env.PROMPT_REFINER_GLOBAL_DIR = testDir;
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
// Re-initialize EventStore for each test
(EventStore as any).instance = null;
});
afterEach(() => {
const store = EventStore.getInstance();
store.close();
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
it("should ingest commits from a repo with stats", async () => {
const mockLog = "sha123|author_name|2026-04-12|Commit message";
const mockFiles = "file1.ts\nfile2.ts";
const mockStats = " 2 files changed, 15 insertions(+), 5 deletions(-)";
(execFileSync as any).mockImplementation((_file: string, args: string[]) => {
if (args.includes("log")) return mockLog;
if (args.includes("--shortstat")) return mockStats;
if (args.includes("--name-only")) return mockFiles;
return "";
});
const ingester = new CommitIngester();
const count = await ingester.ingest("C:/repo/test-repo", 1);
expect(count).toBe(1);
const store = EventStore.getInstance();
const db = (store as any).db;
const row = db.prepare("SELECT * FROM commits WHERE sha = ?").get("sha123");
expect(row).toBeDefined();
expect(row.author).toBe("author_name");
expect(row.message).toBe("Commit message");
expect(JSON.parse(row.changed_files_json)).toEqual(["file1.ts", "file2.ts"]);
const stats = JSON.parse(row.diff_stats_json);
expect(stats.insertions).toBe(15);
expect(stats.deletions).toBe(5);
});
it("fails quietly and returns zero for a non-git directory", async () => {
const gitError = new Error("not a git repository");
(execFileSync as any).mockImplementation(() => {
throw gitError;
});
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined);
const ingester = new CommitIngester();
const count = await ingester.ingest(testDir, 1);
expect(count).toBe(0);
expect(execFileSync).toHaveBeenCalledWith(
"git",
["log", "-n", "1", "--pretty=format:%H|%an|%ai|%s"],
expect.objectContaining({
cwd: testDir,
stdio: ["ignore", "pipe", "pipe"],
}),
);
expect(errorSpy).toHaveBeenCalled();
errorSpy.mockRestore();
});
it("uses the static default limit and ignores an empty git log", async () => {
(execFileSync as any).mockReturnValue("");
await expect(CommitIngester.ingestLatest("C:/repo/empty")).resolves.toBe(0);
expect(execFileSync).toHaveBeenCalledWith(
"git",
["log", "-n", "10", "--pretty=format:%H|%an|%ai|%s"],
expect.any(Object),
);
});
it("falls back when the stored SHA is unavailable and tolerates incomplete stats", async () => {
const store = EventStore.getInstance();
const identity = store.ensureRepository("C:/repo/rebased");
store.recordCommit({
id: "old",
repo_id: identity.id,
sha: "old-sha",
author: "author",
message: "old",
committed_at: "2026-01-01T00:00:00Z",
});
(execFileSync as any).mockImplementation((_file: string, args: string[]) => {
if (args[0] === "log" && args[1] === "old-sha..HEAD") {
throw new Error("unknown revision");
}
if (args[0] === "log") {
return "\nsha-new|author|2026-01-02T00:00:00Z|new\nsha-statless|author|2026-01-03T00:00:00Z|statless";
}
if (args.includes("--name-only")) {
return "\nsrc/new.ts\n";
}
if (args.includes("--shortstat") && args.at(-1) === "sha-statless") {
throw new Error("shortstat unavailable");
}
if (args.includes("--shortstat")) {
return " 1 file changed";
}
return "";
});
await expect(new CommitIngester().ingest("C:/repo/rebased", 2)).resolves.toBe(2);
const rows = (store as any).db.prepare("SELECT sha, diff_stats_json FROM commits WHERE repo_id = ? ORDER BY sha").all(identity.id);
expect(rows).toMatchObject([
{ sha: "old-sha" },
{ sha: "sha-new", diff_stats_json: JSON.stringify({ insertions: 0, deletions: 0 }) },
{ sha: "sha-statless", diff_stats_json: JSON.stringify({ insertions: 0, deletions: 0 }) },
]);
});
});