/**
 * RED-GREEN tests for F-NEW-PRODUCT-AGENT-SELF-REGISTRATION (W2.2).
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-AGENT-SELF-REGISTRATION
 */
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import { SelfRegistrationHook } from './self-registration-hook.js';
import { createActionContext } from '../adapters/index.js';
function action(toolName, agentName = 'agent-a', orgId = 'org-x') {
    return createActionContext(toolName, {}, { name: agentName, orgId });
}
describe('F-NEW-PRODUCT-AGENT-SELF-REGISTRATION (W2.2)', () => {
    it('first observe emits AGENT_REGISTERED with the initial tool', () => {
        const events = [];
        const hook = new 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']);
    });
    it('observing the same tool twice is idempotent (no second event)', () => {
        const events = [];
        const hook = new SelfRegistrationHook({ sink: (e) => events.push(e) });
        hook.observe(action('Bash'));
        hook.observe(action('Bash'));
        assert.equal(events.length, 1);
    });
    it('observing a NEW tool on a known agent emits AGENT_TOOL_OBSERVED', () => {
        const events = [];
        const hook = new 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']);
    });
    it('per-org isolation: two orgs with same agentName are separate registrations', () => {
        const events = [];
        const hook = new 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');
    });
    it('listManifests returns all registered agents', () => {
        const hook = new 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']);
    });
    it('getManifest returns the latest snapshot for a known agent', () => {
        const hook = new 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']);
    });
    it('markHookWired flips the right hook flag', () => {
        const hook = new 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);
    });
    it('defaultLlmProvider is baked into observed manifests', () => {
        const hook = new SelfRegistrationHook({ defaultLlmProvider: 'connexum-proxy/anthropic' });
        hook.observe(action('Bash'));
        const m = hook.getManifest('org-x', 'agent-a');
        assert.equal(m?.llmProvider, 'connexum-proxy/anthropic');
    });
    it('downstream: observed manifest feeds autoGenerateCompanyPack', async () => {
        const hook = new SelfRegistrationHook();
        hook.observe(action('Bash'));
        hook.observe(action('WebFetch'));
        const manifest = hook.getManifest('org-x', 'agent-a');
        const { autoGenerateCompanyPack } = await import('./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');
    });
    it('clear() resets state', () => {
        const hook = new SelfRegistrationHook();
        hook.observe(action('Bash'));
        hook.clear();
        assert.equal(hook.listManifests().length, 0);
        assert.equal(hook.getManifest('org-x', 'agent-a'), undefined);
    });
    it('unknown orgId / agentName falls back gracefully', () => {
        const hook = new SelfRegistrationHook();
        // ActionContext with no agent / orgId set
        const ac = 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