"use strict";
/**
 * RED-GREEN tests for F-NEW-PRODUCT-AUTO-PACK-GENERATOR (W2.3).
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-AUTO-PACK-GENERATOR
 */
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 auto_pack_generator_1 = require("./auto-pack-generator");
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");
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const baseManifest = (over = {}) => ({
    agentId: 'agent-x',
    name: 'agent X',
    tools: [],
    hooks: { preToolUse: true, postToolUse: true, postResponse: true },
    llmProvider: 'connexum-proxy/anthropic',
    pluginIntegrityVerified: true,
    rateLimitsConfigured: true,
    declaredApprovalGates: [],
    declaredForbiddenCapabilities: [],
    ...over,
});
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(), 'apg-'));
    const bus = new event_bus_1.EventBus({ eventDir: dir, agentName: 'apg-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-x', orgId: 'org-unknown' });
    return evaluator.evaluate('org-unknown', 'agent-x', ac);
}
(0, node_test_1.describe)('F-NEW-PRODUCT-AUTO-PACK-GENERATOR (W2.3)', () => {
    (0, node_test_1.it)('clean manifest produces a pack with zero accepted rules by default', () => {
        const result = (0, auto_pack_generator_1.autoGenerateCompanyPack)(baseManifest());
        assert.equal(result.acceptedRules.length, 0);
        assert.equal(Object.keys(result.pack.dataPolicy.categoryActions ?? {}).length, 0);
    });
    (0, node_test_1.it)('acceptAllSuggestedRules pulls every scanner suggestion into the pack', () => {
        const manifest = baseManifest({
            tools: ['Bash', 'postgres_query'],
            declaredApprovalGates: [],
        });
        const result = (0, auto_pack_generator_1.autoGenerateCompanyPack)(manifest, { acceptAllSuggestedRules: true });
        // Scanner suggests rules for shell-exec + db-write (A08 findings).
        assert.ok(result.proposedRules.length >= 2);
        assert.equal(result.acceptedRules.length, result.proposedRules.length);
        // Customer-path probe: with accepted rules, Bash now gets REQUIRE_APPROVAL.
        const verdict = evalActionWithPack(result.pack, 'Bash', { command: 'ls' });
        assert.equal(verdict.verdict, 'PENDING');
    });
    (0, node_test_1.it)('explicit acceptedRules override scanner suggestions', () => {
        const manifest = baseManifest({ tools: ['Bash'] });
        const customRule = { capability: capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' };
        const result = (0, auto_pack_generator_1.autoGenerateCompanyPack)(manifest, { acceptedRules: [customRule] });
        assert.equal(result.acceptedRules.length, 1);
        assert.equal(result.acceptedRules[0].action, 'BLOCK');
        const verdict = evalActionWithPack(result.pack, 'Bash', { command: 'ls' });
        assert.equal(verdict.verdict, 'DENY', 'customer-curated BLOCK overrides scanner default of REQUIRE_APPROVAL');
    });
    (0, node_test_1.it)('Lethal Trifecta manifest surfaces A01 in reviewRequiredRisks (no auto-rule for prompt-injection)', () => {
        const result = (0, auto_pack_generator_1.autoGenerateCompanyPack)(baseManifest({ tools: ['WebFetch', 'Bash'] }));
        assert.ok(result.reviewRequiredRisks.some((r) => r.startsWith('A01')));
        // A01 has no suggestedRule -> only A08 shell ungated produces one.
        const shellSuggestions = result.proposedRules.filter((r) => r.capability === capability_classes_1.CAPABILITY_CLASS.SHELL_EXEC);
        assert.equal(shellSuggestions.length, 1);
    });
    (0, node_test_1.it)('generated pack carries auto-generated description with finding counts', () => {
        const result = (0, auto_pack_generator_1.autoGenerateCompanyPack)(baseManifest({ tools: ['Bash'] }), { acceptAllSuggestedRules: true });
        assert.match(result.pack.description ?? '', /Auto-generated from OWASP Agentic scan/);
        assert.match(result.pack.description ?? '', /Build-structure score/);
        assert.match(result.pack.description ?? '', /accepted by customer/);
    });
    (0, node_test_1.it)('packIdPrefix prefixes the generated pack id', () => {
        const result = (0, auto_pack_generator_1.autoGenerateCompanyPack)(baseManifest({ agentId: 'mailer' }), { packIdPrefix: 'custom' });
        assert.equal(result.pack.id, 'custom-mailer');
    });
    (0, node_test_1.it)('isolation option is forwarded to the generated pack', () => {
        const result = (0, auto_pack_generator_1.autoGenerateCompanyPack)(baseManifest({ tools: ['Bash'] }), { acceptAllSuggestedRules: true, isolation: 'strict' });
        assert.equal(result.pack.requiresActionIsolation, 'strict');
    });
    (0, node_test_1.it)('explicit acceptedRules empty array means customer rejected ALL scanner suggestions', () => {
        const manifest = baseManifest({ tools: ['Bash', 'postgres_query'] });
        const result = (0, auto_pack_generator_1.autoGenerateCompanyPack)(manifest, { acceptedRules: [] });
        assert.equal(result.acceptedRules.length, 0);
        assert.ok(result.proposedRules.length > 0, 'scanner still produced suggestions for visibility');
    });
});
//# sourceMappingURL=auto-pack-generator.test.js.map