import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; import { NetworkCollector } from '../../src/plugins/collectors/NetworkCollector'; import { DOMSnapshotCollector } from '../../src/plugins/collectors/DOMSnapshotCollector'; import { IAnomaly, IAction } from '../../src/core/interfaces'; import { IInteractionAgent } from '../../src/plugins/interfaces'; function makeAnomaly(id = 'anom-001'): IAnomaly { return { id, type: 'http_error', severity: 'high', observationId: 'obs-1', actionTrace: [] as IAction[], description: 'Test anomaly', evidence: { httpLog: [{ url: '/api', status: 500, method: 'POST', durationMs: 100 }], }, timestamp: Date.now(), }; } function makeMockAgent(domSnapshot = '
hello
'): IInteractionAgent { return { launch: jest.fn(), close: jest.fn(), discoverActions: jest.fn(), executeAction: jest.fn(), captureState: jest.fn().mockResolvedValue({ id: 'state-1', url: 'http://localhost/', title: 'Test', timestamp: Date.now(), domSnapshot, visitCount: 0, }), }; } describe('NetworkCollector', () => { it('returns httpLog from anomaly evidence', async () => { const collector = new NetworkCollector(); const anomaly = makeAnomaly(); const agent = makeMockAgent(); const evidence = await collector.collect(anomaly, agent); expect(evidence.httpLog).toHaveLength(1); expect(evidence.httpLog![0].status).toBe(500); }); it('returns empty httpLog when none present', async () => { const collector = new NetworkCollector(); const anomaly: IAnomaly = { ...makeAnomaly(), evidence: {} }; const agent = makeMockAgent(); const evidence = await collector.collect(anomaly, agent); expect(evidence.httpLog).toEqual([]); }); it('has correct name', () => { expect(new NetworkCollector().name).toBe('NetworkCollector'); }); }); describe('DOMSnapshotCollector', () => { let tmpDir: string; beforeEach(() => { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'abe-test-')); }); afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); }); it('writes DOM snapshot to disk', async () => { const collector = new DOMSnapshotCollector(tmpDir); const anomaly = makeAnomaly('anom-dom'); const agent = makeMockAgent('