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-geometry-features-verification.json'), 'utf8'))
if (report.status !== 'pass' || report.browserId !== 'chrome' || !/Chrome/i.test(report.browser?.product || '')) throw new Error('Chrome geometry feature verification is not a Chrome pass.')
const expected = ['fcstd-brep-import', 'extrude', 'revolution', 'fillet-selected-edge', 'chamfer-selected-edge', 'draft-selected-face', 'thickness', 'hole-counterbore', 'hole-countersink', 'hole-counterdrill', 'hole-angled-drill-point', 'hole-angled-included-depth', 'hole-tapered', 'loft', 'loft-ruled', 'loft-additive', 'loft-subtractive', 'pipe', 'extrude-two-sided', 'revolution-symmetric', 'revolution-two-angles', 'pad-two-sided', 'pocket-two-sided', 'pad-tapered', 'pocket-tapered', 'pocket-through-all', 'pocket-up-to-face', 'pad-midplane', 'linear-pattern-spacing', 'polar-pattern-spacing']
if (JSON.stringify(report.operations?.map((operation) => operation.name)) !== JSON.stringify(expected)) throw new Error('Chrome geometry feature operation order is incomplete.')
if (!report.operations.every((operation) => operation.meshVertices >= 3 && operation.meshTriangles >= 1)) throw new Error('Chrome geometry feature mesh evidence is incomplete.')
if (!['pad-tapered', 'pocket-tapered', 'pocket-through-all', 'pocket-up-to-face', 'pad-midplane'].every((name) => Number.isFinite(report.operations.find((operation) => operation.name === name)?.volume) && report.operations.find((operation) => operation.name === name).volume > 0)) throw new Error('Chrome PartDesign base mass evidence is incomplete.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error('Chrome geometry feature handles were not fully released.')
if (!Number.isFinite(report.performance?.durationMs) || report.performance.durationMs <= 0 || report.performance.operationCount !== report.operations.length || !Number.isFinite(report.performance.averageOperationMs) || report.performance.peakShapeCount < report.performance.operationCount || report.performance.stressObjectCount !== 1000 || !Number.isFinite(report.performance.stressDurationMs) || report.performance.stressDurationMs <= 0 || report.performance.stressPeakShapeCount < report.performance.stressObjectCount) throw new Error('Chrome geometry performance evidence is incomplete.')
const triangleStress = report.performance.triangleStress
if (!triangleStress || triangleStress.radius !== 100 || triangleStress.precision !== 0.01 || !Number.isFinite(triangleStress.triangles) || triangleStress.triangles < 100_000 || !Number.isFinite(triangleStress.vertices) || triangleStress.vertices < 3 || !Number.isFinite(triangleStress.durationMs) || triangleStress.durationMs <= 0) throw new Error('Chrome high-tessellation performance evidence is incomplete.')
if (!report.fcstdBrep || report.fcstdBrep.references !== 1 || report.fcstdBrep.topology?.faces !== 6 || report.fcstdBrep.topology?.edges !== 12 || report.fcstdBrep.topology?.vertices !== 8 || Math.abs(report.fcstdBrep.massProperties?.source?.volume - 24) > 1e-7 || Math.abs(report.fcstdBrep.massProperties?.source?.volume - report.fcstdBrep.massProperties?.imported?.volume) > 1e-7 || Math.abs(report.fcstdBrep.massProperties?.source?.surfaceArea - report.fcstdBrep.massProperties?.imported?.surfaceArea) > 1e-7 || !report.fcstdBrep.emptyRejected || !report.fcstdBrep.cancelledBeforeImport || !report.fcstdBrep.cancelledResultReleased) throw new Error('Chrome FCStd BRep instantiation evidence is incomplete.')
if (!report.exchangeFormats?.iges || report.exchangeFormats.iges.sourceBytes <= 0 || Math.abs(report.exchangeFormats.iges.sourceVolume - report.exchangeFormats.iges.importedVolume) > 1e-7 || report.exchangeFormats.iges.importedFaces !== 6 || report.exchangeFormats.iges.importedEdges < 1 || report.exchangeFormats.iges.importedVertices < 1) throw new Error('Chrome IGES exchange evidence is incomplete.')
console.log(JSON.stringify({ status: 'chrome-geometry-features-pass', browser: report.browser.product, operations: report.operations.length, afterRelease: report.afterRelease }))
