tui-engine-local-deltas.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. CURSOR_MARKER,
  4. Editor,
  5. Input,
  6. SelectList,
  7. Text,
  8. TuiMainScreen,
  9. type Component,
  10. type TUI,
  11. visibleWidth,
  12. } from '../../src/tui/engine/public.js';
  13. import { VirtualTerminal } from '../pi-084-upstream/virtual-terminal.js';
  14. const passthrough = (value: string): string => value;
  15. const selectListTheme = {
  16. selectedPrefix: passthrough,
  17. selectedText: passthrough,
  18. description: passthrough,
  19. scrollInfo: passthrough,
  20. noMatch: passthrough,
  21. };
  22. class RecordingVirtualTerminal extends VirtualTerminal {
  23. private writes: string[] = [];
  24. override write(data: string): void {
  25. this.writes.push(data);
  26. super.write(data);
  27. }
  28. takeWrites(): string {
  29. const output = this.writes.join('');
  30. this.writes = [];
  31. return output;
  32. }
  33. }
  34. class MutableLines implements Component {
  35. lines: string[] = [];
  36. render(): string[] {
  37. return [...this.lines];
  38. }
  39. invalidate(): void {}
  40. }
  41. describe('MCode Pi Engine local deltas', () => {
  42. it('fits Text padding within narrow terminal widths', () => {
  43. const text = new Text('content', 2, 0);
  44. for (const width of [1, 2, 3, 4]) {
  45. expect(text.render(width).every((line) => visibleWidth(line) <= width)).toBe(true);
  46. }
  47. });
  48. it('preserves native scrollback when transient rows shrink above the viewport', async () => {
  49. const terminal = new RecordingVirtualTerminal(40, 5);
  50. const tui = new TuiMainScreen(terminal);
  51. const component = new MutableLines();
  52. component.lines = [
  53. 'header',
  54. 'activity-1',
  55. 'activity-2',
  56. 'activity-3',
  57. 'activity-4',
  58. 'status',
  59. 'composer',
  60. ];
  61. tui.addChild(component);
  62. tui.renderNow();
  63. await terminal.flush();
  64. terminal.takeWrites();
  65. component.lines = ['header', 'status', 'composer'];
  66. tui.renderNow();
  67. await terminal.flush();
  68. const writes = terminal.takeWrites();
  69. expect(writes).toContain('\x1b[2J\x1b[H');
  70. expect(writes).not.toContain('\x1b[3J');
  71. expect(terminal.getScrollBuffer()).toContain('activity-1');
  72. expect(terminal.getViewport()).toEqual(['header', 'status', 'composer', '', '']);
  73. expect(tui.fullRedraws).toBe(2);
  74. });
  75. it('renders an urgent product interaction without resetting Main diff state', async () => {
  76. const terminal = new RecordingVirtualTerminal(40, 5);
  77. const tui = new TuiMainScreen(terminal);
  78. const component = new MutableLines();
  79. component.lines = ['stable', 'tail'];
  80. tui.addChild(component);
  81. tui.renderNow();
  82. await terminal.flush();
  83. terminal.takeWrites();
  84. component.lines.push('interaction');
  85. tui.requestImmediateRender();
  86. await new Promise<void>((resolve) => setImmediate(resolve));
  87. await terminal.flush();
  88. const writes = terminal.takeWrites();
  89. expect(writes).toContain('interaction');
  90. expect(writes).not.toContain('\x1b[2J');
  91. expect(writes).not.toContain('\x1b[3J');
  92. });
  93. it('keeps OSC 133 zone sentinels out of regular-mode terminal writes', async () => {
  94. const terminal = new RecordingVirtualTerminal(40, 5);
  95. const tui = new TuiMainScreen(terminal);
  96. const component = new MutableLines();
  97. const zoneStart = '\x1b]133;A\x07';
  98. const zoneClose = '\x1b]133;B\x07\x1b]133;C\x07';
  99. component.lines = [`${zoneStart}user prompt`, `${zoneClose}assistant reply`, 'composer'];
  100. tui.addChild(component);
  101. tui.renderNow();
  102. await terminal.flush();
  103. const initialWrites = terminal.takeWrites();
  104. expect(initialWrites).toContain('user prompt');
  105. expect(initialWrites).toContain('assistant reply');
  106. expect(initialWrites).not.toContain('\x1b]133;');
  107. component.lines = [`${zoneStart}user prompt`, `${zoneClose}assistant reply updated`, 'composer'];
  108. tui.renderNow();
  109. await terminal.flush();
  110. const diffWrites = terminal.takeWrites();
  111. expect(diffWrites).toContain('assistant reply updated');
  112. expect(diffWrites).not.toContain('\x1b]133;');
  113. });
  114. it('limits Input extensions to prompt, mask, and paste transformation', () => {
  115. const input = new Input({
  116. prompt: 'Search: ',
  117. mask: '•',
  118. transformPaste: (value) => value.replace(/\s+/gu, ' '),
  119. });
  120. input.handleInput('\u001B[200~secret\nvalue\u001B[201~');
  121. input.focused = true;
  122. const rendered = input.render(30)[0] ?? '';
  123. expect(input.getValue()).toBe('secret value');
  124. expect(rendered).toContain('Search: ');
  125. expect(rendered).toContain('•');
  126. expect(rendered).not.toContain('secret');
  127. expect(rendered).toContain(CURSOR_MARKER);
  128. });
  129. it('retains grouped SelectList headings while using Pi filtering and single-line descriptions', () => {
  130. const list = new SelectList(
  131. [
  132. {
  133. value: 'docs',
  134. label: 'Documents',
  135. description: 'Create, edit, search and share documents.',
  136. groupLabel: 'Files',
  137. },
  138. {
  139. value: 'code',
  140. label: 'Code',
  141. description: 'Inspect and modify source code.',
  142. groupLabel: 'Files',
  143. },
  144. ],
  145. 2,
  146. selectListTheme,
  147. );
  148. list.setFilter('docs');
  149. const rendered = list.render(48);
  150. expect(rendered.join('\n')).toContain('Files');
  151. expect(rendered.join('\n')).toContain('Documents');
  152. expect(rendered.join('\n')).not.toContain('Code');
  153. expect(rendered).toHaveLength(2);
  154. expect(rendered.every((line) => visibleWidth(line) <= 48)).toBe(true);
  155. });
  156. it('exposes only the generic Editor hooks required by a product Draft adapter', () => {
  157. const tui = {
  158. terminal: { rows: 24 },
  159. requestRender: () => undefined,
  160. } as TUI;
  161. const editor = new Editor(tui, { borderColor: passthrough, selectList: selectListTheme }, {
  162. transformPaste: (value) => value.replaceAll('\r', ''),
  163. });
  164. let extensionState = 'empty';
  165. editor.captureUndoExtensionState = () => extensionState;
  166. editor.restoreUndoExtensionState = (state) => {
  167. extensionState = String(state);
  168. };
  169. editor.handleInput('a');
  170. extensionState = 'typed';
  171. editor.handleInput('\x1f');
  172. expect(editor.getText()).toBe('');
  173. expect(extensionState).toBe('empty');
  174. editor.onPaste = (value) => value.endsWith('.png');
  175. editor.handleInput('\u001B[200~/tmp/image.png\r\u001B[201~');
  176. expect(editor.getText()).toBe('');
  177. const pasted = 'x'.repeat(1_001);
  178. editor.onPaste = undefined;
  179. editor.handleInput(`\u001B[200~${pasted}\u001B[201~`);
  180. const snapshot = editor.captureState();
  181. editor.setText('replacement');
  182. expect(editor.restoreState(snapshot)).toBe(true);
  183. expect(editor.getExpandedText()).toBe(pasted);
  184. let submitted = '';
  185. editor.onSubmit = (value) => {
  186. submitted = value;
  187. };
  188. editor.submit();
  189. expect(submitted).toBe(pasted);
  190. });
  191. });