-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathgetDiff.test.ts
More file actions
234 lines (209 loc) · 6.49 KB
/
Copy pathgetDiff.test.ts
File metadata and controls
234 lines (209 loc) · 6.49 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { describe, it, expect } from 'vitest';
import { GetDiffResult } from '@/features/git';
// Extract the formatting function for testing
function formatDiffAsGitDiff(result: GetDiffResult): string {
let output = '';
for (const file of result.files) {
const oldPath = file.oldPath ?? '/dev/null';
const newPath = file.newPath ?? '/dev/null';
output += `--- a/${oldPath}\n`;
output += `+++ b/${newPath}\n`;
for (const hunk of file.hunks) {
const oldStart = hunk.oldRange.start;
const oldLines = hunk.oldRange.lines;
const newStart = hunk.newRange.start;
const newLines = hunk.newRange.lines;
output += `@@ -${oldStart},${oldLines} +${newStart},${newLines} @@`;
if (hunk.heading) {
output += ` ${hunk.heading}`;
}
output += '\n';
output += hunk.body;
if (!hunk.body.endsWith('\n')) {
output += '\n';
}
}
}
return output;
}
describe('formatDiffAsGitDiff', () => {
it('should format a simple file change correctly', () => {
const input: GetDiffResult = {
files: [
{
oldPath: 'file.txt',
newPath: 'file.txt',
hunks: [
{
oldRange: { start: 1, lines: 3 },
newRange: { start: 1, lines: 4 },
heading: undefined,
body: ' context line\n-removed line\n+added line\n context line',
},
],
},
],
};
const expected = `--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
context line
-removed line
+added line
context line
`;
expect(formatDiffAsGitDiff(input)).toBe(expected);
});
it('should handle file deletion', () => {
const input: GetDiffResult = {
files: [
{
oldPath: 'deleted.txt',
newPath: null,
hunks: [
{
oldRange: { start: 1, lines: 2 },
newRange: { start: 0, lines: 0 },
heading: undefined,
body: '-line 1\n-line 2',
},
],
},
],
};
const expected = `--- a/deleted.txt
+++ b//dev/null
@@ -1,2 +0,0 @@
-line 1
-line 2
`;
expect(formatDiffAsGitDiff(input)).toBe(expected);
});
it('should handle file addition', () => {
const input: GetDiffResult = {
files: [
{
oldPath: null,
newPath: 'new.txt',
hunks: [
{
oldRange: { start: 0, lines: 0 },
newRange: { start: 1, lines: 2 },
heading: undefined,
body: '+line 1\n+line 2',
},
],
},
],
};
const expected = `--- a//dev/null
+++ b/new.txt
@@ -0,0 +1,2 @@
+line 1
+line 2
`;
expect(formatDiffAsGitDiff(input)).toBe(expected);
});
it('should include hunk heading when present', () => {
const input: GetDiffResult = {
files: [
{
oldPath: 'code.ts',
newPath: 'code.ts',
hunks: [
{
oldRange: { start: 10, lines: 5 },
newRange: { start: 10, lines: 6 },
heading: 'function myFunction()',
body: ' function myFunction() {\n+ console.log("new line");\n return true;\n }',
},
],
},
],
};
const expected = `--- a/code.ts
+++ b/code.ts
@@ -10,5 +10,6 @@ function myFunction()
function myFunction() {
+ console.log("new line");
return true;
}
`;
expect(formatDiffAsGitDiff(input)).toBe(expected);
});
it('should handle multiple files', () => {
const input: GetDiffResult = {
files: [
{
oldPath: 'file1.txt',
newPath: 'file1.txt',
hunks: [
{
oldRange: { start: 1, lines: 1 },
newRange: { start: 1, lines: 2 },
heading: undefined,
body: ' old\n+new',
},
],
},
{
oldPath: 'file2.txt',
newPath: 'file2.txt',
hunks: [
{
oldRange: { start: 1, lines: 1 },
newRange: { start: 1, lines: 1 },
heading: undefined,
body: ' unchanged',
},
],
},
],
};
const expected = `--- a/file1.txt
+++ b/file1.txt
@@ -1,1 +1,2 @@
old
+new
--- a/file2.txt
+++ b/file2.txt
@@ -1,1 +1,1 @@
unchanged
`;
expect(formatDiffAsGitDiff(input)).toBe(expected);
});
it('should handle multiple hunks in a single file', () => {
const input: GetDiffResult = {
files: [
{
oldPath: 'file.txt',
newPath: 'file.txt',
hunks: [
{
oldRange: { start: 1, lines: 2 },
newRange: { start: 1, lines: 2 },
heading: undefined,
body: ' line 1\n+line 2',
},
{
oldRange: { start: 10, lines: 2 },
newRange: { start: 11, lines: 2 },
heading: undefined,
body: '-line 10\n line 11',
},
],
},
],
};
const expected = `--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,2 @@
line 1
+line 2
@@ -10,2 +11,2 @@
-line 10
line 11
`;
expect(formatDiffAsGitDiff(input)).toBe(expected);
});
});