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

const root = resolve(import.meta.dirname, '..')
const report = JSON.parse(await readFile(resolve(root, 'config/browser-matrix-verification.json'), 'utf8'))
const expected = ['firefox', 'webkit']
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.browsers?.length !== expected.length) throw new Error('Browser matrix report is incomplete.')
for (const engine of expected) {
  const entry = report.browsers.find((candidate) => candidate.engine === engine)
  const persistencePass = engine === 'firefox' ? entry?.opfs === true && entry?.persistence === 'opfs' : entry && ['opfs', 'memory-fallback'].includes(entry.persistence)
  if (!entry || entry.status !== 'pass' || entry.pageErrors?.length !== 0 || entry.shell !== true || entry.workspace !== true || entry.crossOriginIsolated !== true || entry.sharedArrayBuffer !== true || !persistencePass || entry.workbench !== 'Part Design' || entry.canvas?.geometrySource !== 'bitbybit-occt' || entry.canvas.width < 100 || entry.canvas.height < 100 || entry.screenshotBytes < 10_000) throw new Error(`Browser matrix ${engine} evidence is incomplete.`)
}
console.log(JSON.stringify({ status: 'browser-matrix-pass', browsers: report.browsers.map(({ engine, userAgent, persistence, canvas }) => ({ engine, userAgent, persistence, canvas })) }, null, 2))
