// Run a public CLI call in the harness's project and capture the actual process result. import assert from 'node:assert/strict'; import { spawn, spawnSync } from 'node:child_process'; import { once } from 'node:events'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const cli = fileURLToPath(new URL('../bin/pixelbolt.js', import.meta.url)); export function runCLI(args, cwd, input = '') { const child = spawnSync(process.execPath, [cli, ...args], { cwd, input, encoding: 'utf8', windowsHide: true }); if (child.error) throw child.error; assert.equal(child.signal, null, 'the CLI must exit normally'); return child; } // A regression can turn a rejected preview into a live server. Stop it as soon as it announces itself. export async function rejectedPreview(args, cwd) { const child = spawn(process.execPath, [cli, ...args], { cwd, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] }); const closed = once(child, 'close'); let stdout = '', stderr = ''; child.stdout.setEncoding('utf8').on('data', chunk => { stdout += chunk; child.kill(); }); child.stderr.setEncoding('utf8').on('data', chunk => { stderr += chunk; }); const [status] = await closed; return { status, stdout, stderr }; } export function mcpRequests(requests, cwd, serverArgs = ['--root', cwd, '--out', path.join(cwd, 'output')]) { const messages = [ { id: 1, method: 'initialize', params: { protocolVersion: '2025-11-25', capabilities: {}, clientInfo: { name: 'interface-check', version: '1' } } }, ...requests.map((request, i) => ({ ...request, id: i + 2 })) ]; const input = messages.map(message => JSON.stringify({ jsonrpc: '2.0', ...message })).join('\n') + '\n'; const child = runCLI(['mcp', ...serverArgs], cwd, input); assert.equal(child.status, 0, child.stderr); assert.equal(child.stderr, ''); const replies = new Map(child.stdout.trim().split(/\r?\n/).map(line => { const reply = JSON.parse(line); return [reply.id, reply]; })); assert.equal(replies.get(1)?.result?.protocolVersion, '2025-11-25'); return requests.map(({ method }, i) => { const reply = replies.get(i + 2); assert.ok(reply, method + ' must get a response'); return reply; }); } export function mcpRequest(method, params, cwd) { return mcpRequests([{ method, params }], cwd)[0]; }