// The MCP page's pure logic: credential kinds, the nagging condition, the // copy lines, the TUI's command-line rules, and the exact PATCH bodies. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; const source = await readFile( new URL('../src/lib/settings/mcp.ts', import.meta.url), 'utf8', ); const code = stripTypeScriptTypes(source, { mode: 'transform' }); const moduleURL = `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`; const { credential, oauthCapable, needsSignIn, tone, chip, summary, authLine, splitCommand, joinCommand, nameFromUrl, nameFromCommand, draftFor, problems, patchFor, sameDefinition, CALLBACK, } = await import(moduleURL); const server = (over = {}) => ({ name: 'test', enabled: true, transport: 'http', command: null, arguments: [], cwd: null, url: 'https://mcp.example/mcp', environment: {}, headers: {}, bearer_token_env_var: null, enabled_tools: null, disabled_tools: [], startup_timeout_ms: 20000, call_timeout_ms: 60000, secret_presence: { bearer_token: false, environment: [], headers: [] }, auth: { state: 'none', account: null, expires_at: null, scope: null, step_up: null, reason: null, client: null, }, ...over, }); // --- credential kinds --- test('credential: plain http is oauth', () => { assert.deepEqual(credential(server()), { kind: 'oauth' }); }); test('credential: stored bearer token', () => { const s = server({ secret_presence: { bearer_token: true, environment: [], headers: [] }, }); assert.deepEqual(credential(s), { kind: 'bearer' }); }); test('credential: bearer token from an env var', () => { const s = server({ bearer_token_env_var: 'LINEAR_TOKEN' }); assert.deepEqual(credential(s), { kind: 'bearer-env', variable: 'LINEAR_TOKEN', }); }); test('credential: a public Authorization header switches oauth off', () => { const s = server({ headers: { Authorization: { source: 'literal', value: 'Bearer x' } }, }); assert.deepEqual(credential(s), { kind: 'auth-header' }); }); test('credential: a stored Authorization header too, case-insensitively', () => { const s = server({ secret_presence: { bearer_token: false, environment: [], headers: ['aUthorization'], }, }); assert.deepEqual(credential(s), { kind: 'auth-header' }); }); test('credential: stdio is a process', () => { const s = server({ transport: 'stdio', command: '/bin/thing', url: null }); assert.deepEqual(credential(s), { kind: 'process' }); }); test('oauthCapable: http without credentials only', () => { assert.equal(oauthCapable(server()), true); assert.equal(oauthCapable(server({ bearer_token_env_var: 'T' })), false); assert.equal( oauthCapable(server({ transport: 'stdio', command: '/bin/x', url: null })), false, ); }); // --- needsSignIn across states --- test('needsSignIn: enabled × state × step_up', () => { for (const state of ['none', 'needs_sign_in', 'signed_in', 'signed_out']) { assert.equal( needsSignIn(server({ auth: { state } })), state === 'needs_sign_in', state, ); assert.equal( needsSignIn(server({ auth: { state, step_up: 'write:all' } })), true, `${state} + step_up`, ); } assert.equal( needsSignIn(server({ enabled: false, auth: { state: 'needs_sign_in' } })), false, ); assert.equal( needsSignIn( server({ bearer_token_env_var: 'T', auth: { state: 'needs_sign_in' } }), ), false, ); }); // --- tone, chip, summary --- test('tone covers off, attention, ok, idle', () => { assert.equal(tone(server({ enabled: false })), 'off'); assert.equal(tone(server({ auth: { state: 'needs_sign_in' } })), 'attention'); assert.equal(tone(server({ auth: { state: 'signed_in' } })), 'ok'); assert.equal(tone(server()), 'idle'); }); test('chip precedence', () => { const s = server({ auth: { state: 'needs_sign_in' } }); assert.equal(chip(s, true), 'signing in'); assert.equal(chip(s, false), 'needs sign-in'); assert.equal( chip(server({ auth: { state: 'signed_in', step_up: 'w' } }), false), 'needs wider access', ); assert.equal( chip(server({ auth: { state: 'signed_in' } }), false), 'signed in', ); assert.equal( chip(server({ auth: { state: 'signed_out' } }), false), 'signed out', ); assert.equal(chip(server({ enabled: false }), false), 'off'); assert.equal(chip(server(), false), 'on'); // a bearer server shows no auth chip even with a leftover record assert.equal( chip( server({ bearer_token_env_var: 'T', auth: { state: 'needs_sign_in' }, }), false, ), 'on', ); }); test('summary: http url, stdio command line, cwd', () => { assert.equal(summary(server()), 'http · https://mcp.example/mcp'); assert.equal( summary( server({ transport: 'stdio', url: null, command: '/usr/bin/env', arguments: ['python3', 'bridge.py'], cwd: '/home/x', }), ), 'stdio · /usr/bin/env python3 bridge.py · in /home/x', ); }); test('authLine: each oauth state', () => { assert.equal( authLine(server()), 'No sign-in asked for. If the server asks, it shows here.', ); assert.equal( authLine( server({ auth: { state: 'needs_sign_in', reason: 'token expired' } }), ), 'Needs sign-in · token expired', ); assert.equal( authLine(server({ auth: { state: 'signed_in', account: 'a@b.c' } })), 'Signed in as a@b.c', ); assert.equal( authLine(server({ auth: { state: 'signed_out' } })), 'Signed out · sign in to use its tools again', ); // non-OAuth http servers name their credential, never a leftover sign-in assert.equal( authLine( server({ bearer_token_env_var: 'T', auth: { state: 'signed_in' } }), ), "Uses a bearer token from $T on the daemon's machine", ); assert.equal( authLine( server({ secret_presence: { bearer_token: true, environment: [], headers: [] }, }), ), 'Uses a saved bearer token', ); assert.equal( authLine( server({ secret_presence: { bearer_token: false, environment: [], headers: ['Authorization'], }, }), ), 'Sends its own Authorization header', ); // stdio says nothing assert.equal( authLine(server({ transport: 'stdio', command: '/bin/x', url: null })), null, ); }); test('authLine: token renewal formats across midnight', () => { const now = new Date('2026-11-12T15:00:00'); const today = new Date('2026-11-12T21:14:00'); const other = new Date('2026-11-09T09:00:00'); assert.equal( authLine( server({ auth: { state: 'signed_in', expires_at: today.toISOString() } }), now, ), 'Signed in · token renews 21:14', ); const line = authLine( server({ auth: { state: 'signed_in', expires_at: other.toISOString() } }), now, ); assert.match(line, /token renews (Nov 9|9 Nov) 09:00/); }); // --- command lines (the TUI's rules) --- test('splitCommand: quotes and escapes', () => { assert.deepEqual(splitCommand('npx -y @x/server-thing'), [ 'npx', '-y', '@x/server-thing', ]); assert.deepEqual(splitCommand('sh -c \'a b\' "c d"'), [ 'sh', '-c', 'a b', 'c d', ]); assert.deepEqual(splitCommand('a "b\'c" d\\\\e'), ['a', "b'c", 'd\\e']); assert.throws(() => splitCommand('a "b'), /unterminated quote/); assert.throws(() => splitCommand("a 'b"), /unterminated quote/); assert.deepEqual(splitCommand(' '), []); }); test('joinCommand quotes only when needed, and round-trips', () => { assert.equal(joinCommand('/bin/x', []), '/bin/x'); assert.equal(joinCommand('/bin/x', ['a b']), "/bin/x 'a b'"); assert.equal(joinCommand('/a b/x', []), "'/a b/x'"); assert.equal(joinCommand('x', ["it's"]), "x 'it'\\''s'"); const line = 'python3 \'/a b/c.py\' --tag "two words"'; assert.deepEqual( splitCommand(line).flatMap((a) => [a]), ['python3', '/a b/c.py', '--tag', 'two words'], ); assert.equal( joinCommand(...[line.split(' ')[0], splitCommand(line).slice(1)]).length > 0, true, ); assert.deepEqual( splitCommand(joinCommand('python3', ['/a b/c.py', '--tag', 'two words'])), ['python3', '/a b/c.py', '--tag', 'two words'], ); }); test('nameFromUrl strips mcp/api/www and handles IPs', () => { assert.equal(nameFromUrl('https://mcp.linear.app/sse'), 'linear'); assert.equal(nameFromUrl('https://api.github.com/mcp'), 'github'); assert.equal(nameFromUrl('http://192.168.1.4:3000/mcp'), 'mcp-192-168-1-4'); assert.equal(nameFromUrl('not a url'), ''); }); test('nameFromCommand finds the package', () => { assert.equal( nameFromCommand(['npx', '-y', '@modelcontextprotocol/server-filesystem']), 'filesystem', ); assert.equal(nameFromCommand(['uvx', 'mcp-server-git']), 'git'); assert.equal( nameFromCommand(['/usr/bin/python3', '/x/bridge.py']), 'bridge-py', ); assert.equal(nameFromCommand([]), ''); }); // --- draft + problems --- test('draftFor: blank http draft with suggestions untouched', () => { const d = draftFor(); assert.equal(d.transport, 'http'); assert.equal(d.signIn, 'oauth'); assert.equal(d.nameTouched, false); }); test('problems: name rules', () => { const d = { ...draftFor(), url: 'https://x.example/mcp', name: 'bad name!' }; assert.match(problems(d, []).name, /letters, digits/); assert.match( problems({ ...d, name: 'thing' }, ['thing']).name, /already exists/, ); assert.equal(problems({ ...d, name: 'thing' }, []).name, undefined); }); test('problems: address rules', () => { const base = draftFor(); assert.match(problems({ ...base }, []).url, /Enter an address/); assert.match( problems({ ...base, url: 'ftp://x/' }, []).url, /Enter an address/, ); assert.match( problems({ ...base, url: 'https://me:pw@x.example/' }, []).url, /username/, ); assert.match( problems({ ...base, url: 'https://x.example/#frag' }, []).url, /fragment/, ); assert.equal( problems({ ...base, url: 'https://x.example/mcp' }, []).url, undefined, ); }); test('problems: command, token, headers, timeouts', () => { const stdio = { ...draftFor(), transport: 'stdio', name: 'x' }; assert.match(problems(stdio, []).command, /Enter the command/); assert.match(problems({ ...stdio, command: "a 'b" }, []).command, /quotes/); const http = { ...draftFor(), url: 'https://x/', name: 'x', signIn: 'bearer', }; assert.match(problems(http, []).token, /Enter the token/); assert.equal(problems({ ...http, tokenStored: true }, []).token, undefined); const badHeader = { ...draftFor(), url: 'https://x/', name: 'x', headers: [{ name: 'bad header', value: 'v', kept: 'plain', stored: false }], }; assert.match(problems(badHeader, []).headers, /not a valid header/); const dup = { ...draftFor(), url: 'https://x/', name: 'x', headers: [ { name: 'X-Key', value: 'a', kept: 'plain', stored: false }, { name: 'x-key', value: 'b', kept: 'plain', stored: false }, ], }; assert.match(problems(dup, []).headers, /appears twice/); const timeout = { ...draftFor(), url: 'https://x/', name: 'x', startupSeconds: '9999', }; assert.match(problems(timeout, []).startupSeconds, /1–300/); }); // --- patchFor --- test('patchFor: a new http oauth server sends the whole public shape', () => { const d = { ...draftFor(), name: 'linear', url: 'https://mcp.linear.app/sse', }; const patch = patchFor(d); assert.deepEqual(patch, { enabled: true, transport: 'http', url: 'https://mcp.linear.app/sse', command: null, arguments: [], cwd: null, bearer_token_env_var: null, headers: {}, environment: null, enabled_tools: null, disabled_tools: [], }); }); test('patchFor: a new stdio server', () => { const d = { ...draftFor(), transport: 'stdio', name: 'files', command: 'npx -y @x/server-files /tmp', }; const patch = patchFor(d); assert.equal(patch.command, 'npx'); assert.deepEqual(patch.arguments, ['-y', '@x/server-files', '/tmp']); assert.equal(patch.url, null); assert.equal(patch.headers, null); }); test('patchFor: edit keeps a stored token when blank, replaces when typed, removes when asked', () => { const base = server({ secret_presence: { bearer_token: true, environment: [], headers: [] }, auth: { state: 'signed_in', account: 'a', expires_at: null, scope: null, step_up: null, reason: null, client: null, }, }); const d = { ...draftFor(base), signIn: 'bearer', name: 'test' }; assert.equal(patchFor(d, base).secrets, undefined); assert.equal( patchFor({ ...d, token: 'new' }, base).secrets.bearer_token, 'new', ); assert.equal( patchFor({ ...d, removeToken: true }, base).secrets.bearer_token, null, ); // exactly what [remove saved token] does to the draft (McpServerForm) const pressed = { ...d, removeToken: true, tokenStored: false }; assert.deepEqual(problems(pressed, ['test'], base), {}); assert.equal(patchFor(pressed, base).secrets.bearer_token, null); }); test('patchFor: moving off a stored token removes it', () => { const base = server({ secret_presence: { bearer_token: true, environment: [], headers: [] }, }); const d = { ...draftFor(base), signIn: 'oauth', name: 'test' }; assert.equal(patchFor(d, base).secrets.bearer_token, null); assert.equal(patchFor(d, base).bearer_token_env_var, null); }); test('patchFor: switching to an env var also drops the stored token', () => { const base = server({ secret_presence: { bearer_token: true, environment: [], headers: [] }, }); const d = { ...draftFor(base), signIn: 'bearer-env', envVariable: 'TOKEN', name: 'test', }; const patch = patchFor(d, base); assert.equal(patch.bearer_token_env_var, 'TOKEN'); assert.equal(patch.secrets.bearer_token, null); }); test('patchFor: header moves plain→secret and secret→plain', () => { const base = server({ headers: { 'X-Key': { source: 'literal', value: 'plain-value' } }, }); const d = { ...draftFor(base), name: 'test', headers: [ { name: 'X-Key', value: 'secret-value', kept: 'secret', stored: false }, ], }; const patch = patchFor(d, base); assert.equal(patch.headers['X-Key'], undefined); assert.equal(patch.secrets.headers['X-Key'], 'secret-value'); const stored = server({ secret_presence: { bearer_token: false, environment: [], headers: ['X-Key'], }, }); const d2 = { ...draftFor(stored), name: 'test', headers: [ { name: 'X-Key', value: 'now-plain', kept: 'plain', stored: false }, ], }; const patch2 = patchFor(d2, stored); assert.deepEqual(patch2.headers['X-Key'], { source: 'literal', value: 'now-plain', }); assert.equal(patch2.secrets.headers['X-Key'], null); }); test('patchFor: removing a public header sends null; removing a stored one too', () => { const base = server({ headers: { 'X-A': { source: 'literal', value: '1' } }, }); const d = { ...draftFor(base), name: 'test', headers: [] }; assert.equal(patchFor(d, base).headers['X-A'], null); const stored = server({ headers: { 'X-A': { source: 'literal', value: '1' } }, secret_presence: { bearer_token: false, environment: [], headers: ['X-B'] }, }); const d2 = { ...draftFor(stored), name: 'test', headers: [] }; const patch = patchFor(d2, stored); assert.equal(patch.headers['X-A'], null); assert.equal(patch.secrets.headers['X-B'], null); }); test('patchFor: an edited url on a signed-in server signs it out', () => { const base = server({ auth: { state: 'signed_in', account: 'a', expires_at: null, scope: null, step_up: null, reason: null, client: null, }, }); const d = { ...draftFor(base), name: 'test', url: 'https://other.example/mcp', }; assert.equal(patchFor(d, base).secrets.oauth, null); // same url: no sign-out const same = patchFor({ ...draftFor(base), name: 'test' }, base); assert.equal(same.secrets?.oauth, undefined); }); test('patchFor: tools and timeouts', () => { const d = { ...draftFor(), name: 'x', url: 'https://x/', tools: 'only', toolList: 'a, b', startupSeconds: '30', callSeconds: '90', }; const patch = patchFor(d); assert.deepEqual(patch.enabled_tools, ['a', 'b']); assert.equal(patch.startup_timeout_ms, 30000); assert.equal(patch.call_timeout_ms, 90000); const except = patchFor({ ...d, tools: 'except' }, undefined); assert.deepEqual(except.disabled_tools, ['a', 'b']); assert.equal(except.enabled_tools, null); }); test('patchFor: an untouched edit leaves timeouts alone; clearing restores the default', () => { const base = server({ startup_timeout_ms: 1500, call_timeout_ms: 90000 }); const untouched = patchFor(draftFor(base), base); assert.equal( 'startup_timeout_ms' in untouched, false, '1500 ms is not rewritten as 2000', ); assert.equal('call_timeout_ms' in untouched, false); const cleared = patchFor( { ...draftFor(base), startupSeconds: '', callSeconds: '' }, base, ); assert.equal(cleared.startup_timeout_ms, 20000); assert.equal(cleared.call_timeout_ms, 60000); assert.equal( patchFor({ ...draftFor(base), callSeconds: '120' }, base).call_timeout_ms, 120000, ); // an add with empty fields sends nothing (the daemon's defaults) const fresh = patchFor({ ...draftFor(), name: 'x', url: 'https://x/' }); assert.equal('startup_timeout_ms' in fresh, false); }); test('patchFor: a client id typed and then hidden (not oauth) is not sent', () => { const d = { ...draftFor(), name: 'x', url: 'https://x/', clientId: 'cid', clientSecret: 's', }; assert.equal(patchFor(d).secrets.oauth_client.client_id, 'cid'); assert.equal( patchFor({ ...d, signIn: 'bearer', token: 't' }).secrets.oauth_client, undefined, ); }); test('sameDefinition ignores auth only', () => { const a = server(); const b = server({ auth: { state: 'signed_in', account: 'x', expires_at: null, scope: null, step_up: null, reason: null, client: null, }, }); assert.equal(sameDefinition(a, b), true); assert.equal(sameDefinition(a, server({ url: 'https://other/' })), false); }); test('callback address is the documented one', () => { assert.equal(CALLBACK, 'http://127.0.0.1:33418/callback'); });