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-bspline-verification.json'), 'utf8'))
if (report.schemaVersion !== 1 || report.browserId !== 'chrome' || report.status !== 'pass' || report.crossOriginIsolated !== true) throw new Error('Chrome Sketcher B-spline verification is not passing.')
const edit = report.edit ?? {}
if (edit.stableId !== true || edit.degree !== 1 || edit.poleCount !== 3 || edit.middlePole?.x !== 2 || edit.middlePole?.y !== 3 || JSON.stringify(edit.weights) !== JSON.stringify([1, 0.5, 1]) || JSON.stringify(edit.knots) !== JSON.stringify([0, 0, 0.5, 1, 1]) || edit.periodic !== true || edit.sourceUnchanged !== true) throw new Error('Chrome B-spline pole, weight, knot, degree, or periodic evidence is incomplete.')
const errors = report.validationErrors ?? {}
if (!/weights/.test(errors.invalidWeight ?? '') || !/knots/.test(errors.invalidKnots ?? '') || !/degree \+ 1/.test(errors.invalidDegree ?? '')) throw new Error('Chrome B-spline validation error evidence is incomplete.')
const roundTrip = report.roundTrip ?? {}
if (!(roundTrip.archiveBytes > 0) || roundTrip.geometryEqual !== true || roundTrip.constraintsEqual !== true || JSON.stringify(roundTrip.constraintTypes) !== '["weight","internalAlignment","weight"]' || JSON.stringify(roundTrip.weights) !== '[1,1.5,1.25,1]' || JSON.stringify(roundTrip.knots) !== '[0,0,0,0,1,1,1,1]') throw new Error('Chrome B-spline FCStd round-trip evidence is incomplete.')
const planegcs = JSON.parse(await readFile(resolve(root, 'config/chrome-planegcs-verification.json'), 'utf8'))
const native = planegcs.bsplineWeightSolve
const provider = planegcs.bsplineWeightProvider
if (planegcs.status !== 'pass' || native?.solveStatus > 1 || JSON.stringify(native?.weights) !== '[1,1.5,1.25,1]' || Math.abs(native?.alignmentResidual) > 1e-7 || Math.abs(native?.residual) > 1e-7 || provider?.completed !== 'completed' || provider?.degreesOfFreedom !== 11 || JSON.stringify(provider?.bspline?.weights) !== '[1,1.5,1.25,1]' || provider?.bspline?.periodic !== false) throw new Error('Chrome planegcs B-spline Weight/InternalAlignment evidence is incomplete.')
const freecad = JSON.parse(await readFile(resolve(root, 'config/freecad-sketcher-constraint-oracle.json'), 'utf8'))
const weightFixture = freecad.successCases?.find((entry) => entry.constraintType === 'Weight')
if (freecad.status !== 'pass' || weightFixture?.observed !== 'success' || weightFixture.solveStatus !== 0 || Math.abs(weightFixture.residual) > freecad.tolerance) throw new Error('FreeCAD B-spline Weight oracle evidence is incomplete.')
if (report.opfs?.markerSuite !== 'SK-10' || report.opfs.markerFields !== 8 || report.opfs.markerRemoved !== true) throw new Error('Chrome Sketcher B-spline OPFS marker was not verified and removed.')
console.log(JSON.stringify({ status: 'chrome-sketcher-bspline-pass', browser: report.browserId, edit: report.edit, roundTrip: report.roundTrip, nativeWeight: native, providerWeight: provider, freecadWeight: weightFixture, validationErrors: report.validationErrors, opfs: report.opfs }, null, 2))
