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-engineering-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 engineering closure verification is not passing.')
if (report.integrity?.complete !== true || report.integrity.artifacts !== 4 || report.integrity.kinds !== 4 || report.integrity.missingKinds !== 0 || report.integrity.missingRefs !== 0 || report.integrity.projectVersion !== 8 || JSON.stringify(report.integrity.artifactVersions) !== JSON.stringify([2, 2, 2, 2]) || report.integrity.reopenedComplete !== true) throw new Error('ENG-CLOSURE integrity and version evidence is incomplete.')
if (report.workflow?.assemblyComponents !== 2 || report.workflow.bimElements !== 1 || report.workflow.meshTriangles <= 0 || report.workflow.surfacePatches !== 5 || report.workflow.edited.assemblyX !== 6 || report.workflow.edited.fireRating !== 'A1' || report.workflow.edited.meshMinX !== -2.75 || report.workflow.edited.surfaceOffset !== 0.5 || Object.values(report.workflow.exports).some((value) => typeof value !== 'number' || value <= 0)) throw new Error('ENG-CLOSURE cross-workbench edit/export evidence is incomplete.')
if (report.scale?.components !== 1000 || report.scale.bomRows !== 20 || report.scale.durationMs > report.scale.budgetMs || report.scale.budgetMs !== 1000) throw new Error('ENG-CLOSURE large-project budget 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('ENG-CLOSURE OPFS resource evidence is incomplete.')
if (report.opfs?.markerSuite !== 'ENG-CLOSURE' || report.opfs.artifacts !== 4 || report.opfs.markerRemoved !== true) throw new Error('ENG-CLOSURE OPFS marker evidence is incomplete.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('ENG-CLOSURE harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-engineering-pass', browser: report.browser.product, integrity: report.integrity, workflow: report.workflow, scale: report.scale, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
