Elffuss BraidHub

kikocis/continue public

87f75892af56 · 10.5 KiBRawPermalink
1const { annotateDiff } = require("./annotate-diff.js");
2
3describe("annotateDiff", () => {
4 test("single file, single hunk", () => {
5 const diff = `diff --git a/file.js b/file.js
6index abc123..def456 100644
7--- a/file.js
8+++ b/file.js
9@@ -1,3 +1,4 @@
10 line1
11 line2
12+added line
13 line3`;
14
15 const result = annotateDiff(diff);
16 expect(result).toContain("[POS:1] line1");
17 expect(result).toContain("[POS:2] line2");
18 expect(result).toContain("[POS:3] +added line");
19 expect(result).toContain("[POS:4] line3");
20 });
21
22 test("single file, multiple hunks with +1 between", () => {
23 const diff = `diff --git a/file.js b/file.js
24index abc123..def456 100644
25--- a/file.js
26+++ b/file.js
27@@ -1,3 +1,3 @@
28 line1
29-old line
30+new line
31 line3
32@@ -10,3 +10,3 @@
33 line10
34-old line 11
35+new line 11
36 line12`;
37
38 const result = annotateDiff(diff);
39
40 // First hunk
41 expect(result).toContain("[POS:1] line1");
42 expect(result).toContain("[POS:2] -old line");
43 expect(result).toContain("[POS:3] +new line");
44 expect(result).toContain("[POS:4] line3");
45
46 // Second hunk (should start at 6, because 4 lines + 1 for between hunks)
47 expect(result).toContain("[POS:6] line10");
48 expect(result).toContain("[POS:7] -old line 11");
49 expect(result).toContain("[POS:8] +new line 11");
50 expect(result).toContain("[POS:9] line12");
51 });
52
53 test("multiple files reset position counter", () => {
54 const diff = `diff --git a/file1.js b/file1.js
55index abc123..def456 100644
56--- a/file1.js
57+++ b/file1.js
58@@ -1,2 +1,2 @@
59 line1
60-old
61+new
62diff --git a/file2.js b/file2.js
63index abc123..def456 100644
64--- a/file2.js
65+++ b/file2.js
66@@ -1,2 +1,2 @@
67 first
68-old2
69+new2`;
70
71 const result = annotateDiff(diff);
72
73 // File 1
74 expect(result).toContain("[POS:1] line1");
75 expect(result).toContain("[POS:2] -old");
76 expect(result).toContain("[POS:3] +new");
77
78 // File 2 - positions should reset
79 const file2Section = result.split("diff --git a/file2.js")[1];
80 expect(file2Section).toContain("[POS:1] first");
81 expect(file2Section).toContain("[POS:2] -old2");
82 expect(file2Section).toContain("[POS:3] +new2");
83 });
84
85 test("handles new file", () => {
86 const diff = `diff --git a/newfile.js b/newfile.js
87new file mode 100644
88index 0000000..abc123
89--- /dev/null
90+++ b/newfile.js
91@@ -0,0 +1,3 @@
92+line1
93+line2
94+line3`;
95
96 const result = annotateDiff(diff);
97 expect(result).toContain("new file mode 100644");
98 expect(result).toContain("[POS:1] +line1");
99 expect(result).toContain("[POS:2] +line2");
100 expect(result).toContain("[POS:3] +line3");
101 });
102
103 test("handles deleted file", () => {
104 const diff = `diff --git a/deleted.js b/deleted.js
105deleted file mode 100644
106index abc123..0000000
107--- a/deleted.js
108+++ /dev/null
109@@ -1,3 +0,0 @@
110-line1
111-line2
112-line3`;
113
114 const result = annotateDiff(diff);
115 expect(result).toContain("deleted file mode 100644");
116 expect(result).toContain("[POS:1] -line1");
117 expect(result).toContain("[POS:2] -line2");
118 expect(result).toContain("[POS:3] -line3");
119 });
120
121 test("handles renamed file with changes", () => {
122 const diff = `diff --git a/old-name.js b/new-name.js
123similarity index 95%
124rename from old-name.js
125rename to new-name.js
126index abc123..def456 100644
127--- a/old-name.js
128+++ b/new-name.js
129@@ -1,3 +1,3 @@
130 line1
131-old line
132+new line
133 line3`;
134
135 const result = annotateDiff(diff);
136 expect(result).toContain("rename from old-name.js");
137 expect(result).toContain("rename to new-name.js");
138 expect(result).toContain("[POS:1] line1");
139 expect(result).toContain("[POS:2] -old line");
140 expect(result).toContain("[POS:3] +new line");
141 });
142
143 test("handles no newline at end of file", () => {
144 const diff = `diff --git a/file.js b/file.js
145index abc123..def456 100644
146--- a/file.js
147+++ b/file.js
148@@ -1,2 +1,2 @@
149 line1
150-old line
151\\ No newline at end of file
152+new line
153\\ No newline at end of file`;
154
155 const result = annotateDiff(diff);
156 expect(result).toContain("[POS:1] line1");
157 expect(result).toContain("[POS:2] -old line");
158 // Position 3 is the first "No newline" marker (not annotated but counted)
159 expect(result).toContain("\\ No newline at end of file");
160 expect(result).toContain("[POS:4] +new line"); // Position 4 due to "No newline" at position 3
161 // Position 5 is the second "No newline" marker (not annotated but counted)
162 expect(result).toContain("\\ No newline at end of file");
163 // No newline markers should not get [POS:N] annotations
164 expect(result).not.toContain("[POS:3] \\");
165 expect(result).not.toContain("[POS:5] \\");
166 });
167
168 test("handles file mode changes", () => {
169 const diff = `diff --git a/script.sh b/script.sh
170old mode 100644
171new mode 100755
172index abc123..def456 100644
173--- a/script.sh
174+++ b/script.sh
175@@ -1,2 +1,2 @@
176 #!/bin/bash
177-echo "old"
178+echo "new"`;
179
180 const result = annotateDiff(diff);
181 expect(result).toContain("old mode 100644");
182 expect(result).toContain("new mode 100755");
183 expect(result).toContain("[POS:1] #!/bin/bash");
184 expect(result).toContain('[POS:2] -echo "old"');
185 expect(result).toContain('[POS:3] +echo "new"');
186 });
187
188 test("empty diff returns empty", () => {
189 const diff = "";
190 const result = annotateDiff(diff);
191 expect(result).toBe("");
192 });
193
194 test("handles binary files - modified", () => {
195 const diff = `diff --git a/image.png b/image.png
196index abc123..def456 100644
197Binary files a/image.png and b/image.png differ`;
198
199 const result = annotateDiff(diff);
200 expect(result).toContain("diff --git a/image.png b/image.png");
201 expect(result).toContain("Binary files a/image.png and b/image.png differ");
202 // Binary files line should not be annotated
203 expect(result).not.toContain("[POS:");
204 });
205
206 test("handles binary files - added", () => {
207 const diff = `diff --git a/newimage.png b/newimage.png
208new file mode 100644
209index 0000000..abc123
210Binary files /dev/null and b/newimage.png differ`;
211
212 const result = annotateDiff(diff);
213 expect(result).toContain("diff --git a/newimage.png b/newimage.png");
214 expect(result).toContain("new file mode 100644");
215 expect(result).toContain(
216 "Binary files /dev/null and b/newimage.png differ",
217 );
218 // Binary files line should not be annotated
219 expect(result).not.toContain("[POS:");
220 });
221
222 test("handles binary files - deleted", () => {
223 const diff = `diff --git a/oldimage.png b/oldimage.png
224deleted file mode 100644
225index abc123..0000000
226Binary files a/oldimage.png and /dev/null differ`;
227
228 const result = annotateDiff(diff);
229 expect(result).toContain("diff --git a/oldimage.png b/oldimage.png");
230 expect(result).toContain("deleted file mode 100644");
231 expect(result).toContain(
232 "Binary files a/oldimage.png and /dev/null differ",
233 );
234 // Binary files line should not be annotated
235 expect(result).not.toContain("[POS:");
236 });
237
238 test("handles mixed text and binary files", () => {
239 const diff = `diff --git a/text.js b/text.js
240index abc123..def456 100644
241--- a/text.js
242+++ b/text.js
243@@ -1,2 +1,2 @@
244 line1
245-old
246+new
247diff --git a/image.png b/image.png
248index 111111..222222 100644
249Binary files a/image.png and b/image.png differ
250diff --git a/another.js b/another.js
251index 333333..444444 100644
252--- a/another.js
253+++ b/another.js
254@@ -1,1 +1,1 @@
255-foo
256+bar`;
257
258 const result = annotateDiff(diff);
259
260 // First file (text)
261 expect(result).toContain("[POS:1] line1");
262 expect(result).toContain("[POS:2] -old");
263 expect(result).toContain("[POS:3] +new");
264
265 // Second file (binary) - no positions
266 expect(result).toContain("Binary files a/image.png and b/image.png differ");
267
268 // Third file (text) - positions reset
269 const lastFileSection = result.split("diff --git a/another.js")[1];
270 expect(lastFileSection).toContain("[POS:1] -foo");
271 expect(lastFileSection).toContain("[POS:2] +bar");
272 });
273
274 test("multiple no newline markers accumulate positions", () => {
275 const diff = `diff --git a/file.js b/file.js
276index abc123..def456 100644
277--- a/file.js
278+++ b/file.js
279@@ -1,3 +1,3 @@
280 line1
281-old1
282\\ No newline at end of file
283+new1
284\\ No newline at end of file
285@@ -10,3 +10,3 @@
286 line10
287-old2
288\\ No newline at end of file
289+new2
290\\ No newline at end of file`;
291
292 const result = annotateDiff(diff);
293
294 // First hunk
295 expect(result).toContain("[POS:1] line1");
296 expect(result).toContain("[POS:2] -old1");
297 // Position 3 is the first "No newline" marker
298 expect(result).toContain("[POS:4] +new1");
299 // Position 5 is the second "No newline" marker
300
301 // Second hunk (starts at position 7 = 5 + 1 for between hunks + 1)
302 expect(result).toContain("[POS:7] line10");
303 expect(result).toContain("[POS:8] -old2");
304 // Position 9 is the third "No newline" marker
305 expect(result).toContain("[POS:10] +new2");
306 // Position 11 is the fourth "No newline" marker
307
308 // Verify "No newline" markers are not annotated
309 expect(result).not.toContain("[POS:3] \\");
310 expect(result).not.toContain("[POS:5] \\");
311 expect(result).not.toContain("[POS:9] \\");
312 expect(result).not.toContain("[POS:11] \\");
313 });
314
315 test("complex multi-file, multi-hunk diff", () => {
316 const diff = `diff --git a/src/app.js b/src/app.js
317index abc123..def456 100644
318--- a/src/app.js
319+++ b/src/app.js
320@@ -10,4 +10,5 @@ function example() {
321 console.log("start");
322 const x = 1;
323- const y = 2;
324+ const y = 3;
325+ const z = 4;
326 return x + y;
327@@ -20,3 +21,4 @@ function another() {
328 let a = 1;
329 let b = 2;
330+ let c = 3;
331 return a + b;
332diff --git a/src/util.js b/src/util.js
333index 111111..222222 100644
334--- a/src/util.js
335+++ b/src/util.js
336@@ -1,2 +1,3 @@
337+import fs from 'fs';
338 export function util() {
339 return true;`;
340
341 const result = annotateDiff(diff);
342
343 // src/app.js - first hunk
344 expect(result).toContain('[POS:1] console.log("start");');
345 expect(result).toContain("[POS:2] const x = 1;");
346 expect(result).toContain("[POS:3] - const y = 2;");
347 expect(result).toContain("[POS:4] + const y = 3;");
348 expect(result).toContain("[POS:5] + const z = 4;");
349 expect(result).toContain("[POS:6] return x + y;");
350
351 // src/app.js - second hunk (7 = 6 + 1 for between hunks)
352 expect(result).toContain("[POS:8] let a = 1;");
353 expect(result).toContain("[POS:9] let b = 2;");
354 expect(result).toContain("[POS:10] + let c = 3;");
355 expect(result).toContain("[POS:11] return a + b;");
356
357 // src/util.js - new file, positions reset
358 const utilSection = result.split("diff --git a/src/util.js")[1];
359 expect(utilSection).toContain("[POS:1] +import fs from 'fs';");
360 expect(utilSection).toContain("[POS:2] export function util() {");
361 expect(utilSection).toContain("[POS:3] return true;");
362 });
363});