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-groove-history-verification.json'), 'utf8'))
if (report.status !== 'pass' || report.browserId !== 'chrome' || report.crossOriginIsolated !== true) throw new Error('Chrome native Groove history verification is not passing.')
if (report.capabilities?.status !== 'ready' || report.nativeCapabilities?.availability !== 'available' || report.nativeCapabilities?.transport !== 'step-text' || report.nativeCapabilities?.operations?.includes('groove') !== true) throw new Error('Native OCCT Groove Worker capability evidence is incomplete.')
const stages = report.history?.stages || []
if (!Number.isSafeInteger(report.history?.recordCount) || report.history.recordCount <= 0 || stages.length !== 2 || stages[0]?.operation !== 'revolution' || JSON.stringify(stages[0]?.inputObjectIds) !== JSON.stringify(['Profile']) || stages[0]?.recordCount <= 0 || stages[0]?.topologyEntries <= 0 || stages[1]?.operation !== 'cut' || JSON.stringify(stages[1]?.inputObjectIds) !== JSON.stringify(['Base', stages[0]?.resultObjectId]) || stages[1]?.recordCount !== report.history.recordCount || stages[1]?.topologyEntries <= 0 || !report.history.sourceObjects?.includes('Base') || !report.history.sourceObjects?.includes('Profile') || !report.history.sourceObjects?.includes(stages[0]?.resultObjectId)) throw new Error('Native OCCT Groove staged source relation evidence is incomplete.')
if (report.history.relations?.modified <= 0 || report.history.relations.generated + report.history.relations.deleted <= 0) throw new Error('Native OCCT Groove history relation evidence is incomplete.')
const twoAngleStages = report.twoAngleHistory?.stages || []
if (report.nativeCapabilities?.operations?.includes('rotate') !== true || report.twoAngleHistory?.recordCount <= 0 || report.twoAngleHistory?.structuralValid !== true || report.twoAngleHistory?.unexplainedStructuralErrors !== 0 || report.twoAngleHistory?.solids !== 1 || Math.abs(report.twoAngleHistory.volume - (800 - 3 * Math.PI)) > 1e-5 || twoAngleStages.length !== 3 || twoAngleStages[0]?.operation !== 'rotate' || JSON.stringify(twoAngleStages[0]?.inputObjectIds) !== JSON.stringify(['Profile']) || twoAngleStages[0]?.topologyEntries <= 0 || twoAngleStages[1]?.operation !== 'revolution' || JSON.stringify(twoAngleStages[1]?.inputObjectIds) !== JSON.stringify([twoAngleStages[0]?.resultObjectId]) || twoAngleStages[1]?.topologyEntries <= 0 || twoAngleStages[2]?.operation !== 'cut' || JSON.stringify(twoAngleStages[2]?.inputObjectIds) !== JSON.stringify(['Base', twoAngleStages[1]?.resultObjectId]) || twoAngleStages[2]?.resultObjectId !== 'GrooveTwoAngles' || twoAngleStages[2]?.topologyEntries <= 0 || !report.twoAngleHistory.sourceObjects?.includes('Base') || !report.twoAngleHistory.sourceObjects?.includes('Profile') || !report.twoAngleHistory.sourceObjects?.includes(twoAngleStages[0]?.resultObjectId) || !report.twoAngleHistory.sourceObjects?.includes(twoAngleStages[1]?.resultObjectId)) throw new Error('Native OCCT Groove TwoAngles offset-sweep evidence is incomplete.')
if (report.result?.structuralValid !== true || report.result?.solids !== 1 || Math.abs(report.result.volume - (800 - 6 * Math.PI)) > 1e-5) throw new Error('Native OCCT Groove result evidence is invalid.')
if (report.opfs?.markerOperation !== 'groove' || report.opfs?.markerRemoved !== true) throw new Error('Chrome native Groove OPFS marker was not verified and removed.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error('Chrome native Groove history handles were not released.')
console.log(JSON.stringify({ status: 'chrome-native-groove-history-pass', browser: report.browserId, occtVersion: report.nativeCapabilities.occtVersion, records: report.history.recordCount, relations: report.history.relations, stages, twoAngleStages, volume: report.result.volume, twoAngleVolume: report.twoAngleHistory.volume, afterRelease: report.afterRelease, opfs: report.opfs }, null, 2))
