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-sketcher-diagnostics-verification.json'), 'utf8'))
const oracle = JSON.parse(await readFile(resolve(root, 'config/freecad-sketcher-constraint-oracle.json'), 'utf8'))
if (report.status !== 'pass' || report.browserId !== 'chrome' || report.crossOriginIsolated !== true) throw new Error('Chrome Sketcher diagnostics verification is not passing.')
if (report.fixtures?.reference?.status !== 'under-constrained' || report.fixtures.reference.degreesOfFreedom !== 3 || report.fixtures.reference.diagnostic !== true || report.fixtures.reference.unchangedEnd !== true) throw new Error('Chrome reference dimensional fixture evidence is incomplete.')
if (report.fixtures?.redundant?.status !== 'under-constrained' || report.fixtures.redundant.diagnostic !== true) throw new Error('Chrome redundant dimensional fixture evidence is incomplete.')
if (report.fixtures?.conflicting?.status !== 'conflicting' || report.fixtures.conflicting.diagnostic !== true || report.fixtures.conflicting.notConverged !== true) throw new Error('Chrome conflicting dimensional fixture evidence is incomplete.')
const referenceOracle = oracle.classificationCases?.find((entry) => entry.id === 'reference-dimension')
const redundantOracle = oracle.classificationCases?.find((entry) => entry.id === 'redundant-dimension')
const conflictingOracle = oracle.classificationCases?.find((entry) => entry.id === 'conflicting-dimension')
if (oracle.status !== 'pass' || referenceOracle?.solveStatus !== 0 || referenceOracle?.degreesOfFreedom !== report.fixtures.reference.degreesOfFreedom || referenceOracle?.driving !== false || redundantOracle?.solveStatus !== -2 || JSON.stringify(redundantOracle?.redundant) !== '[2]' || conflictingOracle?.solveStatus !== -3 || JSON.stringify(conflictingOracle?.conflicting) !== '[1,2]') throw new Error('Chrome Sketcher diagnostic classifications do not match the locked FreeCAD oracle.')
if (report.fixtures?.coincident?.status !== 'under-constrained' || report.fixtures.coincident.residual !== 0 || report.fixtures.coincident.sharedPoint !== true) throw new Error('Chrome coincident constraint evidence is incomplete.')
if (report.fixtures?.block?.status !== 'solved' || report.fixtures.block.degreesOfFreedom !== 0 || report.fixtures.block.unchanged !== true) throw new Error('Chrome block constraint evidence is incomplete.')
if (report.opfs?.markerSuite !== 'SK-06/SK-07' || report.opfs?.markerRemoved !== true) throw new Error('Chrome Sketcher diagnostics OPFS marker was not verified and removed.')
console.log(JSON.stringify({ status: 'chrome-sketcher-diagnostics-pass', browser: report.browserId, fixtures: report.fixtures, opfs: report.opfs }, null, 2))
