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-secondary-formats-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 secondary format verification is not passing.')
const expected = ['DXF', 'SVG', 'OBJ', 'PLY', 'STL', 'PDF', 'IFC', 'CSV']
if (report.formats?.descriptors !== expected.length || report.formats.records !== expected.length || report.formats.nativeExports !== 3 || report.formats.proxyImports !== expected.length || report.formats.bytes <= 0 || JSON.stringify(report.formats.categories) !== JSON.stringify(['2d', 'bim', 'data', 'mesh']) || JSON.stringify(report.formats.matrix?.map((entry) => entry.format)) !== JSON.stringify(expected) || report.formats.matrix.some((entry) => !entry.imported || !entry.rejectedInvalid || !entry.byteExactRoundTrip || !entry.deterministic || !entry.proxy || entry.byteLength <= 0)) throw new Error('Secondary format success/failure/round-trip matrix is incomplete.')
if (report.persistence?.mode !== 'sqlite-opfs' || report.persistence.byteLength <= 0 || report.persistence.roundTrip !== true || report.persistence.released !== true) throw new Error('Secondary format OPFS resource evidence is incomplete.')
if (report.opfs?.markerSuite !== 'FC-09' || report.opfs.records !== expected.length || report.opfs.markerRemoved !== true) throw new Error('Secondary format OPFS marker evidence is incomplete.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('Secondary format harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-secondary-formats-pass', browser: report.browser.product, formats: report.formats, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
