"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"));
async function runAsk(message, 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, 'ask', message], {
            stdio: ['ignore', 'pipe', 'pipe'],
            env: { ...process.env, OPENLIFE_ASK_TIMEOUT_MS: '5000' }
        });
        child.stdout?.on('data', () => { });
        child.stderr?.on('data', () => { });
        let timedOut = false;
        const killer = setTimeout(() => {
            timedOut = true;
            child.kill('SIGKILL');
        }, timeoutMs);
        child.on('exit', (code, signal) => {
            clearTimeout(killer);
            resolve({ code, signal, elapsed: Date.now() - t0, timedOut });
        });
    });
}
async function main() {
    // Generous timeout — Brain/Gateway/Gatekeeper top-level require chain alone takes
    // ~24s in WSL (Story 2.1 investigation). Real Brain.think() adds another 1-3s. Plus
    // first-token-latency variability from Gemini API. 60s comfortable margin.
    // The bug being tested is forever-hang (Story 1.2), not latency.
    const result = await runAsk('say AUDIT-OK', 60000);
    if (result.timedOut) {
        throw new Error(`ASK_DID_NOT_EXIT_WITHIN_60S (elapsed=${result.elapsed}ms) — possible regression of Story 1.2 fix`);
    }
    console.log(`TEST_ASK_EXIT_OK (code=${result.code}, signal=${result.signal}, elapsed=${result.elapsed}ms)`);
}
main().catch((err) => {
    console.error('TEST_ASK_EXIT_FAIL:', err?.message || err);
    process.exit(1);
});
