mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
770 lines
20 KiB
TypeScript
770 lines
20 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import type { Terminal as XTerm } from "@xterm/xterm";
|
|
|
|
import { createOutputFlowController } from "./outputFlowController.ts";
|
|
import {
|
|
filterTerminalInterruptDisplayOutput,
|
|
prioritizeTerminalInput,
|
|
releaseTerminalFlowOutputForTerm,
|
|
teardownTerminalOutputPipeline,
|
|
} from "./terminalOutputPipeline.ts";
|
|
import { FLOW_LOW_WATER_MARK } from "./terminalFlowConstants.ts";
|
|
import {
|
|
enqueueCoalescedTerminalWrite,
|
|
flushTerminalWriteCoalescer,
|
|
getTerminalWriteCoalescerPendingBytes,
|
|
resetTerminalWriteCoalescer,
|
|
} from "./terminalWriteCoalescer.ts";
|
|
import { enqueueTerminalWrite } from "./terminalWriteQueue.ts";
|
|
import { accumulateDeferredTerminalWriteAck } from "./terminalWriteAckDeferral.ts";
|
|
import { clearTerminalSessionFlowAck } from "./terminalFlowAckBuffer.ts";
|
|
|
|
const createFakeTerm = () => ({}) as XTerm;
|
|
|
|
test("teardownTerminalOutputPipeline resumes renderer pause and clears backlog", () => {
|
|
const term = createFakeTerm();
|
|
const events: string[] = [];
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 50,
|
|
lowWaterMark: 10,
|
|
onPause: () => events.push("pause"),
|
|
onResume: () => events.push("resume"),
|
|
});
|
|
const backend = {
|
|
setSessionFlowPaused: (_sessionId: string, paused: boolean) => {
|
|
events.push(paused ? "ipc-pause" : "ipc-resume");
|
|
},
|
|
ackSessionFlow: () => {},
|
|
};
|
|
|
|
flow.received(60);
|
|
enqueueTerminalWrite(term, 20, (done) => done());
|
|
teardownTerminalOutputPipeline(
|
|
{ terminalBackend: backend, sessionRef: { current: "sess-1" } } as never,
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
);
|
|
|
|
assert.deepEqual(events, ["pause", "resume", "ipc-resume"]);
|
|
});
|
|
|
|
test("releaseTerminalFlowOutputForTerm resumes renderer pause without a flow controller", () => {
|
|
const term = createFakeTerm();
|
|
const events: string[] = [];
|
|
const backend = {
|
|
setSessionFlowPaused: (_sessionId: string, paused: boolean) => {
|
|
events.push(paused ? "ipc-pause" : "ipc-resume");
|
|
},
|
|
ackSessionFlow: () => {},
|
|
};
|
|
|
|
releaseTerminalFlowOutputForTerm(term, backend, "sess-1", undefined);
|
|
|
|
assert.deepEqual(events, ["ipc-resume"]);
|
|
});
|
|
|
|
test("prioritizeTerminalInput flushes batched ack remainders after dropping bytes", () => {
|
|
const term = createFakeTerm();
|
|
const acked: number[] = [];
|
|
const deferred: Array<() => void> = [];
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
const backend = {
|
|
setSessionFlowPaused: () => {},
|
|
ackSessionFlow: (_sessionId: string, bytes: number) => {
|
|
acked.push(bytes);
|
|
},
|
|
};
|
|
|
|
flow.received(FLOW_LOW_WATER_MARK + 80);
|
|
enqueueTerminalWrite(term, 50, () => {});
|
|
enqueueTerminalWrite(term, 30, () => {});
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => deferred.push(callback),
|
|
);
|
|
|
|
assert.deepEqual(acked, []);
|
|
deferred[0]!();
|
|
assert.deepEqual(acked, [30]);
|
|
});
|
|
|
|
test("prioritizeTerminalInput flushes deferred xterm write ack bytes", () => {
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
const term = createFakeTerm();
|
|
const acked: number[] = [];
|
|
const deferred: Array<() => void> = [];
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
const backend = {
|
|
ackSessionFlow: (_sessionId: string, bytes: number) => {
|
|
acked.push(bytes);
|
|
},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
|
|
accumulateDeferredTerminalWriteAck(term, 42);
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => deferred.push(callback),
|
|
);
|
|
|
|
assert.deepEqual(acked, []);
|
|
deferred[0]!();
|
|
assert.deepEqual(acked, [42]);
|
|
assert.equal(flow.pendingBytes(), 0);
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
});
|
|
|
|
test("prioritizeTerminalInput drains backlog before user input is forwarded", () => {
|
|
const term = createFakeTerm();
|
|
const events: string[] = [];
|
|
const deferred: Array<() => void> = [];
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => events.push("pause"),
|
|
onResume: () => events.push("resume"),
|
|
});
|
|
const backend = {
|
|
setSessionFlowPaused: (_sessionId: string, paused: boolean) => {
|
|
events.push(paused ? "ipc-pause" : "ipc-resume");
|
|
},
|
|
ackSessionFlow: () => {},
|
|
};
|
|
|
|
flow.received(FLOW_LOW_WATER_MARK + 1024);
|
|
let release: (() => void) | null = null;
|
|
enqueueTerminalWrite(term, 30, (done) => {
|
|
release = done;
|
|
});
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => deferred.push(callback),
|
|
);
|
|
release?.();
|
|
|
|
assert.equal(events.includes("ipc-resume"), false);
|
|
events.push("input-forwarded");
|
|
deferred[0]!();
|
|
assert.ok(events.includes("ipc-resume"));
|
|
assert.deepEqual(events.slice(-2), ["input-forwarded", "ipc-resume"]);
|
|
});
|
|
|
|
test("prioritizeTerminalInput does not resume while collecting dropped bytes", () => {
|
|
const term = createFakeTerm();
|
|
const events: string[] = [];
|
|
const deferred: Array<() => void> = [];
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => events.push("pause"),
|
|
onResume: () => events.push("resume"),
|
|
});
|
|
const backend = {
|
|
setSessionFlowPaused: (_sessionId: string, paused: boolean) => {
|
|
events.push(paused ? "ipc-pause" : "ipc-resume");
|
|
},
|
|
ackSessionFlow: () => {},
|
|
};
|
|
|
|
flow.received(110);
|
|
enqueueTerminalWrite(term, 10, () => {});
|
|
enqueueTerminalWrite(term, 100, () => {});
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => deferred.push(callback),
|
|
);
|
|
|
|
assert.deepEqual(events, ["pause"]);
|
|
events.push("input-forwarded");
|
|
deferred[0]!();
|
|
|
|
assert.deepEqual(events, ["pause", "input-forwarded", "ipc-resume"]);
|
|
});
|
|
|
|
test("prioritizeTerminalInput defers source resume until after input is forwarded", () => {
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
const term = createFakeTerm();
|
|
const events: string[] = [];
|
|
const deferred: Array<() => void> = [];
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => events.push("pause"),
|
|
onResume: () => events.push("resume"),
|
|
});
|
|
const backend = {
|
|
setSessionFlowPaused: (_sessionId: string, paused: boolean) => {
|
|
events.push(paused ? "ipc-pause" : "ipc-resume");
|
|
},
|
|
ackSessionFlow: () => {},
|
|
};
|
|
|
|
flow.received(FLOW_LOW_WATER_MARK + 1024);
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => deferred.push(callback),
|
|
);
|
|
|
|
assert.equal(flow.isPaused(), false);
|
|
assert.deepEqual(events, ["pause"]);
|
|
assert.equal(deferred.length, 1);
|
|
|
|
events.push("input-forwarded");
|
|
deferred[0]!();
|
|
|
|
assert.deepEqual(events, ["pause", "input-forwarded", "ipc-resume"]);
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
});
|
|
|
|
test("ordinary input priority preserves pending line-edit echo when flushing deferred acks", () => {
|
|
clearTerminalSessionFlowAck("sess-edit");
|
|
const scheduler = globalThis as typeof globalThis & {
|
|
requestAnimationFrame?: (callback: FrameRequestCallback) => number;
|
|
cancelAnimationFrame?: (handle: number) => void;
|
|
};
|
|
const originalRequestAnimationFrame = scheduler.requestAnimationFrame;
|
|
const originalCancelAnimationFrame = scheduler.cancelAnimationFrame;
|
|
scheduler.requestAnimationFrame = () => 1;
|
|
scheduler.cancelAnimationFrame = () => {};
|
|
|
|
const term = createFakeTerm();
|
|
const echoedHistoryCommand = "systemctl dddd";
|
|
const written: string[] = [];
|
|
const acked: number[] = [];
|
|
const deferred: Array<() => void> = [];
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
const backend = {
|
|
ackSessionFlow: (_sessionId: string, bytes: number) => {
|
|
acked.push(bytes);
|
|
},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
|
|
try {
|
|
accumulateDeferredTerminalWriteAck(term, 2);
|
|
flow.received(echoedHistoryCommand.length);
|
|
enqueueCoalescedTerminalWrite(
|
|
term,
|
|
echoedHistoryCommand,
|
|
(data, ingressBytes) => {
|
|
written.push(data);
|
|
flow.written(ingressBytes);
|
|
},
|
|
echoedHistoryCommand.length,
|
|
);
|
|
assert.equal(getTerminalWriteCoalescerPendingBytes(term), echoedHistoryCommand.length);
|
|
|
|
const priority = prioritizeTerminalInput(
|
|
term,
|
|
"sess-edit",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => deferred.push(callback),
|
|
{ reason: "input" },
|
|
);
|
|
|
|
assert.equal(priority.ackAfterInputBytes, 2);
|
|
assert.equal(flow.pendingBytes(), echoedHistoryCommand.length);
|
|
assert.equal(getTerminalWriteCoalescerPendingBytes(term), echoedHistoryCommand.length);
|
|
|
|
deferred[0]!();
|
|
assert.deepEqual(acked, [2]);
|
|
|
|
flushTerminalWriteCoalescer(term);
|
|
assert.deepEqual(written, [echoedHistoryCommand]);
|
|
assert.equal(flow.pendingBytes(), 0);
|
|
} finally {
|
|
resetTerminalWriteCoalescer(term);
|
|
scheduler.requestAnimationFrame = originalRequestAnimationFrame;
|
|
scheduler.cancelAnimationFrame = originalCancelAnimationFrame;
|
|
clearTerminalSessionFlowAck("sess-edit");
|
|
}
|
|
});
|
|
|
|
test("interrupt priority drains queued display output for ssh-like interrupts", () => {
|
|
const term = createFakeTerm();
|
|
const events: string[] = [];
|
|
const backend = {
|
|
ackSessionFlow: (_sessionId: string, bytes: number) => {
|
|
events.push(`ack:${bytes}`);
|
|
},
|
|
setSessionFlowPaused: (_sessionId: string, paused: boolean) => {
|
|
events.push(paused ? "ipc-pause" : "ipc-resume");
|
|
},
|
|
};
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => events.push("pause"),
|
|
onResume: () => {},
|
|
});
|
|
flow.received(FLOW_LOW_WATER_MARK + 1024);
|
|
enqueueTerminalWrite(term, 20, () => {});
|
|
enqueueTerminalWrite(term, 30, () => {});
|
|
|
|
const priority = prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => {
|
|
events.push("input-forwarded");
|
|
callback();
|
|
},
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: true,
|
|
now: 4000,
|
|
quietMs: 100,
|
|
promptQuietMs: 80,
|
|
maxDrainMs: 1000,
|
|
},
|
|
);
|
|
|
|
assert.equal(priority.skippedReason, undefined);
|
|
assert.equal(priority.scheduledBackendResume, true);
|
|
assert.equal(priority.ackAfterInputBytes, 30);
|
|
assert.equal(priority.writeQueueDepth, 1);
|
|
assert.equal(flow.pendingBytes(), 0);
|
|
assert.deepEqual(events, ["pause", "input-forwarded", "ack:30", "ipc-resume"]);
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "old output", { now: 4001 }),
|
|
{ accepted: false, data: "", droppedBytes: 10, reason: "draining" },
|
|
);
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "more old^C\r\n$ ", { now: 4002 }),
|
|
{ accepted: true, data: "^C\r\n$ ", droppedBytes: 8, reason: "interrupt-echo" },
|
|
);
|
|
});
|
|
|
|
test("interrupt display drain preserves alternate-screen exit controls", () => {
|
|
const term = createFakeTerm();
|
|
const backend = {
|
|
ackSessionFlow: () => {},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
flow.received(FLOW_LOW_WATER_MARK + 1);
|
|
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => callback(),
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: true,
|
|
now: 6100,
|
|
quietMs: 500,
|
|
promptQuietMs: 80,
|
|
maxDrainMs: 1000,
|
|
},
|
|
);
|
|
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "stale frame\x1b[?1049l", { now: 6101 }),
|
|
{
|
|
accepted: true,
|
|
data: "\x1b[?1049l",
|
|
droppedBytes: "stale frame".length,
|
|
reason: "draining",
|
|
},
|
|
);
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "stale frame\x1b[?1049l\r\n$ ", { now: 6200 }),
|
|
{
|
|
accepted: true,
|
|
data: "\x1b[?1049l$ ",
|
|
droppedBytes: "stale frame\r\n".length,
|
|
reason: "prompt-gap",
|
|
},
|
|
);
|
|
});
|
|
|
|
test("interrupt display drain preserves split alternate-screen exit controls", () => {
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
const term = createFakeTerm();
|
|
const backend = {
|
|
ackSessionFlow: () => {},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
flow.received(FLOW_LOW_WATER_MARK + 1);
|
|
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => callback(),
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: true,
|
|
now: 7100,
|
|
quietMs: 500,
|
|
promptQuietMs: 80,
|
|
maxDrainMs: 1000,
|
|
},
|
|
);
|
|
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "stale frame\x1b[?104", { now: 7101 }),
|
|
{
|
|
accepted: false,
|
|
data: "",
|
|
droppedBytes: "stale frame".length,
|
|
reason: "draining",
|
|
},
|
|
);
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "9l^C\r\n$ ", { now: 7102 }),
|
|
{
|
|
accepted: true,
|
|
data: "\x1b[?1049l^C\r\n$ ",
|
|
droppedBytes: 0,
|
|
reason: "interrupt-echo",
|
|
},
|
|
);
|
|
});
|
|
|
|
test("interrupt display drain preserves restore controls on split caret echo", () => {
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
const term = createFakeTerm();
|
|
const backend = {
|
|
ackSessionFlow: () => {},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
flow.received(FLOW_LOW_WATER_MARK + 1);
|
|
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => callback(),
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: true,
|
|
now: 7150,
|
|
quietMs: 500,
|
|
promptQuietMs: 80,
|
|
maxDrainMs: 1000,
|
|
},
|
|
);
|
|
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "stale frame\x1b[?104", { now: 7151 }),
|
|
{
|
|
accepted: false,
|
|
data: "",
|
|
droppedBytes: "stale frame".length,
|
|
reason: "draining",
|
|
},
|
|
);
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "9l^", { now: 7152 }),
|
|
{
|
|
accepted: true,
|
|
data: "\x1b[?1049l",
|
|
droppedBytes: 1,
|
|
reason: "draining",
|
|
},
|
|
);
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "C\r\n$ ", { now: 7153 }),
|
|
{
|
|
accepted: true,
|
|
data: "^C\r\n$ ",
|
|
droppedBytes: 0,
|
|
acceptedBytes: 5,
|
|
reason: "interrupt-echo",
|
|
},
|
|
);
|
|
});
|
|
|
|
test("interrupt display drain does not preserve unsafe combined private modes", () => {
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
const term = createFakeTerm();
|
|
const backend = {
|
|
ackSessionFlow: () => {},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
flow.received(FLOW_LOW_WATER_MARK + 1);
|
|
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => callback(),
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: true,
|
|
now: 7200,
|
|
quietMs: 500,
|
|
promptQuietMs: 80,
|
|
maxDrainMs: 1000,
|
|
},
|
|
);
|
|
|
|
const unsafeSequence = "\x1b[?1049;25h";
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, `stale frame${unsafeSequence}^C\r\n$ `, {
|
|
now: 7201,
|
|
}),
|
|
{
|
|
accepted: true,
|
|
data: "^C\r\n$ ",
|
|
droppedBytes: "stale frame".length + unsafeSequence.length,
|
|
reason: "interrupt-echo",
|
|
},
|
|
);
|
|
});
|
|
|
|
test("interrupt display drain accepts prompt candidates with OSC title and spaces", () => {
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
const term = createFakeTerm();
|
|
const backend = {
|
|
ackSessionFlow: () => {},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
flow.received(FLOW_LOW_WATER_MARK + 1);
|
|
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => callback(),
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: true,
|
|
now: 7300,
|
|
quietMs: 500,
|
|
promptQuietMs: 80,
|
|
maxDrainMs: 1000,
|
|
},
|
|
);
|
|
|
|
assert.equal(
|
|
filterTerminalInterruptDisplayOutput(term, "old output", { now: 7301 }).accepted,
|
|
false,
|
|
);
|
|
const prompt = "\x1b]0;~/My Project\x07~/My Project$ ";
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, prompt, { now: 7400 }),
|
|
{
|
|
accepted: true,
|
|
data: prompt,
|
|
droppedBytes: 0,
|
|
reason: "prompt-gap",
|
|
},
|
|
);
|
|
});
|
|
|
|
test("interrupt display drain accepts split OSC prompt candidates", () => {
|
|
clearTerminalSessionFlowAck("sess-1");
|
|
const term = createFakeTerm();
|
|
const backend = {
|
|
ackSessionFlow: () => {},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
flow.received(FLOW_LOW_WATER_MARK + 1);
|
|
|
|
prioritizeTerminalInput(
|
|
term,
|
|
"sess-1",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => callback(),
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: true,
|
|
now: 7500,
|
|
quietMs: 500,
|
|
promptQuietMs: 80,
|
|
maxDrainMs: 1000,
|
|
},
|
|
);
|
|
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "old output\x1b]0;~/My ", { now: 7501 }),
|
|
{
|
|
accepted: false,
|
|
data: "",
|
|
droppedBytes: "old output".length,
|
|
reason: "draining",
|
|
},
|
|
);
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "Project\x07~/My Project$ ", { now: 7600 }),
|
|
{
|
|
accepted: true,
|
|
data: "\x1b]0;~/My Project\x07~/My Project$ ",
|
|
droppedBytes: 0,
|
|
reason: "prompt-gap",
|
|
},
|
|
);
|
|
});
|
|
|
|
test("interrupt priority skips stale display drain but still flushes deferred acks", () => {
|
|
clearTerminalSessionFlowAck("sess-deferred");
|
|
const term = createFakeTerm();
|
|
const acked: number[] = [];
|
|
const deferred: Array<() => void> = [];
|
|
const backend = {
|
|
ackSessionFlow: (_sessionId: string, bytes: number) => {
|
|
acked.push(bytes);
|
|
},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
|
|
accumulateDeferredTerminalWriteAck(term, 42);
|
|
const priority = prioritizeTerminalInput(
|
|
term,
|
|
"sess-deferred",
|
|
undefined,
|
|
backend,
|
|
(callback: () => void) => deferred.push(callback),
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: false,
|
|
now: 5000,
|
|
quietMs: 500,
|
|
promptQuietMs: 80,
|
|
maxDrainMs: 100,
|
|
},
|
|
);
|
|
|
|
assert.equal(priority.skippedReason, undefined);
|
|
assert.equal(priority.deferredAckBytes, 42);
|
|
assert.equal(priority.scheduledBackendResume, true);
|
|
assert.equal(priority.ackAfterInputBytes, 42);
|
|
assert.equal(deferred.length, 1);
|
|
deferred[0]!();
|
|
assert.deepEqual(acked, [42]);
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(term, "KeyboardInterrupt\r\n$ ", { now: 5001 }),
|
|
{
|
|
accepted: true,
|
|
data: "KeyboardInterrupt\r\n$ ",
|
|
droppedBytes: 0,
|
|
reason: "inactive",
|
|
},
|
|
);
|
|
clearTerminalSessionFlowAck("sess-deferred");
|
|
});
|
|
|
|
test("interrupt priority leaves display output alone below pressure threshold", () => {
|
|
clearTerminalSessionFlowAck("sess-low-pressure");
|
|
const term = createFakeTerm();
|
|
const flow = createOutputFlowController({
|
|
highWaterMark: 100,
|
|
lowWaterMark: 20,
|
|
onPause: () => {},
|
|
onResume: () => {},
|
|
});
|
|
const backend = {
|
|
ackSessionFlow: () => {},
|
|
setSessionFlowPaused: () => {},
|
|
};
|
|
|
|
flow.received(119);
|
|
const priority = prioritizeTerminalInput(
|
|
term,
|
|
"sess-low-pressure",
|
|
flow,
|
|
backend,
|
|
(callback: () => void) => callback(),
|
|
{
|
|
reason: "interrupt",
|
|
drainStaleOutput: true,
|
|
now: 7800,
|
|
},
|
|
);
|
|
|
|
assert.equal(priority.skippedReason, "below-threshold");
|
|
assert.deepEqual(
|
|
filterTerminalInterruptDisplayOutput(
|
|
term,
|
|
"Type :qa and press <Enter> to exit Vim",
|
|
{ now: 7801 },
|
|
),
|
|
{
|
|
accepted: true,
|
|
data: "Type :qa and press <Enter> to exit Vim",
|
|
droppedBytes: 0,
|
|
reason: "inactive",
|
|
},
|
|
);
|
|
clearTerminalSessionFlowAck("sess-low-pressure");
|
|
});
|