| 1 | import { |
| 2 | COLLAPSE_SIZE, |
| 3 | RAPID_INPUT_THRESHOLD, |
| 4 | TextBuffer, |
| 5 | } from "./TextBuffer.js"; |
| 6 | |
| 7 | describe("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 detection and collapse", () => { |
| 317 | describe("basic collapse behavior", () => { |
| 318 | it("should not collapse short content", () => { |
| 319 | const shortText = "hello world"; |
| 320 | buffer.handleInput(shortText, { ctrl: false, meta: false } as any); |
| 321 | expect(buffer.text).toBe("hello world"); |
| 322 | }); |
| 323 | |
| 324 | it("should collapse content exceeding threshold", () => { |
| 325 | const longText = "a".repeat(COLLAPSE_SIZE + 1); |
| 326 | buffer.handleInput(longText, { ctrl: false, meta: false } as any); |
| 327 | buffer.flushPendingInput(); |
| 328 | expect(buffer.text).toBe("[Paste #1]"); |
| 329 | }); |
| 330 | |
| 331 | it("should count lines correctly in placeholders", () => { |
| 332 | const multiLineText = |
| 333 | "line1\nline2\nline3\n" + "a".repeat(COLLAPSE_SIZE); |
| 334 | buffer.handleInput(multiLineText, { ctrl: false, meta: false } as any); |
| 335 | buffer.flushPendingInput(); |
| 336 | expect(buffer.text).toBe("[Paste #1, 4 lines]"); |
| 337 | }); |
| 338 | }); |
| 339 | |
| 340 | describe("expansion and state management", () => { |
| 341 | it("should expand collapsed content on demand", () => { |
| 342 | const longText = "content " + "a".repeat(COLLAPSE_SIZE); |
| 343 | buffer.handleInput(longText, { ctrl: false, meta: false } as any); |
| 344 | buffer.flushPendingInput(); |
| 345 | |
| 346 | expect(buffer.text).toBe("[Paste #1]"); |
| 347 | buffer.expandAllPasteBlocks(); |
| 348 | expect(buffer.text).toBe(longText); |
| 349 | }); |
| 350 | |
| 351 | it("should increment paste counter for multiple pastes", () => { |
| 352 | const text1 = "a".repeat(COLLAPSE_SIZE + 1); |
| 353 | const text2 = "b".repeat(COLLAPSE_SIZE + 1); |
| 354 | |
| 355 | buffer.handleInput(text1, { ctrl: false, meta: false } as any); |
| 356 | buffer.flushPendingInput(); |
| 357 | buffer.insertText(" "); |
| 358 | buffer.handleInput(text2, { ctrl: false, meta: false } as any); |
| 359 | buffer.flushPendingInput(); |
| 360 | |
| 361 | expect(buffer.text).toBe("[Paste #1] [Paste #2]"); |
| 362 | }); |
| 363 | |
| 364 | it("should normalize line endings when expanding", () => { |
| 365 | const textWithMixedEndings = |
| 366 | "line1\r\nline2\rline3\n" + "a".repeat(COLLAPSE_SIZE); |
| 367 | buffer.handleInput(textWithMixedEndings, { |
| 368 | ctrl: false, |
| 369 | meta: false, |
| 370 | } as any); |
| 371 | buffer.flushPendingInput(); |
| 372 | |
| 373 | buffer.expandAllPasteBlocks(); |
| 374 | const expected = "line1\nline2\nline3\n" + "a".repeat(COLLAPSE_SIZE); |
| 375 | expect(buffer.text).toBe(expected); |
| 376 | }); |
| 377 | |
| 378 | it("should clear paste map after expansion", () => { |
| 379 | const longText = "a".repeat(COLLAPSE_SIZE + 1); |
| 380 | buffer.handleInput(longText, { ctrl: false, meta: false } as any); |
| 381 | buffer.flushPendingInput(); |
| 382 | |
| 383 | buffer.expandAllPasteBlocks(); |
| 384 | buffer.expandAllPasteBlocks(); // Second call should be no-op |
| 385 | expect(buffer.text).toBe(longText); |
| 386 | }); |
| 387 | }); |
| 388 | |
| 389 | describe("rapid input detection (Terminal.app/Ghostty)", () => { |
| 390 | it("should detect large single input as rapid paste", () => { |
| 391 | const rapidText = "x".repeat(RAPID_INPUT_THRESHOLD + 1); |
| 392 | buffer.handleInput(rapidText, { ctrl: false, meta: false } as any); |
| 393 | |
| 394 | expect(buffer.text).toBe(""); // Should be buffered |
| 395 | expect(buffer.isInRapidInputMode()).toBe(true); |
| 396 | |
| 397 | buffer.flushPendingInput(); |
| 398 | expect(buffer.text).toBe(rapidText); // < COLLAPSE_SIZE, so not collapsed |
| 399 | }); |
| 400 | |
| 401 | it("should combine multiple chunks into single paste", () => { |
| 402 | const chunk1 = "a".repeat(200); |
| 403 | const chunk2 = "b".repeat(700); // Combined > COLLAPSE_SIZE |
| 404 | |
| 405 | buffer.handleInput(chunk1, { ctrl: false, meta: false } as any); |
| 406 | buffer.handleInput(chunk2, { ctrl: false, meta: false } as any); |
| 407 | buffer.flushPendingInput(); |
| 408 | |
| 409 | expect(buffer.text).toBe("[Paste #1]"); |
| 410 | buffer.expandAllPasteBlocks(); |
| 411 | expect(buffer.text).toBe(chunk1 + chunk2); |
| 412 | }); |
| 413 | |
| 414 | it("should detect 50+ char chunks as start of paste session", () => { |
| 415 | const smallChunk = "x".repeat(60); // >= 50, should trigger |
| 416 | const largeChunk = "y".repeat(800); |
| 417 | |
| 418 | buffer.handleInput(smallChunk, { ctrl: false, meta: false } as any); |
| 419 | expect(buffer.text).toBe(""); |
| 420 | |
| 421 | buffer.handleInput(largeChunk, { ctrl: false, meta: false } as any); |
| 422 | buffer.flushPendingInput(); |
| 423 | |
| 424 | expect(buffer.text).toBe("[Paste #1]"); |
| 425 | buffer.expandAllPasteBlocks(); |
| 426 | expect(buffer.text).toBe(smallChunk + largeChunk); |
| 427 | }); |
| 428 | }); |
| 429 | |
| 430 | describe("bracketed paste (iTerm2)", () => { |
| 431 | it("should handle bracketed paste sequences", () => { |
| 432 | const content = "function test() { return 'hello'; }" + "x".repeat(800); |
| 433 | |
| 434 | buffer.handleInput("\u001b[200~", { ctrl: false, meta: false } as any); |
| 435 | expect(buffer.isInPasteMode()).toBe(true); |
| 436 | expect(buffer.text).toBe(""); |
| 437 | |
| 438 | buffer.handleInput(content, { ctrl: false, meta: false } as any); |
| 439 | expect(buffer.text).toBe(""); // Nothing visible during paste |
| 440 | |
| 441 | buffer.handleInput("\u001b[201~", { ctrl: false, meta: false } as any); |
| 442 | expect(buffer.isInPasteMode()).toBe(false); |
| 443 | expect(buffer.text).toBe("[Paste #1]"); |
| 444 | |
| 445 | buffer.expandAllPasteBlocks(); |
| 446 | expect(buffer.text).toBe(content); |
| 447 | }); |
| 448 | |
| 449 | it("should not collapse short bracketed content", () => { |
| 450 | const shortContent = "Hello world!"; |
| 451 | |
| 452 | buffer.handleInput("\u001b[200~", { ctrl: false, meta: false } as any); |
| 453 | buffer.handleInput(shortContent, { ctrl: false, meta: false } as any); |
| 454 | buffer.handleInput("\u001b[201~", { ctrl: false, meta: false } as any); |
| 455 | |
| 456 | expect(buffer.text).toBe(shortContent); |
| 457 | }); |
| 458 | |
| 459 | it("should prioritize bracketed paste over rapid input", () => { |
| 460 | const largeContent = "x".repeat(1000); |
| 461 | |
| 462 | buffer.handleInput("\u001b[200~", { ctrl: false, meta: false } as any); |
| 463 | buffer.handleInput(largeContent, { ctrl: false, meta: false } as any); |
| 464 | |
| 465 | expect(buffer.isInPasteMode()).toBe(true); |
| 466 | expect(buffer.isInRapidInputMode()).toBe(false); |
| 467 | |
| 468 | buffer.handleInput("\u001b[201~", { ctrl: false, meta: false } as any); |
| 469 | expect(buffer.text).toBe("[Paste #1]"); |
| 470 | }); |
| 471 | }); |
| 472 | }); |
| 473 | |
| 474 | describe("timing behavior", () => { |
| 475 | beforeEach(() => { |
| 476 | vi.useFakeTimers(); |
| 477 | }); |
| 478 | |
| 479 | afterEach(() => { |
| 480 | vi.useRealTimers(); |
| 481 | }); |
| 482 | |
| 483 | it("should combine chunks within 200ms timing window", () => { |
| 484 | const chunk1 = "x".repeat(100); |
| 485 | const chunk2 = "y".repeat(800); |
| 486 | |
| 487 | buffer.handleInput(chunk1, { ctrl: false, meta: false } as any); |
| 488 | |
| 489 | vi.advanceTimersByTime(100); // Within 200ms window |
| 490 | buffer.handleInput(chunk2, { ctrl: false, meta: false } as any); |
| 491 | |
| 492 | vi.advanceTimersByTime(250); // Finalize |
| 493 | |
| 494 | expect(buffer.text).toBe("[Paste #1]"); |
| 495 | buffer.expandAllPasteBlocks(); |
| 496 | expect(buffer.text).toBe(chunk1 + chunk2); |
| 497 | }); |
| 498 | |
| 499 | it("should finalize buffered content after 200ms timeout", () => { |
| 500 | const chunk = "x".repeat(100); |
| 501 | |
| 502 | buffer.handleInput(chunk, { ctrl: false, meta: false } as any); |
| 503 | expect(buffer.text).toBe(""); |
| 504 | |
| 505 | vi.advanceTimersByTime(250); |
| 506 | expect(buffer.text).toBe(chunk); |
| 507 | }); |
| 508 | |
| 509 | it("should separate sessions when timing window expires", () => { |
| 510 | const chunk1 = "a".repeat(60); |
| 511 | const chunk2 = "b".repeat(900); |
| 512 | |
| 513 | buffer.handleInput(chunk1, { ctrl: false, meta: false } as any); |
| 514 | vi.advanceTimersByTime(300); // Exceed timing window |
| 515 | expect(buffer.text).toBe(chunk1); |
| 516 | |
| 517 | buffer.handleInput(chunk2, { ctrl: false, meta: false } as any); |
| 518 | vi.advanceTimersByTime(250); |
| 519 | expect(buffer.text).toBe(chunk1 + "[Paste #1]"); |
| 520 | }); |
| 521 | }); |
| 522 | |
| 523 | describe("regression tests", () => { |
| 524 | it("should prevent typing during paste accumulation (Terminal.app fix)", () => { |
| 525 | const largeChunk = "x".repeat(100); |
| 526 | buffer.handleInput(largeChunk, { ctrl: false, meta: false } as any); |
| 527 | |
| 528 | // Small typed input should not be mixed into paste |
| 529 | const typedChar = "a"; |
| 530 | buffer.handleInput(typedChar, { ctrl: false, meta: false } as any); |
| 531 | expect(buffer.text).toBe("a"); |
| 532 | |
| 533 | buffer.flushPendingInput(); |
| 534 | expect(buffer.text).toBe("a" + largeChunk); |
| 535 | }); |
| 536 | |
| 537 | it("should preserve all chunks when expanding (content loss fix)", () => { |
| 538 | const chunks = ["a".repeat(1000), "b".repeat(1000), "c".repeat(1000)]; |
| 539 | |
| 540 | chunks.forEach((chunk) => { |
| 541 | buffer.handleInput(chunk, { ctrl: false, meta: false } as any); |
| 542 | }); |
| 543 | buffer.flushPendingInput(); |
| 544 | |
| 545 | expect(buffer.text).toBe("[Paste #1]"); |
| 546 | |
| 547 | buffer.expandAllPasteBlocks(); |
| 548 | expect(buffer.text).toBe(chunks.join("")); |
| 549 | expect(buffer.text.length).toBe(3000); |
| 550 | }); |
| 551 | |
| 552 | it("should handle complete paste-to-submit workflow", () => { |
| 553 | const pastedCode = 'console.log("test");' + "x".repeat(800); |
| 554 | |
| 555 | buffer.handleInput(pastedCode, { ctrl: false, meta: false } as any); |
| 556 | buffer.flushPendingInput(); |
| 557 | expect(buffer.text).toBe("[Paste #1]"); |
| 558 | |
| 559 | // Simulate Enter key - expand before submission |
| 560 | buffer.expandAllPasteBlocks(); |
| 561 | expect(buffer.text).toBe(pastedCode); |
| 562 | |
| 563 | buffer.clear(); |
| 564 | expect(buffer.text).toBe(""); |
| 565 | }); |
| 566 | }); |
| 567 | |
| 568 | describe("handleInput", () => { |
| 569 | beforeEach(() => { |
| 570 | buffer.setText("hello"); |
| 571 | buffer.setCursor(2); |
| 572 | }); |
| 573 | |
| 574 | it("should handle regular character input", () => { |
| 575 | const result = buffer.handleInput("X", {} as any); |
| 576 | expect(result).toBe(true); |
| 577 | expect(buffer.text).toBe("heXllo"); |
| 578 | expect(buffer.cursor).toBe(3); |
| 579 | }); |
| 580 | |
| 581 | it("should handle multi-character input (paste)", () => { |
| 582 | const result = buffer.handleInput("XYZ", {} as any); |
| 583 | expect(result).toBe(true); |
| 584 | expect(buffer.text).toBe("heXYZllo"); |
| 585 | expect(buffer.cursor).toBe(5); |
| 586 | }); |
| 587 | |
| 588 | it("should handle ctrl+a (move to start)", () => { |
| 589 | const result = buffer.handleInput("a", { ctrl: true } as any); |
| 590 | expect(result).toBe(true); |
| 591 | expect(buffer.cursor).toBe(0); |
| 592 | }); |
| 593 | |
| 594 | it("should handle ctrl+e (move to end)", () => { |
| 595 | const result = buffer.handleInput("e", { ctrl: true } as any); |
| 596 | expect(result).toBe(true); |
| 597 | expect(buffer.cursor).toBe(5); |
| 598 | }); |
| 599 | |
| 600 | it("should handle ctrl+u (delete to start)", () => { |
| 601 | const result = buffer.handleInput("u", { ctrl: true } as any); |
| 602 | expect(result).toBe(true); |
| 603 | expect(buffer.text).toBe("llo"); |
| 604 | expect(buffer.cursor).toBe(0); |
| 605 | }); |
| 606 | |
| 607 | it("should handle ctrl+k (delete to end)", () => { |
| 608 | const result = buffer.handleInput("k", { ctrl: true } as any); |
| 609 | expect(result).toBe(true); |
| 610 | expect(buffer.text).toBe("he"); |
| 611 | expect(buffer.cursor).toBe(2); |
| 612 | }); |
| 613 | |
| 614 | it("should handle arrow keys", () => { |
| 615 | let result = buffer.handleInput("", { leftArrow: true } as any); |
| 616 | expect(result).toBe(true); |
| 617 | expect(buffer.cursor).toBe(1); |
| 618 | |
| 619 | result = buffer.handleInput("", { rightArrow: true } as any); |
| 620 | expect(result).toBe(true); |
| 621 | expect(buffer.cursor).toBe(2); |
| 622 | }); |
| 623 | |
| 624 | it("should handle delete key", () => { |
| 625 | const result = buffer.handleInput("", { delete: true } as any); |
| 626 | expect(result).toBe(true); |
| 627 | expect(buffer.text).toBe("hllo"); |
| 628 | expect(buffer.cursor).toBe(1); |
| 629 | }); |
| 630 | |
| 631 | it("should handle backspace key", () => { |
| 632 | const result = buffer.handleInput("", { backspace: true } as any); |
| 633 | expect(result).toBe(true); |
| 634 | expect(buffer.text).toBe("hllo"); |
| 635 | expect(buffer.cursor).toBe(1); |
| 636 | }); |
| 637 | |
| 638 | it("should handle meta key combinations", () => { |
| 639 | const result = buffer.handleInput("", { |
| 640 | meta: true, |
| 641 | leftArrow: true, |
| 642 | } as any); |
| 643 | expect(result).toBe(true); |
| 644 | expect(buffer.cursor).toBe(0); |
| 645 | }); |
| 646 | |
| 647 | it("should handle option key sequences", () => { |
| 648 | const result = buffer.handleInput("\u001b\u007f", {} as any); |
| 649 | expect(result).toBe(true); |
| 650 | // Should delete word backward |
| 651 | expect(buffer.text).toBe("llo"); |
| 652 | expect(buffer.cursor).toBe(0); |
| 653 | }); |
| 654 | |
| 655 | it("should return false for unhandled input", () => { |
| 656 | const result = buffer.handleInput("", { ctrl: true } as any); |
| 657 | expect(result).toBe(false); |
| 658 | }); |
| 659 | }); |
| 660 | |
| 661 | describe("renderWithCursor", () => { |
| 662 | it("should return text and cursor position", () => { |
| 663 | buffer.setText("hello"); |
| 664 | buffer.setCursor(2); |
| 665 | const result = buffer.renderWithCursor(); |
| 666 | expect(result).toEqual({ |
| 667 | text: "hello", |
| 668 | cursorPosition: 2, |
| 669 | }); |
| 670 | }); |
| 671 | |
| 672 | it("should return placeholder when text is empty", () => { |
| 673 | const result = buffer.renderWithCursor("Enter text..."); |
| 674 | expect(result).toEqual({ |
| 675 | text: "Enter text...", |
| 676 | cursorPosition: 0, |
| 677 | }); |
| 678 | }); |
| 679 | |
| 680 | it("should return actual text even when placeholder is provided", () => { |
| 681 | buffer.setText("hello"); |
| 682 | buffer.setCursor(2); |
| 683 | const result = buffer.renderWithCursor("Enter text..."); |
| 684 | expect(result).toEqual({ |
| 685 | text: "hello", |
| 686 | cursorPosition: 2, |
| 687 | }); |
| 688 | }); |
| 689 | }); |
| 690 | }); |