mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 23:50:45 +00:00
141 lines
4.0 KiB
TypeScript
141 lines
4.0 KiB
TypeScript
// Created: 2026-07-21
|
|
// Purpose: Verify SFTP prefers live terminal session reuse before fresh auth.
|
|
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
createPinnedReconnectSideResolver,
|
|
openSftpWithSessionPreference,
|
|
rejectHostKeyVerificationRequest,
|
|
resolvePinnedReconnectSide,
|
|
} from "./useSftpConnections.ts";
|
|
|
|
const openOptions = {
|
|
sessionId: "sftp-request-1",
|
|
hostname: "192.168.9.138",
|
|
username: "zhlrs",
|
|
port: 22,
|
|
} as NetcattySSHOptions;
|
|
|
|
test("openSftpWithSessionPreference opens session-backed SFTP before authing again", async () => {
|
|
const calls: string[] = [];
|
|
const sftpId = await openSftpWithSessionPreference({
|
|
bridge: {
|
|
openSftpForSession: async (sessionId: string) => {
|
|
calls.push(`openForSession:${sessionId}`);
|
|
return "session-backed-sftp";
|
|
},
|
|
openSftp: async () => {
|
|
calls.push("openSftp");
|
|
return "fresh-sftp";
|
|
},
|
|
},
|
|
sourceSessionId: "ssh-session-1",
|
|
openOptions,
|
|
});
|
|
|
|
assert.equal(sftpId, "session-backed-sftp");
|
|
assert.deepEqual(calls, ["openForSession:ssh-session-1"]);
|
|
});
|
|
|
|
test("openSftpWithSessionPreference falls back to normal SFTP when session reuse fails", async () => {
|
|
const calls: string[] = [];
|
|
const sftpId = await openSftpWithSessionPreference({
|
|
bridge: {
|
|
openSftpForSession: async (sessionId: string) => {
|
|
calls.push(`openForSession:${sessionId}`);
|
|
throw new Error("channel unavailable");
|
|
},
|
|
openSftp: async (options: NetcattySSHOptions) => {
|
|
calls.push(`openSftp:${options.sessionId}`);
|
|
return "fresh-sftp";
|
|
},
|
|
},
|
|
sourceSessionId: "ssh-session-1",
|
|
openOptions,
|
|
});
|
|
|
|
assert.equal(sftpId, "fresh-sftp");
|
|
assert.deepEqual(calls, ["openForSession:ssh-session-1", "openSftp:sftp-request-1"]);
|
|
});
|
|
|
|
test("openSftpWithSessionPreference opens normal SFTP without a source session", async () => {
|
|
const calls: string[] = [];
|
|
const sftpId = await openSftpWithSessionPreference({
|
|
bridge: {
|
|
openSftpForSession: async () => {
|
|
calls.push("openForSession");
|
|
return "session-backed-sftp";
|
|
},
|
|
openSftp: async (options: NetcattySSHOptions) => {
|
|
calls.push(`openSftp:${options.sessionId}`);
|
|
return "fresh-sftp";
|
|
},
|
|
},
|
|
sourceSessionId: undefined,
|
|
openOptions,
|
|
});
|
|
|
|
assert.equal(sftpId, "fresh-sftp");
|
|
assert.deepEqual(calls, ["openSftp:sftp-request-1"]);
|
|
});
|
|
|
|
test("resolvePinnedReconnectSide follows a tab moved to the other side", () => {
|
|
assert.equal(
|
|
resolvePinnedReconnectSide("left", "tab-1", [], [{ id: "tab-1" }]),
|
|
"right",
|
|
);
|
|
assert.equal(
|
|
resolvePinnedReconnectSide("right", "tab-1", [{ id: "tab-1" }], []),
|
|
"left",
|
|
);
|
|
assert.equal(
|
|
resolvePinnedReconnectSide("left", "tab-1", [{ id: "tab-1" }], []),
|
|
"left",
|
|
);
|
|
assert.equal(
|
|
resolvePinnedReconnectSide("left", undefined, [], [{ id: "tab-1" }]),
|
|
"left",
|
|
);
|
|
assert.throws(
|
|
() => resolvePinnedReconnectSide("left", "gone", [], []),
|
|
/SFTP tab is no longer available/,
|
|
);
|
|
});
|
|
|
|
test("rejectHostKeyVerificationRequest rejects an orphaned verification", () => {
|
|
const responses: Array<[string, boolean, boolean]> = [];
|
|
|
|
rejectHostKeyVerificationRequest({
|
|
respondHostKeyVerification: async (requestId, accept, addToKnownHosts) => {
|
|
responses.push([requestId, accept, addToKnownHosts]);
|
|
return { success: true };
|
|
},
|
|
}, "hostkey-1");
|
|
|
|
assert.deepEqual(responses, [["hostkey-1", false, false]]);
|
|
});
|
|
|
|
test("pinned reconnect side resolver follows moves across async boundaries", async () => {
|
|
let leftTabs: ReadonlyArray<{ id: string }> = [{ id: "tab-1" }];
|
|
let rightTabs: ReadonlyArray<{ id: string }> = [];
|
|
const resolveSide = createPinnedReconnectSideResolver(
|
|
"left",
|
|
"tab-1",
|
|
() => leftTabs,
|
|
() => rightTabs,
|
|
);
|
|
|
|
assert.equal(resolveSide(), "left");
|
|
|
|
await Promise.resolve();
|
|
leftTabs = [];
|
|
rightTabs = [{ id: "tab-1" }];
|
|
|
|
assert.equal(resolveSide(), "right");
|
|
|
|
rightTabs = [];
|
|
assert.equal(resolveSide(), "right");
|
|
});
|