Elffuss BraidHub

kikocis/continue public

main · 11.5 KiBRawPermalink
1import { describe, expect, test } from "vitest";
2import { Position } from "../..";
3import {
4 contextSnippetsBlock,
5 currentFileContentBlock,
6 editHistoryBlock,
7} from "./instinct";
8
9describe("contextSnippetsBlock", () => {
10 test("should format empty context snippets", () => {
11 const result = contextSnippetsBlock("");
12 expect(result).toBe("");
13 });
14
15 test("should format single context snippet with codestral style header", () => {
16 const input = `+++++ src/utils.ts
17function helper() {
18 return "test";
19}`;
20
21 const expected = `<|context_file|>: src/utils.ts
22<|snippet|>
23function helper() {
24 return "test";
25}`;
26
27 const result = contextSnippetsBlock(input);
28 expect(result).toBe(expected);
29 });
30
31 test("should format multiple context snippets", () => {
32 const input = `+++++ src/file1.ts
33const var1 = "value1";
34
35+++++ src/file2.ts
36const var2 = "value2";
37function test() {}`;
38
39 const expected = `<|context_file|>: src/file1.ts
40<|snippet|>
41const var1 = "value1";
42
43<|context_file|>: src/file2.ts
44<|snippet|>
45const var2 = "value2";
46function test() {}`;
47
48 const result = contextSnippetsBlock(input);
49 expect(result).toBe(expected);
50 });
51
52 test("should handle context snippet without content", () => {
53 const input = "+++++ src/empty.ts";
54
55 const expected = `<|context_file|>: src/empty.ts`;
56
57 const result = contextSnippetsBlock(input);
58 expect(result).toBe(expected);
59 });
60
61 test("should handle malformed headers gracefully", () => {
62 const input = `not a header
63+++++ src/valid.ts
64valid content`;
65
66 const expected = `not a header
67<|context_file|>: src/valid.ts
68<|snippet|>
69valid content`;
70
71 const result = contextSnippetsBlock(input);
72 expect(result).toBe(expected);
73 });
74
75 test("should handle multiple consecutive headers", () => {
76 const input = `+++++ src/file1.ts
77+++++ src/file2.ts
78content after headers`;
79
80 const expected = `<|context_file|>: src/file1.ts
81<|context_file|>: src/file2.ts
82<|snippet|>
83content after headers`;
84
85 const result = contextSnippetsBlock(input);
86 expect(result).toBe(expected);
87 });
88});
89
90describe("currentFileContentBlock", () => {
91 test("should insert cursor and editable region tokens correctly", () => {
92 const content = `line 0
93line 1
94line 2
95line 3
96line 4`;
97 const windowStart = 0;
98 const windowEnd = 4;
99 const cursorPosition: Position = { line: 2, character: 4 };
100 const editableRegionStartLine = 1;
101 const editableRegionEndLine = 3;
102
103 const result = currentFileContentBlock(
104 content,
105 windowStart,
106 windowEnd,
107 editableRegionStartLine,
108 editableRegionEndLine,
109 cursorPosition,
110 );
111
112 const expected = `line 0
113<|editable_region_start|>
114line 1
115line<|user_cursor_is_here|> 2
116line 3
117<|editable_region_end|>
118line 4`;
119
120 expect(result).toBe(expected);
121 });
122
123 test("should handle cursor at beginning of line", () => {
124 const content = `line 0
125line 1
126line 2`;
127 const windowStart = 0;
128 const windowEnd = 2;
129 const cursorPosition: Position = { line: 1, character: 0 };
130 const editableRegionStartLine = 0;
131 const editableRegionEndLine = 2;
132
133 const result = currentFileContentBlock(
134 content,
135 windowStart,
136 windowEnd,
137 editableRegionStartLine,
138 editableRegionEndLine,
139 cursorPosition,
140 );
141
142 const expected = `<|editable_region_start|>
143line 0
144<|user_cursor_is_here|>line 1
145line 2
146<|editable_region_end|>`;
147
148 expect(result).toBe(expected);
149 });
150
151 test("should handle cursor at end of line", () => {
152 const content = `line 0
153line 1
154line 2`;
155 const windowStart = 0;
156 const windowEnd = 2;
157 const cursorPosition: Position = { line: 1, character: 6 }; // end of "line 1"
158 const editableRegionStartLine = 0;
159 const editableRegionEndLine = 2;
160
161 const result = currentFileContentBlock(
162 content,
163 windowStart,
164 windowEnd,
165 editableRegionStartLine,
166 editableRegionEndLine,
167 cursorPosition,
168 );
169
170 const expected = `<|editable_region_start|>
171line 0
172line 1<|user_cursor_is_here|>
173line 2
174<|editable_region_end|>`;
175
176 expect(result).toBe(expected);
177 });
178
179 test("should handle single line editable region", () => {
180 const content = `line 0
181line 1
182line 2`;
183 const windowStart = 0;
184 const windowEnd = 2;
185 const cursorPosition: Position = { line: 1, character: 2 };
186 const editableRegionStartLine = 1;
187 const editableRegionEndLine = 1;
188
189 const result = currentFileContentBlock(
190 content,
191 windowStart,
192 windowEnd,
193 editableRegionStartLine,
194 editableRegionEndLine,
195 cursorPosition,
196 );
197
198 const expected = `line 0
199<|editable_region_start|>
200li<|user_cursor_is_here|>ne 1
201<|editable_region_end|>
202line 2`;
203
204 expect(result).toBe(expected);
205 });
206
207 test("should handle cursor outside bounds gracefully (this shouldn't happen)", () => {
208 const content = `line 0
209line 1`;
210 const windowStart = 0;
211 const windowEnd = 1;
212 const cursorPosition: Position = { line: 10, character: 0 }; // out of bounds
213 const editableRegionStartLine = 0;
214 const editableRegionEndLine = 1;
215
216 const result = currentFileContentBlock(
217 content,
218 windowStart,
219 windowEnd,
220 editableRegionStartLine,
221 editableRegionEndLine,
222 cursorPosition,
223 );
224
225 const expected = `<|editable_region_start|>
226line 0
227line 1
228<|editable_region_end|>`;
229
230 expect(result).toBe(expected);
231 });
232
233 test("should handle editable region covering entire file", () => {
234 const content = `line 0
235line 1
236line 2`;
237 const windowStart = 0;
238 const windowEnd = 2;
239 const cursorPosition: Position = { line: 1, character: 0 };
240 const editableRegionStartLine = 0;
241 const editableRegionEndLine = 2;
242
243 const result = currentFileContentBlock(
244 content,
245 windowStart,
246 windowEnd,
247 editableRegionStartLine,
248 editableRegionEndLine,
249 cursorPosition,
250 );
251
252 const expected = `<|editable_region_start|>
253line 0
254<|user_cursor_is_here|>line 1
255line 2
256<|editable_region_end|>`;
257
258 expect(result).toBe(expected);
259 });
260
261 test("should handle windowing - content outside window not included", () => {
262 const content = `line 0
263line 1
264line 2
265line 3
266line 4`;
267 const windowStart = 1;
268 const windowEnd = 3;
269 const cursorPosition: Position = { line: 2, character: 2 };
270 const editableRegionStartLine = 2;
271 const editableRegionEndLine = 2;
272
273 const result = currentFileContentBlock(
274 content,
275 windowStart,
276 windowEnd,
277 editableRegionStartLine,
278 editableRegionEndLine,
279 cursorPosition,
280 );
281
282 const expected = `line 1
283<|editable_region_start|>
284li<|user_cursor_is_here|>ne 2
285<|editable_region_end|>
286line 3`;
287
288 expect(result).toBe(expected);
289 });
290});
291
292describe("editHistoryBlock", () => {
293 test("should format empty edit history", () => {
294 const result = editHistoryBlock([]);
295 expect(result).toBe("");
296 });
297
298 test("should format single diff history entry", () => {
299 const diff = `Index: test.js
300===================================================================
301--- a/test.js 2023-01-01 10:00:00.000000000 +0000
302+++ b/test.js 2023-01-01 10:01:00.000000000 +0000
303@@ -1,3 +1,3 @@
304 function test() {
305- console.log("old");
306+ console.log("new");
307 }`;
308
309 const result = editHistoryBlock([diff]);
310 const expected = `User edited file "test.js"
311
312\`\`\`diff
313@@ -1,3 +1,3 @@
314 function test() {
315- console.log("old");
316+ console.log("new");
317 }
318\`\`\``;
319
320 expect(result).toBe(expected);
321 });
322
323 test("should format multiple diff history entries in unified format", () => {
324 // Multiple diffs should be concatenated into a single string
325 const unifiedDiff = `Index: file1.js
326===================================================================
327--- a/file1.js 2023-01-01 10:00:00.000000000 +0000
328+++ b/file1.js 2023-01-01 10:01:00.000000000 +0000
329@@ -1 +1 @@
330-const old = 1;
331+const new = 1;
332Index: file2.js
333===================================================================
334--- a/file2.js 2023-01-01 10:02:00.000000000 +0000
335+++ b/file2.js 2023-01-01 10:03:00.000000000 +0000
336@@ -1 +1 @@
337-let x = "old";
338+let x = "new";`;
339
340 const result = editHistoryBlock([unifiedDiff]);
341 const expected = `User edited file "file1.js"
342
343\`\`\`diff
344@@ -1 +1 @@
345-const old = 1;
346+const new = 1;
347\`\`\`
348User edited file "file2.js"
349
350\`\`\`diff
351@@ -1 +1 @@
352-let x = "old";
353+let x = "new";
354\`\`\``;
355
356 expect(result).toBe(expected);
357 });
358
359 test("should format multiple diff history entries", () => {
360 // Multiple diffs should be concatenated into a single string
361 const unifiedDiffs = [
362 `Index: file1.js
363===================================================================
364--- a/file1.js 2023-01-01 10:00:00.000000000 +0000
365+++ b/file1.js 2023-01-01 10:01:00.000000000 +0000
366@@ -1 +1 @@
367-const old = 1;
368+const new = 1;`,
369 `Index: file2.js
370===================================================================
371--- a/file2.js 2023-01-01 10:02:00.000000000 +0000
372+++ b/file2.js 2023-01-01 10:03:00.000000000 +0000
373@@ -1 +1 @@
374-let x = "old";
375+let x = "new";`,
376 ];
377
378 const result = editHistoryBlock(unifiedDiffs);
379 const expected = `User edited file "file1.js"
380
381\`\`\`diff
382@@ -1 +1 @@
383-const old = 1;
384+const new = 1;
385\`\`\`
386User edited file "file2.js"
387
388\`\`\`diff
389@@ -1 +1 @@
390-let x = "old";
391+let x = "new";
392\`\`\``;
393
394 expect(result).toBe(expected);
395 });
396
397 // Fix remaining tests to pass strings instead of arrays
398 test("should handle diff with complex filename paths", () => {
399 const diff = `Index: src/components/Button/Button.tsx
400===================================================================
401--- a/src/components/Button/Button.tsx 2023-01-01 10:00:00.000000000 +0000
402+++ b/src/components/Button/Button.tsx 2023-01-01 10:01:00.000000000 +0000
403@@ -1,2 +1,2 @@
404-export const Button = () => <button>Old</button>;
405+export const Button = () => <button>New</button>;`;
406
407 const result = editHistoryBlock([diff]);
408 const expected = `User edited file "src/components/Button/Button.tsx"
409
410\`\`\`diff
411@@ -1,2 +1,2 @@
412-export const Button = () => <button>Old</button>;
413+export const Button = () => <button>New</button>;
414\`\`\``;
415
416 expect(result).toBe(expected);
417 });
418
419 test("should handle diff with new file creation", () => {
420 const diff = `Index: newfile.js
421===================================================================
422--- /dev/null 2023-01-01 10:00:00.000000000 +0000
423+++ b/newfile.js 2023-01-01 10:01:00.000000000 +0000
424@@ -0,0 +1,3 @@
425+function newFunction() {
426+ return "hello world";
427+}`;
428
429 const result = editHistoryBlock([diff]);
430 const expected = `User edited file "newfile.js"
431
432\`\`\`diff
433@@ -0,0 +1,3 @@
434+function newFunction() {
435+ return "hello world";
436+}
437\`\`\``;
438
439 expect(result).toBe(expected);
440 });
441
442 test("should handle diff with file deletion", () => {
443 const diff = `Index: deletedfile.js
444===================================================================
445--- a/deletedfile.js 2023-01-01 10:00:00.000000000 +0000
446+++ /dev/null 2023-01-01 10:01:00.000000000 +0000
447@@ -1,3 +0,0 @@
448-function deletedFunction() {
449- return "goodbye";
450-}`;
451
452 const result = editHistoryBlock([diff]);
453 const expected = `User edited file "deletedfile.js"
454
455\`\`\`diff
456@@ -1,3 +0,0 @@
457-function deletedFunction() {
458- return "goodbye";
459-}
460\`\`\``;
461
462 expect(result).toBe(expected);
463 });
464
465 test("should handle minimal diff format", () => {
466 const diff = `Index: simple.txt
467===================================================================
468@@ -1 +1 @@
469-old text
470+new text`;
471
472 const result = editHistoryBlock([diff]);
473 const expected = `User edited file "simple.txt"
474
475\`\`\`diff
476@@ -1 +1 @@
477-old text
478+new text
479\`\`\``;
480
481 expect(result).toBe(expected);
482 });
483});