"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const TOP_LEVEL_COMMANDS = [
    'install', 'ask', 'phase1-check', 'status', 'doctor', 'restart', 'update', 'up',
    'agent', 'start', 'dream',
    'auth', 'models', 'system', 'memory', 'swarm', 'governance', 'benchmark',
    'agents', 'squads', 'skills', 'route', 'mcp', 'catalog', 'teams', 'networks',
    'media', 'orchestration', 'smoke', 'service', 'task', 'job', 'runtime',
    'reversa', 'sources', 'teammate', 'learning', 'context', 'plugin', 'pilot',
    'eval', 'learn', 'enterprise', 'designmd', 'aiobuilder'
];
function runHelp(args, timeoutMs) {
    return new Promise((resolve) => {
        const t0 = Date.now();
        const cliPath = path.join(process.cwd(), 'dist', 'index.js');
        const child = (0, child_process_1.spawn)('node', [cliPath, ...args], { stdio: ['ignore', 'pipe', 'pipe'] });
        let stdoutBytes = 0;
        let stderr = '';
        child.stdout?.on('data', (d) => { stdoutBytes += d.length; });
        child.stderr?.on('data', (d) => { stderr += d.toString(); });
        const killer = setTimeout(() => child.kill('SIGKILL'), timeoutMs);
        child.on('exit', (code) => {
            clearTimeout(killer);
            resolve({ command: args.join(' '), code, elapsed: Date.now() - t0, stdoutBytes, stderr: stderr.slice(0, 200) });
        });
    });
}
async function main() {
    const cliPath = path.join(process.cwd(), 'dist', 'index.js');
    if (!fs.existsSync(cliPath))
        throw new Error(`MISSING_BUILD: ${cliPath}`);
    const failures = [];
    const slow = [];
    // Root help (expect verbose) + version (expect ~6 bytes for "1.0.0\n")
    for (const { args, minBytes } of [{ args: ['--help'], minBytes: 50 }, { args: ['--version'], minBytes: 3 }]) {
        const r = await runHelp(args, 5000);
        if (r.code !== 0)
            failures.push(`${r.command} → exit=${r.code} stderr=${r.stderr}`);
        if (r.stdoutBytes < minBytes)
            failures.push(`${r.command} → stdout too small (${r.stdoutBytes} bytes, expected >= ${minBytes})`);
        if (r.elapsed > 3000)
            slow.push(`${r.command} (${r.elapsed}ms)`);
    }
    // Per-group help
    let okCount = 0;
    for (const cmd of TOP_LEVEL_COMMANDS) {
        const r = await runHelp([cmd, '--help'], 5000);
        if (r.code !== 0) {
            failures.push(`${cmd} --help → exit=${r.code} stderr=${r.stderr}`);
            continue;
        }
        if (r.stdoutBytes < 10) {
            failures.push(`${cmd} --help → empty stdout (only ${r.stdoutBytes} bytes)`);
            continue;
        }
        if (r.elapsed > 3000)
            slow.push(`${cmd} --help (${r.elapsed}ms)`);
        okCount++;
    }
    if (failures.length > 0) {
        throw new Error(`HELP_SURFACE_FAILURES (${failures.length}):\n${failures.join('\n')}`);
    }
    console.log(`TEST_CLI_HELP_SURFACE_OK (${okCount}/${TOP_LEVEL_COMMANDS.length} groups + 2 root invocations)`);
    if (slow.length > 0)
        console.log(`  SLOW (>3s): ${slow.join(', ')}`);
}
main().catch((err) => {
    console.error('TEST_CLI_HELP_SURFACE_FAIL:', err?.message || err);
    process.exit(1);
});
