Files
Netcatty/components/terminal/runtime/terminalKeystrokeBroadcastGate.test.ts
T
陈大猫 dcf558e685 fix(terminal): clear sticky broadcast latch on vim ex (#4107) (#4109)
Fixes #4107.

Merge gate: GitHub Codex clean on tip b7efbe347; local CLEAN residual (unknown colon-prefixed alt-screen challenges) left as documented thrash; CI green (lint-and-test / windows-fido / windows-broadcast-ssh); Electron smoke PASS NC_OUT=/workspace/netcatty-smoke-shots/pr-4109-b7efbe347-20261104-122316 (both panes exited vim via toolbar broadcast :q!).
2026-11-03 20:38:28 -08:00

92 lines
4.1 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import { runInNewContext } from "node:vm";
import { shouldBroadcastDuringSensitivePrompt } from "../../../domain/terminalBroadcast.ts";
import { shouldBroadcastTerminalUserInput } from "./terminalUserPaste.ts";
test("toolbar broadcast still fans terminal keystrokes out to peers", () => {
const term = {};
assert.equal(
shouldBroadcastTerminalUserInput(term, "ls", {
isBroadcastEnabled: true,
hasBroadcastInputHandler: true,
}),
true,
);
assert.equal(
shouldBroadcastTerminalUserInput(term, "ls", {
isBroadcastEnabled: false,
hasBroadcastInputHandler: true,
}),
false,
);
});
test("keystroke fan-out stays on the toolbar gate and ignores the compose-bar target", () => {
const runtime = readFileSync(new URL("./createXTermRuntime.ts", import.meta.url), "utf8");
assert.match(runtime, /isBroadcastEnabled: ctx\.isBroadcastEnabledRef\.current/);
assert.match(
runtime,
/onBroadcastInput\?\.\(broadcastData, ctx\.sessionId, sensitive \? \{ sourceSensitive: true \} : undefined\)/,
);
assert.doesNotMatch(runtime, /composeSendToAll|composePeerFanOut/);
const layer = readFileSync(new URL("../../TerminalLayer.tsx", import.meta.url), "utf8");
assert.match(layer, /includeGlobalPeersWithoutToolbar: options\?\.composePeerFanOut === true/);
const keystrokeCall = layer.slice(
layer.indexOf("const handleBroadcastInput"),
layer.indexOf("const handleCommandSubmitted"),
);
assert.doesNotMatch(keystrokeCall, /isComposeSendToAll/);
});
const runtimeSource = readFileSync(new URL("./createXTermRuntime.ts", import.meta.url), "utf8");
const gateStart = runtimeSource.indexOf(" const canBroadcastInput =");
const gateEnd = runtimeSource.indexOf(" const willBroadcastInput =", gateStart);
assert.ok(gateStart > 0 && gateEnd > gateStart);
const gateCode = runtimeSource.slice(gateStart, gateEnd) + "\ncanBroadcastInput;";
const kittyStart = runtimeSource.indexOf(" isSensitiveInput: () => !shouldBroadcastDuringSensitivePrompt(");
const kittyEnd = runtimeSource.indexOf(" // Dispatching from an active prompt", kittyStart);
assert.ok(kittyStart > 0 && kittyEnd > kittyStart);
const kittyCode = "({" + runtimeSource.slice(kittyStart, kittyEnd) + "}).isSensitiveInput()";
for (const alternateScreen of [false, true]) {
for (const source of ['heuristic', 'explicit'] as const) {
test(`raw and Kitty broadcast respect ${source} sensitivity on ${alternateScreen ? 'alternate' : 'primary'} screen`, () => {
const context = {
ctx: {
passwordPromptActiveRef: { current: true, source },
broadcastPasswordBypassRef: { current: false },
isBroadcastEnabledRef: { current: true },
},
term: { buffer: { active: { type: alternateScreen ? 'alternate' : 'normal' } } },
sensitive: true, carriedSensitive: false,
options: undefined, inputSource: 'terminal', handlingKittyBroadcast: false,
suppressTerminalBroadcast: false, id: 'session', broadcastDataBeforeSudo: 'password',
onBroadcastInput: () => {},
shouldBroadcastDuringSensitivePrompt, shouldBroadcastTerminalUserInput,
};
assert.equal(runInNewContext(gateCode, context), false);
assert.equal(runInNewContext(kittyCode, context), true);
const pendingParseContext = {
...context,
term: { buffer: { active: { type: 'normal' } } },
ctx: { ...context.ctx, isPromptSecurityAlternateScreenActive: () => true },
};
assert.equal(runInNewContext(gateCode, pendingParseContext), false);
assert.equal(runInNewContext(kittyCode, pendingParseContext), true);
const bypassContext = {
...context,
ctx: { ...context.ctx, broadcastPasswordBypassRef: { current: true } },
};
assert.equal(runInNewContext(gateCode, bypassContext), true);
assert.equal(runInNewContext(kittyCode, bypassContext), false);
assert.equal(runInNewContext(gateCode, { ...context, carriedSensitive: true }), false,
'a carried sensitive paste stays closed even on the alternate screen');
});
}
}