tui-terminal-text-paste.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { PassThrough } from 'node:stream';
  2. import { afterEach, describe, expect, it, vi } from 'vitest';
  3. import { ProcessTerminal, StdinBuffer } from '../../src/tui/engine/public.js';
  4. import { Editor } from '../../src/tui/widgets/editor/editor.js';
  5. const startPaste = '\x1b[200~';
  6. const endPaste = '\x1b[201~';
  7. const identity = (text: string) => text;
  8. function createHarness() {
  9. const stdin = Object.assign(new PassThrough(), {
  10. isRaw: false,
  11. setRawMode: vi.fn(),
  12. });
  13. vi.spyOn(process, 'stdin', 'get').mockReturnValue(stdin as typeof process.stdin);
  14. vi.spyOn(process, 'kill').mockReturnValue(true);
  15. const output = vi.spyOn(process.stdout, 'write').mockReturnValue(true);
  16. vi.stubEnv('TERM_PROGRAM', '');
  17. const editor = new Editor(
  18. { terminal: { rows: 24 }, requestRender: vi.fn() },
  19. {
  20. borderColor: identity,
  21. selectList: {
  22. selectedPrefix: identity, selectedText: identity, description: identity,
  23. scrollInfo: identity, noMatch: identity,
  24. },
  25. },
  26. );
  27. const submitted = vi.fn();
  28. editor.onSubmit = submitted;
  29. const terminal = new ProcessTerminal();
  30. const input = vi.fn((data: string) => editor.handleInput(data));
  31. const start = () => terminal.start(input, () => {});
  32. start();
  33. return { editor, submitted, terminal, input, output, stdin, start,
  34. send: (data: string) => stdin.emit('data', data) };
  35. }
  36. afterEach(() => {
  37. vi.restoreAllMocks();
  38. vi.unstubAllEnvs();
  39. });
  40. describe('terminal text paste', () => {
  41. it.each(['\r', '\n', '\r\n'])('preserves unframed multiline chunks with %j line endings', (eol) => {
  42. const h = createHarness();
  43. try {
  44. h.send(`first${eol}第二行 😀${eol}last${eol}`);
  45. expect(h.submitted).not.toHaveBeenCalled();
  46. expect(h.editor.getExpandedText()).toBe('first\n第二行 😀\nlast\n');
  47. h.send('\r');
  48. expect(h.submitted.mock.calls.map(([text]) => text)).toEqual(['first\n第二行 😀\nlast']);
  49. } finally { h.terminal.stop(); }
  50. });
  51. it('retains immediate Enter and shortcut dispatch for ordinary key input', () => {
  52. const h = createHarness();
  53. try {
  54. for (const character of 'typed quickly') h.send(character);
  55. h.send('\r');
  56. h.send('batched text\r');
  57. expect(h.submitted.mock.calls.map(([text]) => text)).toEqual(['typed quickly', 'batched text']);
  58. h.send('\x1b[A\x03\x1b[13;2u');
  59. expect(h.input.mock.calls.slice(-3)).toEqual([['\x1b[A'], ['\x03'], ['\x1b[13;2u']]);
  60. } finally { h.terminal.stop(); }
  61. });
  62. it('uses the paste path for a large payload with tabs and Unicode', () => {
  63. const h = createHarness();
  64. const payload = Array.from({ length: 20 }, (_, i) => `line ${i}\t中文 😀`).join('\r\n');
  65. try {
  66. h.send(payload);
  67. expect(h.submitted).not.toHaveBeenCalled();
  68. expect(h.editor.getText()).toMatch(/^\[paste #1 /);
  69. expect(h.editor.getExpandedText()).toBe(payload.replace(/\r\n/g, '\n').replace(/\t/g, ' '));
  70. } finally { h.terminal.stop(); }
  71. });
  72. it('reassembles bracketed paste at every chunk boundary before accepting Enter', () => {
  73. const h = createHarness();
  74. const payload = 'first\r\n第二行\rlast\n';
  75. const wire = `${startPaste}${payload}${endPaste}`;
  76. try {
  77. for (let split = 1; split < wire.length; split++) {
  78. h.editor.setText('');
  79. h.submitted.mockClear();
  80. h.send(wire.slice(0, split));
  81. h.send(wire.slice(split));
  82. expect(h.submitted).not.toHaveBeenCalled();
  83. expect(h.editor.getExpandedText()).toBe('first\n第二行\nlast\n');
  84. h.send('\r');
  85. expect(h.submitted.mock.calls.map(([text]) => text)).toEqual(['first\n第二行\nlast']);
  86. }
  87. } finally { h.terminal.stop(); }
  88. });
  89. it('enables paste mode after restart and discards an unfinished previous paste', () => {
  90. const h = createHarness();
  91. try {
  92. expect(h.output).toHaveBeenCalledWith('\x1b[?2004h');
  93. h.send(`${startPaste}unfinished\r`);
  94. h.terminal.stop();
  95. expect(h.output).toHaveBeenCalledWith('\x1b[?2004l');
  96. expect(h.stdin.listenerCount('data')).toBe(0);
  97. h.output.mockClear();
  98. h.start();
  99. expect(h.output).toHaveBeenCalledWith('\x1b[?2004h');
  100. h.send(`${startPaste}new\r\ntext${endPaste}\r`);
  101. expect(h.submitted.mock.calls.map(([text]) => text)).toEqual(['new\ntext']);
  102. } finally { h.terminal.stop(); }
  103. });
  104. it('keeps control sequences and incomplete escape sequences out of the unframed fallback', () => {
  105. const buffer = new StdinBuffer();
  106. const paste = vi.fn();
  107. const data = vi.fn();
  108. buffer.on('paste', paste);
  109. buffer.on('data', data);
  110. try {
  111. buffer.process('first\r\x03last');
  112. expect(paste).not.toHaveBeenCalled();
  113. expect(data.mock.calls).toContainEqual(['\x03']);
  114. buffer.process('\x1b[');
  115. buffer.process('first\rlast');
  116. expect(paste).not.toHaveBeenCalled();
  117. } finally { buffer.destroy(); }
  118. });
  119. it('does not time out a bracketed payload or merge neighboring keyboard events into it', () => {
  120. vi.useFakeTimers();
  121. const buffer = new StdinBuffer();
  122. const events: string[][] = [];
  123. buffer.on('paste', (text) => events.push(['paste', text]));
  124. buffer.on('data', (text) => events.push(['data', text]));
  125. try {
  126. buffer.process(`a${startPaste}first\r`);
  127. vi.advanceTimersByTime(1_000);
  128. buffer.process(`\nsecond${endPaste}${startPaste}${endPaste}\r`);
  129. expect(events).toEqual([
  130. ['data', 'a'], ['paste', 'first\r\nsecond'], ['paste', ''], ['data', '\r'],
  131. ]);
  132. } finally {
  133. buffer.destroy();
  134. vi.useRealTimers();
  135. }
  136. });
  137. });