70160
Ce
cel-tui / files
cel-tui / packages / core / src / text-layout.test.ts
4.3 KB
1import { describe, expect, test } from "bun:test"; 2import { layoutText, measureTextLineCount } from "./text-layout.js"; 3import { visibleWidth } from "./width.js"; 4 5describe("text-layout", () => { 6 test("preserves whitespace at soft wraps and maps offsets using visual lines", () => { 7 const result = layoutText("foo bar baz", 6, "word"); 8 9 expect(result.lines.map((line) => line.text)).toEqual([ 10 "foo ", 11 "bar ", 12 "baz", 13 ]); 14 expect(result.lineCount).toBe(3); 15 16 expect(result.offsetToPosition(4)).toEqual({ line: 1, col: 0 }); 17 expect(result.offsetToPosition(5)).toEqual({ line: 1, col: 1 }); 18 expect(result.offsetToPosition(8)).toEqual({ line: 2, col: 0 }); 19 20 expect(result.positionToOffset(1, 0)).toBe(4); 21 expect(result.positionToOffset(1, 1)).toBe(5); 22 expect(result.positionToOffset(2, 0)).toBe(8); 23 }); 24 25 test("preserves repeated and trailing spaces", () => { 26 const result = layoutText("a b ", 3, "word"); 27 28 expect(result.lines.map((line) => line.text)).toEqual(["a ", "b "]); 29 expect(result.offsetToPosition(3)).toEqual({ line: 1, col: 0 }); 30 expect(result.offsetToPosition(5)).toEqual({ line: 1, col: 2 }); 31 }); 32 33 test("preserves explicit trailing newline as an empty final visual line", () => { 34 const result = layoutText("a\n", 10, "word"); 35 36 expect(result.lines.map((line) => line.text)).toEqual(["a", ""]); 37 expect(result.lineCount).toBe(2); 38 expect(result.offsetToPosition(1)).toEqual({ line: 0, col: 1 }); 39 expect(result.offsetToPosition(2)).toEqual({ line: 1, col: 0 }); 40 }); 41 42 test("wraps tabs using visual tab-stop width", () => { 43 const result = layoutText("\tfoo", 6, "word"); 44 45 expect( 46 result.lines.map((line) => ({ text: line.text, width: line.width })), 47 ).toEqual([ 48 { text: "\t", width: 4 }, 49 { text: "foo", width: 3 }, 50 ]); 51 expect(result.offsetToPosition(1)).toEqual({ line: 1, col: 0 }); 52 }); 53 54 test("recomputes tab stops after soft wrapping", () => { 55 const result = layoutText("abcde\tXYZ", 6, "word"); 56 57 expect(result.lines.every((line) => line.width <= 6)).toBe(true); 58 expect(result.lines.map((line) => line.width)).toEqual( 59 result.lines.map((line) => visibleWidth(line.text)), 60 ); 61 expect(measureTextLineCount("abcde\tXYZ", 6, "word")).toBe( 62 result.lineCount, 63 ); 64 }); 65 66 test("strips ANSI control sequences while preserving visible text", () => { 67 const result = layoutText("\x1b[31mred\x1b[0m", 10, "none"); 68 69 expect(result.lines).toEqual([ 70 { 71 text: "red", 72 startOffset: 0, 73 endOffset: 12, 74 width: 3, 75 }, 76 ]); 77 expect(result.offsetToPosition(0)).toEqual({ line: 0, col: 0 }); 78 expect(result.offsetToPosition(4)).toEqual({ line: 0, col: 0 }); 79 expect(result.offsetToPosition(12)).toEqual({ line: 0, col: 3 }); 80 expect(result.positionToOffset(0, 3)).toBe(12); 81 expect(measureTextLineCount("\x1b[31m", 10, "word")).toBe(1); 82 }); 83 84 test("does not create a blank wrapped line for ANSI before a wide grapheme", () => { 85 const result = layoutText("\x1b[31m界\x1b[0m", 1, "word"); 86 87 expect(result.lines).toEqual([ 88 { text: "界", startOffset: 0, endOffset: 10, width: 2 }, 89 ]); 90 expect(result.lineCount).toBe(1); 91 }); 92 93 test("wraps unbroken wide graphemes by grapheme boundaries", () => { 94 const result = layoutText("世界世界", 5, "word"); 95 96 expect( 97 result.lines.map((line) => ({ text: line.text, width: line.width })), 98 ).toEqual([ 99 { text: "世界", width: 4 }, 100 { text: "世界", width: 4 }, 101 ]); 102 expect(result.offsetToPosition(2)).toEqual({ line: 1, col: 0 }); 103 expect(result.positionToOffset(1, 1)).toBe(3); 104 }); 105 106 test("measureTextLineCount fast path counts hard lines for wrap none", () => { 107 expect(measureTextLineCount("", 10, "none")).toBe(1); 108 expect(measureTextLineCount("hello", 1, "none")).toBe(1); 109 expect(measureTextLineCount("a\nb\n", 1, "none")).toBe(3); 110 }); 111 112 test("measureTextLineCount matches layoutText lineCount for wrapped cases", () => { 113 const cases = [ 114 ["foo bar baz", 6], 115 ["a b ", 3], 116 ["a\n", 10], 117 ["\tfoo", 6], 118 ["世界世界", 5], 119 ] as const; 120 121 for (const [value, width] of cases) { 122 expect(measureTextLineCount(value, width, "word")).toBe( 123 layoutText(value, width, "word").lineCount, 124 ); 125 } 126 }); 127});