Agent Police Department v1.0.0 MVP

This commit is contained in:
BunBun Builder
2026-06-14 13:18:56 +00:00
commit 672e9ef17c
30 changed files with 7619 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
// tests/audit.test.js
require('../__mocks__/chrome.js');
const { addEvent, addViolation, getStats, getAuditLog, getViolations, exportAuditLog, searchAuditLog, verifyIntegrity } = require('../lib/audit.js');
describe('Audit', () => {
beforeEach(() => {
global.__chromeStore = {};
});
test('addEvent stores event', async () => {
const event = { id: 'e1', type: 'tool_use', timestamp: new Date().toISOString() };
await addEvent(event);
const log = await getAuditLog(10);
expect(log).toHaveLength(1);
expect(log[0].id).toBe('e1');
});
test('addViolation stores violation', async () => {
const violation = { id: 'v1', event_id: 'e1', detector: 'disallowed_tools', severity: 'critical', timestamp: new Date().toISOString() };
await addViolation(violation);
const list = await getViolations(10);
expect(list).toHaveLength(1);
expect(list[0].severity).toBe('critical');
});
test('getStats returns correct counts', async () => {
await addEvent({ id: 'e1', type: 'tool_use', timestamp: new Date().toISOString() });
await addEvent({ id: 'e2', type: 'api_call', timestamp: new Date().toISOString() });
await addViolation({ id: 'v1', event_id: 'e1', detector: 'disallowed_tools', severity: 'high', timestamp: new Date().toISOString() });
const stats = await getStats();
expect(stats.totalEvents).toBe(2);
expect(stats.totalViolations).toBe(1);
expect(stats.severityCounts.high).toBe(1);
});
test('exportAuditLog includes integrity hash', async () => {
await addEvent({ id: 'e1', type: 'tool_use', timestamp: new Date().toISOString() });
const exported = await exportAuditLog();
expect(exported.totalEvents).toBe(1);
expect(exported.integrity).toBeDefined();
expect(exported.integrity.length).toBeGreaterThan(0);
});
test('searchAuditLog filters by query', async () => {
await addEvent({ id: 'e1', type: 'tool_use', tool: 'read_file', target: 'app.js', timestamp: new Date().toISOString() });
await addEvent({ id: 'e2', type: 'tool_use', tool: 'edit_file', target: 'config.js', timestamp: new Date().toISOString() });
const results = await searchAuditLog('config');
expect(results).toHaveLength(1);
expect(results[0].target).toBe('config.js');
});
test('verifyIntegrity returns valid for empty log', async () => {
const result = await verifyIntegrity();
expect(result.valid).toBe(true);
expect(result.eventsChecked).toBe(0);
});
test('max events limit enforced', async () => {
for (let i = 0; i < 1100; i++) {
await addEvent({ id: `e${i}`, type: 'tool_use', timestamp: new Date().toISOString() });
}
const log = await getAuditLog(2000);
expect(log.length).toBeLessThanOrEqual(1000);
});
});