/**
 * RED-GREEN tests for the runtime event renderer (W3.1-support).
 *
 * @connexum/ai-governance  F-NEW-PRODUCT-RUNTIME-LIVE-VIEW
 */
import { describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import { fromReplayEvent, fromOverrideEvent, fromPackVersionEvent, fromInfoActionVerdict, sortTimelineRows, filterByMinTone, } from './runtime-event-renderer.js';
describe('runtime event renderer (W3.1 support)', () => {
    it('fromReplayEvent maps TOOL_CALL_REQUESTED with INFO severity', () => {
        const e = {
            tMs: 100,
            kind: 'TOOL_CALL_REQUESTED',
            toolName: 'Bash',
            detail: 'Agent invokes Bash(command="ls")',
            severity: 'INFO',
        };
        const row = fromReplayEvent(e, '2026-05-12T00:00:00.000Z');
        assert.equal(row.kind, 'tool-call');
        assert.equal(row.tone, 'info');
        assert.equal(row.toolName, 'Bash');
        assert.equal(row.severityLabel, 'INFO');
        assert.equal(row.tMs, 100);
        assert.equal(row.at, '2026-05-12T00:00:00.100Z');
    });
    it('fromReplayEvent maps VERDICT_DENY to verdict-deny + critical tone', () => {
        const e = {
            tMs: 500,
            kind: 'VERDICT_DENY',
            packId: 'hipaa',
            checkId: 'phi-recipient-allowlist',
            detail: 'Action denied. PHI cannot flow to non-BAA recipient.',
            severity: 'CRITICAL',
        };
        const row = fromReplayEvent(e, '2026-05-12T00:00:00.000Z');
        assert.equal(row.kind, 'verdict-deny');
        assert.equal(row.tone, 'critical');
        assert.equal(row.packId, 'hipaa');
        assert.equal(row.severityLabel, 'CRITICAL');
    });
    it('fromOverrideEvent renders an override row with baseline -> relaxed action', () => {
        const ev = {
            overrideId: 'ov-001',
            orgId: 'org-x',
            agentId: 'a',
            capability: 'capability:mcp',
            baselineAction: 'BLOCK',
            relaxedAction: 'LOG',
            approver: 'ciso-alice',
            justification: 'MCP is internal-only',
            approvedAt: '2026-05-12T10:00:00.000Z',
            composedAt: '2026-05-12T10:00:01.000Z',
        };
        const row = fromOverrideEvent(ev);
        assert.equal(row.kind, 'override');
        assert.equal(row.tone, 'warn');
        assert.match(row.headline, /capability:mcp/);
        assert.match(row.headline, /BLOCK → LOG/);
        assert.equal(row.actor, 'ciso-alice');
    });
    it('fromPackVersionEvent maps PACK_ROLLED_BACK to warn tone', () => {
        const ev = {
            kind: 'PACK_ROLLED_BACK',
            packId: 'co-pack',
            version: '1.0.0',
            previousVersion: '1.1.0',
            actor: 'ops-bob',
            timestamp: '2026-05-12T11:00:00.000Z',
        };
        const row = fromPackVersionEvent(ev);
        assert.equal(row.kind, 'pack-version-change');
        assert.equal(row.tone, 'warn');
        assert.match(row.headline, /rolled back to 1.0.0/);
        assert.equal(row.actor, 'ops-bob');
        assert.equal(row.severityLabel, 'WARN');
    });
    it('fromPackVersionEvent maps VERSION_ACTIVATED to notice tone', () => {
        const ev = {
            kind: 'VERSION_ACTIVATED',
            packId: 'co-pack',
            version: '1.1.0',
            actor: 'alice',
            timestamp: '2026-05-12T11:30:00.000Z',
        };
        const row = fromPackVersionEvent(ev);
        assert.equal(row.tone, 'notice');
        assert.match(row.headline, /activated/);
    });
    it('fromInfoActionVerdict maps DENY to critical', () => {
        const v = {
            verdict: 'DENY',
            mode: 'strict',
            contributingPackIds: ['pack-1'],
            reason: 'consequential action blocked',
        };
        const row = fromInfoActionVerdict(v, {
            at: '2026-05-12T12:00:00.000Z',
            toolName: 'Bash',
        });
        assert.equal(row.kind, 'verdict-deny');
        assert.equal(row.tone, 'critical');
        assert.match(row.headline, /Info\/Action separation/);
        assert.equal(row.toolName, 'Bash');
    });
    it('sortTimelineRows orders by ISO timestamp ascending', () => {
        const rows = [
            makeRow('2026-05-12T01:00:00.000Z'),
            makeRow('2026-05-12T00:00:00.000Z'),
            makeRow('2026-05-12T00:30:00.000Z'),
        ];
        const sorted = sortTimelineRows(rows);
        assert.deepEqual(sorted.map((r) => r.at), [
            '2026-05-12T00:00:00.000Z',
            '2026-05-12T00:30:00.000Z',
            '2026-05-12T01:00:00.000Z',
        ]);
    });
    it('filterByMinTone drops below-threshold rows', () => {
        const rows = [
            makeRow('t1', 'info'),
            makeRow('t2', 'warn'),
            makeRow('t3', 'critical'),
            makeRow('t4', 'notice'),
        ];
        const filtered = filterByMinTone(rows, 'warn');
        assert.equal(filtered.length, 2); // warn + critical
        assert.ok(filtered.every((r) => r.tone === 'warn' || r.tone === 'critical'));
    });
    it('headline truncates over 80 chars', () => {
        const e = {
            tMs: 0,
            kind: 'AUDIT_RECORDED',
            detail: 'x'.repeat(150),
            severity: 'INFO',
        };
        const row = fromReplayEvent(e, '2026-05-12T00:00:00.000Z');
        assert.ok(row.headline.length <= 80);
        assert.ok(row.headline.endsWith('...'));
    });
    it('severity overrides the default kind tone', () => {
        const e = {
            tMs: 0,
            kind: 'AUDIT_RECORDED', // default tone: info
            detail: 'incident filed: room double-booked',
            severity: 'CRITICAL',
        };
        const row = fromReplayEvent(e, '2026-05-12T00:00:00.000Z');
        assert.equal(row.tone, 'critical', 'severity CRITICAL overrides AUDIT_RECORDED default info');
    });
});
function makeRow(at, tone = 'info') {
    return {
        at,
        kind: 'audit',
        tone,
        headline: 'h',
        severityLabel: tone === 'info' ? 'INFO' : tone === 'notice' ? 'NOTICE' : tone === 'warn' ? 'WARN' : tone === 'error' ? 'ERROR' : 'CRITICAL',
    };
}
//# sourceMappingURL=runtime-event-renderer.test.js.map