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-assembly-verification.json'), 'utf8'))
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.browserId !== 'chrome' || !/Chrome/i.test(report.browser?.product || '') || report.crossOriginIsolated !== true) throw new Error('Chrome Assembly verification is not passing.')
if (report.geometry?.components !== 2 || report.geometry.validShapes !== 2) throw new Error('Assembly component/Bitbybit shape evidence is incomplete.')
if (report.solver?.status !== 'solved' || report.solver.iterations !== 2 || report.solver.coincident !== 'solved' || report.solver.distance !== 'solved' || report.solver.shaftX !== 5 || report.solver.diagnostics !== 0 || report.solver.workerStatus !== 'solved' || report.solver.angleStatus !== 'solved' || Math.abs(report.solver.angleYaw - Math.PI / 2) > 1e-12) throw new Error('Assembly Worker/fixed/distance/angle solver evidence is incomplete.')
if (report.tools?.bomRows !== 2 || report.tools.collisionCount !== 1 || report.tools.explodedMoved !== true || report.tools.variantX !== 7 || report.tools.motionFrames !== 5 || report.tools.motionEndX !== 9) throw new Error('Assembly tools and motion evidence is incomplete.')
if (report.scale?.components !== 1000 || report.scale.bomRows !== 10 || report.scale.durationMs > report.scale.budgetMs || report.scale.budgetMs !== 1000) throw new Error('Assembly large-model budget evidence is incomplete.')
if (report.fcstd?.bytes <= 0 || report.fcstd.objects !== 4 || report.fcstd.links !== 1 || report.fcstd.reopenedObjects !== 4 || report.fcstd.roundTrip !== true || report.fcstd.released !== true) throw new Error('Assembly FCStd link round-trip evidence is incomplete.')
if (report.persistence?.mode !== 'sqlite-opfs' || report.persistence.byteLength <= 0 || !/^[0-9a-f]{64}$/.test(report.persistence.hash) || report.persistence.roundTrip !== true || report.persistence.released !== true) throw new Error('Assembly OPFS resource evidence is incomplete.')
if (report.opfs?.markerSuite !== 'ASM-TOOLS' || report.opfs.markerComponents !== 2 || report.opfs.markerRemoved !== true) throw new Error('Assembly OPFS marker evidence is incomplete.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('Assembly harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-assembly-pass', browser: report.browser.product, geometry: report.geometry, solver: report.solver, tools: report.tools, scale: report.scale, fcstd: report.fcstd, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
