|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import childProcess from "node:child_process"; |
| 3 | +import { EventEmitter } from "node:events"; |
| 4 | +import fs from "node:fs"; |
| 5 | +import os from "node:os"; |
| 6 | +import path from "node:path"; |
| 7 | +import { syncBuiltinESMExports } from "node:module"; |
| 8 | +import { PassThrough, Writable } from "node:stream"; |
| 9 | +import { test, type TestContext } from "node:test"; |
| 10 | +import { setTimeout } from "node:timers/promises"; |
| 11 | +import React from "react"; |
| 12 | +import { render } from "ink"; |
| 13 | +import { PromptInput, type PromptDraft, type PromptSubmission } from "../ui/views/PromptInput"; |
| 14 | +import { parseTerminalInput } from "../ui/hooks"; |
| 15 | +import { stripTerminalControls } from "./terminal-output"; |
| 16 | + |
| 17 | +async function harness(t: TestContext, busy = false) { |
| 18 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), "deepcode-copy-input-")); |
| 19 | + const frames: string[] = []; |
| 20 | + const output = Object.assign( |
| 21 | + new Writable({ |
| 22 | + write(chunk, _encoding, done) { |
| 23 | + const text = stripTerminalControls(chunk.toString()); |
| 24 | + if (text.trim()) frames.push(text); |
| 25 | + done(); |
| 26 | + }, |
| 27 | + }), |
| 28 | + { columns: 100, rows: 30, isTTY: true } |
| 29 | + ); |
| 30 | + const input = Object.assign(new PassThrough(), { isTTY: true, setRawMode() {}, ref() {}, unref() {} }); |
| 31 | + const copies: { text: string; finish: (code: number) => void }[] = []; |
| 32 | + let holdCopy = false; |
| 33 | + t.mock.method(os, "homedir", () => root); |
| 34 | + t.mock.method(childProcess, "spawn", () => { |
| 35 | + const stdin = new PassThrough(); |
| 36 | + const child = Object.assign(new EventEmitter(), { stdin, kill: () => true }); |
| 37 | + const copy = { text: "", finish: (code: number) => void child.emit("exit", code) }; |
| 38 | + stdin.on("data", (chunk: Buffer) => (copy.text += chunk.toString("utf8"))); |
| 39 | + copies.push(copy); |
| 40 | + if (!holdCopy) queueMicrotask(() => copy.finish(0)); |
| 41 | + return child; |
| 42 | + }); |
| 43 | + syncBuiltinESMExports(); |
| 44 | + const submissions: PromptSubmission[] = []; |
| 45 | + let props = { |
| 46 | + projectRoot: root, |
| 47 | + screenWidth: 100, |
| 48 | + skills: [], |
| 49 | + modelConfig: { model: "test", thinkingEnabled: false, reasoningEffort: "high" as const }, |
| 50 | + promptHistory: ["previous prompt"], |
| 51 | + busy, |
| 52 | + disabled: false, |
| 53 | + promptDraft: undefined as PromptDraft | undefined, |
| 54 | + planMode: false, |
| 55 | + onSubmit: (submission: PromptSubmission) => void submissions.push(submission), |
| 56 | + onPromptDraftApplied() {}, |
| 57 | + onModelConfigChange: () => "", |
| 58 | + onPermissionSettingsChange: () => "", |
| 59 | + onPlanModeChange() {}, |
| 60 | + onInterrupt() {}, |
| 61 | + }; |
| 62 | + const app = render(React.createElement(PromptInput, props), { |
| 63 | + stdin: input as unknown as NodeJS.ReadStream, |
| 64 | + stdout: output as unknown as NodeJS.WriteStream, |
| 65 | + stderr: output as unknown as NodeJS.WriteStream, |
| 66 | + debug: true, |
| 67 | + patchConsole: false, |
| 68 | + exitOnCtrlC: false, |
| 69 | + }); |
| 70 | + t.after(() => { |
| 71 | + app.unmount(); |
| 72 | + app.cleanup(); |
| 73 | + t.mock.restoreAll(); |
| 74 | + syncBuiltinESMExports(); |
| 75 | + fs.rmSync(root, { recursive: true, force: true }); |
| 76 | + }); |
| 77 | + async function flush() { |
| 78 | + await setTimeout(80); |
| 79 | + await app.waitUntilRenderFlush(); |
| 80 | + } |
| 81 | + await flush(); |
| 82 | + return { |
| 83 | + copies, |
| 84 | + submissions, |
| 85 | + flush, |
| 86 | + latest: () => frames.at(-1) ?? "", |
| 87 | + holdCopy: () => (holdCopy = true), |
| 88 | + async send(text: string) { |
| 89 | + input.write(text); |
| 90 | + await flush(); |
| 91 | + }, |
| 92 | + async update(changes: Partial<typeof props>) { |
| 93 | + props = { ...props, ...changes }; |
| 94 | + app.rerender(React.createElement(PromptInput, props)); |
| 95 | + await flush(); |
| 96 | + }, |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +test("raw Ctrl+Y is parsed as the copy shortcut", () => { |
| 101 | + const { input, key } = parseTerminalInput("\x19"); |
| 102 | + assert.equal(input, "y"); |
| 103 | + assert.equal(key.ctrl, true); |
| 104 | + assert.equal(key.meta, false); |
| 105 | + assert.equal(key.paste, false); |
| 106 | +}); |
| 107 | + |
| 108 | +test("Ctrl+Y preserves input, cursor and undo while the model is busy", async (t) => { |
| 109 | + const h = await harness(t, true); |
| 110 | + await h.send("hello"); |
| 111 | + await h.send("\x1b[D"); |
| 112 | + await h.send("!"); |
| 113 | + await h.send("\x19"); |
| 114 | + assert.equal(h.copies[0].text, "hell!o"); |
| 115 | + assert.match(h.latest(), /Copied to clipboard/); |
| 116 | + assert.equal(h.submissions.length, 0); |
| 117 | + await h.send("?"); |
| 118 | + assert.match(h.latest(), /hell!\?o/); |
| 119 | + await h.send("\x1b[45;5u"); |
| 120 | + await h.send("\x1b[45;5u"); |
| 121 | + await h.update({ busy: false }); |
| 122 | + await h.send("\r"); |
| 123 | + assert.equal(h.submissions[0].text, "hello"); |
| 124 | +}); |
| 125 | + |
| 126 | +test("Ctrl+Y expands pasted text and excludes attached images", async (t) => { |
| 127 | + const h = await harness(t); |
| 128 | + const original = " 中文 😀\n".repeat(100); |
| 129 | + await h.update({ promptDraft: { nonce: 1, text: "prefix ", imageUrls: ["data:image/png;base64,test"] } }); |
| 130 | + await h.send(`\x1b[200~${original}\x1b[201~`); |
| 131 | + assert.match(h.latest(), /paste #1/); |
| 132 | + await h.send("\x19"); |
| 133 | + assert.equal(h.copies[0].text, "prefix " + original); |
| 134 | + assert.match(h.latest(), /paste #1/); |
| 135 | + assert.match(h.latest(), /1 image/); |
| 136 | + await h.send("\r"); |
| 137 | + assert.equal(h.submissions[0].text, h.copies[0].text); |
| 138 | + assert.deepEqual(h.submissions[0].imageUrls, ["data:image/png;base64,test"]); |
| 139 | +}); |
| 140 | + |
| 141 | +test("empty, search and modal inputs do not copy", async (t) => { |
| 142 | + const h = await harness(t); |
| 143 | + await h.send("\x19"); |
| 144 | + assert.equal(h.copies.length, 0, "empty input"); |
| 145 | + await h.send("draft"); |
| 146 | + await h.send("\x12"); |
| 147 | + await h.send("\x19"); |
| 148 | + assert.equal(h.copies.length, 0, "history search"); |
| 149 | + assert.match(h.latest(), /reverse-i-search/); |
| 150 | + await h.send("\x1b"); |
| 151 | + await h.update({ promptDraft: { nonce: 2, text: "/skills", imageUrls: [] } }); |
| 152 | + await h.send("\r"); |
| 153 | + await h.send("\x19"); |
| 154 | + assert.equal(h.copies.length, 0); |
| 155 | +}); |
| 156 | + |
| 157 | +test("disabled input does not copy", async (t) => { |
| 158 | + const h = await harness(t); |
| 159 | + await h.send("draft"); |
| 160 | + await h.update({ disabled: true }); |
| 161 | + await h.send("\x19"); |
| 162 | + assert.equal(h.copies.length, 0); |
| 163 | +}); |
| 164 | + |
| 165 | +test("Alt combinations and bracketed paste do not trigger copying", async (t) => { |
| 166 | + const h = await harness(t); |
| 167 | + await h.send("draft"); |
| 168 | + await h.send("\x1b[121;7u"); |
| 169 | + await h.send("\x1b[200~\x19\x1b[201~"); |
| 170 | + assert.equal(h.copies.length, 0); |
| 171 | +}); |
| 172 | + |
| 173 | +test("copy uses a snapshot and ignores repeated presses until completion", async (t) => { |
| 174 | + const h = await harness(t); |
| 175 | + h.holdCopy(); |
| 176 | + await h.send("before"); |
| 177 | + await h.send("\x19\x19"); |
| 178 | + await h.send(" after"); |
| 179 | + await h.send("\x19"); |
| 180 | + assert.equal(h.copies.length, 1); |
| 181 | + assert.equal(h.copies[0].text, "before"); |
| 182 | + h.copies[0].finish(0); |
| 183 | + await h.flush(); |
| 184 | + await h.send("\x19"); |
| 185 | + assert.equal(h.copies[1].text, "before after"); |
| 186 | + h.copies[1].finish(0); |
| 187 | + await h.flush(); |
| 188 | +}); |
| 189 | + |
| 190 | +test("copy failure shows a short status and preserves the draft", async (t) => { |
| 191 | + const h = await harness(t); |
| 192 | + h.holdCopy(); |
| 193 | + await h.send("keep me"); |
| 194 | + await h.send("\x19"); |
| 195 | + h.copies[0].finish(1); |
| 196 | + await h.flush(); |
| 197 | + if (process.platform === "linux") { |
| 198 | + h.copies[1].finish(1); |
| 199 | + await h.flush(); |
| 200 | + } |
| 201 | + assert.match(h.latest(), /Failed to copy/); |
| 202 | + await h.send("\r"); |
| 203 | + assert.equal(h.submissions[0].text, "keep me"); |
| 204 | +}); |
0 commit comments