|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { stripSoftCursor } from "../src/strip.js"; |
| 3 | + |
| 4 | +const REV = "\x1b[7m"; |
| 5 | +const RST = "\x1b[0m"; |
| 6 | +const BOLD = "\x1b[1m"; |
| 7 | +const DIM = "\x1b[2m"; |
| 8 | +const FG = "\x1b[38;2;100;100;100m"; |
| 9 | + |
| 10 | +describe("stripSoftCursor", () => { |
| 11 | + // --- passthrough cases --- |
| 12 | + |
| 13 | + it("passes plain text through unchanged", () => { |
| 14 | + expect(stripSoftCursor("hello world")).toBe("hello world"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("passes empty string through", () => { |
| 18 | + expect(stripSoftCursor("")).toBe(""); |
| 19 | + }); |
| 20 | + |
| 21 | + it("returns line unchanged when reverse-on has no matching close", () => { |
| 22 | + expect(stripSoftCursor(`before${REV}dangling`)).toBe(`before${REV}dangling`); |
| 23 | + }); |
| 24 | + |
| 25 | + it("returns line unchanged when only reset exists (no reverse-on)", () => { |
| 26 | + expect(stripSoftCursor(`text${RST}more`)).toBe(`text${RST}more`); |
| 27 | + }); |
| 28 | + |
| 29 | + // --- basic stripping --- |
| 30 | + |
| 31 | + it("strips a single reverse-video span", () => { |
| 32 | + expect(stripSoftCursor(`before${REV}X${RST}after`)).toBe("beforeXafter"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("strips reverse-video span with multi-char content", () => { |
| 36 | + expect(stripSoftCursor(`${REV}hello${RST}`)).toBe("hello"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("strips reverse-video around a space (end-of-line cursor)", () => { |
| 40 | + expect(stripSoftCursor(`text${REV} ${RST}`)).toBe("text "); |
| 41 | + }); |
| 42 | + |
| 43 | + it("strips reverse-video around unicode grapheme", () => { |
| 44 | + expect(stripSoftCursor(`${REV}🚀${RST}`)).toBe("🚀"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("handles line with only reverse-video", () => { |
| 48 | + expect(stripSoftCursor(`${REV}X${RST}`)).toBe("X"); |
| 49 | + }); |
| 50 | + |
| 51 | + // --- last-only behavior --- |
| 52 | + |
| 53 | + it("strips only the last reverse-video span when multiple exist", () => { |
| 54 | + const input = `${REV}A${RST} gap ${REV}B${RST}`; |
| 55 | + expect(stripSoftCursor(input)).toBe(`${REV}A${RST} gap B`); |
| 56 | + }); |
| 57 | + |
| 58 | + it("handles adjacent reverse-video spans — strips only last", () => { |
| 59 | + expect(stripSoftCursor(`${REV}A${RST}${REV}B${RST}`)).toBe(`${REV}A${RST}B`); |
| 60 | + }); |
| 61 | + |
| 62 | + it("preserves earlier reverse-video in a line with cursor at end", () => { |
| 63 | + const line = `${REV}label${RST}: value${REV}X${RST}`; |
| 64 | + expect(stripSoftCursor(line)).toBe(`${REV}label${RST}: valueX`); |
| 65 | + }); |
| 66 | + |
| 67 | + // --- interaction with other ANSI sequences --- |
| 68 | + |
| 69 | + it("preserves bold+reset before the cursor span", () => { |
| 70 | + const input = `${BOLD}bold${RST} ${REV}X${RST} plain`; |
| 71 | + expect(stripSoftCursor(input)).toBe(`${BOLD}bold${RST} X plain`); |
| 72 | + }); |
| 73 | + |
| 74 | + it("handles a reset after the cursor span (known boundary)", () => { |
| 75 | + // If a \x1b[0m appears after the cursor's \x1b[0m on the same line, the |
| 76 | + // function matches the outer pair: last REV to last RST. This eats the |
| 77 | + // inner reset and any color opener, but the visible text is preserved. |
| 78 | + // In practice the editor never emits color sequences after the cursor on |
| 79 | + // content lines, so this path doesn't fire — we test it to document the |
| 80 | + // boundary and confirm no text is lost. |
| 81 | + const input = `${REV}X${RST}${FG}colored${RST}`; |
| 82 | + // Strips from REV(0) to final RST — inner resets and FG become "content" |
| 83 | + expect(stripSoftCursor(input)).toBe(`X${RST}${FG}colored`); |
| 84 | + }); |
| 85 | + |
| 86 | + it("handles color-only lines without reverse-video (border lines)", () => { |
| 87 | + // borderColor wraps produce \x1b[38;2;…m…\x1b[0m but no \x1b[7m |
| 88 | + const border = `${FG}${"─".repeat(40)}${RST}`; |
| 89 | + expect(stripSoftCursor(border)).toBe(border); |
| 90 | + }); |
| 91 | + |
| 92 | + it("handles dim color wrapping border indicators", () => { |
| 93 | + const indicator = `${DIM}─── ↑ 3 more ${RST}${"─".repeat(20)}`; |
| 94 | + expect(stripSoftCursor(indicator)).toBe(indicator); |
| 95 | + }); |
| 96 | + |
| 97 | + // --- realistic editor output --- |
| 98 | + |
| 99 | + it("handles content line: padding + text + cursor + padding", () => { |
| 100 | + // Editor renders: " hello[cursor:w]orld " |
| 101 | + const line = ` hello${REV}w${RST}orld${" ".repeat(5)}`; |
| 102 | + expect(stripSoftCursor(line)).toBe(` helloworld${" ".repeat(5)}`); |
| 103 | + }); |
| 104 | + |
| 105 | + it("handles content line: cursor at end of text + padding", () => { |
| 106 | + // Editor renders: " hello[cursor: ] " |
| 107 | + const line = ` hello${REV} ${RST}${" ".repeat(5)}`; |
| 108 | + expect(stripSoftCursor(line)).toBe(` hello ${" ".repeat(5)}`); |
| 109 | + }); |
| 110 | + |
| 111 | + it("handles content line with hardware cursor marker before soft cursor", () => { |
| 112 | + // The hardware cursor marker is a zero-width APC sequence placed before the soft cursor |
| 113 | + const CURSOR_MARKER = "\x1b_pi:c\x07"; |
| 114 | + const line = ` hello${CURSOR_MARKER}${REV}w${RST}orld `; |
| 115 | + expect(stripSoftCursor(line)).toBe(` hello${CURSOR_MARKER}world `); |
| 116 | + }); |
| 117 | +}); |
0 commit comments