|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { PassThrough, Writable } from "node:stream"; |
| 3 | +import { test, type TestContext } from "node:test"; |
| 4 | +import { setTimeout } from "node:timers/promises"; |
| 5 | +import React, { useState } from "react"; |
| 6 | +import { render, Text } from "ink"; |
| 7 | +import { EMPTY_BUFFER, expandPasteMarkers, insertText } from "../ui/core/prompt-buffer"; |
| 8 | +import { usePasteHandling } from "../ui/hooks/usePasteHandling"; |
| 9 | +import { useTerminalInput, type InputKey } from "../ui/hooks/useTerminalInput"; |
| 10 | + |
| 11 | +const PASTE_START = "\u001B[200~"; |
| 12 | +const PASTE_END = "\u001B[201~"; |
| 13 | + |
| 14 | +async function harness(t: TestContext) { |
| 15 | + const events: { input: string; key: InputKey }[] = []; |
| 16 | + const submissions: string[] = []; |
| 17 | + let text = ""; |
| 18 | + |
| 19 | + function Probe({ active = true }: { active?: boolean }) { |
| 20 | + const [buffer, setBuffer] = useState(EMPTY_BUFFER); |
| 21 | + const { handlePaste, pastesRef } = usePasteHandling(buffer, setBuffer, () => {}); |
| 22 | + text = buffer.text; |
| 23 | + useTerminalInput( |
| 24 | + (input, key) => { |
| 25 | + events.push({ input, key }); |
| 26 | + if (key.paste) handlePaste(input); |
| 27 | + else if (key.return) submissions.push(expandPasteMarkers(buffer.text, pastesRef.current)); |
| 28 | + else if (input && !key.ctrl && !key.meta) setBuffer((state) => insertText(state, input)); |
| 29 | + }, |
| 30 | + { isActive: active } |
| 31 | + ); |
| 32 | + return React.createElement(Text, null, buffer.text || "ready"); |
| 33 | + } |
| 34 | + |
| 35 | + const stdin = Object.assign(new PassThrough(), { isTTY: true, setRawMode() {}, ref() {}, unref() {} }); |
| 36 | + const stdout = Object.assign( |
| 37 | + new Writable({ |
| 38 | + write(_chunk, _encoding, callback) { |
| 39 | + callback(); |
| 40 | + }, |
| 41 | + }), |
| 42 | + { isTTY: true, columns: 80, rows: 24 } |
| 43 | + ); |
| 44 | + const app = render(React.createElement(Probe), { |
| 45 | + stdin: stdin as unknown as NodeJS.ReadStream, |
| 46 | + stdout: stdout as unknown as NodeJS.WriteStream, |
| 47 | + stderr: stdout as unknown as NodeJS.WriteStream, |
| 48 | + debug: true, |
| 49 | + patchConsole: false, |
| 50 | + exitOnCtrlC: false, |
| 51 | + }); |
| 52 | + t.after(() => { |
| 53 | + app.unmount(); |
| 54 | + app.cleanup(); |
| 55 | + }); |
| 56 | + async function flush() { |
| 57 | + await setTimeout(0); |
| 58 | + await app.waitUntilRenderFlush(); |
| 59 | + } |
| 60 | + await flush(); |
| 61 | + return { |
| 62 | + events, |
| 63 | + submissions, |
| 64 | + text: () => text, |
| 65 | + send: (chunk: string | Buffer) => stdin.emit("data", chunk), |
| 66 | + flush, |
| 67 | + async setActive(active: boolean) { |
| 68 | + app.rerender(React.createElement(Probe, { active })); |
| 69 | + await flush(); |
| 70 | + }, |
| 71 | + unmount: () => app.unmount(), |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +test("paste markers can split at every start/end boundary", async (t) => { |
| 76 | + const h = await harness(t); |
| 77 | + for (let start = 1; start < PASTE_START.length; start++) { |
| 78 | + for (let end = 1; end < PASTE_END.length; end++) { |
| 79 | + const payload = `payload-${start}-${end}\n你好`; |
| 80 | + h.events.length = 0; |
| 81 | + h.send(PASTE_START.slice(0, start)); |
| 82 | + h.send(PASTE_START.slice(start) + payload + PASTE_END.slice(0, end)); |
| 83 | + h.send(PASTE_END.slice(end)); |
| 84 | + h.send("x"); |
| 85 | + h.send("\r"); |
| 86 | + assert.equal(h.events.length, 3, `start=${start}, end=${end}`); |
| 87 | + assert.equal(h.events[0]?.input, payload); |
| 88 | + assert.equal(h.events[0]?.key.paste, true); |
| 89 | + assert.equal(h.events[1]?.input, "x"); |
| 90 | + assert.equal(h.events[2]?.key.return, true); |
| 91 | + } |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +test("a 1014-byte payload split at 1024 bytes collapses and remains editable and submittable", async (t) => { |
| 96 | + const h = await harness(t); |
| 97 | + const payload = JSON.stringify({ ciphertext: "a".repeat(997) }); |
| 98 | + assert.equal(Buffer.byteLength(payload), 1014); |
| 99 | + const packet = Buffer.from(PASTE_START + payload + PASTE_END); |
| 100 | + h.send(packet.subarray(0, 1024)); |
| 101 | + assert.equal(h.events.length, 0); |
| 102 | + h.send(packet.subarray(1024)); |
| 103 | + await h.flush(); |
| 104 | + assert.equal(h.text(), "[paste #1 1014 chars]"); |
| 105 | + h.send("x"); |
| 106 | + await h.flush(); |
| 107 | + h.send("\r"); |
| 108 | + assert.deepEqual(h.submissions, [payload + "x"]); |
| 109 | +}); |
| 110 | + |
| 111 | +test("one-character chunks preserve pasted control characters and marker-like content", async (t) => { |
| 112 | + const h = await harness(t); |
| 113 | + const payload = `abc\r\n\t\u007F\u0003${PASTE_START}literal\u001B[20x201~`; |
| 114 | + for (const char of PASTE_START + payload + PASTE_END) h.send(char); |
| 115 | + assert.equal(h.events.length, 1); |
| 116 | + assert.equal(h.events[0]?.input, payload); |
| 117 | + assert.equal(h.events[0]?.key.paste, true); |
| 118 | +}); |
| 119 | + |
| 120 | +test("coalesced input preserves text before a paste, consecutive pastes, and trailing Enter", async (t) => { |
| 121 | + const h = await harness(t); |
| 122 | + h.send(`before${PASTE_START}first${PASTE_END}${PASTE_START}${PASTE_END}${PASTE_START}second${PASTE_END}\r`); |
| 123 | + assert.deepEqual( |
| 124 | + h.events.map(({ input }) => input), |
| 125 | + ["before", "first", "second", "\r"] |
| 126 | + ); |
| 127 | + assert.equal(h.events[1]?.key.paste, true); |
| 128 | + assert.equal(h.events[2]?.key.paste, true); |
| 129 | + assert.equal(h.events[3]?.key.return, true); |
| 130 | +}); |
| 131 | + |
| 132 | +test("a delayed paste end stays buffered while standalone Esc remains responsive", async (t) => { |
| 133 | + const h = await harness(t); |
| 134 | + h.send(PASTE_START + "abc" + PASTE_END.slice(0, 4)); |
| 135 | + await setTimeout(50); |
| 136 | + assert.equal(h.events.length, 0); |
| 137 | + h.send(PASTE_END.slice(4)); |
| 138 | + assert.equal(h.events[0]?.input, "abc"); |
| 139 | + h.send("\u001B"); |
| 140 | + await setTimeout(50); |
| 141 | + assert.equal(h.events.length, 2); |
| 142 | + assert.equal(h.events[1]?.key.escape, true); |
| 143 | + h.send("\u001B[A"); |
| 144 | + h.send("\u001Bb"); |
| 145 | + assert.equal(h.events[2]?.key.upArrow, true); |
| 146 | + assert.equal(h.events[3]?.key.leftArrow, true); |
| 147 | + assert.equal(h.events[3]?.key.meta, true); |
| 148 | +}); |
| 149 | + |
| 150 | +test("deactivating input discards an unfinished paste and cancels pending Esc", async (t) => { |
| 151 | + const h = await harness(t); |
| 152 | + h.send(PASTE_START + "unfinished"); |
| 153 | + await h.setActive(false); |
| 154 | + await h.setActive(true); |
| 155 | + h.send("x"); |
| 156 | + assert.equal(h.events[0]?.input, "x"); |
| 157 | + h.events.length = 0; |
| 158 | + h.send("\u001B"); |
| 159 | + await h.setActive(false); |
| 160 | + await setTimeout(50); |
| 161 | + assert.equal(h.events.length, 0); |
| 162 | +}); |
| 163 | + |
| 164 | +test("unmounting input cancels pending Esc", async (t) => { |
| 165 | + const h = await harness(t); |
| 166 | + h.send("\u001B"); |
| 167 | + h.unmount(); |
| 168 | + await setTimeout(50); |
| 169 | + assert.equal(h.events.length, 0); |
| 170 | +}); |
0 commit comments