Elffuss BraidHub

kikocis/continue public

2cb7a36ca613 · 19.9 KiBRawPermalink
1import {
2 TextBuffer,
3 COLLAPSE_SIZE,
4 RAPID_INPUT_THRESHOLD,
5} from "./TextBuffer.js";
6
7describe("TextBuffer", () => {
8 let buffer: TextBuffer;
9
10 beforeEach(() => {
11 buffer = new TextBuffer();
12 });
13
14 describe("constructor", () => {
15 it("should initialize with empty text by default", () => {
16 expect(buffer.text).toBe("");
17 expect(buffer.cursor).toBe(0);
18 });
19
20 it("should initialize with provided text", () => {
21 const buffer = new TextBuffer("hello");
22 expect(buffer.text).toBe("hello");
23 expect(buffer.cursor).toBe(5);
24 });
25 });
26
27 describe("setText", () => {
28 it("should set text and maintain cursor position", () => {
29 buffer.setText("hello");
30 expect(buffer.text).toBe("hello");
31 expect(buffer.cursor).toBe(0);
32 });
33
34 it("should adjust cursor if it exceeds text length", () => {
35 buffer.setText("hello world");
36 buffer.setCursor(5);
37 buffer.setText("hi");
38 expect(buffer.text).toBe("hi");
39 expect(buffer.cursor).toBe(2);
40 });
41 });
42
43 describe("setCursor", () => {
44 beforeEach(() => {
45 buffer.setText("hello");
46 });
47
48 it("should set cursor to valid position", () => {
49 buffer.setCursor(3);
50 expect(buffer.cursor).toBe(3);
51 });
52
53 it("should clamp cursor to minimum of 0", () => {
54 buffer.setCursor(-5);
55 expect(buffer.cursor).toBe(0);
56 });
57
58 it("should clamp cursor to maximum of text length", () => {
59 buffer.setCursor(10);
60 expect(buffer.cursor).toBe(5);
61 });
62 });
63
64 describe("insertText", () => {
65 it("should insert text at cursor position", () => {
66 buffer.setText("hello");
67 buffer.setCursor(2);
68 buffer.insertText("XX");
69 expect(buffer.text).toBe("heXXllo");
70 expect(buffer.cursor).toBe(4);
71 });
72
73 it("should insert text at beginning", () => {
74 buffer.setText("world");
75 buffer.setCursor(0);
76 buffer.insertText("hello ");
77 expect(buffer.text).toBe("hello world");
78 expect(buffer.cursor).toBe(6);
79 });
80
81 it("should insert text at end", () => {
82 buffer.setText("hello");
83 buffer.setCursor(5);
84 buffer.insertText(" world");
85 expect(buffer.text).toBe("hello world");
86 expect(buffer.cursor).toBe(11);
87 });
88 });
89
90 describe("deleteCharAt", () => {
91 beforeEach(() => {
92 buffer.setText("hello");
93 buffer.setCursor(3);
94 });
95
96 it("should delete character at specified position", () => {
97 buffer.deleteCharAt(2);
98 expect(buffer.text).toBe("helo");
99 expect(buffer.cursor).toBe(2);
100 });
101
102 it("should not adjust cursor if deleting before cursor", () => {
103 buffer.deleteCharAt(1);
104 expect(buffer.text).toBe("hllo");
105 expect(buffer.cursor).toBe(2);
106 });
107
108 it("should not delete if position is out of bounds", () => {
109 buffer.deleteCharAt(10);
110 expect(buffer.text).toBe("hello");
111 expect(buffer.cursor).toBe(3);
112 });
113
114 it("should not delete if position is negative", () => {
115 buffer.deleteCharAt(-1);
116 expect(buffer.text).toBe("hello");
117 expect(buffer.cursor).toBe(3);
118 });
119 });
120
121 describe("deleteBackward", () => {
122 beforeEach(() => {
123 buffer.setText("hello");
124 buffer.setCursor(3);
125 });
126
127 it("should delete character before cursor", () => {
128 buffer.deleteBackward();
129 expect(buffer.text).toBe("helo");
130 expect(buffer.cursor).toBe(2);
131 });
132
133 it("should not delete if cursor is at beginning", () => {
134 buffer.setCursor(0);
135 buffer.deleteBackward();
136 expect(buffer.text).toBe("hello");
137 expect(buffer.cursor).toBe(0);
138 });
139 });
140
141 describe("deleteForward", () => {
142 beforeEach(() => {
143 buffer.setText("hello");
144 buffer.setCursor(2);
145 });
146
147 it("should delete character at cursor position", () => {
148 buffer.deleteForward();
149 expect(buffer.text).toBe("helo");
150 expect(buffer.cursor).toBe(2);
151 });
152
153 it("should not delete if cursor is at end", () => {
154 buffer.setCursor(5);
155 buffer.deleteForward();
156 expect(buffer.text).toBe("hello");
157 expect(buffer.cursor).toBe(5);
158 });
159 });
160
161 describe("moveWordLeft", () => {
162 beforeEach(() => {
163 buffer.setText("hello world test");
164 });
165
166 it("should move to start of current word", () => {
167 buffer.setCursor(8); // middle of "world"
168 buffer.moveWordLeft();
169 expect(buffer.cursor).toBe(6); // start of "world"
170 });
171
172 it("should move to start of previous word", () => {
173 buffer.setCursor(6); // start of "world"
174 buffer.moveWordLeft();
175 expect(buffer.cursor).toBe(0); // start of "hello"
176 });
177
178 it("should skip whitespace", () => {
179 buffer.setCursor(11); // space after "world"
180 buffer.moveWordLeft();
181 expect(buffer.cursor).toBe(6); // start of "world"
182 });
183
184 it("should not move beyond beginning", () => {
185 buffer.setCursor(0);
186 buffer.moveWordLeft();
187 expect(buffer.cursor).toBe(0);
188 });
189 });
190
191 describe("moveWordRight", () => {
192 beforeEach(() => {
193 buffer.setText("hello world test");
194 });
195
196 it("should move to end of current word", () => {
197 buffer.setCursor(2); // middle of "hello"
198 buffer.moveWordRight();
199 expect(buffer.cursor).toBe(5); // end of "hello"
200 });
201
202 it("should skip whitespace to next word", () => {
203 buffer.setCursor(5); // end of "hello"
204 buffer.moveWordRight();
205 expect(buffer.cursor).toBe(11); // end of "world"
206 });
207
208 it("should not move beyond end", () => {
209 buffer.setCursor(16);
210 buffer.moveWordRight();
211 expect(buffer.cursor).toBe(16);
212 });
213 });
214
215 describe("deleteWordBackward", () => {
216 beforeEach(() => {
217 buffer.setText("hello world test");
218 });
219
220 it("should delete from cursor to start of word", () => {
221 buffer.setCursor(8); // middle of "world"
222 buffer.deleteWordBackward();
223 expect(buffer.text).toBe("hello rld test");
224 expect(buffer.cursor).toBe(6);
225 });
226
227 it("should delete entire previous word", () => {
228 buffer.setCursor(6); // start of "world"
229 buffer.deleteWordBackward();
230 expect(buffer.text).toBe("world test");
231 expect(buffer.cursor).toBe(0);
232 });
233 });
234
235 describe("deleteWordForward", () => {
236 beforeEach(() => {
237 buffer.setText("hello world test");
238 });
239
240 it("should delete from cursor to end of word", () => {
241 buffer.setCursor(2); // middle of "hello"
242 buffer.deleteWordForward();
243 expect(buffer.text).toBe("he world test");
244 expect(buffer.cursor).toBe(2);
245 });
246
247 it("should delete to next word boundary", () => {
248 buffer.setCursor(5); // end of "hello"
249 buffer.deleteWordForward();
250 expect(buffer.text).toBe("hello test");
251 expect(buffer.cursor).toBe(5);
252 });
253 });
254
255 describe("movement methods", () => {
256 beforeEach(() => {
257 buffer.setText("hello");
258 buffer.setCursor(2);
259 });
260
261 it("should move to start", () => {
262 buffer.moveToStart();
263 expect(buffer.cursor).toBe(0);
264 });
265
266 it("should move to end", () => {
267 buffer.moveToEnd();
268 expect(buffer.cursor).toBe(5);
269 });
270
271 it("should move left", () => {
272 buffer.moveLeft();
273 expect(buffer.cursor).toBe(1);
274 });
275
276 it("should not move left beyond start", () => {
277 buffer.setCursor(0);
278 buffer.moveLeft();
279 expect(buffer.cursor).toBe(0);
280 });
281
282 it("should move right", () => {
283 buffer.moveRight();
284 expect(buffer.cursor).toBe(3);
285 });
286
287 it("should not move right beyond end", () => {
288 buffer.setCursor(5);
289 buffer.moveRight();
290 expect(buffer.cursor).toBe(5);
291 });
292 });
293
294 describe("clear", () => {
295 it("should clear text and reset cursor", () => {
296 buffer.setText("hello world");
297 buffer.setCursor(5);
298 buffer.clear();
299 expect(buffer.text).toBe("");
300 expect(buffer.cursor).toBe(0);
301 });
302
303 it("should clear paste map when clearing", () => {
304 const longText = "a".repeat(COLLAPSE_SIZE + 1);
305 buffer.handleInput(longText, { ctrl: false, meta: false } as any);
306 buffer.flushPendingInput();
307
308 expect(buffer.text).toBe("[Paste #1]");
309
310 buffer.clear();
311 expect(buffer.text).toBe("");
312 expect(buffer.cursor).toBe(0);
313 });
314 });
315
316 describe("paste collapse", () => {
317 it("should not collapse short pasted text", () => {
318 const shortText = "hello world";
319 buffer.handleInput(shortText, { ctrl: false, meta: false } as any);
320
321 expect(buffer.text).toBe("hello world");
322 });
323
324 it("should collapse long pasted text exceeding collapse threshold", () => {
325 const longText = "a".repeat(COLLAPSE_SIZE + 1);
326 buffer.handleInput(longText, { ctrl: false, meta: false } as any);
327 buffer.flushPendingInput();
328
329 expect(buffer.text).toBe("[Paste #1]");
330 });
331
332 it("should count lines correctly in collapsed text", () => {
333 const multiLineText = "line1\nline2\nline3\n" + "a".repeat(COLLAPSE_SIZE);
334 buffer.handleInput(multiLineText, { ctrl: false, meta: false } as any);
335 buffer.flushPendingInput();
336
337 expect(buffer.text).toBe("[Paste #1, 4 lines]");
338 });
339
340 it("should expand collapsed content on expandAllPasteBlocks", () => {
341 const longText = "hello " + "a".repeat(COLLAPSE_SIZE);
342 buffer.handleInput(longText, { ctrl: false, meta: false } as any);
343 buffer.flushPendingInput();
344
345 // Verify it was collapsed
346 expect(buffer.text).toBe("[Paste #1]");
347
348 // Expand it
349 buffer.expandAllPasteBlocks();
350
351 // Verify it was expanded with normalized line endings
352 expect(buffer.text).toBe(longText);
353 });
354
355 it("should handle typing before and after collapsed content", () => {
356 buffer.setText("before ");
357 buffer.setCursor(7);
358
359 const longText = "a".repeat(COLLAPSE_SIZE + 1);
360 buffer.handleInput(longText, { ctrl: false, meta: false } as any);
361 buffer.flushPendingInput();
362
363 expect(buffer.text).toBe("before [Paste #1]");
364
365 // Type after collapsed content
366 buffer.insertText(" after");
367 expect(buffer.text).toBe("before [Paste #1] after");
368
369 // Move cursor to beginning and type
370 buffer.setCursor(0);
371 buffer.insertText("start ");
372 expect(buffer.text).toBe("start before [Paste #1] after");
373 });
374
375 it("should handle exactly collapse threshold without collapsing", () => {
376 const exactText = "a".repeat(COLLAPSE_SIZE); // exactly at threshold
377 buffer.handleInput(exactText, { ctrl: false, meta: false } as any);
378 buffer.flushPendingInput(); // This will go into rapid input mode due to size
379
380 expect(buffer.text).toBe(exactText); // Should not be collapsed
381 });
382
383 it("should handle paste operations as single large inputs", () => {
384 // This simulates the most common paste scenario: a single large input
385 const longText = "x".repeat(COLLAPSE_SIZE + 1);
386
387 expect(longText.length).toBeGreaterThan(COLLAPSE_SIZE);
388
389 buffer.handleInput(longText, { ctrl: false, meta: false } as any);
390 buffer.flushPendingInput();
391
392 // Should be collapsed
393 expect(buffer.text).toBe("[Paste #1]");
394 });
395
396 it("should handle bracketed paste mode", () => {
397 // Simulate bracketed paste: start sequence, content, end sequence
398
399 // Start paste mode
400 buffer.handleInput("\u001b[200~", {} as any);
401 expect(buffer.isInPasteMode()).toBe(true);
402
403 // Add some content (in chunks like real paste)
404 const chunks = [
405 "Hello ",
406 "world! ",
407 "This is a long piece of text. ".repeat(30),
408 ];
409 const totalContent = chunks.join("");
410
411 chunks.forEach((chunk) => {
412 buffer.handleInput(chunk, { ctrl: false, meta: false } as any);
413 });
414
415 // Text should NOT be visible during paste mode (to avoid visual bugs)
416 expect(buffer.text).toBe("");
417
418 // End paste mode
419 buffer.handleInput("\u001b[201~", {} as any);
420 expect(buffer.isInPasteMode()).toBe(false);
421
422 // Now should be collapsed if > COLLAPSE_SIZE
423 if (totalContent.length > COLLAPSE_SIZE) {
424 expect(buffer.text).toBe("[Paste #1]");
425 }
426 });
427
428 it("should not collapse short bracketed paste content", () => {
429 // Start paste mode
430 buffer.handleInput("\u001b[200~", {} as any);
431
432 // Add short content
433 const shortContent = "Hello world!";
434 buffer.handleInput(shortContent, { ctrl: false, meta: false } as any);
435
436 // End paste mode
437 buffer.handleInput("\u001b[201~", {} as any);
438
439 // Should not be collapsed
440 expect(buffer.text).toBe(shortContent);
441 });
442
443 it("should normalize line endings when expanding", () => {
444 const textWithCarriageReturns =
445 "line1\r\nline2\rline3\n" + "a".repeat(COLLAPSE_SIZE);
446 buffer.handleInput(textWithCarriageReturns, {
447 ctrl: false,
448 meta: false,
449 } as any);
450 buffer.flushPendingInput();
451
452 expect(buffer.text).toBe("[Paste #1, 4 lines]");
453
454 buffer.expandAllPasteBlocks();
455
456 // Should have normalized line endings
457 const expected = "line1\nline2\nline3\n" + "a".repeat(COLLAPSE_SIZE);
458 expect(buffer.text).toBe(expected);
459 });
460
461 it("should increment paste counter for multiple pastes", () => {
462 const longText1 = "a".repeat(COLLAPSE_SIZE + 1);
463 const longText2 = "b".repeat(COLLAPSE_SIZE + 1);
464
465 buffer.handleInput(longText1, { ctrl: false, meta: false } as any);
466 buffer.flushPendingInput();
467 expect(buffer.text).toBe("[Paste #1]");
468
469 buffer.insertText(" ");
470 buffer.handleInput(longText2, { ctrl: false, meta: false } as any);
471 buffer.flushPendingInput();
472 expect(buffer.text).toBe("[Paste #1] [Paste #2]");
473 });
474
475 it("should detect rapid input above threshold", () => {
476 // Test that input above RAPID_INPUT_THRESHOLD triggers rapid input detection
477 const rapidText = "x".repeat(RAPID_INPUT_THRESHOLD + 1);
478 buffer.handleInput(rapidText, { ctrl: false, meta: false } as any);
479
480 // Should go into rapid input buffer (not displayed yet)
481 expect(buffer.text).toBe("");
482 expect(buffer.isInRapidInputMode()).toBe(true);
483
484 // Flush to finalize
485 buffer.flushPendingInput();
486
487 // Now should be displayed (but not collapsed since < COLLAPSE_SIZE)
488 expect(buffer.text).toBe(rapidText);
489 });
490
491 it("should combine split paste chunks", () => {
492 // Simulate macOS Terminal splitting paste
493 const firstChunk = "a".repeat(COLLAPSE_SIZE + 200); // Large first chunk
494 buffer.handleInput(firstChunk, { ctrl: false, meta: false } as any);
495
496 // First chunk goes into rapid input mode - no placeholder yet
497 expect(buffer.text).toBe("");
498
499 // Second chunk should be combined with the first (needs to be > 50 chars)
500 const secondChunk = "b".repeat(100); // Large enough to trigger split paste detection
501 buffer.handleInput(secondChunk, { ctrl: false, meta: false } as any);
502
503 // Flush to finalize the rapid input synchronously
504 buffer.flushPendingInput();
505
506 // Should show only one placeholder with combined content
507 expect(buffer.text).toBe("[Paste #1]");
508
509 // Verify the combined content is stored correctly
510 buffer.expandAllPasteBlocks();
511 const expectedContent = firstChunk + secondChunk;
512 expect(buffer.text).toBe(expectedContent);
513 });
514
515 it("should expand paste blocks on submission (simulating Enter key behavior)", () => {
516 // Add some text before the paste
517 buffer.insertText("Command: ");
518
519 // Add a large paste that gets collapsed
520 const pastedCode =
521 'function example() {\n console.log("hello");\n ' +
522 "x".repeat(COLLAPSE_SIZE);
523 buffer.handleInput(pastedCode, { ctrl: false, meta: false } as any);
524 buffer.flushPendingInput();
525
526 // Verify it's collapsed
527 expect(buffer.text).toBe("Command: [Paste #1, 3 lines]");
528
529 // Simulate what happens when user presses Enter - UserInput calls expandAllPasteBlocks()
530 buffer.expandAllPasteBlocks();
531
532 // Should now show the full content with normalized line endings
533 const expectedFullText =
534 'Command: function example() {\n console.log("hello");\n ' +
535 "x".repeat(COLLAPSE_SIZE);
536 expect(buffer.text).toBe(expectedFullText);
537
538 // Verify paste map is cleared after expansion
539 buffer.expandAllPasteBlocks(); // Should be no-op now
540 expect(buffer.text).toBe(expectedFullText); // Should remain the same
541 });
542 });
543
544 describe("handleInput", () => {
545 beforeEach(() => {
546 buffer.setText("hello");
547 buffer.setCursor(2);
548 });
549
550 it("should handle regular character input", () => {
551 const result = buffer.handleInput("X", {} as any);
552 expect(result).toBe(true);
553 expect(buffer.text).toBe("heXllo");
554 expect(buffer.cursor).toBe(3);
555 });
556
557 it("should handle multi-character input (paste)", () => {
558 const result = buffer.handleInput("XYZ", {} as any);
559 expect(result).toBe(true);
560 expect(buffer.text).toBe("heXYZllo");
561 expect(buffer.cursor).toBe(5);
562 });
563
564 it("should handle ctrl+a (move to start)", () => {
565 const result = buffer.handleInput("a", { ctrl: true } as any);
566 expect(result).toBe(true);
567 expect(buffer.cursor).toBe(0);
568 });
569
570 it("should handle ctrl+e (move to end)", () => {
571 const result = buffer.handleInput("e", { ctrl: true } as any);
572 expect(result).toBe(true);
573 expect(buffer.cursor).toBe(5);
574 });
575
576 it("should handle ctrl+u (delete to start)", () => {
577 const result = buffer.handleInput("u", { ctrl: true } as any);
578 expect(result).toBe(true);
579 expect(buffer.text).toBe("llo");
580 expect(buffer.cursor).toBe(0);
581 });
582
583 it("should handle ctrl+k (delete to end)", () => {
584 const result = buffer.handleInput("k", { ctrl: true } as any);
585 expect(result).toBe(true);
586 expect(buffer.text).toBe("he");
587 expect(buffer.cursor).toBe(2);
588 });
589
590 it("should handle arrow keys", () => {
591 let result = buffer.handleInput("", { leftArrow: true } as any);
592 expect(result).toBe(true);
593 expect(buffer.cursor).toBe(1);
594
595 result = buffer.handleInput("", { rightArrow: true } as any);
596 expect(result).toBe(true);
597 expect(buffer.cursor).toBe(2);
598 });
599
600 it("should handle delete key", () => {
601 const result = buffer.handleInput("", { delete: true } as any);
602 expect(result).toBe(true);
603 expect(buffer.text).toBe("hllo");
604 expect(buffer.cursor).toBe(1);
605 });
606
607 it("should handle backspace key", () => {
608 const result = buffer.handleInput("", { backspace: true } as any);
609 expect(result).toBe(true);
610 expect(buffer.text).toBe("hllo");
611 expect(buffer.cursor).toBe(1);
612 });
613
614 it("should handle meta key combinations", () => {
615 const result = buffer.handleInput("", {
616 meta: true,
617 leftArrow: true,
618 } as any);
619 expect(result).toBe(true);
620 expect(buffer.cursor).toBe(0);
621 });
622
623 it("should handle option key sequences", () => {
624 const result = buffer.handleInput("\u001b\u007f", {} as any);
625 expect(result).toBe(true);
626 // Should delete word backward
627 expect(buffer.text).toBe("llo");
628 expect(buffer.cursor).toBe(0);
629 });
630
631 it("should return false for unhandled input", () => {
632 const result = buffer.handleInput("", { ctrl: true } as any);
633 expect(result).toBe(false);
634 });
635 });
636
637 describe("renderWithCursor", () => {
638 it("should return text and cursor position", () => {
639 buffer.setText("hello");
640 buffer.setCursor(2);
641 const result = buffer.renderWithCursor();
642 expect(result).toEqual({
643 text: "hello",
644 cursorPosition: 2,
645 });
646 });
647
648 it("should return placeholder when text is empty", () => {
649 const result = buffer.renderWithCursor("Enter text...");
650 expect(result).toEqual({
651 text: "Enter text...",
652 cursorPosition: 0,
653 });
654 });
655
656 it("should return actual text even when placeholder is provided", () => {
657 buffer.setText("hello");
658 buffer.setCursor(2);
659 const result = buffer.renderWithCursor("Enter text...");
660 expect(result).toEqual({
661 text: "hello",
662 cursorPosition: 2,
663 });
664 });
665 });
666});