"use strict";
/**
 * test_guided_creator_cli.ts — Stories 3.2 + 3.3
 *
 * Spawns the CLI to verify:
 *   1. `openlife create --help` lists the 5 sub-commands
 *   2. `openlife create capability "<desc>"` invokes the GenesisEngine
 *      and produces a draft pack (against a sandbox catalogRoot via
 *      OPENLIFE_STATE_DIR + working in a temp cwd)
 *   3. `openlife aiobuilder create-capability` exposes the same handler
 *   4. `openlife create workflow <id>` returns the stub guidance
 */
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 fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const CLI = path.join(process.cwd(), 'dist', 'index.js');
function assertTrue(cond, label) {
    if (!cond)
        throw new Error(`ASSERT_FAILED[${label}]`);
}
function runCli(args, cwd) {
    const r = (0, child_process_1.spawnSync)('node', [CLI, ...args], { cwd, encoding: 'utf-8', timeout: 30000 });
    return { ok: r.status === 0, status: r.status, out: (r.stdout || '') + (r.stderr || '') };
}
// ── Scenario 1: `create --help` lists sub-commands ──────────────────
const h = runCli(['create', '--help']);
assertTrue(h.ok, `create --help ok (status=${h.status})`);
assertTrue(/capability /.test(h.out), 'create capability subcommand listed');
assertTrue(/squad /.test(h.out), 'create squad subcommand listed');
assertTrue(/skill /.test(h.out), 'create skill subcommand listed');
assertTrue(/agent /.test(h.out), 'create agent subcommand listed');
assertTrue(/workflow /.test(h.out), 'create workflow subcommand listed');
console.log('[3.2] create --help lists 5 sub-commands ✓');
// ── Scenario 2: aiobuilder create-capability --help ─────────────────
const ah = runCli(['aiobuilder', 'create-capability', '--help']);
assertTrue(ah.ok, `aiobuilder create-capability --help ok (status=${ah.status})`);
assertTrue(/--mode/.test(ah.out), 'mode option documented');
assertTrue(/--actor/.test(ah.out), 'actor option documented');
console.log('[3.3] aiobuilder create-capability --help works ✓');
// ── Scenario 3: end-to-end create capability in temp cwd ────────────
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'guided-creator-'));
try {
    // Cli writes to <cwd>/.catalog/capabilities/<id>/ and <cwd>/.openlife/...
    const r = runCli([
        'create', 'capability', 'integration test brief',
        '--id', 'guided-test-pack',
        '--mode', 'quick',
        '--actor', 'cli-test',
        '--skills', 'guided-test-skill',
    ], tmp);
    assertTrue(r.ok, `create capability ok (status=${r.status})\nout: ${r.out.slice(0, 500)}`);
    const packDir = path.join(tmp, '.catalog', 'capabilities', 'guided-test-pack');
    assertTrue(fs.existsSync(path.join(packDir, 'capability.yaml')), 'capability.yaml written');
    assertTrue(fs.existsSync(path.join(packDir, 'INDEX.md')), 'INDEX.md written');
    console.log('[3.3] end-to-end create capability ✓');
    // ── Scenario 4: workflow stub returns expected guidance ───────────
    const wf = runCli(['create', 'workflow', 'some-id'], tmp);
    assertTrue(!wf.ok, 'create workflow returns non-zero');
    assertTrue(/use_create_capability/.test(wf.out), 'workflow stub points to capability');
    console.log('[3.2] create workflow stub guidance ✓');
}
finally {
    fs.rmSync(tmp, { recursive: true, force: true });
}
console.log('TEST_GUIDED_CREATOR_CLI_OK');
