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

const root = resolve(new URL('..', import.meta.url).pathname)
const [report, artifact] = await Promise.all([
  readFile(resolve(root, 'config/chrome-camotics-wasm-verification.json'), 'utf8').then(JSON.parse),
  readFile(resolve(root, 'config/camotics-wasm-artifact.json'), 'utf8').then(JSON.parse),
])

if (report.schemaVersion !== 1 || report.status !== 'pass' || report.browserId !== 'chrome' || report.crossOriginIsolated !== true) throw new Error('Chrome CAMotics WASM report is not passing.')
if (!/^Chrome\//.test(report.browser?.product || '')) throw new Error('CAMotics WASM report was not produced by Chrome.')
if (report.kernel?.backend !== 'camotics-upstream-sweep-wasm' || report.kernel.sourceRevision !== artifact.upstream.revision || report.kernel.artifactSha256 !== artifact.artifact.sha256 || report.kernel.abiVersion !== 1 || report.kernel.imports !== 0 || report.kernel.fullCamoticsProgram !== false || report.kernel.gcodeParserIncluded !== false) throw new Error('Chrome CAMotics WASM identity or scope is invalid.')
if (report.sweep?.conicInsideDepth !== 1 || report.sweep.conicOutsideDepth !== -1 || report.sweep.spheroidInsideDepth !== 1 || report.sweep.longMoveBoundingBoxes !== 3 || report.sweep.deterministic !== true) throw new Error('Chrome CAMotics sweep results are invalid.')
if (report.parserAuthority !== 'linuxcnc-wasm') throw new Error('LinuxCNC WASM must remain the canonical G-code parser authority.')

console.log(JSON.stringify({ status: 'chrome-camotics-wasm-pass', browser: report.browser.product, kernel: report.kernel, sweep: report.sweep, parserAuthority: report.parserAuthority }, null, 2))
