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-native-revolution-history-verification.json'), 'utf8'))
if (report.status !== 'pass' || report.browserId !== 'chrome' || report.crossOriginIsolated !== true) throw new Error('Chrome native Revolution history verification is not passing.')
if (report.capabilities?.status !== 'ready' || report.nativeCapabilities?.availability !== 'available' || report.nativeCapabilities?.transport !== 'step-text' || report.nativeCapabilities?.operations?.includes('revolution') !== true) throw new Error('Native OCCT Revolution Worker capability evidence is incomplete.')
if (!Number.isSafeInteger(report.history?.recordCount) || report.history.recordCount <= 0 || report.history.crossKindRecords <= 0 || report.history.relations?.modified <= 0 || report.history.relations?.generated <= 0) throw new Error('Native OCCT Revolution history relation evidence is incomplete.')
const twoSidedStages = report.twoSidedHistory?.stages || []
if (report.nativeCapabilities?.operations?.includes('rotate') !== true || report.twoSidedHistory?.recordCount <= 0 || report.twoSidedHistory?.structuralValid !== true || report.twoSidedHistory?.unexplainedStructuralErrors !== 0 || report.twoSidedHistory?.solids !== 1 || twoSidedStages.length !== 2 || twoSidedStages[0]?.operation !== 'rotate' || JSON.stringify(twoSidedStages[0]?.inputObjectIds) !== JSON.stringify(['Profile']) || twoSidedStages[0]?.topologyEntries <= 0 || twoSidedStages[1]?.operation !== 'revolution' || JSON.stringify(twoSidedStages[1]?.inputObjectIds) !== JSON.stringify([twoSidedStages[0]?.resultObjectId]) || twoSidedStages[1]?.resultObjectId !== 'RevolutionTwoSided' || twoSidedStages[1]?.topologyEntries <= 0 || Math.abs(report.twoSidedHistory.volume - 12 * Math.PI) > 1e-5) throw new Error('Native OCCT offset Revolution stage evidence is incomplete.')
if (report.result?.structuralValid !== true || report.result?.solids !== 1 || Math.abs(report.result.volume - 24 * Math.PI) > 1e-5) throw new Error('Native OCCT Revolution result evidence is invalid.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error('Chrome native Revolution history handles were not released.')
console.log(JSON.stringify({ status: 'chrome-native-revolution-history-pass', browser: report.browserId, occtVersion: report.nativeCapabilities.occtVersion, records: report.history.recordCount, relations: report.history.relations, twoSidedStages, volume: report.result.volume, afterRelease: report.afterRelease }, null, 2))
