xonecas.io
70160
Ce

cel-tui / files

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

13.8 KB
1import { describe, expect, test } from "bun:test";
2import type { Theme } from "@cel-tui/types";
3import { type Cell, CellBuffer } from "./cell-buffer.js";
4import { defaultTheme, emitBuffer, emitDiff } from "./emitter.js";
5
6function cell(char: string, overrides: Partial<Cell> = {}): Cell {
7  return {
8    char,
9    fgColor: null,
10    bgColor: null,
11    bold: false,
12    italic: false,
13    underline: false,
14    ...overrides,
15  };
16}
17
18describe("emitBuffer", () => {
19  test("emits plain text", () => {
20    const buf = new CellBuffer(5, 1);
21    buf.set(0, 0, cell("H"));
22    buf.set(1, 0, cell("e"));
23    buf.set(2, 0, cell("l"));
24    buf.set(3, 0, cell("l"));
25    buf.set(4, 0, cell("o"));
26    const output = emitBuffer(buf);
27    // Should contain "Hello" somewhere in the output
28    expect(output).toContain("Hello");
29  });
30
31  test("emits multiple rows", () => {
32    const buf = new CellBuffer(2, 2);
33    buf.set(0, 0, cell("A"));
34    buf.set(1, 0, cell("B"));
35    buf.set(0, 1, cell("C"));
36    buf.set(1, 1, cell("D"));
37    const output = emitBuffer(buf);
38    expect(output).toContain("AB");
39    expect(output).toContain("CD");
40  });
41
42  test("emits foreground color using default ANSI theme", () => {
43    const buf = new CellBuffer(1, 1);
44    buf.set(0, 0, cell("X", { fgColor: "color01" }));
45    const output = emitBuffer(buf);
46    // color01 = ANSI 1 (red) → fg SGR 31
47    expect(output).toContain("\x1b[31m");
48    expect(output).toContain("X");
49  });
50
51  test("emits background color using default ANSI theme", () => {
52    const buf = new CellBuffer(1, 1);
53    buf.set(0, 0, cell("X", { bgColor: "color04" }));
54    const output = emitBuffer(buf);
55    // color04 = ANSI 4 (blue) → bg SGR 44
56    expect(output).toContain("\x1b[44m");
57    expect(output).toContain("X");
58  });
59
60  test("emits bright color variants", () => {
61    const buf = new CellBuffer(1, 1);
62    buf.set(0, 0, cell("X", { fgColor: "color08" }));
63    const output = emitBuffer(buf);
64    // color08 = ANSI 8 (bright black) → fg SGR 90
65    expect(output).toContain("\x1b[90m");
66  });
67
68  test("emits bold", () => {
69    const buf = new CellBuffer(1, 1);
70    buf.set(0, 0, cell("X", { bold: true }));
71    const output = emitBuffer(buf);
72    expect(output).toContain("\x1b[1m");
73  });
74
75  test("emits italic", () => {
76    const buf = new CellBuffer(1, 1);
77    buf.set(0, 0, cell("X", { italic: true }));
78    const output = emitBuffer(buf);
79    expect(output).toContain("\x1b[3m");
80  });
81
82  test("emits underline", () => {
83    const buf = new CellBuffer(1, 1);
84    buf.set(0, 0, cell("X", { underline: true }));
85    const output = emitBuffer(buf);
86    expect(output).toContain("\x1b[4m");
87  });
88
89  test("combines multiple style attributes", () => {
90    const buf = new CellBuffer(1, 1);
91    buf.set(
92      0,
93      0,
94      cell("X", { bold: true, fgColor: "color02", bgColor: "color00" }),
95    );
96    const output = emitBuffer(buf);
97    expect(output).toContain("X");
98    // Should contain bold (1), green fg (32), black bg (40) in some SGR
99    expect(output).toContain("1");
100    expect(output).toContain("32");
101    expect(output).toContain("40");
102  });
103
104  test("resets style between differently styled cells", () => {
105    const buf = new CellBuffer(2, 1);
106    buf.set(0, 0, cell("A", { fgColor: "color01" }));
107    buf.set(1, 0, cell("B", { fgColor: "color04" }));
108    const output = emitBuffer(buf);
109    expect(output).toContain("A");
110    expect(output).toContain("B");
111    // Should contain reset between the two
112    expect(output).toContain("\x1b[0m");
113  });
114
115  test("wraps output in synchronized output markers", () => {
116    const buf = new CellBuffer(1, 1);
117    const output = emitBuffer(buf);
118    // Begin synchronized output
119    expect(output.startsWith("\x1b[?2026h")).toBe(true);
120    // End synchronized output
121    expect(output.endsWith("\x1b[?2026l")).toBe(true);
122  });
123
124  test("positions cursor at home before rendering", () => {
125    const buf = new CellBuffer(1, 1);
126    const output = emitBuffer(buf);
127    // Should contain cursor home (CSI H)
128    expect(output).toContain("\x1b[H");
129  });
130
131  test("establishes a default SGR baseline before rendering the full frame", () => {
132    const buf = new CellBuffer(1, 1);
133    const output = emitBuffer(buf);
134
135    expect(output.startsWith("\x1b[?2026h\x1b[H\x1b[0m")).toBe(true);
136  });
137
138  test("empty cells render as spaces", () => {
139    const buf = new CellBuffer(3, 1);
140    // Leave all cells empty
141    const output = emitBuffer(buf);
142    expect(output).toContain("   ");
143  });
144
145  test("wide character continuation cells are skipped", () => {
146    const buf = new CellBuffer(5, 1);
147    // Simulate a wide char at col 0 with continuation at col 1
148    buf.set(0, 0, cell("\u4e16")); // CJK char (2 cols)
149    buf.set(1, 0, cell("")); // continuation marker
150    buf.set(2, 0, cell("x"));
151    const output = emitBuffer(buf);
152    // The output should contain the wide char and "x" without
153    // an extra space between them (continuation is skipped)
154    expect(output).toContain("\u4e16x");
155    // Should NOT contain "\u4e16 x" (space from continuation)
156    expect(output).not.toContain("\u4e16 x");
157  });
158
159  test("custom theme with ANSI index remapping", () => {
160    const buf = new CellBuffer(1, 1);
161    buf.set(0, 0, cell("X", { fgColor: "color01" }));
162    // Remap color01 to ANSI index 6 (cyan) instead of 1 (red)
163    const theme: Theme = {
164      color00: 0,
165      color01: 6,
166      color02: 2,
167      color03: 3,
168      color04: 4,
169      color05: 5,
170      color06: 1,
171      color07: 7,
172      color08: 8,
173      color09: 9,
174      color10: 10,
175      color11: 11,
176      color12: 12,
177      color13: 13,
178      color14: 14,
179      color15: 15,
180    };
181    const output = emitBuffer(buf, theme);
182    // color01 remapped to ANSI 6 → fg SGR 36 (cyan)
183    expect(output).toContain("\x1b[36m");
184    expect(output).not.toContain("\x1b[31m");
185  });
186
187  test("custom theme with true color hex values", () => {
188    const buf = new CellBuffer(1, 1);
189    buf.set(0, 0, cell("X", { fgColor: "color01", bgColor: "color00" }));
190    const theme: Theme = {
191      color00: "#1e1e2e",
192      color01: "#f38ba8",
193      color02: 2,
194      color03: 3,
195      color04: 4,
196      color05: 5,
197      color06: 6,
198      color07: 7,
199      color08: 8,
200      color09: 9,
201      color10: 10,
202      color11: 11,
203      color12: 12,
204      color13: 13,
205      color14: 14,
206      color15: 15,
207    };
208    const output = emitBuffer(buf, theme);
209    // color01 = "#f38ba8" → fg 38;2;243;139;168
210    expect(output).toContain("38;2;243;139;168");
211    // color00 = "#1e1e2e" → bg 48;2;30;30;46
212    expect(output).toContain("48;2;30;30;46");
213  });
214});
215
216describe("emitDiff", () => {
217  test("returns empty string when buffers are identical", () => {
218    const prev = new CellBuffer(5, 2);
219    const next = new CellBuffer(5, 2);
220    prev.set(0, 0, cell("A"));
221    next.set(0, 0, cell("A"));
222    const output = emitDiff(prev, next);
223    expect(output).toBe("");
224  });
225
226  test("returns empty string when cells and visible cursor are unchanged", () => {
227    const prev = new CellBuffer(5, 1);
228    const next = new CellBuffer(5, 1);
229
230    const output = emitDiff(prev, next, defaultTheme, {
231      cursor: { visible: true, x: 2, y: 0 },
232      previousCursor: { visible: true, x: 2, y: 0 },
233    });
234
235    expect(output).toBe("");
236  });
237
238  test("returns empty string when the explicit cursor style is unchanged", () => {
239    const prev = new CellBuffer(5, 1);
240    const next = new CellBuffer(5, 1);
241
242    const output = emitDiff(prev, next, defaultTheme, {
243      cursor: { visible: true, x: 2, y: 0, style: "bar" },
244      previousCursor: { visible: true, x: 2, y: 0, style: "bar" },
245    });
246
247    expect(output).toBe("");
248  });
249
250  test("emits a deterministic shape-only cursor update", () => {
251    const prev = new CellBuffer(5, 1);
252    const next = new CellBuffer(5, 1);
253
254    const output = emitDiff(prev, next, defaultTheme, {
255      cursor: { visible: true, x: 2, y: 0, style: "underline" },
256      previousCursor: { visible: true, x: 2, y: 0, style: "bar" },
257    });
258
259    expect(output).toBe("\x1b[?2026h\x1b[3 q\x1b[?2026l");
260  });
261
262  test("emits a cursor-only update when its position changes", () => {
263    const prev = new CellBuffer(5, 1);
264    const next = new CellBuffer(5, 1);
265
266    const output = emitDiff(prev, next, defaultTheme, {
267      cursor: { visible: true, x: 3, y: 0 },
268      previousCursor: { visible: true, x: 2, y: 0 },
269    });
270
271    expect(output).toBe("\x1b[?2026h\x1b[1;4H\x1b[?2026l");
272  });
273
274  test("emits only changed cells with cursor positioning", () => {
275    const prev = new CellBuffer(5, 2);
276    const next = new CellBuffer(5, 2);
277    prev.set(0, 0, cell("A"));
278    next.set(0, 0, cell("A"));
279    // Change cell at (2, 1)
280    next.set(2, 1, cell("X"));
281    const output = emitDiff(prev, next);
282    // Should position cursor at (2, 1) = CSI row;col H (1-indexed)
283    expect(output).toContain("\x1b[2;3H");
284    expect(output).toContain("X");
285  });
286
287  test("emits styled changed cells", () => {
288    const prev = new CellBuffer(3, 1);
289    const next = new CellBuffer(3, 1);
290    next.set(1, 0, cell("Z", { fgColor: "color01", bold: true }));
291    const output = emitDiff(prev, next);
292    expect(output).toContain("\x1b[1;2H"); // cursor to (1, 0)
293    expect(output).toContain("Z");
294    expect(output).toContain("\x1b[1;31m"); // bold + red
295  });
296
297  test("wraps in synchronized output markers", () => {
298    const prev = new CellBuffer(1, 1);
299    const next = new CellBuffer(1, 1);
300    next.set(0, 0, cell("Q"));
301    const output = emitDiff(prev, next);
302    expect(output.startsWith("\x1b[?2026h")).toBe(true);
303    expect(output.endsWith("\x1b[?2026l")).toBe(true);
304  });
305
306  test("batches consecutive changed cells on same row", () => {
307    const prev = new CellBuffer(5, 1);
308    const next = new CellBuffer(5, 1);
309    next.set(1, 0, cell("A"));
310    next.set(2, 0, cell("B"));
311    next.set(3, 0, cell("C"));
312    const output = emitDiff(prev, next);
313    // Should position once and emit ABC together
314    expect(output).toContain("ABC");
315    // Only one cursor positioning for this run
316    // biome-ignore lint/suspicious/noControlCharactersInRegex: matching terminal escape sequences requires ESC bytes.
317    const cursorMoves = output.match(/\x1b\[\d+;\d+H/g) || [];
318    expect(cursorMoves.length).toBe(1);
319  });
320
321  test("wide character continuation cells are skipped in diff", () => {
322    const prev = new CellBuffer(5, 1);
323    const next = new CellBuffer(5, 1);
324    // Wide char at col 0 with continuation at col 1
325    next.set(0, 0, cell("\u4e16"));
326    next.set(1, 0, cell("")); // continuation
327    next.set(2, 0, cell("x"));
328    const output = emitDiff(prev, next);
329    // Should emit the wide char and x without extra space
330    expect(output).toContain("\u4e16");
331    expect(output).toContain("x");
332    // Continuation cell should not produce any output character
333    expect(output).not.toContain("\u4e16 ");
334  });
335
336  test("restores an unchanged visible cursor inside synchronized diff output", () => {
337    const prev = new CellBuffer(5, 1);
338    const next = new CellBuffer(5, 1);
339    next.set(2, 0, cell("X"));
340
341    const output = emitDiff(prev, next, defaultTheme, {
342      cursor: { visible: true, x: 4, y: 2 },
343      previousCursor: { visible: true, x: 4, y: 2 },
344    });
345
346    expect(output.startsWith("\x1b[?2026h\x1b7")).toBe(true);
347    expect(output.endsWith("\x1b8\x1b[?2026l")).toBe(true);
348    expect(output).not.toContain("\x1b[?25h");
349  });
350
351  test("moves and shows the cursor before ending synchronized full output", () => {
352    const buf = new CellBuffer(2, 1);
353    const output = emitBuffer(buf, defaultTheme, {
354      cursor: { visible: true, x: 1, y: 0 },
355      previousCursor: { visible: false },
356    });
357
358    expect(output.endsWith("\x1b[1;2H\x1b[1 q\x1b[?25h\x1b[?2026l")).toBe(true);
359  });
360
361  for (const [style, sequence] of [
362    ["block", "\x1b[1 q"],
363    ["underline", "\x1b[3 q"],
364    ["bar", "\x1b[5 q"],
365  ] as const) {
366    test(`full output selects the ${style} native cursor shape`, () => {
367      const output = emitBuffer(new CellBuffer(2, 1), defaultTheme, {
368        cursor: { visible: true, x: 1, y: 0, style },
369        previousCursor: { visible: false },
370      });
371
372      expect(output).toContain(`\x1b[1;2H${sequence}\x1b[?25h`);
373    });
374  }
375
376  test("full output repairs cursor state even when the logical cursor is unchanged", () => {
377    const buf = new CellBuffer(2, 1);
378    const output = emitBuffer(buf, defaultTheme, {
379      cursor: { visible: true, x: 1, y: 0 },
380      previousCursor: { visible: true, x: 1, y: 0 },
381    });
382
383    expect(output).not.toContain("\x1b7");
384    expect(output).not.toContain("\x1b8");
385    expect(output.endsWith("\x1b[1;2H\x1b[1 q\x1b[?25h\x1b[?2026l")).toBe(true);
386  });
387
388  test("full output explicitly hides the cursor from an unknown terminal state", () => {
389    const output = emitBuffer(new CellBuffer(1, 1), defaultTheme, {
390      cursor: { visible: false },
391      previousCursor: { visible: false },
392    });
393
394    expect(output.endsWith("\x1b[?25l\x1b[?2026l")).toBe(true);
395  });
396
397  test("hides a previously visible cursor inside synchronized diff output", () => {
398    const prev = new CellBuffer(1, 1);
399    const next = new CellBuffer(1, 1);
400
401    const output = emitDiff(prev, next, defaultTheme, {
402      cursor: { visible: false },
403      previousCursor: { visible: true, x: 0, y: 0 },
404    });
405
406    expect(output).toBe("\x1b[?2026h\x1b[?25l\x1b[?2026l");
407  });
408
409  test("custom theme applies to diff output", () => {
410    const prev = new CellBuffer(3, 1);
411    const next = new CellBuffer(3, 1);
412    next.set(1, 0, cell("Z", { fgColor: "color01" }));
413    const theme: Theme = {
414      color00: "#000000",
415      color01: "#ff0000",
416      color02: 2,
417      color03: 3,
418      color04: 4,
419      color05: 5,
420      color06: 6,
421      color07: 7,
422      color08: 8,
423      color09: 9,
424      color10: 10,
425      color11: 11,
426      color12: 12,
427      color13: 13,
428      color14: 14,
429      color15: 15,
430    };
431    const output = emitDiff(prev, next, theme);
432    // color01 = "#ff0000" → fg 38;2;255;0;0
433    expect(output).toContain("38;2;255;0;0");
434  });
435});