"use strict";
/**
 * RED-GREEN tests for F-NEW-PRODUCT-TRUST-IMPROVEMENT-RECOMMENDATIONS (W1.11).
 *
 * Validates the trademark three-axis remediation engine: every score-lowering
 * finding (build / runtime / human) produces a structured Recommendation
 * with actionable next-step pointer.
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-TRUST-IMPROVEMENT-RECOMMENDATIONS
 */
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 improvement_recommendations_1 = require("./improvement-recommendations");
const owasp_agentic_scanner_1 = require("./owasp-agentic-scanner");
const baseManifest = (over = {}) => ({
    agentId: 'agent-x',
    tools: [],
    hooks: { preToolUse: true, postToolUse: true, postResponse: true },
    llmProvider: 'connexum-proxy/anthropic',
    pluginIntegrityVerified: true,
    rateLimitsConfigured: true,
    declaredApprovalGates: [],
    declaredForbiddenCapabilities: [],
    ...over,
});
(0, node_test_1.describe)('F-NEW-PRODUCT-TRUST-IMPROVEMENT-RECOMMENDATIONS (W1.11)', () => {
    (0, node_test_1.it)('build axis: every OWASP finding becomes a build-axis Recommendation', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(baseManifest({ tools: ['WebFetch', 'Bash'] }));
        const recs = (0, improvement_recommendations_1.buildAxisRecommendations)('agent-x', scan);
        assert.ok(recs.length > 0);
        assert.ok(recs.every((r) => r.axis === 'build'));
        assert.ok(recs.every((r) => r.actionHint.length > 0));
        assert.ok(recs.every((r) => r.sourceRef.startsWith('owasp:')));
    });
    (0, node_test_1.it)('build axis: A01 Lethal Trifecta maps to enable-isolation kind', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(baseManifest({ tools: ['WebFetch', 'Bash'] }));
        const recs = (0, improvement_recommendations_1.buildAxisRecommendations)('agent-x', scan);
        const trifecta = recs.find((r) => /Lethal Trifecta/.test(r.headline));
        assert.ok(trifecta);
        assert.equal(trifecta.kind, 'enable-isolation');
    });
    (0, node_test_1.it)('build axis: A05 plugin integrity maps to verify-plugin-integrity', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(baseManifest({ pluginIntegrityVerified: false }));
        const recs = (0, improvement_recommendations_1.buildAxisRecommendations)('agent-x', scan);
        const a05 = recs.find((r) => r.sourceRef.includes('A05'));
        assert.ok(a05);
        assert.equal(a05.kind, 'verify-plugin-integrity');
    });
    (0, node_test_1.it)('build axis: A06 LLM-not-via-proxy maps to swap-provider', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(baseManifest({ llmProvider: 'anthropic/claude-direct' }));
        const recs = (0, improvement_recommendations_1.buildAxisRecommendations)('agent-x', scan);
        const a06 = recs.find((r) => r.sourceRef.includes('A06'));
        assert.ok(a06);
        assert.equal(a06.kind, 'swap-provider');
    });
    (0, node_test_1.it)('runtime axis: regulatory denial with citation maps to cite-regulatory-clause', () => {
        const recs = (0, improvement_recommendations_1.runtimeAxisRecommendations)('agent-x', [
            {
                packId: 'hipaa',
                checkId: 'phi-recipient',
                severity: 'CRIT',
                citation: 'HIPAA 164.502(e)',
                toolName: 'email_send',
                reason: 'PHI cannot flow to non-BAA recipient',
            },
        ]);
        assert.equal(recs.length, 1);
        assert.equal(recs[0].kind, 'cite-regulatory-clause');
        assert.match(recs[0].actionHint, /HIPAA 164.502\(e\)/);
        assert.equal(recs[0].axis, 'runtime');
    });
    (0, node_test_1.it)('runtime axis: pack denial without citation maps to add-pack-rule', () => {
        const recs = (0, improvement_recommendations_1.runtimeAxisRecommendations)('agent-x', [
            {
                packId: 'company-pack-1',
                checkId: 'capability:mcp',
                severity: 'HIGH',
                toolName: 'mcp__list_resources',
                reason: 'capability forbidden by org-baseline rule',
            },
        ]);
        assert.equal(recs[0].kind, 'add-pack-rule');
    });
    (0, node_test_1.it)('human axis: PHI ingestion maps to assign-training with hipaa-phi-awareness id', () => {
        const recs = (0, improvement_recommendations_1.humanAxisRecommendations)('agent-x', [
            {
                operatorId: 'user-bob',
                kind: 'phi-ingestion',
                severity: 'HIGH',
                count: 7,
                windowLabel: '30d',
            },
        ]);
        assert.equal(recs.length, 1);
        assert.equal(recs[0].axis, 'human');
        assert.equal(recs[0].kind, 'assign-training');
        assert.match(recs[0].actionHint, /training:hipaa-phi-awareness/);
        assert.match(recs[0].actionHint, /user-bob/);
    });
    (0, node_test_1.it)('human axis: all five drift kinds map to distinct training topics', () => {
        const kinds = [
            'phi-ingestion',
            'out-of-scope-prompt',
            'override-abuse',
            'repeated-denial-bypass',
            'low-review-responsiveness',
        ];
        const recs = (0, improvement_recommendations_1.humanAxisRecommendations)('agent-x', kinds.map((k) => ({
            operatorId: 'user-bob',
            kind: k,
            severity: 'MED',
            count: 1,
        })));
        const trainingIds = new Set(recs.map((r) => {
            const m = r.actionHint.match(/training:[A-Za-z0-9-]+/);
            return m?.[0] ?? '';
        }));
        assert.equal(trainingIds.size, 5);
    });
    (0, node_test_1.it)('buildRecommendationSet combines all three axes', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(baseManifest({ tools: ['Bash'] }));
        const set = (0, improvement_recommendations_1.buildRecommendationSet)({
            agentId: 'agent-x',
            buildScan: scan,
            runtimeFindings: [
                {
                    packId: 'hipaa',
                    checkId: 'phi-recipient',
                    severity: 'CRIT',
                    citation: 'HIPAA 164.502(e)',
                    toolName: 'email_send',
                    reason: 'PHI denied',
                },
            ],
            humanFindings: [
                { operatorId: 'user-bob', kind: 'phi-ingestion', severity: 'HIGH', count: 5 },
            ],
        });
        assert.ok(set.countsByAxis.build > 0);
        assert.ok(set.countsByAxis.runtime > 0);
        assert.ok(set.countsByAxis.human > 0);
        assert.ok(set.potentialScoreGain > 0);
        assert.ok(set.potentialScoreGain <= 100);
    });
    (0, node_test_1.it)('buildRecommendationSet potentialScoreGain weights by severity', () => {
        const critOnly = (0, improvement_recommendations_1.buildRecommendationSet)({
            agentId: 'agent-x',
            runtimeFindings: [
                {
                    packId: 'p1',
                    checkId: 'x',
                    severity: 'CRIT',
                    toolName: 'x',
                    reason: 'r',
                },
            ],
        });
        const lowOnly = (0, improvement_recommendations_1.buildRecommendationSet)({
            agentId: 'agent-x',
            runtimeFindings: [
                {
                    packId: 'p1',
                    checkId: 'x',
                    severity: 'LOW',
                    toolName: 'x',
                    reason: 'r',
                },
            ],
        });
        assert.ok(critOnly.potentialScoreGain > lowOnly.potentialScoreGain);
    });
    (0, node_test_1.it)('empty input set returns zero counts and gain', () => {
        const set = (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'agent-x' });
        assert.equal(set.recommendations.length, 0);
        assert.deepEqual(set.countsByAxis, { build: 0, runtime: 0, human: 0 });
        assert.equal(set.potentialScoreGain, 0);
    });
});
//# sourceMappingURL=improvement-recommendations.test.js.map