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-bim-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 BIM verification is not passing.')
if (report.model?.sites !== 1 || report.model.buildings !== 1 || report.model.levels !== 1 || report.model.spaces !== 1 || report.model.materials !== 1 || report.model.elements !== 1 || report.model.scheduleRows !== 1 || report.model.ifc4Bytes <= 0 || report.model.ifc2x3Bytes <= 0 || report.model.ifc4Schema !== 'IFC4' || report.model.ifc2x3Schema !== 'IFC2X3' || report.model.ifc4Entities < 7 || report.model.ifc4Hierarchy !== 4 || report.model.ifc4PropertySets < 1 || report.model.ifc4Classifications !== 1 || report.model.ifc2x3RoundTrip !== true) throw new Error('BIM hierarchy/IFC2X3/IFC4 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('BIM OPFS resource evidence is incomplete.')
if (report.opfs?.markerSuite !== 'BIM-IFC' || report.opfs.elements !== 1 || report.opfs.markerRemoved !== true) throw new Error('BIM OPFS marker was not verified and removed.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('BIM harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-bim-pass', browser: report.browser.product, model: report.model, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
