import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'
const root = resolve(new URL('..', import.meta.url).pathname)
const [report, app] = await Promise.all([
  readFile(resolve(root, 'config/chrome-qa08-verification.json'), 'utf8').then(JSON.parse),
  readFile(resolve(root, 'config/chrome-app-e2e-verification.json'), 'utf8').then(JSON.parse),
])
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.browserId !== 'chrome' || !/Chrome/i.test(report.browser?.product || '') || report.crossOriginIsolated !== true) throw new Error('Chrome QA-08 verification is not passing.')
if (report.locales?.supported !== 3 || report.locales.snapshots !== 3 || report.locales.longLabel !== true || report.locales.decimalSamples.some((sample) => !sample.includes('mm'))) throw new Error('Locale evidence is incomplete.')
if (report.accessibility?.checked !== 4 || report.accessibility.missingNames !== 0 || report.accessibility.duplicates !== 0 || report.accessibility.unnamedFocusable !== 0 || report.accessibility.pass !== true) throw new Error('Accessibility semantics evidence is incomplete.')
const screenshotKeys = Object.keys(app.screenshots || {})
if (app.status !== 'pass' || app.mobile?.viewport?.width !== 390 || app.mobile.bodyHorizontalOverflow > 1 || app.keyboard?.steps !== 8 || app.keyboard.named !== 8 || app.keyboard.unnamedVisible !== 0 || app.screenReader?.nodes < 10 || app.screenReader.controls < 8 || app.screenReader.namedControls !== app.screenReader.controls || app.screenReader.unnamedControls !== 0 || !['desktop', 'mobile'].every((key) => screenshotKeys.includes(key))) throw new Error('Keyboard, screen-reader, locale, or mobile accessibility matrix is incomplete.')
if (report.persistence?.mode !== 'sqlite-opfs' || report.persistence.byteLength <= 0 || report.persistence.roundTrip !== true || report.persistence.released !== true) throw new Error('QA-08 OPFS resource evidence is incomplete.')
if (report.opfs?.markerSuite !== 'QA-08' || report.opfs.localeCount !== 3 || report.opfs.markerRemoved !== true) throw new Error('QA-08 OPFS marker evidence is incomplete.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('QA-08 harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-qa08-pass', browser: report.browser.product, locales: report.locales, accessibility: report.accessibility, application: { mobile: app.mobile, keyboard: app.keyboard, screenReader: app.screenReader, screenshots: Object.keys(app.screenshots) }, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
