"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 assert_1 = require("assert");
const child_process_1 = require("child_process");
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const tempState = fs.mkdtempSync(path.join(os.tmpdir(), 'openlife-enterprise-'));
const runJson = (args, expectedStatus = 0) => {
    const r = (0, child_process_1.spawnSync)('node', ['bin/openlife.js', ...args], {
        cwd: process.cwd(),
        encoding: 'utf-8',
        env: { ...process.env, OPENLIFE_STATE_DIR: tempState },
        timeout: 30_000,
    });
    assert_1.strict.equal(r.status, expectedStatus, `${args.join(' ')} failed: ${r.stderr || r.stdout}`);
    return JSON.parse(r.stdout);
};
const invalidPlugin = runJson(['plugin', 'install', 'bad id spaces', '--risk', 'planet'], 1);
assert_1.strict.equal(invalidPlugin.ok, false);
assert_1.strict.equal(invalidPlugin.error, 'invalid_plugin_id');
const ctx = runJson(['context', 'init', '--name', 'OpenLife Enterprise Core']);
assert_1.strict.equal(ctx.ok, true);
assert_1.strict.ok(ctx.stateDir.startsWith(tempState));
assert_1.strict.ok(fs.existsSync(path.join(tempState, 'institutional-context', 'brand.md')));
assert_1.strict.ok(ctx.files.length >= 8);
const ctxSummary = runJson(['context', 'summary']);
assert_1.strict.equal(ctxSummary.ok, true);
assert_1.strict.ok(ctxSummary.context.requiredFiles.includes('governance.md'));
const ctxDoctor = runJson(['context', 'doctor']);
assert_1.strict.equal(ctxDoctor.ok, true);
assert_1.strict.equal(ctxDoctor.summary.requiredPresent, ctxDoctor.summary.requiredTotal);
const plugin = runJson(['plugin', 'install', 'marketing-growth', '--risk', 'medium', '--capability', 'content.strategy', '--capability', 'campaign.execution']);
assert_1.strict.equal(plugin.ok, true);
assert_1.strict.equal(plugin.plugin.id, 'marketing-growth');
assert_1.strict.equal(plugin.plugin.governance.riskLevel, 'medium');
assert_1.strict.equal(plugin.plugin.status, 'installed');
assert_1.strict.ok(fs.existsSync(path.join(tempState, 'plugins', 'marketing-growth.json')));
const pluginInspect = runJson(['plugin', 'inspect', 'marketing-growth']);
assert_1.strict.equal(pluginInspect.ok, true);
assert_1.strict.equal(pluginInspect.plugin.capabilities.length, 2);
const traversalInspect = runJson(['plugin', 'inspect', '../../package'], 1);
assert_1.strict.equal(traversalInspect.ok, false);
assert_1.strict.equal(traversalInspect.error, 'invalid_plugin_id');
const traversalDisable = runJson(['plugin', 'disable', '../../package'], 1);
assert_1.strict.equal(traversalDisable.ok, false);
assert_1.strict.equal(traversalDisable.error, 'invalid_plugin_id');
const pluginDisable = runJson(['plugin', 'disable', 'marketing-growth']);
assert_1.strict.equal(pluginDisable.ok, true);
assert_1.strict.equal(pluginDisable.plugin.status, 'disabled');
const pluginEnable = runJson(['plugin', 'enable', 'marketing-growth']);
assert_1.strict.equal(pluginEnable.ok, true);
assert_1.strict.equal(pluginEnable.plugin.status, 'installed');
const plugins = runJson(['plugin', 'list']);
assert_1.strict.ok(plugins.plugins.some((p) => p.id === 'marketing-growth'));
const pilot = runJson(['pilot', 'create', 'marketing-content-pilot', '--metric', 'tempo de produção -50%', '--service', 'growth-engine']);
assert_1.strict.equal(pilot.ok, true);
assert_1.strict.equal(pilot.pilot.status, 'active');
const pilots = runJson(['pilot', 'list']);
assert_1.strict.equal(pilots.ok, true);
assert_1.strict.ok(pilots.pilots.some((p) => p.id === 'marketing-content-pilot'));
const pilotReport = runJson(['pilot', 'report', 'marketing-content-pilot']);
assert_1.strict.equal(pilotReport.ok, true);
assert_1.strict.ok(pilotReport.report.successCriteria.length >= 1);
const traversalPilotReport = runJson(['pilot', 'report', '../package'], 1);
assert_1.strict.equal(traversalPilotReport.ok, false);
assert_1.strict.equal(traversalPilotReport.error, 'invalid_pilot_id');
const evalTraversalReport = runJson(['eval', 'report', '../package'], 1);
assert_1.strict.equal(evalTraversalReport.ok, false);
assert_1.strict.equal(evalTraversalReport.error, 'invalid_mission_id');
const pilotComplete = runJson(['pilot', 'complete', 'marketing-content-pilot', '--outcome', 'validated']);
assert_1.strict.equal(pilotComplete.ok, true);
assert_1.strict.equal(pilotComplete.pilot.status, 'completed');
assert_1.strict.equal(pilotComplete.pilot.outcome, 'validated');
const evalRun = runJson(['eval', 'judge', '--mission', 'enterprise-demo', '--criteria', 'quality,governance,actionability']);
assert_1.strict.equal(evalRun.ok, true);
assert_1.strict.ok(evalRun.evaluation.score >= 0 && evalRun.evaluation.score <= 100);
assert_1.strict.ok(fs.existsSync(evalRun.evaluation.path));
const evalReport = runJson(['eval', 'report', 'enterprise-demo']);
assert_1.strict.equal(evalReport.ok, true);
assert_1.strict.equal(evalReport.evaluation.mission, 'enterprise-demo');
const learned = runJson(['learn', 'from-last-run', '--mission', 'enterprise-demo', '--feedback', 'Transformar fluxo aprovado em skill reutilizável']);
assert_1.strict.equal(learned.ok, true);
assert_1.strict.ok(learned.suggestions.some((s) => s.type === 'skill'));
assert_1.strict.ok(learned.suggestions.some((s) => s.type === 'context'));
const learningList = runJson(['learn', 'list']);
assert_1.strict.equal(learningList.ok, true);
assert_1.strict.ok(learningList.records.some((r) => r.mission === 'enterprise-demo'));
const doctor = runJson(['enterprise', 'doctor']);
assert_1.strict.equal(doctor.ok, true);
assert_1.strict.equal(doctor.checks.context.ok, true);
assert_1.strict.ok(doctor.checks.plugins.count >= 1);
assert_1.strict.ok(doctor.checks.pilots.count >= 1);
assert_1.strict.ok(doctor.checks.evaluations.count >= 1);
assert_1.strict.ok(doctor.checks.learning.count >= 1);
console.log('TEST_ENTERPRISE_AGENTIC_CORE_OK');
