"use strict";
/**
 * RED-GREEN tests for F-NEW-PRODUCT-PACK-INHERITANCE (W1.3 of
 * F-NEW-PRODUCT-AGENT-VISIBILITY-CLUSTER-2026-05-12).
 *
 * Customer-path probe:
 *   - Org-baseline declares "no MCP (BLOCK)" and "shell-exec (REQUIRE_APPROVAL)"
 *   - Per-agent pack declares "shell-exec (BLOCK)" and "filesystem-write (REQUIRE_APPROVAL)"
 *   - Composed pack:
 *       capability:mcp -> BLOCK            (from baseline)
 *       capability:shell-exec -> BLOCK     (strictest-wins; agent override)
 *       capability:filesystem-write -> REQUIRE_APPROVAL  (from agent)
 *   - Verify with real PackEvaluator: BLOCK -> DENY, REQUIRE_APPROVAL -> PENDING
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-PACK-INHERITANCE
 */
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 node_test_1 = require("node:test");
const assert = __importStar(require("node:assert/strict"));
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const pack_inheritance_1 = require("./pack-inheritance");
const capability_classes_1 = require("./capability-classes");
const pack_evaluator_1 = require("./pack-evaluator");
const index_1 = require("../packs/index");
const event_bus_1 = require("../audit/event-bus");
const index_2 = require("../adapters/index");
function evalActionWithPack(pack, toolName, input = {}) {
    const registry = new index_1.PackRegistry();
    registry.register(pack);
    registry.activate(pack.id);
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'inh-'));
    const bus = new event_bus_1.EventBus({ eventDir: dir, agentName: 'inh-test' });
    const evaluator = new pack_evaluator_1.PackEvaluator(registry, bus, { ttlMs: 60_000, enableCache: false });
    const ac = (0, index_2.createActionContext)(toolName, input, { name: 'agent-1', orgId: 'org-x' });
    return evaluator.evaluate('org-x', 'agent-1', ac);
}
(0, node_test_1.describe)('F-NEW-PRODUCT-PACK-INHERITANCE — org-baseline + per-agent composition', () => {
    (0, node_test_1.it)('composed pack inherits baseline rules where agent has no override', () => {
        const baseline = (0, pack_inheritance_1.buildOrgBaselinePack)({
            orgId: 'org-x',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        });
        const composed = (0, pack_inheritance_1.composeAgentPack)(baseline, {
            id: 'agent-x-pack',
            name: 'agent x pack',
            orgId: 'org-x',
            agentId: 'agent-x',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.FILESYSTEM_WRITE, action: 'REQUIRE_APPROVAL' }],
        });
        const v1 = evalActionWithPack(composed, 'mcp__list_resources');
        assert.equal(v1.verdict, 'DENY', 'MCP inherited from baseline');
        const v2 = evalActionWithPack(composed, 'Write', { file_path: '/tmp/x' });
        assert.equal(v2.verdict, 'PENDING', 'filesystem-write from agent-specific');
    });
    (0, node_test_1.it)('STRICTEST-WINS: agent BLOCK overrides baseline REQUIRE_APPROVAL', () => {
        const baseline = (0, pack_inheritance_1.buildOrgBaselinePack)({
            orgId: 'org-x',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'REQUIRE_APPROVAL' }],
        });
        const composed = (0, pack_inheritance_1.composeAgentPack)(baseline, {
            id: 'agent-strict',
            name: 'agent strict',
            orgId: 'org-x',
            agentId: 'agent-strict',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' }],
        });
        const verdict = evalActionWithPack(composed, 'Bash', { command: 'ls' });
        assert.equal(verdict.verdict, 'DENY', 'agent BLOCK wins over baseline REQUIRE_APPROVAL');
    });
    (0, node_test_1.it)('STRICTEST-WINS: baseline BLOCK survives agent LOG (less strict)', () => {
        const baseline = (0, pack_inheritance_1.buildOrgBaselinePack)({
            orgId: 'org-x',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        });
        const composed = (0, pack_inheritance_1.composeAgentPack)(baseline, {
            id: 'agent-relaxed',
            name: 'agent relaxed',
            orgId: 'org-x',
            agentId: 'agent-relaxed',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'LOG' }],
        });
        const verdict = evalActionWithPack(composed, 'mcp__list_resources');
        assert.equal(verdict.verdict, 'DENY', 'baseline BLOCK cannot be relaxed by agent LOG');
    });
    (0, node_test_1.it)('composed pack carries provenance via aliases', () => {
        const baseline = (0, pack_inheritance_1.buildOrgBaselinePack)({
            orgId: 'org-x',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        });
        const composed = (0, pack_inheritance_1.composeAgentPack)(baseline, {
            id: 'agent-prov',
            name: 'agent prov',
            orgId: 'org-x',
            agentId: 'agent-prov',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' }],
        });
        assert.ok(composed.aliases?.includes('org-baseline-org-x'));
        assert.ok(composed.aliases?.includes('agent-prov'));
    });
    (0, node_test_1.it)('strictest isolation across baseline + agent', () => {
        const baseline = (0, pack_inheritance_1.buildOrgBaselinePack)({
            orgId: 'org-x',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
            isolation: 'sourced-only',
        });
        const composed = (0, pack_inheritance_1.composeAgentPack)(baseline, {
            id: 'agent-iso',
            name: 'agent iso',
            orgId: 'org-x',
            agentId: 'agent-iso',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' }],
            isolation: 'strict',
        });
        assert.equal(composed.requiresActionIsolation, 'strict');
        const baseline2 = (0, pack_inheritance_1.buildOrgBaselinePack)({
            orgId: 'org-y',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
            isolation: 'strict',
        });
        const composed2 = (0, pack_inheritance_1.composeAgentPack)(baseline2, {
            id: 'agent-iso-2',
            name: 'agent iso 2',
            orgId: 'org-y',
            agentId: 'agent-iso-2',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' }],
            isolation: 'none',
        });
        assert.equal(composed2.requiresActionIsolation, 'strict', 'baseline strict survives agent none');
    });
    (0, node_test_1.it)('buildInheritedAgentPack returns both baseline and composed', () => {
        const { baseline, composed } = (0, pack_inheritance_1.buildInheritedAgentPack)({
            orgId: 'org-x',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' }],
        }, {
            id: 'a-1',
            name: 'a-1',
            orgId: 'org-x',
            agentId: 'a-1',
            rules: [{ capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'REQUIRE_APPROVAL' }],
        });
        assert.equal(baseline.id, 'org-baseline-org-x');
        assert.equal(composed.id, 'a-1');
        assert.ok(composed.aliases?.includes(baseline.id));
        const v = evalActionWithPack(composed, 'mcp__list_resources');
        assert.equal(v.verdict, 'DENY');
    });
    (0, node_test_1.it)('non-overlapping baseline + agent unions all rules', () => {
        const baseline = (0, pack_inheritance_1.buildOrgBaselinePack)({
            orgId: 'org-x',
            rules: [
                { capability: capability_classes_1.CAPABILITY_CLASS.MCP, action: 'BLOCK' },
                { capability: capability_classes_1.CAPABILITY_CLASS.WEB_FETCH, action: 'REQUIRE_APPROVAL' },
            ],
        });
        const composed = (0, pack_inheritance_1.composeAgentPack)(baseline, {
            id: 'a-2',
            name: 'a-2',
            orgId: 'org-x',
            agentId: 'a-2',
            rules: [
                { capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' },
                { capability: capability_classes_1.CAPABILITY_CLASS.FILESYSTEM_WRITE, action: 'REQUIRE_APPROVAL' },
            ],
        });
        const actions = composed.dataPolicy.categoryActions ?? {};
        assert.equal(actions[capability_classes_1.CAPABILITY_CLASS.MCP], 'BLOCK');
        assert.equal(actions[capability_classes_1.CAPABILITY_CLASS.WEB_FETCH], 'REQUIRE_APPROVAL');
        assert.equal(actions[capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC], 'BLOCK');
        assert.equal(actions[capability_classes_1.CAPABILITY_CLASS.FILESYSTEM_WRITE], 'REQUIRE_APPROVAL');
    });
});
//# sourceMappingURL=pack-inheritance.test.js.map