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-cam-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 CAM verification is not passing.')
if (!report.geometry?.valid || report.job?.stockMode !== 'from-base-bound-box' || report.job.tools !== 2 || report.job.controllers !== 2 || report.job.operations !== 3) throw new Error('CAM Job/Stock/ToolBit/Controller evidence is incomplete.')
if (JSON.stringify(report.job.operationKinds) !== JSON.stringify(['profile', 'pocket', 'drilling']) || report.job.operationStatuses?.some((status) => status !== 'generated') || report.job.pathPoints < 20 || report.job.dressups !== 1) throw new Error('CAM operation and dress-up evidence is incomplete.')
if (report.job.setup?.safeHeightOffset !== 1 || report.job.setup.clearanceHeightOffset !== 2 || report.job.setup.coolantMode !== 'flood' || report.job.sanity !== 'pass' || report.job.sanityIssues !== 0) throw new Error('CAM SetupSheet or Sanity Check evidence is incomplete.')
if (report.job.simulation !== 'pass' || report.job.collisions !== 0 || report.job.collisionFixture !== 1 || report.job.simulationDiagnostics !== 0) throw new Error('CAM simulation and collision evidence is incomplete.')
if (report.extended?.commandSurface !== 60 || report.extended.operationKinds !== 22 || report.extended.dressupKinds !== 9 || report.extended.toolAssetRoundTrip !== true || report.extended.jobAssetRoundTrip !== true || report.extended.metadataRoundTrip !== true || report.extended.metadataComments !== 1 || report.extended.metadataProperties !== 3 || report.extended.metadataCompounds !== 1 || report.extended.undoRedo !== true || report.extended.startPointChanged !== true || report.extended.materialRemoval !== true || report.extended.removalTimelinePoints !== report.job.pathPoints || report.extended.fixtureCollision !== true || report.extended.multiAxis !== true) throw new Error('CAM extended command, asset lifecycle, metadata, material removal, collision, multi-axis or path-edit evidence is incomplete.')
if (JSON.stringify(report.job.postprocessors) !== JSON.stringify(['grbl', 'linuxcnc', 'mach3-mach4', 'centroid', 'marlin', 'masso-g3', 'snapmaker']) || report.job.post !== 'linuxcnc' || Object.keys(report.job.postHashes || {}).length !== 7 || Object.values(report.job.postHashes || {}).some((hash) => !/^[0-9a-f]{64}$/.test(hash)) || report.job.unsafePostRejected !== true || report.job.deterministic !== true || report.job.gcodeBytes <= 0 || !/^[0-9a-f]{64}$/.test(report.job.gcodeHash)) throw new Error('CAM sandboxed post/G-code 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('CAM OPFS evidence is incomplete.')
if (report.opfs?.markerSuite !== 'CAM-ALL' || report.opfs.operations !== 3 || report.opfs.markerRemoved !== true || report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('CAM lifecycle evidence is incomplete.')
console.log(JSON.stringify({ status: 'chrome-cam-pass', browser: report.browser.product, geometry: report.geometry, job: report.job, extended: report.extended, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
