mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-22 16:10:50 +00:00
AI 通过 MCP host_open 打开会话时会新建标签并抢占当前焦点,打断用户 正在查看的其他标签。新增"AI 会话静默运行"开关(默认关闭),开启后 host_open 打开的会话不出现在标签栏、不被自动激活,在后台继续正常 运行,可通过 terminal_execute/sftp_*/session_close 等工具操作, 应用重启后也不会被恢复为标签。用户可通过托盘面板查看这些隐藏会话 (带 AI 徽标),外部 MCP 客户端的会话列表不受影响。 Closes #2340 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
115 lines
4.3 KiB
TypeScript
115 lines
4.3 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { describe, it } from 'node:test';
|
|
import {
|
|
createExternalMcpStartupSyncPlan,
|
|
normalizeExternalMcpIdleTimeoutMinutes,
|
|
normalizeExternalMcpMode,
|
|
normalizeSessionIdleTimeoutMinutes,
|
|
readExternalMcpFocusOnHostOpen,
|
|
readExternalMcpSilentSessions,
|
|
shouldStartExternalMcpOnStartup,
|
|
writeExternalMcpFocusOnHostOpen,
|
|
writeExternalMcpSilentSessions,
|
|
} from './useExternalMcpToggleState.ts';
|
|
|
|
function installMemoryLocalStorage() {
|
|
const previousLocalStorage = Object.getOwnPropertyDescriptor(globalThis, 'localStorage');
|
|
const previousDispatchEvent = Object.getOwnPropertyDescriptor(globalThis, 'dispatchEvent');
|
|
const backing = new Map<string, string>();
|
|
|
|
const storage: Storage = {
|
|
get length() { return backing.size; },
|
|
clear() { backing.clear(); },
|
|
getItem(key: string) { return backing.get(key) ?? null; },
|
|
key(index: number) { return Array.from(backing.keys())[index] ?? null; },
|
|
removeItem(key: string) { backing.delete(key); },
|
|
setItem(key: string, value: string) { backing.set(key, value); },
|
|
};
|
|
|
|
Object.defineProperty(globalThis, 'localStorage', { value: storage, configurable: true });
|
|
Object.defineProperty(globalThis, 'dispatchEvent', { value: () => true, configurable: true });
|
|
|
|
return () => {
|
|
if (previousLocalStorage) {
|
|
Object.defineProperty(globalThis, 'localStorage', previousLocalStorage);
|
|
} else {
|
|
Reflect.deleteProperty(globalThis, 'localStorage');
|
|
}
|
|
if (previousDispatchEvent) {
|
|
Object.defineProperty(globalThis, 'dispatchEvent', previousDispatchEvent);
|
|
} else {
|
|
Reflect.deleteProperty(globalThis, 'dispatchEvent');
|
|
}
|
|
};
|
|
}
|
|
|
|
describe('useExternalMcpToggleState helpers', () => {
|
|
it('normalizes mode and idle timeout', () => {
|
|
assert.equal(normalizeExternalMcpMode('persistent'), 'persistent');
|
|
assert.equal(normalizeExternalMcpMode('other'), 'temporary');
|
|
assert.equal(normalizeExternalMcpIdleTimeoutMinutes(null), 10);
|
|
assert.equal(normalizeExternalMcpIdleTimeoutMinutes(0), 1);
|
|
assert.equal(normalizeExternalMcpIdleTimeoutMinutes(99999), 24 * 60);
|
|
assert.equal(normalizeSessionIdleTimeoutMinutes(null), 30);
|
|
assert.equal(normalizeSessionIdleTimeoutMinutes(0), 1);
|
|
});
|
|
|
|
it('only starts on launch for persistent+enabled', () => {
|
|
assert.equal(shouldStartExternalMcpOnStartup({ enabled: true, mode: 'persistent' }), true);
|
|
assert.equal(shouldStartExternalMcpOnStartup({ enabled: true, mode: 'temporary' }), false);
|
|
assert.equal(shouldStartExternalMcpOnStartup({ enabled: false, mode: 'persistent' }), false);
|
|
});
|
|
|
|
it('startup sync plan clears temporary stored enabled', () => {
|
|
const plan = createExternalMcpStartupSyncPlan({
|
|
enabled: true,
|
|
mode: 'temporary',
|
|
idleTimeoutMinutes: 15,
|
|
sessionIdleTimeoutMinutes: 30,
|
|
});
|
|
assert.equal(plan.runtimeEnabled, false);
|
|
assert.equal(plan.storedEnabled, false);
|
|
assert.equal(plan.shouldPersistStoredEnabled, true);
|
|
assert.equal(plan.config.idleTimeoutMinutes, 15);
|
|
assert.equal(plan.config.sessionIdleTimeoutMinutes, 30);
|
|
});
|
|
|
|
it('startup sync plan keeps persistent enabled', () => {
|
|
const plan = createExternalMcpStartupSyncPlan({
|
|
enabled: true,
|
|
mode: 'persistent',
|
|
idleTimeoutMinutes: 20,
|
|
sessionIdleTimeoutMinutes: 45,
|
|
});
|
|
assert.equal(plan.runtimeEnabled, true);
|
|
assert.equal(plan.storedEnabled, true);
|
|
assert.equal(plan.shouldPersistStoredEnabled, false);
|
|
});
|
|
|
|
it('focus-on-host-open defaults to true and round-trips through storage', () => {
|
|
const restore = installMemoryLocalStorage();
|
|
try {
|
|
assert.equal(readExternalMcpFocusOnHostOpen(), true);
|
|
writeExternalMcpFocusOnHostOpen(false);
|
|
assert.equal(readExternalMcpFocusOnHostOpen(), false);
|
|
writeExternalMcpFocusOnHostOpen(true);
|
|
assert.equal(readExternalMcpFocusOnHostOpen(), true);
|
|
} finally {
|
|
restore();
|
|
}
|
|
});
|
|
|
|
it('silent-sessions defaults to false and round-trips through storage', () => {
|
|
const restore = installMemoryLocalStorage();
|
|
try {
|
|
assert.equal(readExternalMcpSilentSessions(), false);
|
|
writeExternalMcpSilentSessions(true);
|
|
assert.equal(readExternalMcpSilentSessions(), true);
|
|
writeExternalMcpSilentSessions(false);
|
|
assert.equal(readExternalMcpSilentSessions(), false);
|
|
} finally {
|
|
restore();
|
|
}
|
|
});
|
|
});
|