// Checks for `mcp.js`: the binary ladder, the provider registration and its // guard on a VS Code without the MCP API, and the copy-config snippets. // // node mcp_test.js // // Exits 0 on success, 1 on any failure. Plain node, no dependencies: a stubbed // `vscode` module is swapped in through `Module._load`, the pattern // `hierarchyView_test.js` uses. See docs/features/mcp-packaging/README.md. const assert = require('assert/strict'); const fs = require('fs'); const Module = require('module'); const os = require('os'); const path = require('path'); /** What the stub's settings answer; each check sets the fields it needs. */ let settings = {}; /** The workspace folders the stub reports. */ let folders = []; let registered = []; let commands = new Map(); let copied = []; let informed = []; let pickLabel; let folderListener; let configListener; class EventEmitter { constructor() { this.fired = 0; this.event = () => ({ dispose() {} }); } fire() { this.fired += 1; } dispose() {} } class McpStdioServerDefinition { constructor(label, command, args, env, version) { Object.assign(this, { label, command, args, env, version }); } } const lm = { registerMcpServerDefinitionProvider(id, provider) { registered.push({ id, provider }); return { dispose() {} }; }, }; const vscode = { EventEmitter, McpStdioServerDefinition, lm, env: { clipboard: { async writeText(text) { copied.push(text); } } }, commands: { registerCommand(id, handler) { commands.set(id, handler); return { dispose() {} }; }, }, window: { async showQuickPick(items) { return items.find((item) => item.label === pickLabel); }, showInformationMessage(text) { informed.push(text); }, }, workspace: { get workspaceFolders() { return folders; }, getConfiguration(section) { assert.equal(section, 'lucisv'); return { get(key) { return settings[key]; }, inspect(key) { return key === 'mcp.path' ? settings.inspectPath : undefined; }, }; }, onDidChangeWorkspaceFolders(handler) { folderListener = handler; return { dispose() {} }; }, onDidChangeConfiguration(handler) { configListener = handler; return { dispose() {} }; }, }, }; const originalLoad = Module._load; Module._load = function load(request, parent, main) { if (request === 'vscode') return vscode; return originalLoad(request, parent, main); }; const mcp = require('./mcp'); Module._load = originalLoad; const { version } = require('./package.json'); function folder(name) { const fsPath = `/work/${name}`; return { name, uri: { fsPath, toString: () => `file://${fsPath}` } }; } function context() { return { subscriptions: [], storageUri: { fsPath: '/storage/ws' }, globalStorageUri: { fsPath: '/storage/global' } }; } /** A `bundledBinary` stand-in that finds `lucisv-mcp` iff `present`. */ function bundled(present) { return (_context, name) => (present ? `/ext/bin/${name}` : undefined); } function reset() { settings = {}; folders = [folder('alpha')]; registered = []; commands = new Map(); copied = []; informed = []; vscode.lm = lm; vscode.McpStdioServerDefinition = McpStdioServerDefinition; } function theLadder() { reset(); settings.inspectPath = { globalValue: '/opt/lucisv-mcp', workspaceValue: ' ' }; assert.equal(mcp.resolveBinary(context(), bundled(true)), '/opt/lucisv-mcp', 'an explicit path wins'); settings.inspectPath = { defaultValue: '/never', globalValue: ' ' }; assert.equal(mcp.resolveBinary(context(), bundled(true)), '/ext/bin/lucisv-mcp', 'blank and default are unset'); assert.equal(mcp.resolveBinary(context(), bundled(false)), 'lucisv-mcp', 'nothing bundled: PATH'); assert.equal(mcp.resolveBinary(context(), undefined), 'lucisv-mcp', 'no helper: PATH'); } function oneDefinitionPerFolder() { reset(); folders = [folder('alpha'), folder('beta')]; const ctx = context(); const log = []; mcp.activate(ctx, { bundledBinary: bundled(true), outputChannel: { appendLine: (l) => log.push(l) } }); assert.equal(registered.length, 1); assert.equal(registered[0].id, 'lucisv.mcp'); assert.deepEqual(log, [], 'nothing to say when everything is present'); const definitions = registered[0].provider.provideMcpServerDefinitions(); assert.deepEqual( definitions.map((d) => ({ ...d, cwd: d.cwd.fsPath })), ['alpha', 'beta'].map((name) => ({ label: `lucisv (${name})`, command: '/ext/bin/lucisv-mcp', args: ['--root', `/work/${name}`, '--index-cache', '/storage/ws'], env: {}, version, cwd: `/work/${name}`, })), ); ctx.storageUri = undefined; assert.deepEqual(registered[0].provider.provideMcpServerDefinitions()[0].args.slice(2), ['--index-cache', '/storage/global']); ctx.globalStorageUri = undefined; assert.deepEqual(registered[0].provider.provideMcpServerDefinitions()[0].args, ['--root', '/work/alpha']); folders = []; assert.deepEqual(registered[0].provider.provideMcpServerDefinitions(), [], 'no folder, no server'); } function changesAreAnnounced() { reset(); const ctx = context(); mcp.activate(ctx, { bundledBinary: bundled(true) }); const emitter = ctx.subscriptions.find((s) => s instanceof EventEmitter); folderListener(); configListener({ affectsConfiguration: (section) => section === 'lucisv.mcp' }); configListener({ affectsConfiguration: () => false }); assert.equal(emitter.fired, 2, 'folder change and lucisv.mcp change fire; other settings do not'); } function disabledOffersNothing() { reset(); settings['mcp.enabled'] = false; const log = []; mcp.activate(context(), { bundledBinary: bundled(true), outputChannel: { appendLine: (l) => log.push(l) } }); assert.deepEqual(registered[0].provider.provideMcpServerDefinitions(), []); assert.equal(log.length, 1, 'one line says why'); settings['mcp.enabled'] = true; assert.equal(registered[0].provider.provideMcpServerDefinitions().length, 1, 'turning it back on needs no reload'); } function anOldVsCodeRegistersNothing() { for (const strip of [() => { delete vscode.lm; }, () => { vscode.lm = {}; }, () => { delete vscode.McpStdioServerDefinition; }]) { reset(); strip(); const log = []; mcp.activate(context(), { outputChannel: { appendLine: (l) => log.push(l) } }); assert.equal(registered.length, 0); assert.equal(log.length, 1, 'one line in the output channel'); assert.ok(commands.has('lucisv.mcp.copyConfig'), 'the copy command still works there'); } reset(); delete vscode.lm; mcp.activate(context(), {}); // no channel: silent, no throw } function aMissingBinaryIsNotStarted() { reset(); const log = []; mcp.activate(context(), { outputChannel: { appendLine: (l) => log.push(l) } }); const provider = registered[0].provider; const here = { command: process.execPath }; assert.equal(provider.resolveMcpServerDefinition(here), here, 'an existing file starts'); const bare = { command: path.basename(process.execPath) }; const pathBefore = process.env.PATH; process.env.PATH = path.dirname(process.execPath); assert.equal(provider.resolveMcpServerDefinition(bare), bare, 'a bare name found on PATH starts'); process.env.PATH = fs.mkdtempSync(path.join(os.tmpdir(), 'lucisv-mcp-test-')); assert.equal(provider.resolveMcpServerDefinition({ command: 'lucisv-mcp' }), undefined); fs.rmdirSync(process.env.PATH); process.env.PATH = pathBefore; assert.equal(provider.resolveMcpServerDefinition({ command: '/nowhere/lucisv-mcp' }), undefined); assert.equal(log.length, 2, 'one line per refused start'); assert.match(log[0], /"lucisv-mcp" not found/); } async function theThreeSnippets() { const want = { 'Claude Code': ['.mcp.json', { mcpServers: { lucisv: { command: '/ext/bin/lucisv-mcp', args: ['--root', '/work/alpha', '--root', '/work/beta'] } } }], Cursor: ['.cursor/mcp.json', { mcpServers: { lucisv: { command: '/ext/bin/lucisv-mcp', args: ['--root', '/work/alpha', '--root', '/work/beta'] } } }], 'VS Code': ['.vscode/mcp.json', { servers: { lucisv: { type: 'stdio', command: '/ext/bin/lucisv-mcp', args: ['--root', '/work/alpha', '--root', '/work/beta'] } } }], }; for (const [label, [file, json]] of Object.entries(want)) { reset(); folders = [folder('alpha'), folder('beta')]; pickLabel = label; mcp.activate(context(), { bundledBinary: bundled(true) }); await commands.get('lucisv.mcp.copyConfig')(); assert.equal(copied.length, 1); assert.equal(copied[0], JSON.stringify(json, null, 2), `${label}: exact JSON`); assert.equal(informed.length, 1); assert.ok(informed[0].includes(file), `${label}: names ${file}`); assert.ok(informed[0].includes('changes when it updates'), `${label}: warns the bundled path moves`); } reset(); settings.inspectPath = { globalValue: '/usr/local/bin/lucisv-mcp' }; pickLabel = 'Claude Code'; mcp.activate(context(), { bundledBinary: bundled(true) }); await commands.get('lucisv.mcp.copyConfig')(); assert.ok(copied[0].includes('"/usr/local/bin/lucisv-mcp"'), 'the setting is what gets copied'); assert.ok(!informed[0].includes('changes when it updates'), 'no warning for a path the user chose'); reset(); pickLabel = undefined; mcp.activate(context(), {}); await commands.get('lucisv.mcp.copyConfig')(); assert.deepEqual(copied, [], 'a dismissed pick copies nothing'); } async function run() { theLadder(); oneDefinitionPerFolder(); changesAreAnnounced(); disabledOffersNothing(); anOldVsCodeRegistersNothing(); aMissingBinaryIsNotStarted(); await theThreeSnippets(); console.log('mcp: OK — binary ladder, one definition per folder, change events, enabled:false, old-VS Code guard, missing binary, three config snippets'); } run().catch((error) => { console.error(`mcp: FAIL — ${error.stack || error}`); process.exitCode = 1; });