scribase-cli/tests/connect.test.ts
caelum0x 92f7df83c4 scribase CLI + TypeScript SDK (public edge repo)
Extracted from the private caelum0x/scribase monorepo's
packages/scribase-js with a fresh, single-commit history (no core
history leaks). The npx scribase CLI and the typed /v1 API client,
unchanged except: prepublishOnly simplified (the monorepo-only tarball
gitleaks gate dropped), repository/homepage/bugs fields added, and one
test that cross-checked embedded template strings against the
monorepo's templates/ directory removed (nothing it tested changed;
that directory isn't shipped here).
2026-11-12 18:10:58 +03:00

303 lines
14 KiB
TypeScript

/**
* Tests for `npx scribase connect`: detection, idempotent JSON/TOML merges,
* the Claude Code CLI path, --dry-run, --tool filtering, and that no token or
* secret is ever written to any config file.
*/
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { connectMain, formatConnect, mergeJsonServer, mergeTomlSection, parseConnectArgs, runConnect } from '../src/cli/connect.js';
import type { ConnectIo, RunOutcome } from '../src/cli/connect.js';
import { InitError } from '../src/cli/init.js';
const HOME = '/home/dev';
const CWD = '/work/app';
function harness(options: {
files?: Record<string, string>;
which?: Record<string, boolean>;
exists?: Record<string, boolean>;
run?: (command: string, args: string[]) => RunOutcome;
}) {
const files: Record<string, string> = { ...(options.files ?? {}) };
const runCalls: { command: string; args: string[] }[] = [];
const io: ConnectIo = {
cwd: CWD,
home: HOME,
env: {},
readFile: (path) => files[path] ?? null,
writeFile: (path, contents) => {
files[path] = contents;
},
exists: (path) => options.exists?.[path] ?? false,
which: (command) => options.which?.[command] ?? false,
run: (command, args) => {
runCalls.push({ command, args });
return options.run?.(command, args) ?? { ok: false, output: 'not found' };
},
};
return { io, files, runCalls };
}
describe('parseConnectArgs', () => {
it('defaults to auto-detect (tools: null)', () => {
assert.deepEqual(parseConnectArgs([]), { json: false, dryRun: false, tools: null });
});
it('reads --dry-run and --json', () => {
const options = parseConnectArgs(['--dry-run', '--json']);
assert.equal(options.dryRun, true);
assert.equal(options.json, true);
});
it('accepts positional tool names and --tool, deduped, with aliases', () => {
const options = parseConnectArgs(['cursor', '--tool', 'claude', '--tool=vs-code', 'cursor']);
assert.deepEqual(options.tools, ['cursor', 'claude-code', 'vscode']);
});
it('rejects an unknown tool name', () => {
assert.throws(() => parseConnectArgs(['not-a-real-tool']), InitError);
});
it('rejects an unknown flag', () => {
assert.throws(() => parseConnectArgs(['--nope']), InitError);
});
it('reads --api-url', () => {
assert.equal(parseConnectArgs(['--api-url', 'https://api.example.test']).apiUrl, 'https://api.example.test');
});
});
describe('mergeJsonServer', () => {
it('creates a fresh file when none exists', () => {
const result = mergeJsonServer(null, '/x/mcp.json', 'mcpServers', 'scribase', { url: 'https://api.scribase.com/mcp' });
assert.equal(result.changed, true);
assert.deepEqual(JSON.parse(result.content), { mcpServers: { scribase: { url: 'https://api.scribase.com/mcp' } } });
});
it('preserves every other key and every other server', () => {
const existing = JSON.stringify({ mcpServers: { other: { url: 'https://other.example/mcp' } }, unrelatedTopLevelKey: 42 });
const result = mergeJsonServer(existing, '/x/mcp.json', 'mcpServers', 'scribase', { url: 'https://api.scribase.com/mcp' });
assert.equal(result.changed, true);
assert.deepEqual(JSON.parse(result.content), {
mcpServers: { other: { url: 'https://other.example/mcp' }, scribase: { url: 'https://api.scribase.com/mcp' } },
unrelatedTopLevelKey: 42,
});
});
it('is idempotent: an identical entry already present changes nothing', () => {
const existing = JSON.stringify({ mcpServers: { scribase: { url: 'https://api.scribase.com/mcp' } } });
const result = mergeJsonServer(existing, '/x/mcp.json', 'mcpServers', 'scribase', { url: 'https://api.scribase.com/mcp' });
assert.equal(result.changed, false);
});
it('is order-independent when comparing the existing entry', () => {
const existing = JSON.stringify({ mcpServers: { scribase: { url: 'https://api.scribase.com/mcp', type: 'http' } } });
const result = mergeJsonServer(existing, '/x/mcp.json', 'mcpServers', 'scribase', { type: 'http', url: 'https://api.scribase.com/mcp' });
assert.equal(result.changed, false);
});
it('updates a stale URL for the same entry', () => {
const existing = JSON.stringify({ mcpServers: { scribase: { url: 'https://old.example/mcp' } } });
const result = mergeJsonServer(existing, '/x/mcp.json', 'mcpServers', 'scribase', { url: 'https://api.scribase.com/mcp' });
assert.equal(result.changed, true);
assert.equal((JSON.parse(result.content).mcpServers as Record<string, { url: string }>).scribase.url, 'https://api.scribase.com/mcp');
});
it('refuses to clobber a file it cannot parse', () => {
assert.throws(() => mergeJsonServer('{ not json', '/x/mcp.json', 'mcpServers', 'scribase', { url: 'x' }), InitError);
});
it('never writes a token, header or secret-shaped key', () => {
const result = mergeJsonServer(null, '/x/mcp.json', 'mcpServers', 'scribase', { url: 'https://api.scribase.com/mcp' });
assert.equal(/authorization|bearer|token|scb_agt_/i.test(result.content), false);
});
});
describe('mergeTomlSection', () => {
it('creates a fresh file when none exists', () => {
const result = mergeTomlSection(null, 'mcp_servers.scribase', ['url = "https://api.scribase.com/mcp"', 'auth = "oauth"']);
assert.equal(result.changed, true);
assert.equal(result.content, '[mcp_servers.scribase]\nurl = "https://api.scribase.com/mcp"\nauth = "oauth"\n');
});
it('appends after existing content without touching it', () => {
const existing = '[other_table]\nkey = "value"\n';
const result = mergeTomlSection(existing, 'mcp_servers.scribase', ['url = "https://api.scribase.com/mcp"']);
assert.equal(result.changed, true);
assert.equal(result.content, '[other_table]\nkey = "value"\n\n[mcp_servers.scribase]\nurl = "https://api.scribase.com/mcp"\n');
});
it('is idempotent: the header already present changes nothing', () => {
const existing = '[mcp_servers.scribase]\nurl = "https://old.example/mcp"\n';
const result = mergeTomlSection(existing, 'mcp_servers.scribase', ['url = "https://api.scribase.com/mcp"']);
assert.equal(result.changed, false);
assert.equal(result.content, existing);
});
});
describe('runConnect — detection', () => {
it('auto mode skips every tool with no trace on this machine', () => {
const { io } = harness({});
const result = runConnect({ json: false, dryRun: false, tools: null }, io);
assert.equal(result.tools.every((tool) => tool.status === 'skipped_not_detected'), true);
assert.equal(result.tools.every((tool) => !tool.detected), true);
});
it('auto mode configures only tools actually detected', () => {
const { io, files } = harness({ which: { claude: true }, exists: { [`${HOME}/.cursor`]: true }, run: () => ({ ok: true, output: 'Added' }) });
const result = runConnect({ json: false, dryRun: false, tools: null }, io);
const byTool = Object.fromEntries(result.tools.map((tool) => [tool.tool, tool]));
assert.equal(byTool['claude-code']?.status, 'configured');
assert.equal(byTool.cursor?.status, 'configured');
assert.equal(byTool.vscode?.status, 'skipped_not_detected');
assert.equal(byTool.zed?.status, 'skipped_not_detected');
assert.equal(JSON.parse(files[`${HOME}/.cursor/mcp.json`] ?? '{}').mcpServers.scribase.url, 'https://api.scribase.com/mcp');
});
it('naming a tool configures it even when not detected', () => {
const { io, files } = harness({});
const result = runConnect({ json: false, dryRun: false, tools: ['zed'] }, io);
assert.equal(result.tools.length, 1);
assert.equal(result.tools[0]?.status, 'configured');
assert.equal(JSON.parse(files[`${HOME}/.config/zed/settings.json`] ?? '{}').context_servers.scribase.url, 'https://api.scribase.com/mcp');
});
});
describe('runConnect — per-tool shapes', () => {
it('vscode writes ./.vscode/mcp.json with the "servers" key and type "http"', () => {
const { io, files } = harness({});
runConnect({ json: false, dryRun: false, tools: ['vscode'] }, io);
assert.deepEqual(JSON.parse(files[`${CWD}/.vscode/mcp.json`] ?? '{}'), { servers: { scribase: { type: 'http', url: 'https://api.scribase.com/mcp' } } });
});
it('windsurf writes mcp_config.json with "serverUrl"', () => {
const { io, files } = harness({});
runConnect({ json: false, dryRun: false, tools: ['windsurf'] }, io);
assert.deepEqual(JSON.parse(files[`${HOME}/.codeium/windsurf/mcp_config.json`] ?? '{}'), { mcpServers: { scribase: { serverUrl: 'https://api.scribase.com/mcp' } } });
});
it('codex appends a [mcp_servers.scribase] TOML table with auth = "oauth"', () => {
const { io, files } = harness({});
runConnect({ json: false, dryRun: false, tools: ['codex'] }, io);
assert.equal(files[`${HOME}/.codex/config.toml`], '[mcp_servers.scribase]\nurl = "https://api.scribase.com/mcp"\nauth = "oauth"\n');
});
it('respects a custom --api-url', () => {
const { io, files } = harness({});
runConnect({ json: false, dryRun: false, tools: ['cursor'], apiUrl: 'https://api.example.test' }, io);
assert.equal(JSON.parse(files[`${HOME}/.cursor/mcp.json`] ?? '{}').mcpServers.scribase.url, 'https://api.example.test/mcp');
});
});
describe('runConnect — Claude Code', () => {
it('runs the documented claude mcp add command', () => {
const { io, runCalls } = harness({ run: () => ({ ok: true, output: 'Added stdio MCP server scribase\n' }) });
const result = runConnect({ json: false, dryRun: false, tools: ['claude-code'] }, io);
assert.deepEqual(runCalls[0], { command: 'claude', args: ['mcp', 'add', '--transport', 'http', '--scope', 'user', 'scribase', 'https://api.scribase.com/mcp'] });
assert.equal(result.tools[0]?.status, 'configured');
});
it('treats "already exists" as already_configured, not a failure', () => {
const { io } = harness({ run: () => ({ ok: false, output: 'Error: MCP server "scribase" already exists\n' }) });
const result = runConnect({ json: false, dryRun: false, tools: ['claude-code'] }, io);
assert.equal(result.tools[0]?.status, 'already_configured');
});
it('reports a real failure without crashing the whole run', () => {
const { io } = harness({ run: () => ({ ok: false, output: 'command not found' }) });
const result = runConnect({ json: false, dryRun: false, tools: ['claude-code'] }, io);
assert.equal(result.tools[0]?.status, 'failed');
});
});
describe('runConnect — --dry-run', () => {
it('never calls writeFile or run', () => {
let wrote = false;
let ran = false;
const io: ConnectIo = {
cwd: CWD,
home: HOME,
env: {},
readFile: () => null,
writeFile: () => {
wrote = true;
},
exists: () => false,
which: () => false,
run: () => {
ran = true;
return { ok: true, output: '' };
},
};
const result = runConnect({ json: false, dryRun: true, tools: ['cursor', 'claude-code'] }, io);
assert.equal(wrote, false);
assert.equal(ran, false);
assert.equal(result.tools.every((tool) => tool.status === 'would_configure'), true);
assert.equal(result.dry_run, true);
});
});
describe('runConnect — idempotency across two runs', () => {
it('the second run reports already_configured for every tool and writes nothing new', () => {
const { io, files } = harness({ which: { claude: true }, run: () => ({ ok: true, output: 'Added' }) });
runConnect({ json: false, dryRun: false, tools: ['cursor', 'windsurf', 'zed', 'vscode', 'codex'] }, io);
const snapshot = { ...files };
const second = runConnect({ json: false, dryRun: false, tools: ['cursor', 'windsurf', 'zed', 'vscode', 'codex'] }, io);
assert.equal(second.tools.every((tool) => tool.status === 'already_configured'), true);
assert.deepEqual(files, snapshot);
});
});
describe('formatConnect', () => {
it('prints the MCP url and one line per tool', () => {
const text = formatConnect({
ok: true,
mcp_url: 'https://api.scribase.com/mcp',
dry_run: false,
tools: [
{ tool: 'cursor', label: 'Cursor', detected: true, status: 'configured', path: '/home/dev/.cursor/mcp.json' },
{ tool: 'zed', label: 'Zed', detected: false, status: 'skipped_not_detected' },
],
});
assert.match(text, /https:\/\/api\.scribase\.com\/mcp/);
assert.match(text, /Cursor\s+done/);
assert.match(text, /Zed\s+not detected, skipped/);
});
});
describe('connectMain', () => {
it('prints JSON with --json and exits 0', () => {
const { io } = harness({});
const lines: string[] = [];
const code = connectMain(['zed', '--json'], io, (text) => lines.push(text));
assert.equal(code, 0);
const parsed = JSON.parse(lines.join(''));
assert.equal(parsed.ok, true);
assert.equal(parsed.tools[0].status, 'configured');
});
it('prints help and exits 0 for --help', () => {
const { io } = harness({});
const lines: string[] = [];
const code = connectMain(['--help'], io, (text) => lines.push(text));
assert.equal(code, 0);
assert.match(lines.join(''), /Usage: npx scribase connect/);
});
it('exits 1 and reports a clear error for an invalid argument', () => {
const { io } = harness({});
const lines: string[] = [];
const code = connectMain(['--nope'], io, (text) => lines.push(text));
assert.equal(code, 1);
assert.match(lines.join(''), /unknown option/);
});
it('exits 1 when a named tool fails', () => {
const { io } = harness({ run: () => ({ ok: false, output: 'boom' }) });
const code = connectMain(['claude-code', '--json'], io, () => {});
assert.equal(code, 1);
});
});