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