23f7624596
ADR-166 MCP Bridge Security Lock / Static-source security lock (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / Compose default binds loopback + Mongo has auth (push) Failing after 2s
CodeQL Advanced / Analyze (rust) (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / plugin-agent-federation bindHost default (push) Failing after 1s
ADR-166 MCP Bridge Security Lock / Runtime behavior — 401 + terminal gate + fail-closed (push) Failing after 4s
business-pods-smoke / smoke (push) Failing after 1s
all-plugins-smoke / smoke-all (push) Failing after 2s
CI/CD Pipeline / Security & Code Quality (push) Failing after 1s
CI/CD Pipeline / Test Suite (ubuntu-latest) (push) Failing after 1s
CI/CD Pipeline / Build & Package (macos-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (ubuntu-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (windows-latest) (push) Has been skipped
CI/CD Pipeline / Documentation & Examples (push) Failing after 1s
Clone Tracker (14-day rolling) / Snapshot clones for ruflo ecosystem (push) Failing after 1s
CodeQL Advanced / Analyze (actions) (push) Failing after 1s
CodeQL Advanced / Analyze (javascript-typescript) (push) Failing after 1s
federation-peer-rust / stable-noop (push) Failing after 1s
metaharness-ci / score (push) Failing after 1s
metaharness-ci / router-compat (push) Failing after 0s
metaharness-ci / similarity-tests (push) Failing after 0s
no-agentbbs-smoke / smoke-without-agentbbs (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (windows-latest) (push) Has been skipped
codex-integration-audit / Codex integration audit (push) Failing after 1s
helpers-manifest-guard / guard (push) Failing after 1s
🔗 Cross-Agent Integration Tests / 🤝 Agent Coordination Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🧠 Memory Sharing Integration (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🛡️ Fault Tolerance Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / ⚡ Performance Integration Tests (push) Has been skipped
metaharness-ci / mcp-scan (push) Failing after 1s
metaharness-ci / eject-dryrun (push) Failing after 1s
metaharness-ci / metaharness-real-data (push) Failing after 0s
no-cli-optdep-bloat-2561 / guard (push) Failing after 1s
no-metaharness-smoke / smoke-without-metaharness (push) Failing after 1s
no-phantom-agentic-flow-subpath / guard (push) Failing after 1s
🔄 Automated Rollback Manager / 🚨 Failure Detection (push) Failing after 1s
V3 CI/CD Pipeline / Plugin hooks smoke / ubuntu-latest / Node 22 (push) Failing after 1s
V3 CI/CD Pipeline / ruflo-graph-intelligence build + test smoke (#2044, ADR-123) (push) Failing after 1s
CVE Audit Gate / Audit root (critical-blocking) (push) Failing after 2s
cost-tracker-smoke / smoke (push) Failing after 3s
oia-audit-weekly / audit (push) Failing after 2s
ruflo-agent-smoke / ruflo-agent structural smoke (push) Failing after 1s
📊 Status Badges Update / 📊 Update Status Badges (push) Failing after 1s
V3 CI/CD Pipeline / Static regression guards (#2267 YAML + (push) Failing after 1s
V3 CI/CD Pipeline / Test V3 Packages (push) Failing after 0s
V3 CI/CD Pipeline / agent_execute provider routing smoke (#2042) (push) Failing after 0s
CVE Audit Gate / Audit v3 (critical-blocking) (push) Failing after 1s
federation-peer-rust / stable-native (push) Failing after 2s
🔗 Cross-Agent Integration Tests / 🚀 Integration Test Setup (push) Failing after 2s
neural-trader-smoke / runtime-smoke (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (macos-latest) (push) Has been skipped
V3 CI/CD Pipeline / Build V3 (ubuntu-latest) (push) Has been skipped
V3 CI/CD Pipeline / Type Check V3 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 24 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 22 (push) Failing after 2s
V3 CI/CD Pipeline / browser rvf create flag smoke (#2015) (push) Failing after 0s
V3 CI/CD Pipeline / Dependency review (#2046) (push) Has been skipped
V3 CI/CD Pipeline / Supply-chain audit (#2046) (push) Failing after 0s
V3 CI/CD Pipeline / witness marker drift smoke (#2021) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader portfolio CG smoke (#2068, ADR-126 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader backtest signing smoke (#2068, ADR-126 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / kg-extract type-import classification smoke (#2049) (push) Failing after 0s
V3 CI/CD Pipeline / witness verify precondition smoke (#1880) (push) Failing after 2s
V3 CI/CD Pipeline / neural-trader pipeline risk-gate smoke (#2068, ADR-126 Phase 5) (push) Failing after 0s
V3 CI/CD Pipeline / neural-trader feature attribution smoke (#2068, ADR-126 Phase 6) (push) Failing after 0s
V3 CI/CD Pipeline / plugin-registry signature verification smoke (#1922, CWE-347) (push) Failing after 4s
V3 CI/CD Pipeline / memory stats legacy-DB smoke (#2120) (push) Failing after 4s
V3 CI/CD Pipeline / github deprecated actions smoke (#2089, ADR-127 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / graph query + pathfinder smoke (ADR-130 P2+P5) (push) Has been skipped
V3 CI/CD Pipeline / graph trajectory hooks smoke (ADR-130 P3) (push) Has been skipped
V3 CI/CD Pipeline / graph plugin adapter smoke (ADR-130 P4) (push) Has been skipped
V3 CI/CD Pipeline / graph benchmark (ADR-130 P6) (push) Has been skipped
V3 CI/CD Pipeline / statusline generator delegation smoke (#2195) (push) Failing after 1s
V3 CI/CD Pipeline / wizard init regression guard (#2206 (push) Failing after 1s
V3 CI/CD Pipeline / memory no-stray-db smoke (ADR-125 P7) (push) Failing after 1s
V3 CI/CD Pipeline / github-safe injection smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github actions pin smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github attribution opt-in smoke (#2089, ADR-127 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / pre-bash hook safety smoke (#2017) (push) Failing after 1s
V3 CI/CD Pipeline / Memory import smoke / ubuntu-latest (push) Failing after 0s
V3 CI/CD Pipeline / MCP protocol smoke / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / ruvllm WASM auto-init smoke (#2086) (push) Failing after 4s
V3 CI/CD Pipeline / MCP paired-tool round-trip smoke (#1889) (push) Failing after 1s
V3 CI/CD Pipeline / Plugin package install-safety (#1902/#1903/#1904) (push) Failing after 1s
V3 CI/CD Pipeline / Tool description discoverability (ADR-112) (push) Failing after 3s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (22) (push) Failing after 1s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (24) (push) Failing after 1s
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Vector-index dimension audit (#1947) (push) Failing after 0s
V3 CI/CD Pipeline / Hook-command install safety (#1921) (push) Failing after 1s
V3 CI/CD Pipeline / ToolOutputGuardrail smoke (ADR-131, (push) Failing after 1s
V3 CI/CD Pipeline / init-bundle invariants smoke (#2095, ADR-128 Phase 5) (push) Failing after 1s
V3 CI/CD Pipeline / wasm provider bridge smoke (ADR-129 P1) (push) Failing after 2s
V3 CI/CD Pipeline / wasm gallery CRUD smoke (ADR-129 P3) (push) Failing after 1s
V3 CI/CD Pipeline / wasm plugin bridge smoke (ADR-129 P4) (push) Failing after 0s
V3 CI/CD Pipeline / wasm compose smoke (ADR-129 P2) (push) Failing after 4s
V3 CI/CD Pipeline / graph schema smoke (ADR-130 P1) (push) Failing after 0s
Validate Marketplace / validate (push) Failing after 1s
🔍 Verification Pipeline / 🚀 Setup Verification (push) Failing after 1s
🔍 Verification Pipeline / 🛡️ Security Verification (push) Has been skipped
🔍 Verification Pipeline / 📝 Code Quality (push) Has been skipped
🔍 Verification Pipeline / 🧪 Test Verification (${{ matrix.os }}, Node ${{ matrix.node }}) (push) Has been skipped
🔍 Verification Pipeline / 🏗️ Build Verification (push) Has been skipped
🔍 Verification Pipeline / 📚 Documentation Verification (push) Has been skipped
CVE Audit Gate / High-severity report (warn only) (push) Has been cancelled
🔄 Automated Rollback Manager / 🔄 Execute Rollback (push) Has been cancelled
🔄 Automated Rollback Manager / ✅ Post-Rollback Verification (push) Has been cancelled
🔄 Automated Rollback Manager / 📊 Rollback Monitoring (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / windows-latest (push) Has been cancelled
🔄 Automated Rollback Manager / ⏳ Manual Rollback Approval (push) Has been cancelled
V3 CI/CD Pipeline / MCP protocol smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Memory import smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / ubuntu-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Publish to npm (alpha) (push) Has been cancelled
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / macos-latest / Node 22 (push) Has been cancelled
V3 CI/CD Pipeline / Plugin hooks smoke / macos-latest / Node 22 (push) Has been cancelled
CI/CD Pipeline / Deploy & Release (push) Has been cancelled
CI/CD Pipeline / CI Status (push) Has been cancelled
🔗 Cross-Agent Integration Tests / 📊 Integration Test Report (push) Has been cancelled
🔄 Automated Rollback Manager / 🔍 Pre-Rollback Validation (push) Has been cancelled
🔍 Verification Pipeline / ⚡ Performance Verification (push) Has been cancelled
🔍 Verification Pipeline / 📊 Verification Report (push) Has been cancelled
561 lines
20 KiB
TypeScript
561 lines
20 KiB
TypeScript
/**
|
|
* AuditService Tests
|
|
*
|
|
* Tests the real AuditService from source with real dependency implementations.
|
|
* No mocks, no simulations, no local reimplementations.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import {
|
|
AuditService,
|
|
type FederationAuditEventType,
|
|
type AuditSeverity,
|
|
type AuditCategory,
|
|
type ComplianceMode,
|
|
type FederationAuditEvent,
|
|
type AuditQuery,
|
|
type AuditExportFormat,
|
|
type AuditServiceDeps,
|
|
} from '../../src/domain/services/audit-service.js';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Real dependency implementations for testing
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function createTestDeps(overrides?: Partial<AuditServiceDeps>): {
|
|
deps: AuditServiceDeps;
|
|
persisted: FederationAuditEvent[];
|
|
} {
|
|
let idCounter = 0;
|
|
const persisted: FederationAuditEvent[] = [];
|
|
|
|
const deps: AuditServiceDeps = {
|
|
generateEventId: () => `evt-${++idCounter}`,
|
|
getLocalNodeId: () => 'test-node-1',
|
|
persistEvent: async (event: FederationAuditEvent) => {
|
|
persisted.push(event);
|
|
},
|
|
queryEvents: async (query: AuditQuery) => {
|
|
let results = [...persisted];
|
|
if (query.eventType) results = results.filter(e => e.eventType === query.eventType);
|
|
if (query.severity) results = results.filter(e => e.severity === query.severity);
|
|
if (query.category) results = results.filter(e => e.category === query.category);
|
|
if (query.nodeId) results = results.filter(e => e.nodeId === query.nodeId);
|
|
if (query.sessionId) results = results.filter(e => e.sessionId === query.sessionId);
|
|
if (query.since) {
|
|
const since = query.since.getTime();
|
|
results = results.filter(e => new Date(e.timestamp).getTime() >= since);
|
|
}
|
|
if (query.until) {
|
|
const until = query.until.getTime();
|
|
results = results.filter(e => new Date(e.timestamp).getTime() <= until);
|
|
}
|
|
if (query.offset) results = results.slice(query.offset);
|
|
if (query.limit) results = results.slice(0, query.limit);
|
|
return results;
|
|
},
|
|
...overrides,
|
|
};
|
|
|
|
return { deps, persisted };
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Expected severity and category mappings (from source)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const EXPECTED_SEVERITY: Record<FederationAuditEventType, AuditSeverity> = {
|
|
peer_discovered: 'info',
|
|
peer_manifest_published: 'info',
|
|
handshake_initiated: 'info',
|
|
handshake_completed: 'info',
|
|
handshake_failed: 'warn',
|
|
handshake_rejected: 'warn',
|
|
session_created: 'info',
|
|
session_renewed: 'info',
|
|
session_expired: 'info',
|
|
session_terminated: 'info',
|
|
message_sent: 'info',
|
|
message_received: 'info',
|
|
message_rejected: 'warn',
|
|
message_timeout: 'warn',
|
|
pii_detected: 'warn',
|
|
pii_stripped: 'info',
|
|
pii_blocked: 'warn',
|
|
threat_detected: 'error',
|
|
threat_blocked: 'critical',
|
|
threat_learned: 'info',
|
|
claim_checked: 'info',
|
|
claim_denied: 'warn',
|
|
trust_level_changed: 'warn',
|
|
consensus_proposed: 'info',
|
|
consensus_voted: 'info',
|
|
consensus_reached: 'info',
|
|
consensus_failed: 'warn',
|
|
};
|
|
|
|
const EXPECTED_CATEGORY: Record<FederationAuditEventType, AuditCategory> = {
|
|
peer_discovered: 'discovery',
|
|
peer_manifest_published: 'discovery',
|
|
handshake_initiated: 'handshake',
|
|
handshake_completed: 'handshake',
|
|
handshake_failed: 'handshake',
|
|
handshake_rejected: 'handshake',
|
|
session_created: 'handshake',
|
|
session_renewed: 'handshake',
|
|
session_expired: 'handshake',
|
|
session_terminated: 'handshake',
|
|
message_sent: 'message',
|
|
message_received: 'message',
|
|
message_rejected: 'message',
|
|
message_timeout: 'message',
|
|
pii_detected: 'pii',
|
|
pii_stripped: 'pii',
|
|
pii_blocked: 'pii',
|
|
threat_detected: 'security',
|
|
threat_blocked: 'security',
|
|
threat_learned: 'security',
|
|
claim_checked: 'security',
|
|
claim_denied: 'security',
|
|
trust_level_changed: 'security',
|
|
consensus_proposed: 'consensus',
|
|
consensus_voted: 'consensus',
|
|
consensus_reached: 'consensus',
|
|
consensus_failed: 'consensus',
|
|
};
|
|
|
|
const ALL_EVENT_TYPES: FederationAuditEventType[] = Object.keys(EXPECTED_SEVERITY) as FederationAuditEventType[];
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Tests
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('AuditService', () => {
|
|
// -----------------------------------------------------------------------
|
|
// log — basic event creation
|
|
// -----------------------------------------------------------------------
|
|
describe('log', () => {
|
|
let service: AuditService;
|
|
let persisted: FederationAuditEvent[];
|
|
|
|
beforeEach(() => {
|
|
const t = createTestDeps();
|
|
persisted = t.persisted;
|
|
service = new AuditService(t.deps);
|
|
});
|
|
|
|
it('should create an event with eventId from generateEventId', async () => {
|
|
const event = await service.log('peer_discovered');
|
|
expect(event.eventId).toBe('evt-1');
|
|
});
|
|
|
|
it('should generate unique ids for consecutive events', async () => {
|
|
const e1 = await service.log('peer_discovered');
|
|
const e2 = await service.log('peer_discovered');
|
|
expect(e1.eventId).not.toBe(e2.eventId);
|
|
});
|
|
|
|
it('should include an ISO 8601 timestamp', async () => {
|
|
const event = await service.log('handshake_initiated');
|
|
expect(event.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
|
|
});
|
|
|
|
it('should include the nodeId from getLocalNodeId', async () => {
|
|
const event = await service.log('message_sent');
|
|
expect(event.nodeId).toBe('test-node-1');
|
|
});
|
|
|
|
it('should set complianceMode from config (default none)', async () => {
|
|
const event = await service.log('peer_discovered');
|
|
expect(event.complianceMode).toBe('none');
|
|
});
|
|
|
|
it('should set complianceMode from config when provided', async () => {
|
|
const t = createTestDeps();
|
|
const svc = new AuditService(t.deps, { complianceMode: 'soc2' });
|
|
const event = await svc.log('peer_discovered');
|
|
expect(event.complianceMode).toBe('soc2');
|
|
});
|
|
|
|
it('should merge additional details into the event', async () => {
|
|
const event = await service.log('message_sent', {
|
|
sourceNodeId: 'node-A',
|
|
targetNodeId: 'node-B',
|
|
latencyMs: 42,
|
|
});
|
|
expect(event.sourceNodeId).toBe('node-A');
|
|
expect(event.targetNodeId).toBe('node-B');
|
|
expect(event.latencyMs).toBe(42);
|
|
});
|
|
|
|
it('should return a FederationAuditEvent with correct eventType', async () => {
|
|
const event = await service.log('consensus_proposed');
|
|
expect(event.eventType).toBe('consensus_proposed');
|
|
});
|
|
});
|
|
|
|
// -----------------------------------------------------------------------
|
|
// Severity and category mappings for all 26 event types
|
|
// -----------------------------------------------------------------------
|
|
describe('severity mapping', () => {
|
|
let service: AuditService;
|
|
|
|
beforeEach(() => {
|
|
const t = createTestDeps();
|
|
service = new AuditService(t.deps);
|
|
});
|
|
|
|
it.each(ALL_EVENT_TYPES)(
|
|
'should assign severity "%s" -> %s',
|
|
async (eventType) => {
|
|
const event = await service.log(eventType);
|
|
expect(event.severity).toBe(EXPECTED_SEVERITY[eventType]);
|
|
},
|
|
);
|
|
});
|
|
|
|
describe('category mapping', () => {
|
|
let service: AuditService;
|
|
|
|
beforeEach(() => {
|
|
const t = createTestDeps();
|
|
service = new AuditService(t.deps);
|
|
});
|
|
|
|
it.each(ALL_EVENT_TYPES)(
|
|
'should assign category "%s" -> %s',
|
|
async (eventType) => {
|
|
const event = await service.log(eventType);
|
|
expect(event.category).toBe(EXPECTED_CATEGORY[eventType]);
|
|
},
|
|
);
|
|
});
|
|
|
|
// -----------------------------------------------------------------------
|
|
// Buffer and flush behavior
|
|
// -----------------------------------------------------------------------
|
|
describe('buffer', () => {
|
|
it('should track buffer size via getBufferSize', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps, { batchSize: 200 });
|
|
expect(service.getBufferSize()).toBe(0);
|
|
await service.log('peer_discovered');
|
|
expect(service.getBufferSize()).toBe(1);
|
|
await service.log('peer_discovered');
|
|
expect(service.getBufferSize()).toBe(2);
|
|
});
|
|
|
|
it('should flush buffer when it reaches batchSize', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps, { batchSize: 3 });
|
|
|
|
await service.log('peer_discovered');
|
|
await service.log('peer_discovered');
|
|
expect(service.getBufferSize()).toBe(2);
|
|
expect(t.persisted).toHaveLength(0);
|
|
|
|
// Third event triggers flush
|
|
await service.log('peer_discovered');
|
|
expect(service.getBufferSize()).toBe(0);
|
|
expect(t.persisted).toHaveLength(3);
|
|
});
|
|
|
|
it('should flush immediately on critical severity', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps, { batchSize: 100 });
|
|
|
|
await service.log('peer_discovered'); // info — stays in buffer
|
|
expect(service.getBufferSize()).toBe(1);
|
|
expect(t.persisted).toHaveLength(0);
|
|
|
|
// threat_blocked is critical — triggers immediate flush
|
|
await service.log('threat_blocked');
|
|
expect(service.getBufferSize()).toBe(0);
|
|
expect(t.persisted).toHaveLength(2);
|
|
});
|
|
|
|
it('should flush all buffered events via flush()', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps, { batchSize: 200 });
|
|
|
|
await service.log('peer_discovered');
|
|
await service.log('message_sent');
|
|
await service.log('handshake_initiated');
|
|
expect(service.getBufferSize()).toBe(3);
|
|
expect(t.persisted).toHaveLength(0);
|
|
|
|
await service.flush();
|
|
expect(service.getBufferSize()).toBe(0);
|
|
expect(t.persisted).toHaveLength(3);
|
|
});
|
|
|
|
it('should persist events via deps.persistEvent', async () => {
|
|
const persistEvent = vi.fn(async () => {});
|
|
const t = createTestDeps({ persistEvent });
|
|
const service = new AuditService(t.deps, { batchSize: 1 });
|
|
|
|
await service.log('peer_discovered');
|
|
expect(persistEvent).toHaveBeenCalledTimes(1);
|
|
expect(persistEvent).toHaveBeenCalledWith(
|
|
expect.objectContaining({ eventType: 'peer_discovered' }),
|
|
);
|
|
});
|
|
});
|
|
|
|
// -----------------------------------------------------------------------
|
|
// onAuditEvent callback
|
|
// -----------------------------------------------------------------------
|
|
describe('onAuditEvent callback', () => {
|
|
it('should call onAuditEvent for every logged event', async () => {
|
|
const onAuditEvent = vi.fn();
|
|
const t = createTestDeps({ onAuditEvent });
|
|
const service = new AuditService(t.deps, { batchSize: 200 });
|
|
|
|
const event = await service.log('peer_discovered');
|
|
expect(onAuditEvent).toHaveBeenCalledTimes(1);
|
|
expect(onAuditEvent).toHaveBeenCalledWith(event);
|
|
});
|
|
|
|
it('should fire onAuditEvent before flushing', async () => {
|
|
const callOrder: string[] = [];
|
|
const onAuditEvent = vi.fn(() => callOrder.push('callback'));
|
|
const persistEvent = vi.fn(async () => { callOrder.push('persist'); });
|
|
const t = createTestDeps({ onAuditEvent, persistEvent });
|
|
const service = new AuditService(t.deps, { batchSize: 1 });
|
|
|
|
await service.log('peer_discovered');
|
|
expect(callOrder[0]).toBe('callback');
|
|
expect(callOrder[1]).toBe('persist');
|
|
});
|
|
});
|
|
|
|
// -----------------------------------------------------------------------
|
|
// HIPAA compliance
|
|
// -----------------------------------------------------------------------
|
|
describe('HIPAA compliance', () => {
|
|
let service: AuditService;
|
|
let persisted: FederationAuditEvent[];
|
|
|
|
beforeEach(() => {
|
|
const t = createTestDeps();
|
|
persisted = t.persisted;
|
|
service = new AuditService(t.deps, { complianceMode: 'hipaa' });
|
|
});
|
|
|
|
it('should strip rawContent from metadata on PII events', async () => {
|
|
const event = await service.log('pii_detected', {
|
|
piiDetected: true,
|
|
metadata: { rawContent: 'SSN: 123-45-6789', context: 'scan' },
|
|
});
|
|
expect(event.metadata).not.toHaveProperty('rawContent');
|
|
expect(event.metadata).toHaveProperty('context', 'scan');
|
|
});
|
|
|
|
it('should strip originalValue from metadata on PII events', async () => {
|
|
const event = await service.log('pii_stripped', {
|
|
piiDetected: true,
|
|
metadata: { originalValue: 'john@example.com', action: 'strip' },
|
|
});
|
|
expect(event.metadata).not.toHaveProperty('originalValue');
|
|
expect(event.metadata).toHaveProperty('action', 'strip');
|
|
});
|
|
|
|
it('should strip both rawContent and originalValue together', async () => {
|
|
const event = await service.log('pii_blocked', {
|
|
piiDetected: true,
|
|
metadata: {
|
|
rawContent: 'sensitive',
|
|
originalValue: 'also-sensitive',
|
|
typesFound: ['email'],
|
|
},
|
|
});
|
|
expect(event.metadata).not.toHaveProperty('rawContent');
|
|
expect(event.metadata).not.toHaveProperty('originalValue');
|
|
expect(event.metadata).toHaveProperty('typesFound');
|
|
});
|
|
|
|
it('should not strip metadata when piiDetected is false', async () => {
|
|
const event = await service.log('message_sent', {
|
|
metadata: { rawContent: 'keep-me' },
|
|
});
|
|
expect(event.metadata).toHaveProperty('rawContent', 'keep-me');
|
|
});
|
|
|
|
it('should not strip metadata when there is no metadata', async () => {
|
|
const event = await service.log('pii_detected', { piiDetected: true });
|
|
expect(event.metadata).toBeUndefined();
|
|
});
|
|
|
|
it('should set complianceMode to hipaa on all events', async () => {
|
|
const event = await service.log('peer_discovered');
|
|
expect(event.complianceMode).toBe('hipaa');
|
|
});
|
|
});
|
|
|
|
// -----------------------------------------------------------------------
|
|
// query
|
|
// -----------------------------------------------------------------------
|
|
describe('query', () => {
|
|
let service: AuditService;
|
|
let persisted: FederationAuditEvent[];
|
|
|
|
beforeEach(async () => {
|
|
const t = createTestDeps();
|
|
persisted = t.persisted;
|
|
service = new AuditService(t.deps, { batchSize: 200 });
|
|
|
|
await service.log('peer_discovered');
|
|
// threat_blocked is critical — triggers immediate flush of buffer (2 events)
|
|
await service.log('threat_blocked', { metadata: { reason: 'malicious' } });
|
|
await service.log('pii_detected', { piiDetected: true });
|
|
await service.log('peer_manifest_published');
|
|
await service.log('message_sent');
|
|
});
|
|
|
|
it('should flush the buffer before querying', async () => {
|
|
// 3 events remain buffered (pii_detected, peer_manifest_published, message_sent)
|
|
// The first 2 were flushed when threat_blocked (critical) was logged
|
|
expect(service.getBufferSize()).toBe(3);
|
|
const results = await service.query({});
|
|
expect(service.getBufferSize()).toBe(0);
|
|
// All 5 events are now persisted and queryable
|
|
expect(results).toHaveLength(5);
|
|
});
|
|
|
|
it('should filter by eventType', async () => {
|
|
const results = await service.query({ eventType: 'peer_discovered' });
|
|
expect(results).toHaveLength(1);
|
|
expect(results[0].eventType).toBe('peer_discovered');
|
|
});
|
|
|
|
it('should filter by severity', async () => {
|
|
const results = await service.query({ severity: 'critical' });
|
|
expect(results).toHaveLength(1);
|
|
expect(results[0].eventType).toBe('threat_blocked');
|
|
});
|
|
|
|
it('should filter by category', async () => {
|
|
const results = await service.query({ category: 'discovery' });
|
|
expect(results).toHaveLength(2);
|
|
});
|
|
|
|
it('should respect limit', async () => {
|
|
const results = await service.query({ limit: 2 });
|
|
expect(results).toHaveLength(2);
|
|
});
|
|
|
|
it('should return empty array when no events match', async () => {
|
|
const results = await service.query({ eventType: 'consensus_failed' });
|
|
expect(results).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
// -----------------------------------------------------------------------
|
|
// export
|
|
// -----------------------------------------------------------------------
|
|
describe('export', () => {
|
|
let service: AuditService;
|
|
|
|
beforeEach(async () => {
|
|
const t = createTestDeps();
|
|
service = new AuditService(t.deps, { batchSize: 200 });
|
|
await service.log('peer_discovered', { sourceNodeId: 'node-A' });
|
|
await service.log('message_sent', { targetNodeId: 'node-B' });
|
|
});
|
|
|
|
it('should export as json (pretty-printed array)', async () => {
|
|
const output = await service.export({}, 'json');
|
|
const parsed = JSON.parse(output);
|
|
expect(Array.isArray(parsed)).toBe(true);
|
|
expect(parsed).toHaveLength(2);
|
|
expect(parsed[0].eventType).toBe('peer_discovered');
|
|
});
|
|
|
|
it('should export as ndjson (one JSON object per line)', async () => {
|
|
const output = await service.export({}, 'ndjson');
|
|
const lines = output.split('\n');
|
|
expect(lines).toHaveLength(2);
|
|
expect(JSON.parse(lines[0]).eventType).toBe('peer_discovered');
|
|
expect(JSON.parse(lines[1]).eventType).toBe('message_sent');
|
|
});
|
|
|
|
it('should export as csv with headers', async () => {
|
|
const output = await service.export({}, 'csv');
|
|
const lines = output.split('\n');
|
|
expect(lines[0]).toContain('eventId');
|
|
expect(lines[0]).toContain('timestamp');
|
|
expect(lines[0]).toContain('severity');
|
|
expect(lines).toHaveLength(3); // header + 2 data rows
|
|
});
|
|
|
|
it('should return empty string for csv with no matching events', async () => {
|
|
const output = await service.export({ eventType: 'consensus_failed' }, 'csv');
|
|
expect(output).toBe('');
|
|
});
|
|
});
|
|
|
|
// -----------------------------------------------------------------------
|
|
// getBufferSize
|
|
// -----------------------------------------------------------------------
|
|
describe('getBufferSize', () => {
|
|
it('should return 0 initially', () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps);
|
|
expect(service.getBufferSize()).toBe(0);
|
|
});
|
|
|
|
it('should increment after each log (when batchSize is large)', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps, { batchSize: 500 });
|
|
await service.log('peer_discovered');
|
|
expect(service.getBufferSize()).toBe(1);
|
|
await service.log('message_sent');
|
|
expect(service.getBufferSize()).toBe(2);
|
|
});
|
|
|
|
it('should reset to 0 after flush', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps, { batchSize: 500 });
|
|
await service.log('peer_discovered');
|
|
await service.log('peer_discovered');
|
|
await service.flush();
|
|
expect(service.getBufferSize()).toBe(0);
|
|
});
|
|
});
|
|
|
|
// -----------------------------------------------------------------------
|
|
// Config defaults
|
|
// -----------------------------------------------------------------------
|
|
describe('config defaults', () => {
|
|
it('should default complianceMode to none', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps);
|
|
const event = await service.log('peer_discovered');
|
|
expect(event.complianceMode).toBe('none');
|
|
});
|
|
|
|
it('should default batchSize to 100', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps);
|
|
// Log 99 events — buffer should not flush
|
|
for (let i = 0; i < 99; i++) {
|
|
await service.log('peer_discovered');
|
|
}
|
|
expect(service.getBufferSize()).toBe(99);
|
|
expect(t.persisted).toHaveLength(0);
|
|
|
|
// 100th event triggers flush
|
|
await service.log('peer_discovered');
|
|
expect(service.getBufferSize()).toBe(0);
|
|
expect(t.persisted).toHaveLength(100);
|
|
});
|
|
|
|
it('should allow overriding dataResidency', async () => {
|
|
const t = createTestDeps();
|
|
const service = new AuditService(t.deps, { dataResidency: 'us-east-1' });
|
|
const event = await service.log('peer_discovered');
|
|
expect(event.dataResidency).toBe('us-east-1');
|
|
});
|
|
});
|
|
});
|