Files
Netcatty/components/terminal/runtime/serialLocalEcho.test.ts
T
cb06857480 feat(terminal): confirm before pasting multiple lines (#3398) (#3400)
* fix(#3398): automated AI fix

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix: address Codex review on PR #3400

* fix(terminal): protect popup and sanitized multiline pastes

* fix(terminal): preserve input state across paste confirmation

* fix(terminal): retain serial character-mode paste echo

* test(terminal): cover paced input write options

* fix(terminal): retain telnet echo for paced pastes

* test(terminal): scope serial buffer assertion to serial sessions

* fix(terminal): align runtime write options with bridge contract

* fix(terminal): preserve bookkeeping for all paced pastes

* fix(terminal): record confirmed paste lines in order

* fix: record paced paste commands after transport writes

* fix: omit paste receipts for filtered input

* fix: cancel superseded paste batches across input paths

* fix: classify each acknowledged paste line before recording

* fix: stop paced paste when editing buffered serial input

* fix: preserve recording pause boundaries and serial edit cancellation

* fix: treat paced terminal pastes as user input

* fix: preserve paste drafts until write and snippet broadcast origin

* fix: retain filtered paste drafts and invalidate urgent input ownership

* fix: broadcast paced user paste only on guarded line receipts

---------

Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-09-18 15:19:34 +08:00

131 lines
6.9 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { isPrintableInput } from "../../../domain/serialCharMetrics";
import { getSingleBracketedPasteLine } from "./terminalSudoAutofill";
import { formatSerialLocalEcho } from "./serialLocalEcho";
test("formatSerialLocalEcho echoes printable input and normalizes newlines", () => {
assert.equal(formatSerialLocalEcho("show version"), "show version");
assert.equal(formatSerialLocalEcho("\r"), "\r\n");
assert.equal(formatSerialLocalEcho("\n"), "\r\n");
assert.equal(formatSerialLocalEcho("\r\n"), "\r\n");
assert.equal(formatSerialLocalEcho("one\ntwo"), "one\r\ntwo");
});
test("formatSerialLocalEcho renders local editing control keys", () => {
assert.equal(formatSerialLocalEcho("\x7f"), "\b \b");
assert.equal(formatSerialLocalEcho("\b"), "\b \b");
assert.equal(formatSerialLocalEcho("\x03"), "^C");
});
test("formatSerialLocalEcho ignores single non-display control input", () => {
assert.equal(formatSerialLocalEcho("\x15"), "");
});
test("formatSerialLocalEcho returns empty for empty input", () => {
assert.equal(formatSerialLocalEcho(""), "");
});
test("formatSerialLocalEcho erases 1 cell by default for backspace", () => {
assert.equal(formatSerialLocalEcho("\x7f"), "\b \b");
assert.equal(formatSerialLocalEcho("\x7f", 1), "\b \b");
});
test("formatSerialLocalEcho erases 2 cells for wide-character backspace", () => {
// CJK ideograph = 2 cells; backspace must erase both.
assert.equal(formatSerialLocalEcho("\x7f", 2), "\b \b\b \b");
assert.equal(formatSerialLocalEcho("\b", 2), "\b \b\b \b");
});
test("serial byte deletion metadata is forwarded through the runtime input loop", async () => {
const { readFile } = await import("node:fs/promises");
const source = await readFile(new URL("./createXTermRuntime.ts", import.meta.url), "utf8");
assert.match(source, /writeToSession\(id, chunk, \{\s*sensitive,\s*serialEraseChar,[\s\S]*?\}\)/);
});
test("both Enter and submitted paste restore serial tail confidence", async () => {
const { readFile } = await import("node:fs/promises");
const source = await readFile(new URL("./createXTermRuntime.ts", import.meta.url), "utf8");
const pasteBranch = source.indexOf("const pastedCommand = logicalData");
const restoreTail = source.indexOf("if (handledSubmittedInput) lastInputWasPrintable = true;");
const writeInput = source.indexOf("prioritizeTerminalInput(", pasteBranch);
assert.ok(pasteBranch > 0 && restoreTail > pasteBranch && restoreTail < writeInput);
});
test("byte-oriented serial input prevents encoding changes while wire bytes are pending", async () => {
const { readFile } = await import("node:fs/promises");
const { runInNewContext } = await import("node:vm");
const source = await readFile(new URL("../../Terminal.tsx", import.meta.url), "utf8");
const handlerStart = source.indexOf("const handleSetTerminalEncoding = useCallback");
const start = source.indexOf("if (host.protocol === 'serial'", handlerStart);
const end = source.indexOf("setTerminalEncoding(encoding);", start);
assert.ok(handlerStart > 0 && start > handlerStart && end > start);
const guard = source.slice(start, end);
for (const [protocol, byteMode, lineMode, pending, blocked] of [
["serial", true, false, "abc你", true],
["serial", true, false, "", false],
["serial", false, false, "abc你", false],
["serial", true, true, "abc你", false],
["ssh", true, false, "abc你", false],
] as const) {
const notices: string[] = [];
const state = {host: {protocol}, serialConfig: {byteOrientedBackspace: byteMode, lineMode}, commandBufferRef: {current: pending}, toast: {info: (text: string) => notices.push(text)}, t: (key: string) => key};
const changed = runInNewContext(`(() => { ${guard} return true; })()`, state);
assert.equal(changed, blocked ? undefined : true);
assert.equal(notices.length, blocked ? 1 : 0);
}
});
test("urgent Ctrl+C restores serial tail confidence when it clears pending input", async () => {
const { readFile } = await import("node:fs/promises");
const source = await readFile(new URL("./createXTermRuntime.ts", import.meta.url), "utf8");
assert.match(source, /clearTerminalInputStateForInterrupt\(\{[^]*?\}\);\s*lastInputWasPrintable = true;/);
});
test("pending serial snippets share runtime editing bookkeeping", async () => {
const { readFile } = await import("node:fs/promises");
const { runInNewContext } = await import("node:vm");
const source = await readFile(new URL("../../Terminal.tsx", import.meta.url), "utf8");
const start = source.indexOf("if (host.protocol === 'serial' && noAutoRun");
const end = source.indexOf("scrollToBottomAfterProgrammaticInput(data);", start);
assert.ok(start > 0 && end > start);
for (const [protocol, noAutoRun, isMultiLine, lineMode, tracked] of [
["serial", true, false, false, true],
["serial", false, false, false, false],
["serial", true, true, false, true],
["serial", true, false, true, false],
["ssh", true, false, false, false],
] as const) {
const recorded: string[] = [];
const state = {host: {protocol}, noAutoRun, isMultiLine, serialConfig: {lineMode}, data: "你", xtermRuntimeRef: {current: {recordSerialSnippetInput: (text: string) => recorded.push(text)}}};
runInNewContext(source.slice(start, end), state);
assert.deepEqual(recorded, tracked ? ["你"] : []);
}
});
test("serial snippets restore an empty tail but preserve uncertainty on an edited line", async () => {
const { readFile } = await import("node:fs/promises");
const { runInNewContext } = await import("node:vm");
const source = await readFile(new URL("./createXTermRuntime.ts", import.meta.url), "utf8");
const start = source.indexOf("const recordSerialSnippetInput = (data: string) => {");
const end = source.indexOf("\n };", start);
assert.ok(start > 0 && end > start);
const body = source.slice(source.indexOf("{", start) + 1, end);
const helperStart = source.indexOf("const restoreSerialTailForEmptyInput = (data: string) => {");
const helper = source.slice(helperStart, start).replaceAll("data: string", "data");
for (const [pending, confident, localEcho, expected] of [
["", false, true, true],
["", false, false, true],
["abc", false, true, false],
["abc", true, true, true],
] as const) {
const echoed: string[] = [];
const state = {ctx: {commandBufferRef: {current: pending}, serialLocalEcho: localEcho}, lastInputWasPrintable: confident, data: "你", writeLocalTerminalData: (text: string) => echoed.push(text)};
runInNewContext(`(() => { ${helper} ${body} })()`, {...state, isPrintableInput, getSingleBracketedPasteLine, formatSerialLocalEcho, ctx: state.ctx, get lastInputWasPrintable() {return state.lastInputWasPrintable;}, set lastInputWasPrintable(value) {state.lastInputWasPrintable = value;}});
assert.equal(state.lastInputWasPrintable, expected);
assert.equal(state.ctx.commandBufferRef.current, pending + "你");
assert.deepEqual(echoed, localEcho ? ["你"] : []);
}
});