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-spreadsheet-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 Spreadsheet verification is not passing.')
if (report.cells?.width !== 6 || report.cells.length !== 12 || report.cells.updatedLength !== 16 || report.cells.dimension !== 'length') throw new Error('Spreadsheet formula/unit recompute evidence is incomplete.')
if (JSON.stringify(report.aliases) !== JSON.stringify(['Length', 'Width']) || report.dependencies?.length !== 2) throw new Error('Spreadsheet alias/dependency evidence is incomplete.')
if (report.cycle?.detected !== true || JSON.stringify(report.cycle.cells) !== JSON.stringify(['C1', 'D1'])) throw new Error('Spreadsheet cycle diagnostics are incomplete.')
if (report.binding?.alias !== 'Length' || report.binding.objectId !== 'pad' || report.binding.propertyName !== 'Length' || report.binding.appliedValue !== 16 || report.binding.undoValue !== 12 || report.binding.redoValue !== 16) throw new Error('Spreadsheet model-property binding lifecycle evidence is incomplete.')
if (report.expression?.value !== 13 || report.expression.dimension !== 'length' || !report.expression.references.includes('Spreadsheet.Length')) throw new Error('Spreadsheet Facade expression bridge evidence is incomplete.')
if (report.layout?.styledCells !== 4 || report.layout.namedRange !== 'A1:A3' || report.layout.mergedRange !== 'A1:C3' || report.layout.shiftedFormulaAddress !== 'A3') throw new Error('Spreadsheet style/merge/named-range address migration evidence is incomplete.')
if (report.csv?.rows !== 2 || report.csv.columns !== 4 || report.csv.bytes <= 0) throw new Error('Spreadsheet CSV evidence is incomplete.')
if (report.largeSheet?.cells !== 10000 || report.largeSheet.rows !== 100 || report.largeSheet.columns !== 100 || report.largeSheet.evaluationErrors !== 0 || report.largeSheet.csvBytes <= 0 || !(report.largeSheet.durationMs >= 0) || report.largeSheet.durationMs > report.largeSheet.budgetMs || report.largeSheet.budgetMs !== 5000) throw new Error('Spreadsheet large-sheet performance evidence is incomplete.')
if (report.persistence?.mode !== 'sqlite-opfs' || report.persistence.reopenedValue !== 16 || report.persistence.reopenedVersion !== report.persistence.savedVersion) throw new Error('Spreadsheet-bound OPFS project round-trip evidence is incomplete.')
if (!report.resource?.hash || report.resource.byteLength <= 0 || report.resource.roundTrip !== true || report.resource.released !== true) throw new Error('Spreadsheet OPFS resource lifecycle evidence is incomplete.')
if (report.opfs?.markerSuite !== 'SS-ALL' || report.opfs.markerCells !== 9 || report.opfs.markerRemoved !== true) throw new Error('Spreadsheet OPFS marker was not verified and removed.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('Spreadsheet harness leaked ShapeHandles.')
console.log(JSON.stringify({ status: 'chrome-spreadsheet-pass', browser: report.browser.product, cells: report.cells, aliases: report.aliases, dependencies: report.dependencies, cycle: report.cycle, binding: report.binding, persistence: report.persistence, resource: report.resource, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
