tui-overlay-style-leak.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import assert from "node:assert";
  2. import { describe, it } from "node:test";
  3. import type { Terminal as XtermTerminalType } from "@xterm/headless";
  4. import { type Component, TUI } from "../src/tui.ts";
  5. import { VirtualTerminal } from "./virtual-terminal.ts";
  6. class StaticLines implements Component {
  7. private readonly lines: string[];
  8. constructor(lines: string[]) {
  9. this.lines = lines;
  10. }
  11. render(): string[] {
  12. return this.lines;
  13. }
  14. invalidate(): void {}
  15. }
  16. class StaticOverlay implements Component {
  17. private readonly line: string;
  18. constructor(line: string) {
  19. this.line = line;
  20. }
  21. render(): string[] {
  22. return [this.line];
  23. }
  24. invalidate(): void {}
  25. }
  26. function getCellItalic(terminal: VirtualTerminal, row: number, col: number): number {
  27. const xterm = (terminal as unknown as { xterm: XtermTerminalType }).xterm;
  28. const buffer = xterm.buffer.active;
  29. const line = buffer.getLine(buffer.viewportY + row);
  30. assert.ok(line, `Missing buffer line at row ${row}`);
  31. const cell = line.getCell(col);
  32. assert.ok(cell, `Missing cell at row ${row} col ${col}`);
  33. return cell.isItalic();
  34. }
  35. async function renderAndFlush(tui: TUI, terminal: VirtualTerminal): Promise<void> {
  36. tui.requestRender(true);
  37. await new Promise<void>((resolve) => process.nextTick(resolve));
  38. await terminal.waitForRender();
  39. }
  40. describe("TUI overlay compositing", () => {
  41. it("should not leak styles when a trailing reset sits beyond the last visible column (no overlay)", async () => {
  42. const width = 20;
  43. const baseLine = `\x1b[3m${"X".repeat(width)}\x1b[23m`;
  44. const terminal = new VirtualTerminal(width, 6);
  45. const tui = new TUI(terminal);
  46. tui.addChild(new StaticLines([baseLine, "INPUT"]));
  47. tui.start();
  48. await renderAndFlush(tui, terminal);
  49. assert.strictEqual(getCellItalic(terminal, 1, 0), 0);
  50. tui.stop();
  51. });
  52. it("should not leak styles when overlay slicing drops trailing SGR resets", async () => {
  53. const width = 20;
  54. const baseLine = `\x1b[3m${"X".repeat(width)}\x1b[23m`;
  55. const terminal = new VirtualTerminal(width, 6);
  56. const tui = new TUI(terminal);
  57. tui.addChild(new StaticLines([baseLine, "INPUT"]));
  58. tui.showOverlay(new StaticOverlay("OVR"), { row: 0, col: 5, width: 3 });
  59. tui.start();
  60. await renderAndFlush(tui, terminal);
  61. assert.strictEqual(getCellItalic(terminal, 1, 0), 0);
  62. tui.stop();
  63. });
  64. });