/**
 * Per-entry validation probe for olam mcp import.
 *
 * Runs `command + args` with stdin closed and a 5s timeout.
 * Successful exit + non-empty stdout → validated: true.
 * Failed exit → validated: false (import anyway, flagged).
 */
import { spawn } from 'node:child_process';
const VALIDATION_TIMEOUT_MS = 5000;
export async function validateMcpEntry(entry) {
    return new Promise((resolve) => {
        let stdout = '';
        let timedOut = false;
        let child;
        try {
            child = spawn(entry.command, [...(entry.args ?? [])], {
                stdio: ['ignore', 'pipe', 'ignore'],
                env: { ...process.env, ...(entry.env ?? {}) },
            });
        }
        catch (err) {
            resolve({
                name: entry.name,
                validated: false,
                reason: err instanceof Error ? err.message : 'spawn failed',
            });
            return;
        }
        const timer = setTimeout(() => {
            timedOut = true;
            child.kill('SIGKILL');
        }, VALIDATION_TIMEOUT_MS);
        child.stdout?.on('data', (chunk) => {
            stdout += chunk.toString();
        });
        child.on('close', (code) => {
            clearTimeout(timer);
            if (timedOut) {
                resolve({ name: entry.name, validated: false, reason: 'timeout (5s)' });
                return;
            }
            const validated = code === 0 && stdout.trim().length > 0;
            resolve({
                name: entry.name,
                validated,
                reason: validated ? 'ok' : `exit code ${code ?? 'null'}`,
            });
        });
        child.on('error', (err) => {
            clearTimeout(timer);
            resolve({ name: entry.name, validated: false, reason: err.message });
        });
    });
}
//# sourceMappingURL=import-validate.js.map