Files
Netcatty/electron/cli/cliChatSession.test.cjs
T
8017c144fe feat(cli): bind Skills+CLI chat session via host env (#3347)
* feat(workspace): search hosts by tag (#3001)

Co-authored-by: Cursor <cursoragent@cursor.com>

* feat(cli): bind Skills+CLI chat session via host env

Hide --chat-session from agents so they cannot spoof another chat tenant.
The host injects NETCATTY_CLI_CHAT_SESSION_ID, and Copilot/CodeBuddy
approval gates reject leftover flags and env-prefix overrides.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(ai): isolate Skills+CLI chat env across concurrent SDK turns

Cursor and OpenCode mutate process.env around spawn. Serialize those
writes so overlapping chats cannot inherit or restore the wrong
NETCATTY_CLI_CHAT_SESSION_ID.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Revert "feat(workspace): search hosts by tag (#3001)"

This reverts commit 4b651a3841.

* fix(ai): preserve chat isolation when cancelling Cursor startup

* fix(ai): isolate Cursor turns in cancellable workers

* fix(ai): contain native Cursor cleanup in a utility process

* fix(ai): report Cursor utility process fatal errors

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-09-12 12:16:57 +08:00

70 lines
2.4 KiB
JavaScript

"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const {
bindChatSessionId,
describeMissingChatSession,
describeRemovedChatSessionFlag,
isRemovedChatSessionFlag,
injectCliTransportEnv,
TOOL_CLI_CHAT_SESSION_ENV_VAR,
} = require("./cliChatSession.cjs");
const { TOOL_CLI_DISCOVERY_ENV_VAR } = require("./discoveryPath.cjs");
test("bindChatSessionId reads host env", () => {
assert.equal(
bindChatSessionId({ [TOOL_CLI_CHAT_SESSION_ENV_VAR]: " env-id " }),
"env-id",
);
});
test("bindChatSessionId ignores argv-shaped objects and only reads env", () => {
assert.equal(
bindChatSessionId({
[TOOL_CLI_CHAT_SESSION_ENV_VAR]: "env-id",
chatSessionId: "flag-id",
}),
"env-id",
);
});
test("bindChatSessionId returns null when env is blank", () => {
assert.equal(bindChatSessionId({ [TOOL_CLI_CHAT_SESSION_ENV_VAR]: " " }), null);
});
test("isRemovedChatSessionFlag matches the dropped flag forms", () => {
assert.equal(isRemovedChatSessionFlag("--chat-session"), true);
assert.equal(isRemovedChatSessionFlag("--chat-session=chat-1"), true);
assert.equal(isRemovedChatSessionFlag("--session"), false);
});
test("describeMissingChatSession names the env var and not a flag", () => {
assert.match(describeMissingChatSession("env"), /NETCATTY_CLI_CHAT_SESSION_ID/);
assert.doesNotMatch(describeMissingChatSession("env"), /--chat-session/);
});
test("describeRemovedChatSessionFlag points at the host env var", () => {
assert.match(describeRemovedChatSessionFlag(), /NETCATTY_CLI_CHAT_SESSION_ID/);
});
test("injectCliTransportEnv writes discovery path and trusted chat session", () => {
const env = injectCliTransportEnv(
{ PATH: "/usr/bin", [TOOL_CLI_CHAT_SESSION_ENV_VAR]: "spoof" },
{ discoveryFilePath: "/tmp/discovery.json", chatSessionId: " ai_123 " },
);
assert.equal(env.PATH, "/usr/bin");
assert.equal(env[TOOL_CLI_DISCOVERY_ENV_VAR], "/tmp/discovery.json");
assert.equal(env[TOOL_CLI_CHAT_SESSION_ENV_VAR], "ai_123");
});
test("injectCliTransportEnv strips spoofed chat session when host id is absent", () => {
const env = injectCliTransportEnv(
{ [TOOL_CLI_CHAT_SESSION_ENV_VAR]: "spoof" },
{ discoveryFilePath: "/tmp/discovery.json" },
);
assert.equal(env[TOOL_CLI_CHAT_SESSION_ENV_VAR], undefined);
assert.equal(env[TOOL_CLI_DISCOVERY_ENV_VAR], "/tmp/discovery.json");
});