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-partdesign-loft-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 PartDesign loft verification is not passing.')
if (JSON.stringify(report.operations?.map((operation) => operation.command)) !== JSON.stringify(['additive-loft', 'additive-pipe', 'subtractive-loft', 'subtractive-pipe'])) throw new Error('PartDesign loft/pipe operation coverage is incomplete.')
if (report.operations.some((operation) => !operation.objectId || !operation.typeId || !operation.shapeId || operation.recompute !== 'completed' || !Number.isFinite(operation.volume) || operation.volume <= 0)) throw new Error('PartDesign loft/pipe solid recompute evidence is incomplete.')
const recovery = report.failureRecovery
if (!recovery || recovery.loft?.status !== 'failed' || recovery.loft?.code !== 'LOFT_SECTIONS_DUPLICATE' || recovery.loft?.retainedShapeId !== recovery.loft?.previousShapeId || recovery.loft?.restoredStatus !== 'completed' || !recovery.loft?.restoredShapeId) throw new Error('Loft failure recovery evidence is incomplete.')
if (recovery.pipe?.status !== 'failed' || recovery.pipe?.code !== 'PIPE_PATH_BRANCH' || recovery.pipe?.retainedShapeId !== recovery.pipe?.previousShapeId || recovery.pipe?.restoredStatus !== 'completed' || !recovery.pipe?.restoredShapeId) throw new Error('Pipe failure recovery evidence is incomplete.')
if (report.persistence?.mode !== 'sqlite-opfs' || report.persistence.objectCount < 1 || report.persistence.reopenedTip !== report.operations[3]?.objectId) throw new Error('PartDesign loft OPFS round-trip evidence is incomplete.')
if (report.opfs?.markerSuite !== 'PD-LOFT' || report.opfs.markerOperations !== 4 || report.opfs.markerRemoved !== true) throw new Error('PartDesign loft OPFS marker was not verified and removed.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error('PartDesign loft ShapeHandles were not fully released.')
console.log(JSON.stringify({ status: 'chrome-partdesign-loft-pass', browser: report.browser.product, operations: report.operations, failureRecovery: report.failureRecovery, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
