import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const report = JSON.parse(await readFile(resolve(root, 'config/chrome-fault-injection-verification.json'), 'utf8'))
if (report.schemaVersion !== 1 || report.browserId !== 'chrome' || report.executionContext !== 'dedicated-worker' || report.status !== 'pass' || report.crossOriginIsolated !== true || report.sharedArrayBuffer !== true) throw new Error('Chrome QA-06 Worker/WASM isolation evidence is invalid.')
const lifecycle = report.lifecycle
if (!lifecycle || lifecycle.initialAvailable !== true || lifecycle.completed !== 'completed' || lifecycle.cancelled !== 'cancelled' || lifecycle.crashInjected !== true || lifecycle.unavailableAfterCrash !== true || lifecycle.workerFactoryCount < 3 || lifecycle.recovered !== 'completed' || lifecycle.stale !== 'stale' || Math.abs(lifecycle.recoveredEnd?.x - 5) > 1e-7 || Math.abs(lifecycle.recoveredEnd?.y) > 1e-7) throw new Error('Chrome QA-06 cancellation, crash recovery, or stale-result evidence is invalid.')
if (lifecycle.nativeInitialAvailable !== true || lifecycle.nativeCrashInjected !== true || lifecycle.nativeUnavailableAfterCrash !== true || lifecycle.nativeWorkerFactoryCount < 2 || lifecycle.nativeRecoveredAvailable !== true) throw new Error('Chrome QA-06 native history Worker crash recovery evidence is invalid.')
if (!report.opfs || report.opfs.markerBytes !== 0 || report.opfs.markerRemoved !== true || !Number.isSafeInteger(report.activeWorkersBeforeDispose) || report.activeWorkersBeforeDispose < 3 || report.activeWorkersAfterDispose !== report.activeWorkersBeforeDispose || !Number.isSafeInteger(report.activeNativeWorkersBeforeDispose) || report.activeNativeWorkersBeforeDispose < 2 || report.activeNativeWorkersAfterDispose !== report.activeNativeWorkersBeforeDispose) throw new Error('Chrome QA-06 OPFS or Worker resource cleanup evidence is invalid.')
console.log(JSON.stringify({ status: 'chrome-fault-injection-pass', browserId: report.browserId, lifecycle, opfs: report.opfs, nativeWorkers: { beforeDispose: report.activeNativeWorkersBeforeDispose, afterDispose: report.activeNativeWorkersAfterDispose } }, null, 2))
