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-plot-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 Plot verification is not passing.')
if (report.series?.count !== 2 || report.series.boundSource !== 'Spreadsheet' || report.series.points !== 3 || report.series.updatedValue !== 16 || report.series.legend !== true) throw new Error('Plot bound-series update evidence is incomplete.')
if (report.axes?.x !== 'Revision' || report.axes.y !== 'Length (mm)' || JSON.stringify(report.axes.xRange) !== JSON.stringify([0, 2]) || JSON.stringify(report.axes.yRange) !== JSON.stringify([0, 20])) throw new Error('Plot axes evidence is incomplete.')
if (report.logAxis?.xScale !== 'log' || report.logAxis?.yScale !== 'log' || report.logAxis.svgHasLogMapping !== true || report.logAxis.rejectedNonPositive !== true) throw new Error('Plot logarithmic-axis evidence is incomplete.')
if (report.svg?.bytes <= 0 || report.svg.deterministic !== true || report.svg.paths !== 2 || report.svg.accessible !== true || !/^[0-9a-f]{64}$/.test(report.svg.sha256)) throw new Error('Plot deterministic SVG evidence is incomplete.')
if (report.png?.bytes <= 0 || report.png.deterministic !== true || report.png.validSignature !== true || report.png.width !== 640 || report.png.height !== 360 || !/^[0-9a-f]{64}$/.test(report.png.sha256)) throw new Error('Plot deterministic PNG evidence is incomplete.')
if (report.csv?.bytes <= 0 || report.csv.rows !== 7 || !/^[0-9a-f]{64}$/.test(report.csv.sha256)) throw new Error('Plot CSV evidence is incomplete.')
if (!Array.isArray(report.resources) || report.resources.length !== 3 || !report.resources.some((resource) => resource.mediaType === 'image/png') || report.resources.some((resource) => !resource.hash || resource.byteLength <= 0 || resource.roundTrip !== true || resource.released !== true)) throw new Error('Plot OPFS resource lifecycle evidence is incomplete.')
if (report.opfs?.markerSuite !== 'PLOT-ALL' || report.opfs.markerSeries !== 2 || report.opfs.markerRemoved !== true) throw new Error('Plot OPFS marker was not verified and removed.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('Plot harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-plot-pass', browser: report.browser.product, series: report.series, axes: report.axes, svg: report.svg, png: report.png, csv: report.csv, resources: report.resources, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
