// Checks for `mcp.js`: the binary ladder, the provider registration and its // guard on a VS Code without the MCP API, the copy-config snippets, and the // read-only mode (`lucisv.mcp.mode`). // // 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'); // VS Code answers a section prefix of the changed key: `lucisv.mcp.mode` affects `lucisv.mcp`. configListener({ affectsConfiguration: (section) => 'lucisv.mcp.mode'.startsWith(section) }); assert.equal(emitter.fired, 3, 'a change of lucisv.mcp.mode fires'); } function readOnlyModeDefinitions() { const contributed = require('./package.json').contributes.configuration.properties['lucisv.mcp.mode']; assert.equal(contributed.default, 'full'); assert.deepEqual(contributed.enum, ['full', 'read-only']); reset(); folders = [folder('alpha'), folder('beta')]; settings['mcp.mode'] = 'read-only'; mcp.activate(context(), { bundledBinary: bundled(true) }); const provider = registered[0].provider; const definitions = provider.provideMcpServerDefinitions(); assert.deepEqual(definitions.map((d) => d.label), ['lucisv read-only (alpha)', 'lucisv read-only (beta)']); assert.deepEqual(definitions[0].args, ['--root', '/work/alpha', '--index-cache', '/storage/ws', '--read-only']); settings['mcp.mode'] = 'full'; const full = provider.provideMcpServerDefinitions()[0]; assert.equal(full.label, 'lucisv (alpha)', 'back to full: the plain label'); assert.ok(!full.args.includes('--read-only'), 'back to full: no flag'); } function anUnknownModeFailsSafeToReadOnly() { reset(); const log = []; mcp.activate(context(), { bundledBinary: bundled(true), outputChannel: { appendLine: (l) => log.push(l) } }); const provider = registered[0].provider; const first = () => provider.provideMcpServerDefinitions()[0]; // Unset and exactly "full": full mode, nothing logged. for (const value of [undefined, 'full']) { settings['mcp.mode'] = value; assert.equal(first().label, 'lucisv (alpha)', `${value}: full`); assert.ok(!first().args.includes('--read-only'), `${value}: no flag`); } // Exactly "read-only": read-only, nothing logged. settings['mcp.mode'] = 'read-only'; assert.equal(first().label, 'lucisv read-only (alpha)'); assert.deepEqual(log, [], 'a valid setting logs nothing'); // Anything else: read-only, one line naming the value. for (const value of ['Read-Only', 'readonly', 'Full', null, true, 3]) { settings['mcp.mode'] = value; log.length = 0; const definition = first(); assert.equal(definition.label, 'lucisv read-only (alpha)', `${value}: read-only`); assert.equal(definition.args[definition.args.length - 1], '--read-only', `${value}: flag`); assert.deepEqual(log, [`lucisv: lucisv.mcp.mode is "${String(value)}", not "full" or "read-only"; using read-only.`]); } } 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`); assert.ok(informed[0].includes('(full mode)'), `${label}: names the mode`); } 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 theReadOnlySnippets() { const args = ['--root', '/work/alpha', '--read-only']; const want = { 'Claude Code': { mcpServers: { 'lucisv-ro': { command: '/ext/bin/lucisv-mcp', args } } }, Cursor: { mcpServers: { 'lucisv-ro': { command: '/ext/bin/lucisv-mcp', args } } }, 'VS Code': { servers: { 'lucisv-ro': { type: 'stdio', command: '/ext/bin/lucisv-mcp', args } } }, }; for (const [label, json] of Object.entries(want)) { reset(); settings['mcp.mode'] = 'read-only'; pickLabel = label; mcp.activate(context(), { bundledBinary: bundled(true) }); await commands.get('lucisv.mcp.copyConfig')(); assert.equal(copied[0], JSON.stringify(json, null, 2), `${label}: exact read-only JSON`); assert.ok(informed[0].includes('(read-only mode)'), `${label}: names the mode`); } // An unknown value copies the read-only server too, and says why. reset(); settings['mcp.mode'] = 'readonly'; pickLabel = 'Claude Code'; const log = []; mcp.activate(context(), { bundledBinary: bundled(true), outputChannel: { appendLine: (l) => log.push(l) } }); await commands.get('lucisv.mcp.copyConfig')(); assert.equal(copied[0], JSON.stringify(want['Claude Code'], null, 2), 'readonly: the lucisv-ro snippet'); assert.ok(informed[0].includes('(read-only mode)')); assert.deepEqual(log, ['lucisv: lucisv.mcp.mode is "readonly", not "full" or "read-only"; using read-only.']); // Two keys: the full and the read-only server fit in one config file. const host = mcp.HOSTS[0]; const full = JSON.parse(mcp.configSnippet(host, 'b', ['/r'])); const readOnly = JSON.parse(mcp.configSnippet(host, 'b', ['/r'], true)); const both = { mcpServers: { ...full.mcpServers, ...readOnly.mcpServers } }; assert.deepEqual(Object.keys(both.mcpServers), ['lucisv', 'lucisv-ro']); } async function run() { theLadder(); oneDefinitionPerFolder(); changesAreAnnounced(); readOnlyModeDefinitions(); anUnknownModeFailsSafeToReadOnly(); disabledOffersNothing(); anOldVsCodeRegistersNothing(); aMissingBinaryIsNotStarted(); await theThreeSnippets(); await theReadOnlySnippets(); console.log('mcp: OK — binary ladder, one definition per folder, change events, read-only definitions, unknown mode fails safe to read-only, enabled:false, old-VS Code guard, missing binary, three config snippets in both modes'); } run().catch((error) => { console.error(`mcp: FAIL — ${error.stack || error}`); process.exitCode = 1; });