xonecas.io
70160
Ce

cel-tui / files

cel-tui / packages / core / src / width.test.ts

3.2 KB
1import { describe, expect, test } from "bun:test";
2import { visibleWidth, visibleWidthFromColumn } from "./width.js";
3
4describe("visibleWidth", () => {
5  describe("ASCII fast path", () => {
6    test("empty string", () => {
7      expect(visibleWidth("")).toBe(0);
8    });
9
10    test("simple ASCII", () => {
11      expect(visibleWidth("hello")).toBe(5);
12    });
13
14    test("ASCII with spaces", () => {
15      expect(visibleWidth("hello world")).toBe(11);
16    });
17
18    test("ASCII punctuation", () => {
19      expect(visibleWidth("a-b_c.d")).toBe(7);
20    });
21
22    test("single character", () => {
23      expect(visibleWidth("x")).toBe(1);
24    });
25  });
26
27  describe("East Asian wide characters", () => {
28    test("CJK characters are width 2", () => {
29      expect(visibleWidth("世界")).toBe(4);
30    });
31
32    test("mixed ASCII and CJK", () => {
33      expect(visibleWidth("hi世界")).toBe(6);
34    });
35
36    test("fullwidth punctuation", () => {
37      expect(visibleWidth("!")).toBe(2);
38    });
39  });
40
41  describe("emoji", () => {
42    test("simple emoji is width 2", () => {
43      expect(visibleWidth("😀")).toBe(2);
44    });
45
46    test("emoji in text", () => {
47      expect(visibleWidth("hi 😀 there")).toBe(11);
48    });
49
50    test("ZWJ emoji sequence", () => {
51      expect(visibleWidth("👨‍👩‍👧")).toBe(2);
52    });
53
54    test("flag emoji", () => {
55      expect(visibleWidth("🇺🇸")).toBe(2);
56    });
57
58    test("emoji with skin tone", () => {
59      expect(visibleWidth("👋🏽")).toBe(2);
60    });
61  });
62
63  describe("ANSI escape sequences", () => {
64    test("CSI SGR codes are zero width", () => {
65      expect(visibleWidth("\x1b[31mred\x1b[0m")).toBe(3);
66    });
67
68    test("bold + color", () => {
69      expect(visibleWidth("\x1b[1;34mblue\x1b[0m")).toBe(4);
70    });
71
72    test("256 color", () => {
73      expect(visibleWidth("\x1b[38;5;240mgray\x1b[0m")).toBe(4);
74    });
75
76    test("RGB color", () => {
77      expect(visibleWidth("\x1b[38;2;255;128;0morange\x1b[0m")).toBe(6);
78    });
79
80    test("only ANSI codes, no visible content", () => {
81      expect(visibleWidth("\x1b[31m\x1b[0m")).toBe(0);
82    });
83
84    test("CSI sequences accept any standard final byte", () => {
85      expect(visibleWidth("\x1b[2~")).toBe(0);
86      expect(visibleWidth("A\x1b[2~B")).toBe(2);
87    });
88
89    test("OSC hyperlink", () => {
90      expect(
91        visibleWidth("\x1b]8;;https://example.com\x07link\x1b]8;;\x07"),
92      ).toBe(4);
93    });
94  });
95
96  describe("zero-width characters", () => {
97    test("combining marks", () => {
98      // e + combining acute accent = 1 grapheme, width 1
99      expect(visibleWidth("e\u0301")).toBe(1);
100    });
101
102    test("zero-width joiner", () => {
103      expect(visibleWidth("\u200D")).toBe(0);
104    });
105  });
106
107  describe("tabs", () => {
108    test("expand to the next tab stop from column zero", () => {
109      expect(visibleWidth("\tfoo")).toBe(7);
110    });
111
112    test("expand relative to the current column", () => {
113      expect(visibleWidth("a\tb")).toBe(5);
114      expect(visibleWidthFromColumn("\t", 3)).toBe(1);
115    });
116  });
117
118  describe("mixed content", () => {
119    test("CJK + ANSI + ASCII", () => {
120      expect(visibleWidth("\x1b[1m世界\x1b[0m hello")).toBe(10);
121    });
122
123    test("emoji + ANSI", () => {
124      expect(visibleWidth("\x1b[32m😀\x1b[0m ok")).toBe(5);
125    });
126  });
127});