| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- import { describe, expect, it } from 'vitest';
- import {
- CURSOR_MARKER,
- Editor,
- Input,
- SelectList,
- Text,
- TuiMainScreen,
- type Component,
- type TUI,
- visibleWidth,
- } from '../../src/tui/engine/public.js';
- import { VirtualTerminal } from '../pi-084-upstream/virtual-terminal.js';
- const passthrough = (value: string): string => value;
- const selectListTheme = {
- selectedPrefix: passthrough,
- selectedText: passthrough,
- description: passthrough,
- scrollInfo: passthrough,
- noMatch: passthrough,
- };
- class RecordingVirtualTerminal extends VirtualTerminal {
- private writes: string[] = [];
- override write(data: string): void {
- this.writes.push(data);
- super.write(data);
- }
- takeWrites(): string {
- const output = this.writes.join('');
- this.writes = [];
- return output;
- }
- }
- class MutableLines implements Component {
- lines: string[] = [];
- render(): string[] {
- return [...this.lines];
- }
- invalidate(): void {}
- }
- describe('MCode Pi Engine local deltas', () => {
- it('fits Text padding within narrow terminal widths', () => {
- const text = new Text('content', 2, 0);
- for (const width of [1, 2, 3, 4]) {
- expect(text.render(width).every((line) => visibleWidth(line) <= width)).toBe(true);
- }
- });
- it('rebuilds when a transient collapse changes rows already in scrollback', async () => {
- const terminal = new RecordingVirtualTerminal(40, 5);
- const tui = new TuiMainScreen(terminal);
- const component = new MutableLines();
- component.lines = [
- 'header',
- 'activity-1',
- 'activity-2',
- 'activity-3',
- 'activity-4',
- 'status',
- 'composer',
- ];
- tui.addChild(component);
- tui.renderNow();
- await terminal.flush();
- terminal.takeWrites();
- component.lines = ['header', 'status', 'composer'];
- tui.renderNow();
- await terminal.flush();
- const writes = terminal.takeWrites();
- expect(writes).toContain('\x1b[2J\x1b[H');
- expect(writes).toContain('\x1b[3J');
- expect(terminal.getScrollBuffer()).not.toContain('activity-1');
- expect(terminal.getViewport()).toEqual(['header', 'status', 'composer', '', '']);
- expect(terminal.getScrollBuffer().filter((line) => line === 'header')).toHaveLength(1);
- expect(tui.fullRedraws).toBe(2);
- });
- it('keeps every composer row visible after a collapse crosses the viewport origin', async () => {
- const terminal = new RecordingVirtualTerminal(40, 10);
- const tui = new TuiMainScreen(terminal);
- const component = new MutableLines();
- const chat = Array.from({ length: 15 }, (_, index) => `Chat ${index}`);
- const composer = ['Editor 0', 'Editor 1', `Editor 2${CURSOR_MARKER}`];
- component.lines = [...chat, ...composer];
- tui.addChild(component);
- tui.renderNow();
- await terminal.flush();
- component.lines = [
- ...chat,
- ...Array.from({ length: 8 }, (_, index) => `Selector ${index}`),
- ];
- tui.renderNow();
- await terminal.flush();
- component.lines = [...chat, ...composer];
- tui.renderNow();
- await terminal.flush();
- terminal.takeWrites();
- component.lines = [...chat.slice(0, 12), ...composer];
- tui.renderNow();
- await terminal.flush();
- expect(terminal.takeWrites()).toContain('\x1b[3J');
- expect(terminal.getViewport()).toEqual([
- ...chat.slice(5, 12),
- 'Editor 0',
- 'Editor 1',
- 'Editor 2',
- ]);
- expect(terminal.getScrollBuffer()).toEqual([
- ...chat.slice(0, 12),
- 'Editor 0',
- 'Editor 1',
- 'Editor 2',
- ]);
- expect(terminal.getCursorPosition()).toEqual({ x: 8, y: 9 });
- });
- it('does not replay scrolled answer rows after a narrow terminal frame shrinks', async () => {
- const terminal = new RecordingVirtualTerminal(67, 44);
- const tui = new TuiMainScreen(terminal);
- const component = new MutableLines();
- const answer = Array.from({ length: 80 }, (_, index) => `Answer line ${index}`);
- component.lines = [...answer, 'activity', `composer${CURSOR_MARKER}`, 'status'];
- tui.addChild(component);
- tui.renderNow();
- await terminal.flush();
- // Settle the activity row, then repaint a historical line (for example Markdown styling).
- terminal.takeWrites();
- component.lines = [...answer, `composer${CURSOR_MARKER}`, 'status'];
- component.lines[0] = '\x1b[1mAnswer line 0\x1b[0m';
- tui.renderNow();
- await terminal.flush();
- expect(terminal.takeWrites()).not.toContain('\x1b[3J');
- component.lines[0] = '\x1b[3mAnswer line 0\x1b[0m';
- tui.renderNow();
- await terminal.flush();
- expect(terminal.takeWrites()).not.toContain('\x1b[3J');
- expect(terminal.getScrollBuffer()).toEqual([...answer, 'composer', 'status', '']);
- expect(terminal.getCursorPosition()).toEqual({ x: 8, y: 41 });
- // A later burst must continue from the same logical/physical boundary.
- component.lines.splice(80, 0, ...Array.from({ length: 50 }, (_, index) => `More ${index}`));
- tui.renderNow();
- await terminal.flush();
- expect(terminal.getScrollBuffer()).toEqual([
- ...answer,
- ...Array.from({ length: 50 }, (_, index) => `More ${index}`),
- 'composer',
- 'status',
- ]);
- });
- it('keeps every appended row when a historical row changes in the same frame', async () => {
- const terminal = new RecordingVirtualTerminal(67, 44);
- const tui = new TuiMainScreen(terminal);
- const component = new MutableLines();
- const answer = Array.from({ length: 80 }, (_, index) => `Answer line ${index}`);
- component.lines = [...answer, 'composer'];
- tui.addChild(component);
- tui.renderNow();
- await terminal.flush();
- terminal.takeWrites();
- const more = Array.from({ length: 50 }, (_, index) => `More ${index}`);
- component.lines = [...answer, ...more, 'composer'];
- component.lines[0] = '\x1b[1mAnswer line 0\x1b[0m';
- tui.renderNow();
- await terminal.flush();
- expect(terminal.takeWrites()).not.toContain('\x1b[3J');
- expect(terminal.getScrollBuffer()).toEqual([...answer, ...more, 'composer']);
- });
- it('rebuilds the document when shrinking leaves no rows in the previous viewport', async () => {
- const terminal = new RecordingVirtualTerminal(67, 44);
- const tui = new TuiMainScreen(terminal);
- const component = new MutableLines();
- component.lines = Array.from({ length: 100 }, (_, index) => `Old line ${index}`);
- tui.addChild(component);
- tui.renderNow();
- await terminal.flush();
- component.lines = ['answer', `composer${CURSOR_MARKER}`];
- tui.renderNow();
- await terminal.flush();
- expect(terminal.getScrollBuffer().filter(Boolean)).toEqual(['answer', 'composer']);
- expect(terminal.getCursorPosition()).toEqual({ x: 8, y: 1 });
- });
- it('still previews the resized tail and restores ordered scrollback after resize settles', async () => {
- const terminal = new RecordingVirtualTerminal(67, 44);
- const tui = new TuiMainScreen(terminal);
- const component = new MutableLines();
- component.lines = [
- ...Array.from({ length: 80 }, (_, index) => `Answer line ${index}`),
- `composer${CURSOR_MARKER}`,
- ];
- tui.addChild(component);
- try {
- tui.start();
- tui.renderNow();
- await terminal.flush();
- terminal.takeWrites();
- terminal.resize(60, 30);
- tui.renderNow();
- await terminal.flush();
- expect(terminal.takeWrites()).not.toContain('\x1b[3J');
- expect(terminal.getViewport()).toEqual([
- ...Array.from({ length: 29 }, (_, index) => `Answer line ${index + 51}`),
- 'composer',
- ]);
- await new Promise<void>((resolve) => setTimeout(resolve, 200));
- tui.renderNow();
- await terminal.flush();
- expect(terminal.takeWrites()).toContain('\x1b[3J');
- expect(terminal.getScrollBuffer()).toEqual([
- ...Array.from({ length: 80 }, (_, index) => `Answer line ${index}`),
- 'composer',
- ]);
- expect(terminal.getCursorPosition()).toEqual({ x: 8, y: 29 });
- } finally {
- tui.stop();
- }
- });
- it('renders an urgent product interaction without resetting Main diff state', async () => {
- const terminal = new RecordingVirtualTerminal(40, 5);
- const tui = new TuiMainScreen(terminal);
- const component = new MutableLines();
- component.lines = ['stable', 'tail'];
- tui.addChild(component);
- tui.renderNow();
- await terminal.flush();
- terminal.takeWrites();
- component.lines.push('interaction');
- tui.requestImmediateRender();
- await new Promise<void>((resolve) => setImmediate(resolve));
- await terminal.flush();
- const writes = terminal.takeWrites();
- expect(writes).toContain('interaction');
- expect(writes).not.toContain('\x1b[2J');
- expect(writes).not.toContain('\x1b[3J');
- });
- it('keeps OSC 133 zone sentinels out of regular-mode terminal writes', async () => {
- const terminal = new RecordingVirtualTerminal(40, 5);
- const tui = new TuiMainScreen(terminal);
- const component = new MutableLines();
- const zoneStart = '\x1b]133;A\x07';
- const zoneClose = '\x1b]133;B\x07\x1b]133;C\x07';
- component.lines = [`${zoneStart}user prompt`, `${zoneClose}assistant reply`, 'composer'];
- tui.addChild(component);
- tui.renderNow();
- await terminal.flush();
- const initialWrites = terminal.takeWrites();
- expect(initialWrites).toContain('user prompt');
- expect(initialWrites).toContain('assistant reply');
- expect(initialWrites).not.toContain('\x1b]133;');
- component.lines = [`${zoneStart}user prompt`, `${zoneClose}assistant reply updated`, 'composer'];
- tui.renderNow();
- await terminal.flush();
- const diffWrites = terminal.takeWrites();
- expect(diffWrites).toContain('assistant reply updated');
- expect(diffWrites).not.toContain('\x1b]133;');
- });
- it('limits Input extensions to prompt, mask, and paste transformation', () => {
- const input = new Input({
- prompt: 'Search: ',
- mask: '•',
- transformPaste: (value) => value.replace(/\s+/gu, ' '),
- });
- input.handleInput('\u001B[200~secret\nvalue\u001B[201~');
- input.focused = true;
- const rendered = input.render(30)[0] ?? '';
- expect(input.getValue()).toBe('secret value');
- expect(rendered).toContain('Search: ');
- expect(rendered).toContain('•');
- expect(rendered).not.toContain('secret');
- expect(rendered).toContain(CURSOR_MARKER);
- });
- it('retains grouped SelectList headings while using Pi filtering and single-line descriptions', () => {
- const list = new SelectList(
- [
- {
- value: 'docs',
- label: 'Documents',
- description: 'Create, edit, search and share documents.',
- groupLabel: 'Files',
- },
- {
- value: 'code',
- label: 'Code',
- description: 'Inspect and modify source code.',
- groupLabel: 'Files',
- },
- ],
- 2,
- selectListTheme,
- );
- list.setFilter('docs');
- const rendered = list.render(48);
- expect(rendered.join('\n')).toContain('Files');
- expect(rendered.join('\n')).toContain('Documents');
- expect(rendered.join('\n')).not.toContain('Code');
- expect(rendered).toHaveLength(2);
- expect(rendered.every((line) => visibleWidth(line) <= 48)).toBe(true);
- });
- it('exposes only the generic Editor hooks required by a product Draft adapter', () => {
- const tui = {
- terminal: { rows: 24 },
- requestRender: () => undefined,
- } as TUI;
- const editor = new Editor(tui, { borderColor: passthrough, selectList: selectListTheme }, {
- transformPaste: (value) => value.replaceAll('\r', ''),
- });
- let extensionState = 'empty';
- editor.captureUndoExtensionState = () => extensionState;
- editor.restoreUndoExtensionState = (state) => {
- extensionState = String(state);
- };
- editor.handleInput('a');
- extensionState = 'typed';
- editor.handleInput('\x1f');
- expect(editor.getText()).toBe('');
- expect(extensionState).toBe('empty');
- editor.onPaste = (value) => value.endsWith('.png');
- editor.handleInput('\u001B[200~/tmp/image.png\r\u001B[201~');
- expect(editor.getText()).toBe('');
- const pasted = 'x'.repeat(1_001);
- editor.onPaste = undefined;
- editor.handleInput(`\u001B[200~${pasted}\u001B[201~`);
- const snapshot = editor.captureState();
- editor.setText('replacement');
- expect(editor.restoreState(snapshot)).toBe(true);
- expect(editor.getExpandedText()).toBe(pasted);
- let submitted = '';
- editor.onSubmit = (value) => {
- submitted = value;
- };
- editor.submit();
- expect(submitted).toBe(pasted);
- });
- });
|