docs: enterprise refactor plan with ralph specs
This commit is contained in:
102
tests/plugins/collectors.test.ts
Normal file
102
tests/plugins/collectors.test.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
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 = '<body><p>hello</p></body>'): 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('<body><h1>Snapshot</h1></body>');
|
||||
|
||||
const evidence = await collector.collect(anomaly, agent);
|
||||
|
||||
expect(evidence.domSnapshotPath).toBeTruthy();
|
||||
const fullPath = path.join(tmpDir, evidence.domSnapshotPath!);
|
||||
expect(fs.existsSync(fullPath)).toBe(true);
|
||||
const content = fs.readFileSync(fullPath, 'utf8');
|
||||
expect(content).toContain('<h1>Snapshot</h1>');
|
||||
});
|
||||
|
||||
it('creates output directory if it does not exist', async () => {
|
||||
const nestedDir = path.join(tmpDir, 'nested', 'output');
|
||||
const collector = new DOMSnapshotCollector(nestedDir);
|
||||
const anomaly = makeAnomaly('anom-nested');
|
||||
const agent = makeMockAgent();
|
||||
|
||||
await collector.collect(anomaly, agent);
|
||||
expect(fs.existsSync(path.join(nestedDir, 'anom-nested'))).toBe(true);
|
||||
});
|
||||
|
||||
it('has correct name', () => {
|
||||
expect(new DOMSnapshotCollector().name).toBe('DOMSnapshotCollector');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user