31 lines
971 B
TypeScript
31 lines
971 B
TypeScript
/**
|
|
* DOMSnapshotCollector — writes the DOM snapshot at anomaly moment to disk.
|
|
*/
|
|
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import { IAnomaly, IAnomalyEvidence } from '../../core/interfaces';
|
|
import { ICollector, IInteractionAgent } from '../interfaces';
|
|
|
|
interface HasCaptureState {
|
|
captureState(): Promise<{ domSnapshot: string }>;
|
|
}
|
|
|
|
export class DOMSnapshotCollector implements ICollector {
|
|
readonly name = 'DOMSnapshotCollector';
|
|
|
|
constructor(private readonly outputDir: string = './reports') {}
|
|
|
|
async collect(anomaly: IAnomaly, agent: IInteractionAgent): Promise<IAnomalyEvidence> {
|
|
const state = await (agent as HasCaptureState).captureState();
|
|
|
|
const dir = path.join(this.outputDir, anomaly.id);
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
const domPath = path.join(dir, 'dom.html');
|
|
fs.writeFileSync(domPath, state.domSnapshot, 'utf8');
|
|
|
|
return { domSnapshotPath: path.relative(this.outputDir, domPath) };
|
|
}
|
|
}
|