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-draft-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 Draft verification is not passing.')
if (report.workingPlane?.worldPoint.x !== 10 || report.workingPlane.worldPoint.y !== 22 || report.workingPlane.worldPoint.z !== 33 || JSON.stringify(report.workingPlane.snapped) !== JSON.stringify({ x: 1, y: 2.5 })) throw new Error('Draft working plane/grid evidence is incomplete.')
if (report.objects?.total !== 9 || report.objects.lines !== 2 || report.objects.wires !== 1 || report.objects.circles !== 1 || report.objects.clones !== 5 || report.objects.arrays !== 4 || report.objects.layers !== 2 || report.objects.sourceDependencies !== 5) throw new Error('Draft parametric object/layer dependency evidence is incomplete.')
if (!report.operations?.moved || !report.operations.rotated || !report.operations.scaled || report.operations.offsetLength !== 8 || report.operations.trimmedLength !== 4 || report.operations.cloneTranslation.x !== 10 || report.operations.arrayCount !== 4) throw new Error('Draft operation 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('Draft OPFS resource lifecycle evidence is incomplete.')
if (report.opfs?.markerSuite !== 'DRAFT-OPS' || report.opfs.markerObjects !== 9 || report.opfs.markerRemoved !== true) throw new Error('Draft OPFS marker was not verified and removed.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('Draft harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-draft-pass', browser: report.browser.product, workingPlane: report.workingPlane, objects: report.objects, operations: report.operations, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
