import { test } from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { rufloCodexMcpStatus, codexMcpTopology } from '../../src/lib/mcp.mjs'; import { tempDir } from './helpers/temp-dir.mjs'; /** Build a temp $HOME containing (or not) ~/.codex/config.toml with given body. */ function tempHome(configBody) { const home = tempDir('ak-rev'); if (configBody != null) { fs.mkdirSync(path.join(home, '.codex'), { recursive: true }); fs.writeFileSync(path.join(home, '.codex', 'config.toml'), configBody); } return home; } test('rufloCodexMcpStatus detects a registered ruflo MCP server', () => { const home = tempHome('[mcp_servers.ruflo]\ncommand = "ruflo"\nargs = ["mcp", "start"]\n'); const s = rufloCodexMcpStatus({}, { home }); assert.equal(s.registered, true); assert.equal(s.command, 'ruflo'); assert.deepEqual(s.args, ['mcp', 'start']); }); test('rufloCodexMcpStatus reports not-registered when the table is absent', () => { const home = tempHome('[mcp_servers.other]\ncommand = "x"\n'); const s = rufloCodexMcpStatus({}, { home }); assert.equal(s.registered, false); assert.equal(s.command, null); assert.equal(s.args, null); }); test('rufloCodexMcpStatus reports not-registered when config.toml is missing', () => { const home = tempHome(null); // no .codex/config.toml const s = rufloCodexMcpStatus({}, { home }); assert.equal(s.registered, false); }); test('rufloCodexMcpStatus reflects ak ownership from the kit.json marker', () => { const home = tempHome('[mcp_servers.ruflo]\n'); const owned = rufloCodexMcpStatus({ integrations: { ownership: { codex: { reverseMcp: 'ak' } } }, }, { home }); const unowned = rufloCodexMcpStatus({ integrations: {} }, { home }); assert.equal(owned.owned, true); assert.equal(unowned.owned, false); }); test('rufloCodexMcpStatus does not match a similarly-named table', () => { // a table like [mcp_servers.ruflo-extra] must not satisfy the ruflo check const home = tempHome('[mcp_servers.ruflo-extra]\ncommand = "x"\n'); const s = rufloCodexMcpStatus({}, { home }); assert.equal(s.registered, false); }); // Codex (toml_edit) writes `codex mcp add` arguments as a multi-line array. test('rufloCodexMcpStatus reads the multi-line args array codex writes', () => { const home = tempHome('[mcp_servers.ruflo]\ncommand = "ak"\nargs = [\n "x",\n "ruflo-mcp",\n]\n\n[mcp_servers.other]\ncommand = "y"\n'); const s = rufloCodexMcpStatus({}, { home }); assert.equal(s.command, 'ak'); assert.deepEqual(s.args, ['x', 'ruflo-mcp']); }); test('codexMcpTopology reads multi-line args and still classifies exact repair tables', () => { const home = tempHome('[mcp_servers.claude-flow]\ncommand = "ruflo"\nargs = [\n "mcp",\n "start",\n]\n\n[mcp_servers.codex]\ncommand = "codex"\nargs = [\n "mcp-server",\n]\n'); const cwd = tempDir('ak-rev-cwd'); const topology = codexMcpTopology({ cwd, home }); const byName = Object.fromEntries(topology.registrations.map((entry) => [entry.name, entry])); assert.deepEqual(byName['claude-flow'].args, ['mcp', 'start']); assert.equal(byName['claude-flow'].repairKind, 'legacy-ruflo'); assert.equal(byName.codex.repairKind, 'recursive-codex'); assert.equal(topology.selfRegistrations.length, 1); });