Files
Netcatty/electron/bridges/terminalBridge.closeReusedSession.test.cjs
T
bincxzandClaude Opus 4.8 85f486e6cd feat(ssh): reuse authenticated connection for Copy Tab to skip re-MFA
Duplicating an SSH tab ("Copy Tab") used to open a brand-new SSH
connection, forcing a second MFA prompt on hosts with multi-factor auth.
Like Tabby's session multiplexing, open a new shell *channel* on the
source tab's already-authenticated connection instead, so the duplicate
reuses the existing transport and skips key exchange + authentication.

- copySession records the source session id (reuseConnectionFromSessionId)
  for connected, non-mosh SSH sessions; it is threaded down to the SSH
  bridge as options.sourceSessionId.
- startSession.cjs gains a reuse path that opens conn.shell() on the
  source connection. The shell wiring is extracted into a shared
  setupShellSession helper used by both the fresh and reuse paths.
- Connection lifecycle is reference-counted via a new sshConnectionPool
  module (mirrors Tabby ref/unref/destroy): the shared transport + jump
  host chain are torn down only when the last channel closes, so closing
  a copy — or the original while a copy is open — never kills siblings.
  terminalBridge.closeSession routes SSH teardown through the same release.
- Reuse falls back to a fresh connection when the source is gone, and is
  skipped for X11 hosts (X11 is negotiated per channel).

Tests: sshConnectionPool refcount unit tests, bridge reuse integration
tests (reuse vs fresh, sibling survival, X11 skip, fallback), and
terminalBridge close lifecycle tests.

Refs #1204

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 10:43:49 +08:00

109 lines
3.4 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const terminalBridge = require("./terminalBridge.cjs");
const { createConnectionRef, acquireConnectionRef } = require("./sshConnectionPool.cjs");
// Verifies the terminalBridge close path respects connection multiplexing
// (issue #1204): closing one tab that shares an SSH connection must only close
// that tab's channel, and only tear the shared transport down when the last
// tab is gone.
function makeStream() {
return {
closed: 0,
close() { this.closed += 1; },
};
}
function makeConn() {
return {
ended: 0,
end() { this.ended += 1; },
};
}
function makeSession(conn, connRef) {
return {
stream: makeStream(),
conn,
chainConnections: [],
connRef,
zmodemSentry: { cancel() {} },
};
}
test("closing a reused SSH tab keeps the shared connection alive", () => {
const sessions = new Map();
terminalBridge.init({ sessions, electronModule: {} });
const conn = makeConn();
const owner = makeSession(conn, null);
const connRef = createConnectionRef(owner, conn, []);
const copy = makeSession(conn, null);
acquireConnectionRef(copy, connRef);
sessions.set("owner", owner);
sessions.set("copy", copy);
assert.equal(connRef.count, 2);
// Close the copy tab.
terminalBridge.closeSession({ sender: {} }, { sessionId: "copy" });
assert.equal(copy.stream.closed, 1, "copy channel closed");
assert.equal(conn.ended, 0, "shared connection must stay up for the owner");
assert.equal(connRef.count, 1);
assert.equal(sessions.has("copy"), false);
// Close the owner tab — now the last holder, so the connection is ended.
terminalBridge.closeSession({ sender: {} }, { sessionId: "owner" });
assert.equal(owner.stream.closed, 1, "owner channel closed");
assert.equal(conn.ended, 1, "connection ended once last channel closes");
assert.equal(connRef.count, 0);
});
test("closing the owner tab first does not strand the copy's connection", () => {
const sessions = new Map();
terminalBridge.init({ sessions, electronModule: {} });
const conn = makeConn();
const owner = makeSession(conn, null);
const connRef = createConnectionRef(owner, conn, []);
const copy = makeSession(conn, null);
acquireConnectionRef(copy, connRef);
sessions.set("owner", owner);
sessions.set("copy", copy);
// Close the owner first.
terminalBridge.closeSession({ sender: {} }, { sessionId: "owner" });
assert.equal(conn.ended, 0, "connection survives for the still-open copy");
assert.equal(connRef.count, 1);
// Closing the copy (now last) ends the connection.
terminalBridge.closeSession({ sender: {} }, { sessionId: "copy" });
assert.equal(conn.ended, 1);
});
test("legacy SSH session without connRef still ends its own connection + chain", () => {
const sessions = new Map();
terminalBridge.init({ sessions, electronModule: {} });
const conn = makeConn();
const chain = [makeConn(), makeConn()];
const session = {
stream: makeStream(),
conn,
chainConnections: chain,
connRef: null,
zmodemSentry: { cancel() {} },
};
sessions.set("legacy", session);
terminalBridge.closeSession({ sender: {} }, { sessionId: "legacy" });
assert.equal(session.stream.closed, 1);
assert.equal(conn.ended, 1, "non-multiplexed connection ends directly");
assert.equal(chain[0].ended, 1);
assert.equal(chain[1].ended, 1);
});