Files
Netcatty/electron/plugins/terminalInterceptorTransport.test.cjs
T
陈大猫andryan-wong-coder 67f8108713 feat(plugins): add reviewed privileged terminal data pipeline (#2402)
* feat(plugins): add privileged terminal data pipeline

* fix(plugins): harden terminal interceptor lifecycle

* fix(plugins): close terminal interceptor lifecycle gaps

* fix(plugins): preserve terminal interceptor state transitions

* fix(plugin): close terminal pipeline lifecycle gaps

* fix(plugin): harden terminal interceptor startup

* fix(plugin): isolate terminal output route attempts

* fix(plugin): isolate reused terminal sessions

* fix(plugin): harden terminal interceptor boundaries

* fix(plugin): harden terminal replay metadata

* fix(plugin): preserve terminal replay provenance

* fix(plugin): close terminal pipeline lifecycle races

* fix(plugin): harden terminal route handoff

* fix(plugin): contain terminal handoff failures

* fix(plugin): route terminal attachment through RPC

* fix(plugin): validate terminal attachment RPC

* fix(plugin): close unexpected runtime ports

* fix(plugin): canonicalize terminal data frames

* fix(plugin): close terminal pipeline lifecycle gaps

* fix(plugin): close terminal interceptor review gaps

* fix(plugin): harden terminal interceptor protocol handling

* fix(plugin): preserve terminal flow compatibility

* fix(plugin): preserve terminal protocol accounting

* fix(plugin): bound terminal interceptor lifecycle

* fix(plugin): close terminal interceptor review gaps

---------

Co-authored-by: ryan-wong-coder <wangsiyuan19990317@gmail.com>
2026-07-23 09:42:47 +08:00

125 lines
3.5 KiB
JavaScript

"use strict";
const assert = require("node:assert/strict");
const test = require("node:test");
const { runInNewContext } = require("node:vm");
const {
TERMINAL_INTERCEPTOR_MAX_CHUNK_BYTES,
TERMINAL_INTERCEPTOR_MAX_WINDOW_BYTES,
createTerminalInterceptorEnvelope,
} = require("./terminalInterceptorTransport.cjs");
test("terminal interceptor envelopes validate canonical ready, chunk, and result frames", () => {
assert.deepEqual(createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:ready",
sessionId: "session-1",
direction: "output",
windowBytes: TERMINAL_INTERCEPTOR_MAX_WINDOW_BYTES,
}), {
frame: {
type: "netcatty:terminal-interceptor:ready",
sessionId: "session-1",
direction: "output",
windowBytes: TERMINAL_INTERCEPTOR_MAX_WINDOW_BYTES,
},
});
const chunkData = new Uint8Array([1, 2, 3]).buffer;
const chunk = createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:chunk",
sequence: 1,
direction: "input",
creditBytes: TERMINAL_INTERCEPTOR_MAX_CHUNK_BYTES,
byteLength: 3,
}, chunkData);
assert.equal(chunk.transfer, chunkData);
const resultData = runInNewContext("new Uint8Array([4, 5]).buffer");
const result = createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:result",
sequence: Number.MAX_SAFE_INTEGER,
status: "ok",
creditBytes: 3,
byteLength: 2,
}, resultData);
assert.equal(result.transfer, resultData);
assert.deepEqual(createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:result",
sequence: 2,
status: "failed",
}), {
frame: {
type: "netcatty:terminal-interceptor:result",
sequence: 2,
status: "failed",
},
});
});
test("terminal interceptor envelopes fail closed on schema or transfer mismatches", () => {
const data = new ArrayBuffer(4);
assert.throws(
() => createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:chunk",
sequence: 0,
direction: "input",
creditBytes: 1,
byteLength: 4,
}, data),
/violates the plugin contract/,
);
assert.throws(
() => createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:chunk",
sequence: 1,
direction: "input",
creditBytes: 1,
byteLength: 3,
}, data),
/byteLength mismatch/,
);
assert.throws(
() => createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:result",
sequence: 1,
status: "ok",
creditBytes: 4,
byteLength: 4,
}),
/requires a transferred ArrayBuffer/,
);
assert.throws(
() => createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:ready",
sessionId: "session-1",
direction: "input",
windowBytes: TERMINAL_INTERCEPTOR_MAX_CHUNK_BYTES,
extra: true,
}),
/violates the plugin contract/,
);
assert.throws(
() => createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:result",
sequence: 1,
status: "failed",
}, data),
/must not include a transferred buffer/,
);
const detached = new ArrayBuffer(0);
structuredClone(detached, { transfer: [detached] });
assert.throws(
() => createTerminalInterceptorEnvelope({
type: "netcatty:terminal-interceptor:chunk",
sequence: 1,
direction: "input",
creditBytes: 1,
byteLength: 0,
}, detached),
/requires a real, attached ArrayBuffer/,
);
});