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-offline-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('REL-01 Chrome offline verification is not passing.')
const worker = report.serviceWorker
if (worker?.registered !== true || worker.controlled !== true || worker.shellCached !== true || worker.staleCacheRemoved !== true || worker.offlineFallback !== true || !worker.cacheNames.includes('bitbybit-cad-shell-v1')) throw new Error('REL-01 Service Worker cache lifecycle evidence is incomplete.')
if (report.persistence?.mode !== 'sqlite-opfs' || report.persistence.bytes <= 0 || report.persistence.roundTrip !== true || report.persistence.released !== true) throw new Error('REL-01 OPFS resource evidence is incomplete.')
if (report.opfs?.markerSuite !== 'REL-01' || report.opfs.markerRemoved !== true || report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('REL-01 OPFS or Shape ownership evidence is incomplete.')
console.log(JSON.stringify({ status: 'chrome-offline-pass', browser: report.browser.product, serviceWorker: worker, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
