/**
 * RED-GREEN tests for F-NEW-PRODUCT-AUTO-PACK-GENERATOR (W2.3).
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-AUTO-PACK-GENERATOR
 */
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import { autoGenerateCompanyPack } from './auto-pack-generator.js';
import { CAPABILITY_CLASS } from './capability-classes.js';
import { PackEvaluator } from './pack-evaluator.js';
import { PackRegistry } from '../packs/index.js';
import { EventBus } from '../audit/event-bus.js';
import { createActionContext } from '../adapters/index.js';
import * as fs from 'fs';
import * as os from 'os';
import * as path from '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 PackRegistry();
    registry.register(pack);
    registry.activate(pack.id);
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'apg-'));
    const bus = new EventBus({ eventDir: dir, agentName: 'apg-test' });
    const evaluator = new PackEvaluator(registry, bus, { ttlMs: 60_000, enableCache: false });
    const ac = createActionContext(toolName, input, { name: 'agent-x', orgId: 'org-unknown' });
    return evaluator.evaluate('org-unknown', 'agent-x', ac);
}
describe('F-NEW-PRODUCT-AUTO-PACK-GENERATOR (W2.3)', () => {
    it('clean manifest produces a pack with zero accepted rules by default', () => {
        const result = autoGenerateCompanyPack(baseManifest());
        assert.equal(result.acceptedRules.length, 0);
        assert.equal(Object.keys(result.pack.dataPolicy.categoryActions ?? {}).length, 0);
    });
    it('acceptAllSuggestedRules pulls every scanner suggestion into the pack', () => {
        const manifest = baseManifest({
            tools: ['Bash', 'postgres_query'],
            declaredApprovalGates: [],
        });
        const result = 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');
    });
    it('explicit acceptedRules override scanner suggestions', () => {
        const manifest = baseManifest({ tools: ['Bash'] });
        const customRule = { capability: CAPABILITY_CLASS.SHELL_EXEC, action: 'BLOCK' };
        const result = 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');
    });
    it('Lethal Trifecta manifest surfaces A01 in reviewRequiredRisks (no auto-rule for prompt-injection)', () => {
        const result = 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_CLASS.SHELL_EXEC);
        assert.equal(shellSuggestions.length, 1);
    });
    it('generated pack carries auto-generated description with finding counts', () => {
        const result = 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/);
    });
    it('packIdPrefix prefixes the generated pack id', () => {
        const result = autoGenerateCompanyPack(baseManifest({ agentId: 'mailer' }), { packIdPrefix: 'custom' });
        assert.equal(result.pack.id, 'custom-mailer');
    });
    it('isolation option is forwarded to the generated pack', () => {
        const result = autoGenerateCompanyPack(baseManifest({ tools: ['Bash'] }), { acceptAllSuggestedRules: true, isolation: 'strict' });
        assert.equal(result.pack.requiresActionIsolation, 'strict');
    });
    it('explicit acceptedRules empty array means customer rejected ALL scanner suggestions', () => {
        const manifest = baseManifest({ tools: ['Bash', 'postgres_query'] });
        const result = 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