"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 });
/*
 * Regression test for Story 2.1 — Phase 5 v1.0 milestone.
 *
 * Asserts `openlife phase1-check` exits DETERMINISTICALLY (does not hang).
 * Previously, the command would never exit because TestHarness instantiates
 * Gateway → Telegraf, leaving event-loop handles open. Same root-cause pattern
 * as Story 1.2 `ask` exit fix.
 *
 * The test uses a generous timeout (default 120s) to accommodate slow imports
 * in WSL (Brain alone takes ~10s to require; Gateway ~21s). The bug being
 * tested is forever-hang, not slow-but-finite.
 *
 * Wired into test:all via `test:phase1-check-exit` script.
 */
const child_process_1 = require("child_process");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const CLI = path.join(process.cwd(), 'dist', 'index.js');
// Generous shell timeout — Brain/Gateway imports take 10-25s each in WSL.
// The bug is forever-hang, not slowness. If it exits within 120s, fix works.
const SHELL_TIMEOUT_MS = Number(process.env.OPENLIFE_TEST_PHASE1_SHELL_TIMEOUT_MS || 120000);
// Pass tighter internal master timeout to phase1-check
const INTERNAL_TIMEOUT_MS = Number(process.env.OPENLIFE_TEST_PHASE1_INTERNAL_TIMEOUT_MS || 90000);
function runPhase1Check() {
    return new Promise((resolve) => {
        const t0 = Date.now();
        const child = (0, child_process_1.spawn)('node', [CLI, 'phase1-check'], {
            stdio: ['ignore', 'pipe', 'pipe'],
            env: {
                ...process.env,
                OPENLIFE_PHASE1_TIMEOUT_MS: String(INTERNAL_TIMEOUT_MS)
            }
        });
        // Drain stdio so child doesn't block on full pipes
        child.stdout?.on('data', () => { });
        child.stderr?.on('data', () => { });
        let timedOut = false;
        const killer = setTimeout(() => {
            timedOut = true;
            child.kill('SIGKILL');
        }, SHELL_TIMEOUT_MS);
        child.on('exit', (code, signal) => {
            clearTimeout(killer);
            resolve({ code, signal, elapsed: Date.now() - t0, timedOut });
        });
    });
}
async function main() {
    if (!fs.existsSync(CLI))
        throw new Error(`MISSING_BUILD: ${CLI}`);
    const result = await runPhase1Check();
    if (result.timedOut) {
        throw new Error(`PHASE1_CHECK_HUNG: process did not exit within ${SHELL_TIMEOUT_MS}ms — Story 2.1 regression`);
    }
    // Accept exit 0 (all checks green) OR exit 1 (some checks failed but command terminated)
    // Reject signal-only exit (would indicate crash mid-execution)
    if (result.code === null) {
        throw new Error(`PHASE1_CHECK_NO_EXIT_CODE: signal=${result.signal} elapsed=${result.elapsed}ms`);
    }
    if (result.code !== 0 && result.code !== 1) {
        throw new Error(`PHASE1_CHECK_UNEXPECTED_EXIT: code=${result.code} elapsed=${result.elapsed}ms (expected 0 or 1)`);
    }
    console.log(`TEST_PHASE1_CHECK_EXIT_OK (code=${result.code}, elapsed=${result.elapsed}ms, signal=${result.signal})`);
}
main().catch((err) => {
    console.error('TEST_PHASE1_CHECK_EXIT_FAIL:', err?.message || err);
    process.exit(1);
});
