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

103 lines
3.7 KiB
JavaScript

"use strict";
const { describe, it } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const {
getExternalMcpDiscoveryFilePath,
getExternalMcpLauncherPath,
resolveExistingExternalMcpDiscoveryFilePath,
EXTERNAL_MCP_CHAT_SESSION_ID,
EXTERNAL_MCP_DISCOVERY_ENV_VAR,
buildDiscoveryEnv,
formatDiscoveryEnvCliFlags,
} = require("./externalMcpDiscoveryPath.cjs");
const {
buildExternalDiscoveryPayload,
writeExternalDiscovery,
removeExternalDiscovery,
readExternalDiscovery,
} = require("./externalMcpDiscovery.cjs");
describe("externalMcpDiscoveryPath", () => {
it("returns a discovery path under the external-mcp state dir", () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-ext-mcp-"));
const discoveryPath = getExternalMcpDiscoveryFilePath({ userDataDir: tmp });
assert.equal(discoveryPath, path.join(tmp, "external-mcp", "discovery.json"));
assert.equal(EXTERNAL_MCP_CHAT_SESSION_ID, "__external_mcp__");
assert.ok(getExternalMcpLauncherPath().includes("netcatty-external-mcp"));
});
it("honors NETCATTY_EXTERNAL_MCP_DISCOVERY_FILE without falling back", () => {
const previous = process.env[EXTERNAL_MCP_DISCOVERY_ENV_VAR];
const custom = path.join(os.tmpdir(), "missing-external-discovery.json");
process.env[EXTERNAL_MCP_DISCOVERY_ENV_VAR] = custom;
try {
assert.equal(getExternalMcpDiscoveryFilePath(), custom);
assert.equal(resolveExistingExternalMcpDiscoveryFilePath(), custom);
} finally {
if (previous == null) delete process.env[EXTERNAL_MCP_DISCOVERY_ENV_VAR];
else process.env[EXTERNAL_MCP_DISCOVERY_ENV_VAR] = previous;
}
});
it("resolves an existing discovery under candidate userData dirs", () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-ext-mcp-"));
const discoveryPath = path.join(tmp, "external-mcp", "discovery.json");
writeExternalDiscovery(discoveryPath, {
port: 1,
token: "t",
pid: 1,
});
const resolved = resolveExistingExternalMcpDiscoveryFilePath({ userDataDir: tmp });
assert.equal(resolved, discoveryPath);
});
it("builds discovery env CLI flags for clients", () => {
const env = buildDiscoveryEnv("/tmp/discovery.json");
assert.deepEqual(
formatDiscoveryEnvCliFlags(env, "codex"),
["--env", `${EXTERNAL_MCP_DISCOVERY_ENV_VAR}=/tmp/discovery.json`],
);
assert.deepEqual(
formatDiscoveryEnvCliFlags(env, "claude"),
["-e", `${EXTERNAL_MCP_DISCOVERY_ENV_VAR}=/tmp/discovery.json`],
);
});
});
describe("externalMcpDiscovery", () => {
it("writes and reads discovery with chatSessionId", () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-ext-mcp-"));
const filePath = path.join(tmp, "discovery.json");
const payload = writeExternalDiscovery(filePath, {
port: 41234,
token: "abc123",
pid: 99,
permissionMode: "confirm",
chatSessionId: EXTERNAL_MCP_CHAT_SESSION_ID,
});
assert.equal(payload.port, 41234);
assert.equal(payload.chatSessionId, EXTERNAL_MCP_CHAT_SESSION_ID);
const read = readExternalDiscovery(filePath);
assert.equal(read.port, 41234);
assert.equal(read.token, "abc123");
assert.equal(read.chatSessionId, EXTERNAL_MCP_CHAT_SESSION_ID);
removeExternalDiscovery(filePath);
assert.equal(fs.existsSync(filePath), false);
});
it("buildExternalDiscoveryPayload defaults chatSessionId", () => {
const payload = buildExternalDiscoveryPayload({
port: 1,
token: "t",
pid: 1,
});
assert.equal(payload.chatSessionId, "__external_mcp__");
assert.equal(payload.host, "127.0.0.1");
});
});