Files
NetMesh/electron/bridges/terminalFlowAck.test.cjs
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

113 lines
3.8 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const sharedConstants = require("../../infrastructure/config/terminalFlowConstants.cjs");
const {
FLOW_HIGH_WATER_MARK,
FLOW_LOW_WATER_MARK,
clearSessionFlowState,
setBufferedOutputBytes,
setRendererFlowPaused,
shouldAcceptSessionOutput,
shouldProcessSessionOutput,
trackAck,
trackEmitted,
} = require("./terminalFlowAck.cjs");
function makeSession() {
const calls = [];
return {
stream: {
pause() {
calls.push("pause");
},
resume() {
calls.push("resume");
},
},
_calls: calls,
};
}
test("main-process watermarks match shared terminalFlowConstants.cjs", () => {
assert.equal(FLOW_HIGH_WATER_MARK, sharedConstants.FLOW_HIGH_WATER_MARK);
assert.equal(FLOW_LOW_WATER_MARK, sharedConstants.FLOW_LOW_WATER_MARK);
});
test("shouldAcceptSessionOutput returns false when flow pause is applied", () => {
const session = makeSession();
assert.equal(shouldAcceptSessionOutput(session), true);
trackEmitted(session, FLOW_HIGH_WATER_MARK);
assert.equal(shouldAcceptSessionOutput(session), false);
trackAck(session, FLOW_HIGH_WATER_MARK);
assert.equal(shouldAcceptSessionOutput(session), true);
});
test("shouldProcessSessionOutput keeps processing paused output for buffering", () => {
const session = makeSession();
trackEmitted(session, FLOW_HIGH_WATER_MARK);
assert.equal(shouldProcessSessionOutput(session), true);
assert.equal(shouldProcessSessionOutput(session, { isActive: () => false }), true);
assert.equal(shouldProcessSessionOutput(session, { isActive: () => true }), true);
});
test("trackEmitted pauses once when unacked bytes cross the high watermark", () => {
const session = makeSession();
trackEmitted(session, FLOW_HIGH_WATER_MARK);
assert.deepEqual(session._calls, ["pause"]);
trackEmitted(session, 1024);
assert.deepEqual(session._calls, ["pause"]);
});
test("trackAck resumes after draining to the low watermark", () => {
const session = makeSession();
trackEmitted(session, FLOW_HIGH_WATER_MARK + FLOW_LOW_WATER_MARK);
trackAck(session, FLOW_LOW_WATER_MARK);
assert.deepEqual(session._calls, ["pause"]);
trackAck(session, FLOW_HIGH_WATER_MARK);
assert.deepEqual(session._calls, ["pause", "resume"]);
});
test("flow diagnostics remember the renderer session id", () => {
const session = makeSession();
trackEmitted(session, 1024, "session-1");
trackAck(session, 512, "session-1");
assert.equal(session.flowState.sessionId, "session-1");
});
test("renderer pause flag keeps the stream paused until cleared", () => {
const session = makeSession();
trackEmitted(session, FLOW_HIGH_WATER_MARK);
setRendererFlowPaused(session, true);
trackAck(session, FLOW_HIGH_WATER_MARK);
assert.deepEqual(session._calls, ["pause"]);
setRendererFlowPaused(session, false);
assert.deepEqual(session._calls, ["pause", "resume"]);
});
test("buffered output pressure pauses the source while still allowing paced drain", () => {
const session = makeSession();
setBufferedOutputBytes(session, FLOW_HIGH_WATER_MARK);
assert.deepEqual(session._calls, ["pause"]);
assert.equal(shouldAcceptSessionOutput(session), true);
setBufferedOutputBytes(session, FLOW_LOW_WATER_MARK + 1);
assert.deepEqual(session._calls, ["pause"]);
setBufferedOutputBytes(session, FLOW_LOW_WATER_MARK);
assert.deepEqual(session._calls, ["pause", "resume"]);
});
test("clearSessionFlowState resumes and resets counters", () => {
const session = makeSession();
trackEmitted(session, FLOW_HIGH_WATER_MARK);
clearSessionFlowState(session);
assert.deepEqual(session._calls, ["pause", "resume"]);
assert.equal(session.flowState.unackedBytes, 0);
assert.equal(session.flowState.bufferedBytes, 0);
assert.equal(session.flowState.rendererPaused, false);
});