|
1 | 1 | import { describe, test, expect, beforeEach } from "bun:test" |
2 | | -import { MouseParser, type RawMouseEvent } from "./parse.mouse" |
| 2 | +import { MouseParser, type RawMouseEvent, type MouseParseResult } from "./parse.mouse" |
3 | 3 |
|
4 | 4 | // Encode a basic/X10 mouse event: ESC [ M Cb Cx Cy |
5 | 5 | // buttonByte is the logical value (before the +32 wire offset), x/y are 0-based. |
@@ -519,6 +519,114 @@ describe("MouseParser parseAllMouseEvents (multi-event chunks)", () => { |
519 | 519 | }) |
520 | 520 | }) |
521 | 521 |
|
| 522 | +describe("MouseParser parseAllMouseEventsWithOffset (split sequences & mixed data)", () => { |
| 523 | + let parser: MouseParser |
| 524 | + |
| 525 | + beforeEach(() => { |
| 526 | + parser = new MouseParser() |
| 527 | + }) |
| 528 | + |
| 529 | + test("pure mouse data: consumed equals total length, partial is false", () => { |
| 530 | + const buf = Buffer.concat([encodeSGR(0, 10, 5, true), encodeSGR(0, 10, 5, false)]) |
| 531 | + const result = parser.parseAllMouseEventsWithOffset(buf) |
| 532 | + expect(result.events).toHaveLength(2) |
| 533 | + expect(result.consumed).toBe(buf.length) |
| 534 | + expect(result.partial).toBe(false) |
| 535 | + }) |
| 536 | + |
| 537 | + test("mouse followed by non-mouse data: consumed < length, partial is false", () => { |
| 538 | + const mouseData = encodeSGR(0, 10, 5, true) |
| 539 | + const keyData = Buffer.from("hello") |
| 540 | + const buf = Buffer.concat([mouseData, keyData]) |
| 541 | + const result = parser.parseAllMouseEventsWithOffset(buf) |
| 542 | + expect(result.events).toHaveLength(1) |
| 543 | + expect(result.consumed).toBe(mouseData.length) |
| 544 | + expect(result.partial).toBe(false) |
| 545 | + }) |
| 546 | + |
| 547 | + test("mouse followed by partial mouse sequence: partial is true", () => { |
| 548 | + const completeEvent = encodeSGR(0, 10, 5, true) |
| 549 | + const partialEvent = Buffer.from("\x1b[<35;20") |
| 550 | + const buf = Buffer.concat([completeEvent, partialEvent]) |
| 551 | + const result = parser.parseAllMouseEventsWithOffset(buf) |
| 552 | + expect(result.events).toHaveLength(1) |
| 553 | + expect(result.consumed).toBe(completeEvent.length) |
| 554 | + expect(result.partial).toBe(true) |
| 555 | + }) |
| 556 | + |
| 557 | + test("only partial SGR mouse sequence: 0 events, partial is true", () => { |
| 558 | + const buf = Buffer.from("\x1b[<35;20;5") |
| 559 | + const result = parser.parseAllMouseEventsWithOffset(buf) |
| 560 | + expect(result.events).toHaveLength(0) |
| 561 | + expect(result.consumed).toBe(0) |
| 562 | + expect(result.partial).toBe(true) |
| 563 | + }) |
| 564 | + |
| 565 | + test("bare ESC-[ < without digits is partial", () => { |
| 566 | + const buf = Buffer.from("\x1b[<") |
| 567 | + const result = parser.parseAllMouseEventsWithOffset(buf) |
| 568 | + expect(result.events).toHaveLength(0) |
| 569 | + expect(result.consumed).toBe(0) |
| 570 | + expect(result.partial).toBe(true) |
| 571 | + }) |
| 572 | + |
| 573 | + test("partial basic mouse: ESC[M + 1 byte (needs 3)", () => { |
| 574 | + const buf = Buffer.from("\x1b[M\x20") |
| 575 | + const result = parser.parseAllMouseEventsWithOffset(buf) |
| 576 | + expect(result.events).toHaveLength(0) |
| 577 | + expect(result.consumed).toBe(0) |
| 578 | + expect(result.partial).toBe(true) |
| 579 | + }) |
| 580 | + |
| 581 | + test("non-mouse data followed by mouse: consumed is 0, partial is false", () => { |
| 582 | + const keyData = Buffer.from("abc") |
| 583 | + const mouseData = encodeSGR(0, 10, 5, true) |
| 584 | + const buf = Buffer.concat([keyData, mouseData]) |
| 585 | + const result = parser.parseAllMouseEventsWithOffset(buf) |
| 586 | + // Parser starts at offset 0, 'a' is not a mouse introducer — breaks immediately |
| 587 | + expect(result.events).toHaveLength(0) |
| 588 | + expect(result.consumed).toBe(0) |
| 589 | + expect(result.partial).toBe(false) |
| 590 | + }) |
| 591 | + |
| 592 | + test("empty buffer: 0 events, consumed 0, partial false", () => { |
| 593 | + const result = parser.parseAllMouseEventsWithOffset(Buffer.from("")) |
| 594 | + expect(result.events).toHaveLength(0) |
| 595 | + expect(result.consumed).toBe(0) |
| 596 | + expect(result.partial).toBe(false) |
| 597 | + }) |
| 598 | + |
| 599 | + test("non-mouse CSI sequence is not detected as partial", () => { |
| 600 | + // ESC[A (Cursor Up) — valid CSI but not a mouse sequence |
| 601 | + const buf = Buffer.from("\x1b[A") |
| 602 | + const result = parser.parseAllMouseEventsWithOffset(buf) |
| 603 | + expect(result.events).toHaveLength(0) |
| 604 | + expect(result.consumed).toBe(0) |
| 605 | + expect(result.partial).toBe(false) |
| 606 | + }) |
| 607 | + |
| 608 | + test("simulated split: chunk 1 with partial, chunk 2 completes it", () => { |
| 609 | + // Chunk 1: complete event + beginning of a second one |
| 610 | + const complete = encodeSGR(35, 20, 10, true) // move event |
| 611 | + const partial = "\x1b[<35;21" |
| 612 | + const chunk1 = Buffer.concat([complete, Buffer.from(partial)]) |
| 613 | + |
| 614 | + const result1 = parser.parseAllMouseEventsWithOffset(chunk1) |
| 615 | + expect(result1.events).toHaveLength(1) |
| 616 | + expect(result1.consumed).toBe(complete.length) |
| 617 | + expect(result1.partial).toBe(true) |
| 618 | + |
| 619 | + // Chunk 2: remainder of the partial sequence |
| 620 | + const remainder = chunk1.toString().slice(result1.consumed) |
| 621 | + const chunk2str = remainder + ";11M" // completes \x1b[<35;21;11M |
| 622 | + const result2 = parser.parseAllMouseEventsWithOffset(Buffer.from(chunk2str)) |
| 623 | + expect(result2.events).toHaveLength(1) |
| 624 | + expect(result2.events[0]).toMatchObject({ type: "move", x: 20, y: 10 }) |
| 625 | + expect(result2.consumed).toBe(chunk2str.length) |
| 626 | + expect(result2.partial).toBe(false) |
| 627 | + }) |
| 628 | +}) |
| 629 | + |
522 | 630 | describe("MouseParser protocol precedence", () => { |
523 | 631 | let parser: MouseParser |
524 | 632 |
|
|
0 commit comments