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-partdesign-lifecycle-verification.json'), 'utf8'))
if (report.schemaVersion !== 1 || report.browserId !== 'chrome' || report.status !== 'pass' || report.crossOriginIsolated !== true) throw new Error('Chrome PartDesign lifecycle evidence is not passing or isolated.')
if (JSON.stringify(report.structural?.initialOrder) !== JSON.stringify(['sketch', 'pad', 'pocket', 'fillet']) || JSON.stringify(report.structural?.restoredOrder) !== JSON.stringify(report.structural.initialOrder) || report.structural?.reorderRejected !== true || report.structural?.dependencyRemovalRejected !== true || JSON.stringify(report.structural?.deleted?.removed) !== JSON.stringify(['fillet']) || report.structural.deleted.tip !== 'pocket' || report.structural.deleted.filletExists !== false || report.structural.deletionUndone?.tip !== 'fillet' || report.structural.deletionUndone.filletExists !== true || report.structural.deletionRedone?.tip !== 'pocket' || report.structural.deletionRedone.filletExists !== false || report.structural.restoredTip !== 'fillet') throw new Error('Chrome PartDesign reorder/delete/Undo/Redo evidence is invalid.')
if (report.initial?.length !== 42 || report.initial?.tip !== 'pad' || report.initial?.support !== 'XY_Plane' || report.initial?.pocketSuppressed !== true || report.initial?.filletSuppressed !== true || !(report.initial?.volume > 0) || report.initial?.solids !== 1) throw new Error('Chrome PartDesign initial suppression/Tip state is invalid.')
if (report.edited?.length !== 31 || report.edited?.tip !== 'pad' || report.edited?.support !== 'XY_Plane' || report.edited?.recompute !== 'completed' || report.edited?.shapeCount < 1 || report.edited?.kernelReferenceCount < 1 || !(report.edited?.volume > 0) || report.edited?.solids !== 1 || report.edited?.shapeId === report.initial?.shapeId) throw new Error('Chrome PartDesign edited recompute evidence is invalid or stale.')
if (report.undone?.length !== 42 || report.undone?.tip !== 'pad' || report.undone?.support !== 'XY_Plane' || report.undone?.recompute !== 'completed' || report.undone?.shapeCount < 1 || report.undone?.kernelReferenceCount < 1 || !(report.undone?.volume > 0) || report.undone?.solids !== 1 || report.undone?.shapeId === report.edited?.shapeId || Math.abs(report.undone?.volume - report.initial?.volume) > 1e-7) throw new Error('Chrome PartDesign undo/recompute evidence is invalid or stale.')
if (report.reopened?.length !== 42 || report.reopened?.persistedLength !== 42 || report.reopened?.tip !== 'pad' || report.reopened?.support !== 'XY_Plane' || report.reopened?.pocketSuppressed !== true || report.reopened?.filletSuppressed !== true || report.reopened?.recompute !== 'completed' || report.reopened?.persistenceMode !== 'sqlite-opfs' || !(report.reopened?.volume > 0) || report.reopened?.solids !== 1 || report.reopened?.shapeId === report.edited?.shapeId || Math.abs(report.reopened?.volume - report.initial?.volume) > 1e-7) throw new Error('Chrome PartDesign OPFS reopen evidence is invalid or stale.')
if (report.released?.shapeCount !== 0 || report.released?.kernelReferenceCount !== 0) throw new Error('Chrome PartDesign lifecycle leaked Bitbybit shapes.')

console.log(JSON.stringify({ status: 'chrome-partdesign-lifecycle-pass', browserId: report.browserId, structural: report.structural, initial: report.initial, edited: report.edited, undone: report.undone, reopened: report.reopened, released: report.released }, null, 2))
