import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const load = async (name) => JSON.parse(await readFile(resolve(root, `config/${name}`), 'utf8'))
const [assembly, bim, mesh, surface, engineering] = await Promise.all([
  load('chrome-assembly-verification.json'), load('chrome-bim-verification.json'), load('chrome-mesh-verification.json'), load('chrome-surface-verification.json'), load('chrome-engineering-verification.json'),
])
for (const [name, report] of Object.entries({ assembly, bim, mesh, surface, engineering })) {
  if (report.schemaVersion !== 1 || report.status !== 'pass' || report.browserId !== 'chrome' || report.crossOriginIsolated !== true || !/Chrome/i.test(report.browser?.product || '')) throw new Error(`P08 ${name} Chrome evidence is not passing.`)
  if (report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error(`P08 ${name} leaked ShapeHandles.`)
}
if (assembly.solver?.workerStatus !== 'solved' || assembly.solver.angleStatus !== 'solved' || assembly.tools?.motionFrames !== 5 || assembly.scale?.components !== 1000 || assembly.fcstd?.links !== 1 || assembly.fcstd.roundTrip !== true) throw new Error('ASM-CORE/ASM-TOOLS evidence is incomplete.')
if (bim.model?.sites !== 1 || bim.model.buildings !== 1 || bim.model.ifc4Schema !== 'IFC4' || bim.model.ifc2x3Schema !== 'IFC2X3' || bim.model.ifc4PropertySets < 1 || bim.model.ifc4Classifications !== 1) throw new Error('BIM-CORE/BIM-IFC evidence is incomplete.')
if (mesh.geometry?.selfIntersections !== 0 || mesh.repairs?.selfIntersectionFixture !== 1 || mesh.repairs.nonManifoldFixture !== 1 || mesh.repairs.holeFilledTriangles !== 1 || mesh.operations?.workerLodTriangles !== mesh.operations?.lodTriangles) throw new Error('MESH-CORE analysis/repair/Worker LOD evidence is incomplete.')
if (surface.model?.patches !== 5 || surface.model.fillKind !== 'bezier' || surface.model.offsetDistance !== 1.5 || surface.model.topoRefs !== 4 || surface.model.continuity !== 'C0') throw new Error('SURF-CORE modeling/TopoRef evidence is incomplete.')
if (engineering.integrity?.complete !== true || engineering.integrity.projectVersion !== 8 || engineering.integrity.reopenedComplete !== true || JSON.stringify(engineering.integrity.artifactVersions) !== JSON.stringify([2, 2, 2, 2]) || engineering.scale?.components !== 1000 || engineering.persistence?.roundTrip !== true || engineering.persistence.released !== true) throw new Error('ENG-CLOSURE mixed-project evidence is incomplete.')
console.log(JSON.stringify({ status: 'engineering-closure-pass', browser: engineering.browser.product, tasks: ['ASM-CORE', 'ASM-TOOLS', 'BIM-CORE', 'BIM-IFC', 'MESH-CORE', 'SURF-CORE', 'ENG-CLOSURE'], assembly: { solver: assembly.solver, scale: assembly.scale, fcstd: assembly.fcstd }, bim: bim.model, mesh: { geometry: mesh.geometry, operations: mesh.operations, repairs: mesh.repairs }, surface: surface.model, engineering: { integrity: engineering.integrity, workflow: engineering.workflow, scale: engineering.scale } }, null, 2))
