"use strict";
/**
 * RED-GREEN tests for the compliance & insurance report generator.
 *
 * @connexum/ai-governance  Insurance & Compliance Report
 */
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 agent_trust_report_1 = require("./agent-trust-report");
const trust_score_three_dim_1 = require("./trust-score-three-dim");
const owasp_agentic_scanner_1 = require("./owasp-agentic-scanner");
const improvement_recommendations_1 = require("./improvement-recommendations");
const cleanManifest = () => ({
    agentId: 'agent-x',
    tools: [],
    hooks: { preToolUse: true, postToolUse: true, postResponse: true },
    llmProvider: 'connexum-proxy/anthropic',
    pluginIntegrityVerified: true,
    rateLimitsConfigured: true,
    declaredApprovalGates: [],
    declaredForbiddenCapabilities: [],
});
const dirtyManifest = () => ({
    ...cleanManifest(),
    tools: ['WebFetch', 'Bash'],
    pluginIntegrityVerified: false,
    llmProvider: 'anthropic/direct',
});
const dim = (c) => ({
    scope_adherence: c, policy_compliance: c, anomaly_rate: c,
    incident_history: c, behavioral_consistency: c, composite: c,
});
const opDim = (c) => ({
    out_of_scope_ingestion: c, scope_violation_by_team: c,
    scope_violation_by_data_class: c, human_override_frequency: c, composite: c,
});
const bDim = (c) => ({
    owasp_agentic_score: c, hook_coverage_count: 3,
    declared_capability_class_count: 4, has_human_in_loop: true,
    plugin_integrity_verified: true, proxy_routed: true, composite: c,
});
function mockOrgRep(score, confidence) {
    return {
        org_id: 'org-x',
        score,
        confidence,
        based_on_signal_count: 100,
        agent_count: 5,
        axis_breakdown: { agent_activity: score, operator_activity: score, agent_build_structure: score },
        computed_at: '2026-05-12T00:00:00.000Z',
    };
}
(0, node_test_1.describe)('compliance & insurance report', () => {
    (0, node_test_1.it)('CRIT finding -> NON_COMPLIANT verdict regardless of score', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(dirtyManifest());
        assert.ok(scan.countsBySeverity.CRIT > 0);
        const report = (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'agent-x',
            agentName: 'X',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: ['hipaa', 'company-pack-1'],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(95),
                operator_activity: opDim(95),
                agent_build_structure: bDim(95),
            }),
            scanReport: scan,
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'agent-x', buildScan: scan }),
        });
        assert.equal(report.verdict, 'NON_COMPLIANT');
    });
    (0, node_test_1.it)('Clean scan + high score + high-confidence rep -> COMPLIANT', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(cleanManifest());
        const report = (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'agent-x',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: ['soc2'],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(90),
                operator_activity: opDim(90),
                agent_build_structure: bDim(90),
            }),
            scanReport: scan,
            orgReputation: mockOrgRep(90, 'high'),
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'agent-x', buildScan: scan }),
        });
        assert.equal(report.verdict, 'COMPLIANT');
    });
    (0, node_test_1.it)('Clean scan + high score + LOW-confidence rep -> PROVISIONAL', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(cleanManifest());
        const report = (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'agent-x',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: [],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(88),
                operator_activity: opDim(88),
                agent_build_structure: bDim(88),
            }),
            scanReport: scan,
            orgReputation: mockOrgRep(60, 'low'),
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'agent-x', buildScan: scan }),
        });
        assert.equal(report.verdict, 'PROVISIONAL');
    });
    (0, node_test_1.it)('Mid-band composite -> CONDITIONAL / AT_RISK / NON_COMPLIANT', () => {
        const make = (composite) => (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'agent-x',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: [],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(composite),
                operator_activity: opDim(composite),
                agent_build_structure: bDim(composite),
            }),
            scanReport: (0, owasp_agentic_scanner_1.scanAgent)(cleanManifest()),
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'agent-x' }),
        });
        assert.equal(make(70).verdict, 'CONDITIONAL');
        assert.equal(make(50).verdict, 'AT_RISK');
        assert.equal(make(20).verdict, 'NON_COMPLIANT');
    });
    (0, node_test_1.it)('headline + sections + summary populated', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(dirtyManifest());
        const report = (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'agent-x',
            agentName: 'Agent X',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: ['hipaa', 'company-pack-1'],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(60), operator_activity: opDim(60), agent_build_structure: bDim(60),
            }),
            scanReport: scan,
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'agent-x', buildScan: scan }),
        });
        assert.equal(report.headline.composite_score, 60);
        assert.deepEqual(report.headline.pack_bindings, ['hipaa', 'company-pack-1']);
        assert.equal(report.headline.open_critical_findings, scan.countsBySeverity.CRIT);
        assert.match(report.sections.summary.body, /Agent X/);
        assert.match(report.sections.summary.body, /Verdict: NON_COMPLIANT/);
        assert.equal(report.sections.trust_score.body.composite, 60);
        assert.equal(report.sections.recommendations.body.agentId, 'agent-x');
    });
    (0, node_test_1.it)('runtimeTotals appear in the report when supplied', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(cleanManifest());
        const report = (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'a',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: [],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(80), operator_activity: opDim(80), agent_build_structure: bDim(80),
            }),
            scanReport: scan,
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'a' }),
            runtimeTotals: {
                allow: 412, deny: 6, pending: 3, approved: 2, denied_by_human: 1,
            },
        });
        assert.deepEqual(report.sections.runtime_totals.body, {
            allow: 412, deny: 6, pending: 3, approved: 2, denied_by_human: 1,
        });
    });
    (0, node_test_1.it)('id auto-generates when not supplied', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(cleanManifest());
        const r = (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'a-1',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: [],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(80), operator_activity: opDim(80), agent_build_structure: bDim(80),
            }),
            scanReport: scan,
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'a-1' }),
        });
        assert.match(r.id, /^cr-a-1-2026-05-01-2026-05-12$/);
    });
    (0, node_test_1.it)('reportToCsvRow + CSV_HEADER produce a parseable CSV pair', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(cleanManifest());
        const r = (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'a-1',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: ['hipaa', 'soc2'],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(80), operator_activity: opDim(80), agent_build_structure: bDim(80),
            }),
            scanReport: scan,
            orgReputation: mockOrgRep(80, 'medium'),
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'a-1' }),
        });
        const row = (0, agent_trust_report_1.reportToCsvRow)(r);
        const headerCols = agent_trust_report_1.CSV_HEADER.split(',');
        const rowCols = row.split(',');
        assert.equal(rowCols.length, headerCols.length);
        assert.ok(row.includes('hipaa|soc2'));
        assert.ok(row.includes('a-1'));
        assert.ok(row.includes('CONDITIONAL'));
    });
    (0, node_test_1.it)('reportToCsvRow escapes fields containing commas', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(cleanManifest());
        const r = (0, agent_trust_report_1.generateAgentTrustReport)({
            agentId: 'a, with comma',
            orgId: 'org-x',
            period: { from: '2026-05-01T00:00:00.000Z', to: '2026-05-12T00:00:00.000Z' },
            packBindings: [],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(80), operator_activity: opDim(80), agent_build_structure: bDim(80),
            }),
            scanReport: scan,
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'a, with comma' }),
        });
        const row = (0, agent_trust_report_1.reportToCsvRow)(r);
        assert.ok(row.includes('"a, with comma"'));
    });
    (0, node_test_1.it)('topRecommendations filters by severity + applies limit', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(dirtyManifest());
        const set = (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'a', buildScan: scan });
        const top = (0, agent_trust_report_1.topRecommendations)(set, { minSeverity: 'CRIT' });
        assert.ok(top.every((r) => r.severity === 'CRIT'));
        const limited = (0, agent_trust_report_1.topRecommendations)(set, { minSeverity: 'LOW', limit: 2 });
        assert.ok(limited.length <= 2);
    });
    (0, node_test_1.it)('topRecommendations sorts highest-severity-first', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(dirtyManifest());
        const set = (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'a', buildScan: scan });
        const sorted = (0, agent_trust_report_1.topRecommendations)(set, { minSeverity: 'LOW' });
        const SEV = { LOW: 0, MED: 1, HIGH: 2, CRIT: 3 };
        for (let i = 1; i < sorted.length; i++) {
            assert.ok(SEV[sorted[i].severity] <= SEV[sorted[i - 1].severity], `recommendation at index ${i} (${sorted[i].severity}) is more severe than previous (${sorted[i - 1].severity})`);
        }
    });
    (0, node_test_1.it)('missing agentId / orgId / period throws (fail-closed)', () => {
        const scan = (0, owasp_agentic_scanner_1.scanAgent)(cleanManifest());
        const base = {
            orgId: 'o',
            period: { from: 'a', to: 'b' },
            packBindings: [],
            score: (0, trust_score_three_dim_1.composeThreeDimensionalScore)({
                agent_activity: dim(80), operator_activity: opDim(80), agent_build_structure: bDim(80),
            }),
            scanReport: scan,
            recommendations: (0, improvement_recommendations_1.buildRecommendationSet)({ agentId: 'a' }),
        };
        assert.throws(() => (0, agent_trust_report_1.generateAgentTrustReport)({ ...base, agentId: '' }), /agentId is required/);
        assert.throws(() => (0, agent_trust_report_1.generateAgentTrustReport)({ ...base, agentId: 'a', orgId: '' }), /orgId is required/);
        assert.throws(() => (0, agent_trust_report_1.generateAgentTrustReport)({
            ...base,
            agentId: 'a',
            period: { from: '', to: 'b' },
        }), /period.from and period.to are required/);
    });
});
//# sourceMappingURL=agent-trust-report.test.js.map