virtual-terminal.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import type { Terminal as XtermTerminalType } from "@xterm/headless";
  2. import xterm from "@xterm/headless";
  3. import type { Terminal } from "../../src/tui/engine/terminal.js";
  4. // Extract Terminal class from the module
  5. const XtermTerminal = xterm.Terminal;
  6. /**
  7. * Virtual terminal for testing using xterm.js for accurate terminal emulation
  8. */
  9. export class VirtualTerminal implements Terminal {
  10. private xterm: XtermTerminalType;
  11. private inputHandler?: (data: string) => void;
  12. private resizeHandler?: () => void;
  13. private _columns: number;
  14. private _rows: number;
  15. constructor(columns = 80, rows = 24) {
  16. this._columns = columns;
  17. this._rows = rows;
  18. // Create xterm instance with specified dimensions
  19. this.xterm = new XtermTerminal({
  20. cols: columns,
  21. rows: rows,
  22. // Disable all interactive features for testing
  23. disableStdin: true,
  24. allowProposedApi: true,
  25. });
  26. }
  27. start(onInput: (data: string) => void, onResize: () => void): void {
  28. this.inputHandler = onInput;
  29. this.resizeHandler = onResize;
  30. // Enable bracketed paste mode for consistency with ProcessTerminal
  31. this.xterm.write("\x1b[?2004h");
  32. }
  33. async drainInput(_maxMs?: number, _idleMs?: number): Promise<void> {
  34. // No-op for virtual terminal - no stdin to drain
  35. }
  36. stop(): void {
  37. // Disable bracketed paste mode
  38. this.xterm.write("\x1b[?2004l");
  39. this.inputHandler = undefined;
  40. this.resizeHandler = undefined;
  41. }
  42. write(data: string): void {
  43. this.xterm.write(data);
  44. }
  45. get columns(): number {
  46. return this._columns;
  47. }
  48. get rows(): number {
  49. return this._rows;
  50. }
  51. get kittyProtocolActive(): boolean {
  52. // Virtual terminal always reports Kitty protocol as active for testing
  53. return true;
  54. }
  55. moveBy(lines: number): void {
  56. if (lines > 0) {
  57. // Move down
  58. this.xterm.write(`\x1b[${lines}B`);
  59. } else if (lines < 0) {
  60. // Move up
  61. this.xterm.write(`\x1b[${-lines}A`);
  62. }
  63. // lines === 0: no movement
  64. }
  65. hideCursor(): void {
  66. this.xterm.write("\x1b[?25l");
  67. }
  68. showCursor(): void {
  69. this.xterm.write("\x1b[?25h");
  70. }
  71. clearLine(): void {
  72. this.xterm.write("\x1b[K");
  73. }
  74. clearFromCursor(): void {
  75. this.xterm.write("\x1b[J");
  76. }
  77. clearScreen(): void {
  78. this.xterm.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
  79. }
  80. setTitle(title: string): void {
  81. // OSC 0;title BEL - set terminal window title
  82. this.xterm.write(`\x1b]0;${title}\x07`);
  83. }
  84. setProgress(_active: boolean): void {}
  85. // Test-specific methods not in Terminal interface
  86. /**
  87. * Simulate keyboard input
  88. */
  89. sendInput(data: string): void {
  90. if (this.inputHandler) {
  91. this.inputHandler(data);
  92. }
  93. }
  94. /**
  95. * Resize the terminal
  96. */
  97. resize(columns: number, rows: number): void {
  98. this._columns = columns;
  99. this._rows = rows;
  100. this.xterm.resize(columns, rows);
  101. if (this.resizeHandler) {
  102. this.resizeHandler();
  103. }
  104. }
  105. /**
  106. * Wait for all pending writes to complete. Viewport and scroll buffer will be updated.
  107. */
  108. async flush(): Promise<void> {
  109. // Write an empty string to ensure all previous writes are flushed
  110. return new Promise<void>((resolve) => {
  111. this.xterm.write("", () => resolve());
  112. });
  113. }
  114. /**
  115. * Flush and get viewport - convenience method for tests
  116. */
  117. async flushAndGetViewport(): Promise<string[]> {
  118. await this.flush();
  119. return this.getViewport();
  120. }
  121. /**
  122. * Get the visible viewport (what's currently on screen)
  123. * Note: You should use getViewportAfterWrite() for testing after writing data
  124. */
  125. getViewport(): string[] {
  126. const lines: string[] = [];
  127. const buffer = this.xterm.buffer.active;
  128. // Get only the visible lines (viewport)
  129. for (let i = 0; i < this.xterm.rows; i++) {
  130. const line = buffer.getLine(buffer.viewportY + i);
  131. if (line) {
  132. lines.push(line.translateToString(true));
  133. } else {
  134. lines.push("");
  135. }
  136. }
  137. return lines;
  138. }
  139. /**
  140. * Get the entire scroll buffer
  141. */
  142. getScrollBuffer(): string[] {
  143. const lines: string[] = [];
  144. const buffer = this.xterm.buffer.active;
  145. // Get all lines in the buffer (including scrollback)
  146. for (let i = 0; i < buffer.length; i++) {
  147. const line = buffer.getLine(i);
  148. if (line) {
  149. lines.push(line.translateToString(true));
  150. } else {
  151. lines.push("");
  152. }
  153. }
  154. return lines;
  155. }
  156. /**
  157. * Clear the terminal viewport
  158. */
  159. clear(): void {
  160. this.xterm.clear();
  161. }
  162. /**
  163. * Reset the terminal completely
  164. */
  165. reset(): void {
  166. this.xterm.reset();
  167. }
  168. /**
  169. * Get cursor position
  170. */
  171. getCursorPosition(): { x: number; y: number } {
  172. const buffer = this.xterm.buffer.active;
  173. return {
  174. x: buffer.cursorX,
  175. y: buffer.cursorY,
  176. };
  177. }
  178. /** Wait for TUI's throttled render pipeline to settle. */
  179. async waitForRender(): Promise<void> {
  180. await new Promise<void>((resolve) => process.nextTick(resolve));
  181. await new Promise<void>((resolve) => setTimeout(resolve, 20));
  182. await this.flush();
  183. }
  184. }