Files
Netcatty/electron/bridges/mcpServerBridge.readContext.test.cjs
T
GOUKI9999andClaude Opus 5.5 ea36f46d1d fix(ai-cli): stop unit tests from deleting the installed discovery file (#3501) (#3507)
Requiring mcpServerBridge from node --test inherited the production
netcatty-tool-cli/discovery.json path, so cleanup() removed the running
app's bridge pointer. Resolve the discovery path on demand and isolate the
affected tests via NETCATTY_TOOL_CLI_DISCOVERY_FILE.

Rebased onto #3502, which now owns the self-heal (live-host reuse, status
and content validation); this change only adds on-demand path resolution to
those helpers and drops the separate 30s missing-file sweep.

The subprocess regression test redirects HOME/USERPROFILE as well as
APPDATA/XDG_CONFIG_HOME, since macOS derives the default path from
os.homedir(), and refuses to plant its canary anywhere outside the
throwaway root, so it can never write the real app path on any OS.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 09:14:25 +08:00

84 lines
3.6 KiB
JavaScript

"use strict";
const assert = require("node:assert/strict");
const test = require("node:test");
const { listMcpTools } = require("../capabilities/codegen/toolSurfaces.cjs");
// Keep bridge discovery writes/deletes off the installed app's live file.
const { isolateCliDiscoveryFile } = require("./cliDiscoveryTestIsolation.cjs");
isolateCliDiscoveryFile();
function setup(t, mode = "confirm") {
const path = require.resolve("./mcpServerBridge.cjs");
delete require.cache[path];
const bridge = require(path);
bridge.init({ sessions: new Map(), electronModule: null });
bridge.setPermissionMode(mode);
bridge.updateSessionMetadata([{ sessionId: "a" }, { sessionId: "b" }], "chat");
t.after(() => bridge.cleanup());
return bridge;
}
for (const mode of ["observer", "confirm", "auto"]) {
test(`screen reads work without approval in ${mode} mode`, async (t) => {
const bridge = setup(t, mode);
const calls = [];
bridge.setVaultAgentInvoker(async (op, params) => {
calls.push({ op, params });
return { ok: true, content: "existing output" };
});
const tool = listMcpTools().find((tool) => tool.mcpTool === "terminal_read_context");
const result = await bridge.dispatchBuiltinRpc(tool.rpcMethod, {
chatSessionId: "chat", sessionId: "b", range: "tail", maxLines: 10,
});
assert.equal(result.content, "existing output");
assert.deepEqual(calls, [{ op: "terminal.readContext", params: {
sessionId: "b", range: "tail", startLine: undefined, maxLines: 10,
} }]);
});
}
test("missing, ambiguous, empty and foreign scopes never reach the renderer", async (t) => {
const bridge = setup(t);
bridge.setVaultAgentInvoker(() => assert.fail("must not read"));
for (const params of [
{}, { sessionId: "a" }, { chatSessionId: "chat" },
{ chatSessionId: "chat", sessionId: "foreign" },
{ chatSessionId: "chat", sessionId: "a", scopedSessionIds: [] },
{ chatSessionId: "chat", sessionId: "foreign", scopedSessionIds: ["foreign"] },
]) assert.equal((await bridge.dispatchBuiltinRpc("netcatty/readContext", params)).ok, false);
});
test("one allowed terminal is inferred and revoked scope suppresses the result", async (t) => {
const bridge = setup(t);
bridge.setVaultAgentInvoker(async (_op, params) => ({ ok: true, sessionId: params.sessionId }));
const result = await bridge.dispatchBuiltinRpc("netcatty/readContext", {
chatSessionId: "chat", scopedSessionIds: ["b"],
});
assert.equal(result.sessionId, "b");
bridge.setVaultAgentInvoker(async () => {
bridge.updateSessionMetadata([], "chat");
return { ok: true, content: "do not disclose" };
});
const revoked = await bridge.dispatchBuiltinRpc("netcatty/readContext", {
chatSessionId: "chat", sessionId: "a",
});
assert.equal(revoked.ok, false);
assert.equal(revoked.content, undefined);
});
test("read dispatch resolves the owning popup for legacy and worker sessions", async (t) => {
const bridge = setup(t);
const popup = { id: 22, isDestroyed: () => false };
for (const worker of [false, true]) {
bridge.init({
sessions: new Map(worker ? [] : [["a", { webContentsId: 22 }]]),
electronModule: { webContents: { fromId: (id) => id === 22 ? popup : null } },
terminalWorkerManager: worker ? { getSessionOwnerWebContentsId: () => 22 } : null,
});
bridge.setVaultAgentInvoker(async (_op, params, options) => {
assert.equal(options.webContents, popup);
return { ok: true, sessionId: params.sessionId };
});
assert.equal((await bridge.dispatchBuiltinRpc("netcatty/readContext", {
sessionId: "a", chatSessionId: "chat",
})).ok, true);
}
});