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-production-document-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 production document verification is not passing.')
if (report.closure?.complete !== true || report.closure.artifacts !== 4 || report.closure.missingKinds !== 0 || report.closure.missingDependencies !== 0 || report.closure.cycles !== 0 || report.closure.documentVersion !== 8 || report.closure.reopenedComplete !== true || JSON.stringify(report.closure.artifactVersions) !== JSON.stringify([2, 2, 2, 2])) throw new Error('DOC-CLOSURE dependency and reopen evidence is incomplete.')
if (report.workflow?.created.spreadsheetCells !== 2 || report.workflow.created.draftObjects !== 1 || report.workflow.created.techdrawViews !== 1 || report.workflow.created.plotSeries !== 1) throw new Error('DOC-CLOSURE real artifact creation evidence is incomplete.')
if (report.workflow?.edited.spreadsheetValue !== 6 || report.workflow.edited.draftStartX !== 1 || report.workflow.edited.techdrawDimension !== 5 || report.workflow.edited.plotValue !== 6) throw new Error('DOC-CLOSURE cross-workbench edit evidence is incomplete.')
if (report.workflow?.exports.techdrawPdfValid !== true || Object.entries(report.workflow.exports).some(([name, value]) => name !== 'techdrawPdfValid' && (typeof value !== 'number' || value <= 0))) throw new Error('DOC-CLOSURE real export 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('DOC-CLOSURE OPFS resource evidence is incomplete.')
if (report.opfs?.markerSuite !== 'DOC-CLOSURE' || report.opfs.artifacts !== 4 || report.opfs.markerRemoved !== true) throw new Error('DOC-CLOSURE OPFS marker evidence is incomplete.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('DOC-CLOSURE harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-production-document-pass', browser: report.browser.product, closure: report.closure, workflow: report.workflow, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
