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-native-pocket-history-verification.json'), 'utf8'))
if (report.status !== 'pass' || report.browserId !== 'chrome' || report.crossOriginIsolated !== true) throw new Error('Chrome native Pocket history verification is not passing.')
if (report.capabilities?.status !== 'ready' || report.nativeCapabilities?.availability !== 'available' || report.nativeCapabilities?.transport !== 'step-text' || report.nativeCapabilities?.operations?.includes('pocket') !== true) throw new Error('Native OCCT Pocket history Worker capability evidence is incomplete.')
const stages = report.history?.stages || []
if (!Number.isSafeInteger(report.history?.recordCount) || report.history.recordCount <= 0 || stages.length !== 2 || stages[0]?.operation !== 'pad' || JSON.stringify(stages[0]?.inputObjectIds) !== JSON.stringify(['Profile']) || stages[0]?.recordCount <= 0 || stages[0]?.topologyEntries <= 0 || stages[1]?.operation !== 'cut' || JSON.stringify(stages[1]?.inputObjectIds) !== JSON.stringify(['Base', stages[0]?.resultObjectId]) || stages[1]?.recordCount !== report.history.recordCount || stages[1]?.topologyEntries <= 0 || !report.history.sourceObjects?.includes('Base') || !report.history.sourceObjects?.includes('Profile') || !report.history.sourceObjects?.includes(stages[0]?.resultObjectId)) throw new Error('Native OCCT Pocket staged source relation evidence is incomplete.')
const twoSidedStages = report.twoSidedHistory?.stages || []
if (report.nativeCapabilities?.operations?.includes('pad') !== true || report.nativeCapabilities?.operations?.includes('fuse') !== true || report.nativeCapabilities?.operations?.includes('cut') !== true || report.twoSidedHistory?.recordCount <= 0 || report.twoSidedHistory?.structuralValid !== true || report.twoSidedHistory?.solids !== 1 || twoSidedStages.length !== 4 || twoSidedStages[0]?.operation !== 'pad' || JSON.stringify(twoSidedStages[0]?.inputObjectIds) !== JSON.stringify(['Profile']) || twoSidedStages[0]?.topologyEntries <= 0 || twoSidedStages[1]?.operation !== 'pad' || JSON.stringify(twoSidedStages[1]?.inputObjectIds) !== JSON.stringify(['Profile']) || twoSidedStages[1]?.topologyEntries <= 0 || twoSidedStages[2]?.operation !== 'fuse' || JSON.stringify(twoSidedStages[2]?.inputObjectIds) !== JSON.stringify([twoSidedStages[0]?.resultObjectId, twoSidedStages[1]?.resultObjectId]) || twoSidedStages[2]?.topologyEntries <= 0 || twoSidedStages[3]?.operation !== 'cut' || JSON.stringify(twoSidedStages[3]?.inputObjectIds) !== JSON.stringify(['BaseTwoSided', twoSidedStages[2]?.resultObjectId]) || twoSidedStages[3]?.resultObjectId !== 'PocketTwoSided' || twoSidedStages[3]?.topologyEntries <= 0 || !report.twoSidedHistory?.sourceObjects?.includes('BaseTwoSided') || !report.twoSidedHistory?.sourceObjects?.includes('Profile')) throw new Error('Native OCCT two-sided Pocket stage evidence is incomplete.')
if (report.history.relations?.modified <= 0 || report.history.relations.generated + report.history.relations.deleted <= 0) throw new Error('Native OCCT Pocket history relation evidence is incomplete.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error('Chrome native Pocket history handles were not released.')
console.log(JSON.stringify({ status: 'chrome-native-pocket-history-pass', browser: report.browserId, occtVersion: report.nativeCapabilities.occtVersion, records: report.history.recordCount, relations: report.history.relations, stages, twoSidedStages, afterRelease: report.afterRelease }, null, 2))
