viewport-overwrite-repro.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * TUI viewport overwrite repro
  3. *
  4. * Place this file at: packages/tui/test/viewport-overwrite-repro.ts
  5. * Run from repo root: npx tsx packages/tui/test/viewport-overwrite-repro.ts
  6. *
  7. * For reliable repro, run in a small terminal (8-12 rows) or a tmux session:
  8. * tmux new-session -d -s tui-bug -x 80 -y 12
  9. * tmux send-keys -t tui-bug "npx tsx packages/tui/test/viewport-overwrite-repro.ts" Enter
  10. * tmux attach -t tui-bug
  11. *
  12. * Expected behavior:
  13. * - PRE-TOOL lines remain visible above tool output.
  14. * - POST-TOOL lines append after tool output without overwriting earlier content.
  15. *
  16. * Actual behavior (bug):
  17. * - When content exceeds the viewport and new lines arrive after a tool-call pause,
  18. * some earlier PRE-TOOL lines near the bottom are overwritten by POST-TOOL lines.
  19. */
  20. import { ProcessTerminal } from "../src/terminal.ts";
  21. import { type Component, TUI } from "../src/tui.ts";
  22. const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
  23. class Lines implements Component {
  24. private lines: string[] = [];
  25. set(lines: string[]): void {
  26. this.lines = lines;
  27. }
  28. append(lines: string[]): void {
  29. this.lines.push(...lines);
  30. }
  31. render(width: number): string[] {
  32. return this.lines.map((line) => {
  33. if (line.length > width) return line.slice(0, width);
  34. return line.padEnd(width, " ");
  35. });
  36. }
  37. invalidate(): void {}
  38. }
  39. async function streamLines(buffer: Lines, label: string, count: number, delayMs: number, ui: TUI): Promise<void> {
  40. for (let i = 1; i <= count; i += 1) {
  41. buffer.append([`${label} ${String(i).padStart(2, "0")}`]);
  42. ui.requestRender();
  43. await sleep(delayMs);
  44. }
  45. }
  46. async function main(): Promise<void> {
  47. const ui = new TUI(new ProcessTerminal());
  48. const buffer = new Lines();
  49. ui.addChild(buffer);
  50. ui.start();
  51. const height = ui.terminal.rows;
  52. const preCount = height + 8; // Ensure content exceeds viewport
  53. const toolCount = height + 12; // Tool output pushes further into scrollback
  54. const postCount = 6;
  55. buffer.set([
  56. "TUI viewport overwrite repro",
  57. `Viewport rows detected: ${height}`,
  58. "(Resize to ~8-12 rows for best repro)",
  59. "",
  60. "=== PRE-TOOL STREAM ===",
  61. ]);
  62. ui.requestRender();
  63. await sleep(300);
  64. // Phase 1: Stream pre-tool text until viewport is exceeded.
  65. await streamLines(buffer, "PRE-TOOL LINE", preCount, 30, ui);
  66. // Phase 2: Simulate tool call pause and tool output.
  67. buffer.append(["", "--- TOOL CALL START ---", "(pause...)", ""]);
  68. ui.requestRender();
  69. await sleep(700);
  70. await streamLines(buffer, "TOOL OUT", toolCount, 20, ui);
  71. // Phase 3: Post-tool streaming. This is where overwrite often appears.
  72. buffer.append(["", "=== POST-TOOL STREAM ==="]);
  73. ui.requestRender();
  74. await sleep(300);
  75. await streamLines(buffer, "POST-TOOL LINE", postCount, 40, ui);
  76. // Leave the output visible briefly, then restore terminal state.
  77. await sleep(1500);
  78. ui.stop();
  79. }
  80. main().catch((error) => {
  81. // Ensure terminal is restored if something goes wrong.
  82. try {
  83. const ui = new TUI(new ProcessTerminal());
  84. ui.stop();
  85. } catch {
  86. // Ignore restore errors.
  87. }
  88. process.stderr.write(`${String(error)}\n`);
  89. process.exitCode = 1;
  90. });