Files
NetMesh/electron/bridges/keyboardInteractiveHandler.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

154 lines
5.4 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const keyboardInteractiveHandler = require("./keyboardInteractiveHandler.cjs");
test("keyboard-interactive responses from the wrong renderer are rejected and kept pending", () => {
const finishCalls = [];
const requestId = keyboardInteractiveHandler.generateRequestId("test");
keyboardInteractiveHandler.storeRequest(requestId, (responses) => {
finishCalls.push(responses);
}, 101, "session-1");
const wrongResult = keyboardInteractiveHandler.handleResponse(
{ sender: { id: 202 } },
{ requestId, responses: ["wrong"] },
);
assert.deepEqual(wrongResult, { success: false, error: "Wrong sender" });
assert.deepEqual(finishCalls, []);
assert.equal(keyboardInteractiveHandler.getRequests().has(requestId), true);
const correctResult = keyboardInteractiveHandler.handleResponse(
{ sender: { id: 101 } },
{ requestId, responses: ["correct"] },
);
assert.deepEqual(correctResult, { success: true });
assert.deepEqual(finishCalls, [["correct"]]);
assert.equal(keyboardInteractiveHandler.getRequests().has(requestId), false);
});
test("keyboard-interactive responses never write password or OTP contents to logs", (t) => {
const logs = [];
t.mock.method(console, "log", (...args) => logs.push(args));
const finishCalls = [];
const requestId = keyboardInteractiveHandler.generateRequestId("secret-log-test");
const secrets = ["secondary-password-secret", "123456-otp-secret"];
keyboardInteractiveHandler.storeRequest(requestId, (responses) => {
finishCalls.push(responses);
}, 303, "session-secret-log-test");
const result = keyboardInteractiveHandler.handleResponse(
{ sender: { id: 303 } },
{ requestId, responses: secrets, cancelled: false },
);
assert.deepEqual(result, { success: true });
assert.deepEqual(finishCalls, [secrets]);
const serializedLogs = JSON.stringify(logs);
for (const secret of secrets) {
assert.equal(serializedLogs.includes(secret), false);
}
assert.equal(
logs.some((entry) => entry.some((value) => value?.responsesCount === secrets.length)),
true,
);
});
test("keyboard-interactive delivery failures close the renderer prompt", (t) => {
t.mock.method(console, "warn", () => {});
const notifications = [];
const requestId = keyboardInteractiveHandler.generateRequestId("delivery-failure");
keyboardInteractiveHandler.storeRequest(
requestId,
() => {
throw new Error("connection closed");
},
404,
"session-delivery-failure",
{
isDestroyed: () => false,
send: (channel, payload) => notifications.push({ channel, payload }),
},
);
const result = keyboardInteractiveHandler.handleResponse(
{ sender: { id: 404 } },
{ requestId, responses: ["123456"], cancelled: false },
);
assert.deepEqual(result, { success: false, error: "Failed to deliver response" });
assert.equal(keyboardInteractiveHandler.getRequests().has(requestId), false);
assert.deepEqual(notifications, [{
channel: "netcatty:keyboard-interactive-cancelled",
payload: {
requestId,
sessionId: "session-delivery-failure",
reason: "delivery-failed",
},
}]);
});
test("keyboard-interactive responses settle once when delivery closes the session", () => {
const finishCalls = [];
const requestId = keyboardInteractiveHandler.generateRequestId("reentrant-close");
keyboardInteractiveHandler.storeRequest(
requestId,
(responses) => {
finishCalls.push(responses);
keyboardInteractiveHandler.cancelRequestsForSession("session-reentrant-close");
},
505,
"session-reentrant-close",
);
const result = keyboardInteractiveHandler.handleResponse(
{ sender: { id: 505 } },
{ requestId, responses: ["123456"], cancelled: false },
);
assert.deepEqual(result, { success: true });
assert.deepEqual(finishCalls, [["123456"]]);
assert.equal(keyboardInteractiveHandler.getRequests().has(requestId), false);
});
test("keyboard-interactive requests can be cancelled by owning session", () => {
const finishCalls = [];
const notifications = [];
const matchingRequestId = keyboardInteractiveHandler.generateRequestId("matching");
const otherRequestId = keyboardInteractiveHandler.generateRequestId("other");
keyboardInteractiveHandler.storeRequest(
matchingRequestId,
(responses) => finishCalls.push({ requestId: matchingRequestId, responses }),
101,
"tunnel-1",
{
isDestroyed: () => false,
send: (channel, payload) => notifications.push({ channel, payload }),
},
);
keyboardInteractiveHandler.storeRequest(
otherRequestId,
(responses) => finishCalls.push({ requestId: otherRequestId, responses }),
101,
"tunnel-2",
);
const cancelled = keyboardInteractiveHandler.cancelRequestsForSession("tunnel-1", "tunnel-stopped");
assert.equal(cancelled, 1);
assert.deepEqual(finishCalls, [{ requestId: matchingRequestId, responses: [] }]);
assert.deepEqual(notifications, [{
channel: "netcatty:keyboard-interactive-cancelled",
payload: {
requestId: matchingRequestId,
sessionId: "tunnel-1",
reason: "tunnel-stopped",
},
}]);
assert.equal(keyboardInteractiveHandler.getRequests().has(matchingRequestId), false);
assert.equal(keyboardInteractiveHandler.getRequests().has(otherRequestId), true);
keyboardInteractiveHandler.cancelRequestsForSession("tunnel-2");
});