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/freecad-property-file-mutation.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyFile mutation check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-file-mutation' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.propertyType !== 'App::PropertyFile' || report.object?.typeId !== 'Mesh::Import' || report.property?.name !== 'FileName' || report.property?.typeId !== 'App::PropertyFile') fail('baseline or identity is invalid')
const phases = report.phases
if (!phases?.before || !phases.edited || !phases.restored || report.requested?.before !== phases.before.value || report.requested.edited !== phases.edited.value || report.requested.restored !== phases.restored.value) fail('mutation phases are incomplete')
for (const [id, phase] of Object.entries(phases)) if (phase.propertyTypeId !== 'App::PropertyFile' || JSON.stringify(phase.propertyStatus) !== '[]' || JSON.stringify(phase.editorMode) !== '[]' || !Array.isArray(phase.objectState) || !Array.isArray(phase.objectSet) || typeof phase.recomputeResult !== 'boolean') fail(`${id} phase metadata is invalid`)
if (phases.before.statusString !== 'Valid' || phases.before.value !== report.requested.before || phases.edited.value !== report.requested.edited || phases.edited.statusString !== 'File does not exist' || JSON.stringify(phases.edited.objectState) !== '["Touched","Invalid"]' || phases.restored.value !== report.requested.restored || phases.restored.statusString !== 'Valid' || JSON.stringify(phases.restored.objectState) !== '["Up-to-date"]') fail('native mutation or restore semantics changed')
if (report.classification?.valueChanged !== true || report.classification.invalidEditedPath !== true || report.classification.restoreExact !== true || report.classification.objectSetStable !== true || report.classification.unknownSemanticDrift !== false) fail('mutation classification is not exact')
console.log(JSON.stringify({ status: 'freecad-property-file-mutation-pass', before: phases.before.value, edited: phases.edited.value, restored: phases.restored.value, editedState: phases.edited.objectState, restoreExact: report.classification.restoreExact, unknownSemanticDrift: report.classification.unknownSemanticDrift }, null, 2))
