|
1 | 1 | import { assert, describe, test } from "@rezi-ui/testkit"; |
2 | | -import { measureTextCells, truncateMiddle, truncateWithEllipsis } from "../textMeasure.js"; |
| 2 | +import { |
| 3 | + measureTextCells, |
| 4 | + truncateMiddle, |
| 5 | + truncateStart, |
| 6 | + truncateWithEllipsis, |
| 7 | +} from "../textMeasure.js"; |
3 | 8 |
|
4 | 9 | function hasUnpairedSurrogate(text: string): boolean { |
5 | 10 | for (let i = 0; i < text.length; i++) { |
@@ -51,4 +56,62 @@ describe("unicode-safe truncation", () => { |
51 | 56 | assert.equal(hasUnpairedSurrogate(result), false); |
52 | 57 | assert.ok(measureTextCells(result) <= 4); |
53 | 58 | }); |
| 59 | + |
| 60 | + test("truncateStart does not split surrogate pairs", () => { |
| 61 | + const result = truncateStart("A😀B", 3); |
| 62 | + assert.equal(result, "…B"); |
| 63 | + assert.equal(hasUnpairedSurrogate(result), false); |
| 64 | + assert.ok(measureTextCells(result) <= 3); |
| 65 | + }); |
| 66 | + |
| 67 | + test("truncateStart does not split ZWJ emoji clusters", () => { |
| 68 | + const family = "👨👩👧👦"; |
| 69 | + const result = truncateStart(`Z${family}`, 2); |
| 70 | + assert.equal(hasUnpairedSurrogate(result), false); |
| 71 | + assert.ok(measureTextCells(result) <= 2); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("truncateStart", () => { |
| 76 | + test("returns full text when it fits", () => { |
| 77 | + assert.equal(truncateStart("abcd", 4), "abcd"); |
| 78 | + assert.equal(truncateStart("abcd", 10), "abcd"); |
| 79 | + }); |
| 80 | + |
| 81 | + test("empty string returns empty", () => { |
| 82 | + assert.equal(truncateStart("", 5), ""); |
| 83 | + }); |
| 84 | + |
| 85 | + test("width=0 returns empty", () => { |
| 86 | + assert.equal(truncateStart("abcdef", 0), ""); |
| 87 | + }); |
| 88 | + |
| 89 | + test("width=1 returns only ellipsis", () => { |
| 90 | + assert.equal(truncateStart("abcdef", 1), "…"); |
| 91 | + }); |
| 92 | + |
| 93 | + test("width=2 keeps one trailing char", () => { |
| 94 | + assert.equal(truncateStart("abcdef", 2), "…f"); |
| 95 | + }); |
| 96 | + |
| 97 | + test("width=3 keeps two trailing chars", () => { |
| 98 | + assert.equal(truncateStart("abcdef", 3), "…ef"); |
| 99 | + }); |
| 100 | + |
| 101 | + test("width=5 keeps four trailing chars", () => { |
| 102 | + assert.equal(truncateStart("abcdef", 5), "…cdef"); |
| 103 | + }); |
| 104 | + |
| 105 | + test("CJK wide chars counted as 2 cells", () => { |
| 106 | + // "你好世界" = 4 chars, 8 cells |
| 107 | + const result = truncateStart("你好世界", 5); |
| 108 | + assert.equal(result, "…世界"); |
| 109 | + assert.ok(measureTextCells(result) <= 5); |
| 110 | + }); |
| 111 | + |
| 112 | + test("mixed ASCII and CJK", () => { |
| 113 | + const result = truncateStart("ab你好cd", 5); |
| 114 | + assert.equal(result, "…好cd"); |
| 115 | + assert.ok(measureTextCells(result) <= 5); |
| 116 | + }); |
54 | 117 | }); |
0 commit comments