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

85 lines
3.7 KiB
JavaScript

const assert = require("node:assert/strict");
const { readFileSync } = require("node:fs");
const test = require("node:test");
const vm = require("node:vm");
const deepLink = require("./deepLink.cjs");
const { collectOpenTerminalPathArgs } = require("./openTerminalPath.cjs");
function createHarness() {
const source = readFileSync(require.resolve("./main.cjs"), "utf8");
const start = source.indexOf('app.on("second-instance"');
const end = source.indexOf("// Application lifecycle", start);
assert.ok(start >= 0 && end > start);
let handle;
const queued = [];
const paths = [];
let focusCount = 0;
const context = vm.createContext({
...deepLink,
app: { on: (_event, handler) => { handle = handler; } },
sshDeepLinkEnabled: false,
jmsDeepLinkEnabled: false,
queueSshDeepLink: (url) => queued.push(url),
queueTelnetDeepLink: (url) => queued.push(url),
queueJmsDeepLink: (url) => queued.push(url),
collectOpenTerminalPathArgs,
resolveOpenTerminalPathsFromArgs: (argv, { baseDirectory }) => {
paths.push({ argv: Array.from(argv), baseDirectory });
return ["/resolved"];
},
queueResolvedOpenTerminalPaths: (resolved) => paths.push(Array.from(resolved)),
focusMainWindow: () => { focusCount += 1; return true; },
});
vm.runInContext(source.slice(start, end), context);
return { handle, queued, paths, focused: () => focusCount };
}
for (const password of ["secret", "space :/@ password", "-flag-like-password"]) {
test(`second-instance transport copies are released without changing the SSH password: ${password}`, () => {
const h = createHarness();
const eventArgv = ["netcatty", "-ssh", "-P", "-pw", "alice@localhost", "2222", password];
const additionalData = { rawLaunchArgv: ["-ssh", "alice@localhost", "-P", "2222", "-pw", password] };
h.handle(null, eventArgv, "/working-directory", additionalData);
assert.equal(h.queued.length, 1);
const url = new URL(h.queued[0]);
assert.equal(decodeURIComponent(url.password), password);
assert.equal(url.hostname, "localhost");
assert.equal(url.port, "2222");
assert.equal(eventArgv.length, 0, "discard the consumed, potentially reordered event arguments");
assert.equal(additionalData.rawLaunchArgv.length, 0, "discard the consumed forwarding copy");
});
}
test("failed command-line parsing still clears temporary transport copies", () => {
const h = createHarness();
const eventArgv = ["netcatty", "-raw", "-pw", "localhost", "secret"];
const additionalData = { rawLaunchArgv: ["-raw", "localhost", "-pw", "secret"] };
h.handle(null, eventArgv, "/working-directory", additionalData);
assert.deepEqual(h.queued, []);
assert.equal(h.focused(), 1);
assert.equal(eventArgv.length, 0);
assert.equal(additionalData.rawLaunchArgv.length, 0);
});
test("legacy second instances without additional data retain ordered-argv support", () => {
const h = createHarness();
const eventArgv = ["netcatty", "-ssh", "alice@localhost", "-pw", "secret"];
h.handle(null, eventArgv, "/working-directory");
assert.equal(new URL(h.queued[0]).password, "secret");
assert.equal(eventArgv.includes("secret"), false);
});
test("Explorer launch routing uses the ordered copy after transport cleanup", () => {
const h = createHarness();
const ordered = ["--open-terminal-path", "relative-folder"];
const eventArgv = ["netcatty", ...ordered];
const additionalData = { rawLaunchArgv: [...ordered] };
h.handle(null, eventArgv, "/working-directory", additionalData);
assert.deepEqual(h.paths, [
{ argv: ["netcatty", ...ordered], baseDirectory: "/working-directory" },
["/resolved"],
]);
assert.equal(eventArgv.length, 0);
assert.equal(additionalData.rawLaunchArgv.length, 0);
});