import { isDeepStrictEqual } from 'node:util'
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-font-failure.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyFont failure check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-font-failure' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.propertyType !== 'App::PropertyFont') fail('baseline is invalid')
if (report.object?.name !== 'FontProbe' || report.object.typeId !== 'TechDraw::DrawViewAnnotation' || report.property?.name !== 'Font' || report.property.typeId !== 'App::PropertyFont' || report.initial?.value !== 'osifont') fail('failure fixture identity changed')
const failure = report.failure
if (failure?.id !== 'wrong-type' || failure.exception?.type !== 'TypeError' || failure.before?.value !== 'osifont' || failure.after?.value !== 'osifont' || failure.objectsPreserved !== true || !isDeepStrictEqual(failure.beforeObjects, failure.afterObjects)) fail('wrong type did not reject without mutation or pollution')
const disabled = report.disabled
if (disabled?.classification !== 'editor-read-only-and-python-immutable' || !disabled.status?.includes('Immutable') || !disabled.editorMode?.includes('ReadOnly') || disabled.exception?.type !== 'AttributeError' || disabled.exception.message !== "Object attribute 'Font' is read-only" || disabled.before?.value !== 'osifont' || disabled.after?.value !== 'osifont' || !isDeepStrictEqual(disabled.beforeObjects, disabled.afterObjects) || !isDeepStrictEqual(disabled.restoredStatus, []) || !isDeepStrictEqual(disabled.restoredEditorMode, [])) fail('disabled property state did not reject and restore cleanly')
const transaction = report.transaction
if (transaction?.undoMode !== 1 || transaction.pendingAfterEdit !== true || transaction.pendingAfterAbort !== false || transaction.activeAfterEdit?.name !== 'property-font-cancel' || !(transaction.activeAfterEdit.id > 0) || transaction.activeAfterAbort?.name !== '' || transaction.activeAfterAbort.id !== 0 || transaction.before?.value !== 'osifont' || transaction.edited?.value !== 'Transaction Font' || transaction.afterAbort?.value !== 'osifont' || transaction.restored !== true || transaction.objectsRestored !== true) fail('transaction abort did not restore PropertyFont')
if (report.cancellationBoundary?.supported !== false || report.cancellationBoundary.classification !== 'synchronous-property-setter' || report.cancellationBoundary.reason !== 'no-native-cancel-hook' || report.cancellationBoundary.replacement !== 'abort-active-document-transaction') fail('cancellation boundary is not explicit')
if (report.documentIntegrity?.objectsPreserved !== true || report.documentIntegrity.objectCount !== 1 || !isDeepStrictEqual(report.documentIntegrity.initialObjects, report.documentIntegrity.finalObjects)) fail('failure, disabled or cancellation cases polluted the document')
console.log(JSON.stringify({ status: 'freecad-property-font-failure-pass', failure: { type: failure.exception.type, valuePreserved: failure.after.value }, disabled: { status: disabled.status, editorMode: disabled.editorMode, exception: disabled.exception }, transactionRestored: transaction.restored, documentObjectsPreserved: report.documentIntegrity.objectsPreserved, cancellationBoundary: report.cancellationBoundary }, null, 2))
