Skip to content

Commit f66c17e

Browse files
test: add unit tests for getDiff git-diff formatting
Co-authored-by: Brendan Kellam <brendan@sourcebot.dev>
1 parent 2426013 commit f66c17e

1 file changed

Lines changed: 234 additions & 0 deletions

File tree

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { GetDiffResult } from '@/features/git';
3+
4+
// Extract the formatting function for testing
5+
function formatDiffAsGitDiff(result: GetDiffResult): string {
6+
let output = '';
7+
8+
for (const file of result.files) {
9+
const oldPath = file.oldPath ?? '/dev/null';
10+
const newPath = file.newPath ?? '/dev/null';
11+
12+
output += `--- a/${oldPath}\n`;
13+
output += `+++ b/${newPath}\n`;
14+
15+
for (const hunk of file.hunks) {
16+
const oldStart = hunk.oldRange.start;
17+
const oldLines = hunk.oldRange.lines;
18+
const newStart = hunk.newRange.start;
19+
const newLines = hunk.newRange.lines;
20+
21+
output += `@@ -${oldStart},${oldLines} +${newStart},${newLines} @@`;
22+
if (hunk.heading) {
23+
output += ` ${hunk.heading}`;
24+
}
25+
output += '\n';
26+
27+
output += hunk.body;
28+
if (!hunk.body.endsWith('\n')) {
29+
output += '\n';
30+
}
31+
}
32+
}
33+
34+
return output;
35+
}
36+
37+
describe('formatDiffAsGitDiff', () => {
38+
it('should format a simple file change correctly', () => {
39+
const input: GetDiffResult = {
40+
files: [
41+
{
42+
oldPath: 'file.txt',
43+
newPath: 'file.txt',
44+
hunks: [
45+
{
46+
oldRange: { start: 1, lines: 3 },
47+
newRange: { start: 1, lines: 4 },
48+
heading: undefined,
49+
body: ' context line\n-removed line\n+added line\n context line',
50+
},
51+
],
52+
},
53+
],
54+
};
55+
56+
const expected = `--- a/file.txt
57+
+++ b/file.txt
58+
@@ -1,3 +1,4 @@
59+
context line
60+
-removed line
61+
+added line
62+
context line
63+
`;
64+
65+
expect(formatDiffAsGitDiff(input)).toBe(expected);
66+
});
67+
68+
it('should handle file deletion', () => {
69+
const input: GetDiffResult = {
70+
files: [
71+
{
72+
oldPath: 'deleted.txt',
73+
newPath: null,
74+
hunks: [
75+
{
76+
oldRange: { start: 1, lines: 2 },
77+
newRange: { start: 0, lines: 0 },
78+
heading: undefined,
79+
body: '-line 1\n-line 2',
80+
},
81+
],
82+
},
83+
],
84+
};
85+
86+
const expected = `--- a/deleted.txt
87+
+++ b//dev/null
88+
@@ -1,2 +0,0 @@
89+
-line 1
90+
-line 2
91+
`;
92+
93+
expect(formatDiffAsGitDiff(input)).toBe(expected);
94+
});
95+
96+
it('should handle file addition', () => {
97+
const input: GetDiffResult = {
98+
files: [
99+
{
100+
oldPath: null,
101+
newPath: 'new.txt',
102+
hunks: [
103+
{
104+
oldRange: { start: 0, lines: 0 },
105+
newRange: { start: 1, lines: 2 },
106+
heading: undefined,
107+
body: '+line 1\n+line 2',
108+
},
109+
],
110+
},
111+
],
112+
};
113+
114+
const expected = `--- a//dev/null
115+
+++ b/new.txt
116+
@@ -0,0 +1,2 @@
117+
+line 1
118+
+line 2
119+
`;
120+
121+
expect(formatDiffAsGitDiff(input)).toBe(expected);
122+
});
123+
124+
it('should include hunk heading when present', () => {
125+
const input: GetDiffResult = {
126+
files: [
127+
{
128+
oldPath: 'code.ts',
129+
newPath: 'code.ts',
130+
hunks: [
131+
{
132+
oldRange: { start: 10, lines: 5 },
133+
newRange: { start: 10, lines: 6 },
134+
heading: 'function myFunction()',
135+
body: ' function myFunction() {\n+ console.log("new line");\n return true;\n }',
136+
},
137+
],
138+
},
139+
],
140+
};
141+
142+
const expected = `--- a/code.ts
143+
+++ b/code.ts
144+
@@ -10,5 +10,6 @@ function myFunction()
145+
function myFunction() {
146+
+ console.log("new line");
147+
return true;
148+
}
149+
`;
150+
151+
expect(formatDiffAsGitDiff(input)).toBe(expected);
152+
});
153+
154+
it('should handle multiple files', () => {
155+
const input: GetDiffResult = {
156+
files: [
157+
{
158+
oldPath: 'file1.txt',
159+
newPath: 'file1.txt',
160+
hunks: [
161+
{
162+
oldRange: { start: 1, lines: 1 },
163+
newRange: { start: 1, lines: 2 },
164+
heading: undefined,
165+
body: ' old\n+new',
166+
},
167+
],
168+
},
169+
{
170+
oldPath: 'file2.txt',
171+
newPath: 'file2.txt',
172+
hunks: [
173+
{
174+
oldRange: { start: 1, lines: 1 },
175+
newRange: { start: 1, lines: 1 },
176+
heading: undefined,
177+
body: ' unchanged',
178+
},
179+
],
180+
},
181+
],
182+
};
183+
184+
const expected = `--- a/file1.txt
185+
+++ b/file1.txt
186+
@@ -1,1 +1,2 @@
187+
old
188+
+new
189+
--- a/file2.txt
190+
+++ b/file2.txt
191+
@@ -1,1 +1,1 @@
192+
unchanged
193+
`;
194+
195+
expect(formatDiffAsGitDiff(input)).toBe(expected);
196+
});
197+
198+
it('should handle multiple hunks in a single file', () => {
199+
const input: GetDiffResult = {
200+
files: [
201+
{
202+
oldPath: 'file.txt',
203+
newPath: 'file.txt',
204+
hunks: [
205+
{
206+
oldRange: { start: 1, lines: 2 },
207+
newRange: { start: 1, lines: 2 },
208+
heading: undefined,
209+
body: ' line 1\n+line 2',
210+
},
211+
{
212+
oldRange: { start: 10, lines: 2 },
213+
newRange: { start: 11, lines: 2 },
214+
heading: undefined,
215+
body: '-line 10\n line 11',
216+
},
217+
],
218+
},
219+
],
220+
};
221+
222+
const expected = `--- a/file.txt
223+
+++ b/file.txt
224+
@@ -1,2 +1,2 @@
225+
line 1
226+
+line 2
227+
@@ -10,2 +11,2 @@
228+
-line 10
229+
line 11
230+
`;
231+
232+
expect(formatDiffAsGitDiff(input)).toBe(expected);
233+
});
234+
});

0 commit comments

Comments
 (0)