"use strict";
/**
 * RED-GREEN tests for F-NEW-PRODUCT-AGENT-SELF-REGISTRATION (W2.2).
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-AGENT-SELF-REGISTRATION
 */
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 self_registration_hook_1 = require("./self-registration-hook");
const index_1 = require("../adapters/index");
function action(toolName, agentName = 'agent-a', orgId = 'org-x') {
    return (0, index_1.createActionContext)(toolName, {}, { name: agentName, orgId });
}
(0, node_test_1.describe)('F-NEW-PRODUCT-AGENT-SELF-REGISTRATION (W2.2)', () => {
    (0, node_test_1.it)('first observe emits AGENT_REGISTERED with the initial tool', () => {
        const events = [];
        const hook = new self_registration_hook_1.SelfRegistrationHook({ sink: (e) => events.push(e) });
        hook.observe(action('Bash'));
        assert.equal(events.length, 1);
        assert.equal(events[0].kind, 'AGENT_REGISTERED');
        assert.equal(events[0].agentId, 'agent-a');
        assert.equal(events[0].orgId, 'org-x');
        assert.equal(events[0].tool, 'Bash');
        assert.deepEqual(events[0].manifest.tools, ['Bash']);
    });
    (0, node_test_1.it)('observing the same tool twice is idempotent (no second event)', () => {
        const events = [];
        const hook = new self_registration_hook_1.SelfRegistrationHook({ sink: (e) => events.push(e) });
        hook.observe(action('Bash'));
        hook.observe(action('Bash'));
        assert.equal(events.length, 1);
    });
    (0, node_test_1.it)('observing a NEW tool on a known agent emits AGENT_TOOL_OBSERVED', () => {
        const events = [];
        const hook = new self_registration_hook_1.SelfRegistrationHook({ sink: (e) => events.push(e) });
        hook.observe(action('Bash'));
        hook.observe(action('WebFetch'));
        assert.equal(events.length, 2);
        assert.equal(events[1].kind, 'AGENT_TOOL_OBSERVED');
        assert.equal(events[1].tool, 'WebFetch');
        assert.deepEqual(events[1].manifest.tools.sort(), ['Bash', 'WebFetch']);
    });
    (0, node_test_1.it)('per-org isolation: two orgs with same agentName are separate registrations', () => {
        const events = [];
        const hook = new self_registration_hook_1.SelfRegistrationHook({ sink: (e) => events.push(e) });
        hook.observe(action('Bash', 'agent-a', 'org-1'));
        hook.observe(action('Bash', 'agent-a', 'org-2'));
        assert.equal(events.length, 2);
        assert.equal(events[0].orgId, 'org-1');
        assert.equal(events[1].orgId, 'org-2');
        assert.equal(events[0].kind, 'AGENT_REGISTERED');
        assert.equal(events[1].kind, 'AGENT_REGISTERED');
    });
    (0, node_test_1.it)('listManifests returns all registered agents', () => {
        const hook = new self_registration_hook_1.SelfRegistrationHook();
        hook.observe(action('Bash', 'a'));
        hook.observe(action('WebFetch', 'a'));
        hook.observe(action('Read', 'b'));
        const manifests = hook.listManifests();
        assert.equal(manifests.length, 2);
        const names = manifests.map((m) => m.agentId).sort();
        assert.deepEqual(names, ['a', 'b']);
    });
    (0, node_test_1.it)('getManifest returns the latest snapshot for a known agent', () => {
        const hook = new self_registration_hook_1.SelfRegistrationHook();
        hook.observe(action('Bash'));
        hook.observe(action('WebFetch'));
        const m = hook.getManifest('org-x', 'agent-a');
        assert.ok(m);
        assert.deepEqual(m?.tools.sort(), ['Bash', 'WebFetch']);
    });
    (0, node_test_1.it)('markHookWired flips the right hook flag', () => {
        const hook = new self_registration_hook_1.SelfRegistrationHook();
        hook.observe(action('Bash'));
        hook.markHookWired('org-x', 'agent-a', 'postToolUse');
        const m = hook.getManifest('org-x', 'agent-a');
        assert.equal(m?.hooks?.preToolUse, true, 'preToolUse stays true (observe came in via the hook)');
        assert.equal(m?.hooks?.postToolUse, true);
        assert.equal(m?.hooks?.postResponse, false);
    });
    (0, node_test_1.it)('defaultLlmProvider is baked into observed manifests', () => {
        const hook = new self_registration_hook_1.SelfRegistrationHook({ defaultLlmProvider: 'connexum-proxy/anthropic' });
        hook.observe(action('Bash'));
        const m = hook.getManifest('org-x', 'agent-a');
        assert.equal(m?.llmProvider, 'connexum-proxy/anthropic');
    });
    (0, node_test_1.it)('downstream: observed manifest feeds autoGenerateCompanyPack', async () => {
        const hook = new self_registration_hook_1.SelfRegistrationHook();
        hook.observe(action('Bash'));
        hook.observe(action('WebFetch'));
        const manifest = hook.getManifest('org-x', 'agent-a');
        const { autoGenerateCompanyPack } = await Promise.resolve().then(() => __importStar(require('./auto-pack-generator.js')));
        const result = autoGenerateCompanyPack(manifest, { acceptAllSuggestedRules: true });
        // Lethal trifecta -- A01 CRIT finding expected
        assert.ok(result.scanReport.findings.some((f) => f.risk.startsWith('A01')), 'A01 prompt injection detected on Bash+WebFetch agent');
    });
    (0, node_test_1.it)('clear() resets state', () => {
        const hook = new self_registration_hook_1.SelfRegistrationHook();
        hook.observe(action('Bash'));
        hook.clear();
        assert.equal(hook.listManifests().length, 0);
        assert.equal(hook.getManifest('org-x', 'agent-a'), undefined);
    });
    (0, node_test_1.it)('unknown orgId / agentName falls back gracefully', () => {
        const hook = new self_registration_hook_1.SelfRegistrationHook();
        // ActionContext with no agent / orgId set
        const ac = (0, index_1.createActionContext)('Bash', {}, { name: 'fallback-test', orgId: undefined });
        delete ac.orgId;
        delete ac.agent.orgId;
        hook.observe(ac);
        const all = hook.listManifests();
        assert.equal(all.length, 1);
    });
});
//# sourceMappingURL=self-registration-hook.test.js.map