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-transform-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 PartDesign transform verification is not passing.')
const expected = ['hole', 'fillet', 'chamfer', 'draft', 'thickness', 'hole', 'hole', 'linear-pattern', 'mirrored', 'mirrored', 'linear-pattern', 'polar-pattern', 'multi-transform', 'hole']
if (JSON.stringify(report.operations?.map((operation) => operation.command)) !== JSON.stringify(expected)) throw new Error('PartDesign transform operation coverage is incomplete.')
if (report.operations.some((operation) => operation.recompute !== 'completed' || !operation.objectId || !operation.typeId || !operation.shapeId || !(operation.volume > 0) || operation.solids !== 1 || operation.shapeCount < 1 || operation.kernelReferenceCount < 1)) throw new Error('PartDesign transform recompute, single-solid, or ownership evidence is incomplete.')
const featureTransforms = report.operations.filter((operation) => operation.transformMode === 'Features')
if (featureTransforms.length !== 2 || featureTransforms.some((operation) => !Array.isArray(operation.originals) || operation.originals.length !== 1)) throw new Error('PartDesign Feature-list transform evidence is incomplete.')
const threadedHoles = report.operations.filter((operation) => operation.command === 'hole' && operation.threaded === true)
if (threadedHoles.length !== 2 || threadedHoles[0].modelThread !== false || threadedHoles[0].threadType !== 'ISOMetricProfile' || threadedHoles[0].threadSize !== 'M1x0.25' || threadedHoles[1].modelThread !== true || threadedHoles[1].threadDirection !== 'Left') throw new Error('PartDesign Hole thread-standard evidence is incomplete.')
if (report.topologyMigration?.editedPadLength !== 31 || report.topologyMigration?.wrongBindings !== 0 || report.topologyMigration?.stable < 1 || report.topologyMigration?.ambiguous < 1 || report.topologyMigration?.ambiguityDiagnostics !== report.topologyMigration?.ambiguous || JSON.stringify(report.topologyMigration?.migrated?.map((entry) => entry.command)) !== JSON.stringify(['fillet', 'chamfer', 'draft', 'thickness']) || report.topologyMigration.migrated.some((entry) => entry.objectId !== report.operations[0].objectId || !['stable', 'ambiguous'].includes(entry.status) || !entry.persistentId)) throw new Error('PartDesign dress-up topology migration evidence is incomplete.')
if (report.ambiguityRecovery?.command !== 'fillet' || report.ambiguityRecovery?.status !== 'failed' || report.ambiguityRecovery?.code !== 'DRESSUP_EDGE_REFERENCE_UNRESOLVED' || report.ambiguityRecovery?.retainedShapeId !== report.ambiguityRecovery?.previousShapeId || report.ambiguityRecovery?.restoredStatus !== 'completed' || !report.ambiguityRecovery?.restoredShapeId) throw new Error('PartDesign dress-up ambiguity recovery evidence is incomplete.')
if (report.persistence?.mode !== 'sqlite-opfs' || report.persistence.objectCount < 1 || report.persistence.reopenedTip !== report.operations.at(-1)?.objectId) throw new Error('PartDesign transform OPFS round-trip evidence is incomplete.')
if (report.opfs?.markerSuite !== 'PD-TRANSFORM' || report.opfs.markerOperations !== 14 || report.opfs.markerRemoved !== true) throw new Error('PartDesign transform OPFS marker was not verified and removed.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error('PartDesign transform ShapeHandles were not fully released.')
console.log(JSON.stringify({ status: 'chrome-partdesign-transform-pass', browser: report.browser.product, operations: report.operations, persistence: report.persistence, opfs: report.opfs, afterRelease: report.afterRelease }, null, 2))
