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-integerset-failure.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyIntegerSet failure check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-integerset-failure' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.propertyType !== 'App::PropertyIntegerSet') fail('baseline is invalid')
if (report.object?.name !== 'NodesProbe' || report.object.typeId !== 'Fem::FemSetNodesObject' || report.property?.name !== 'Nodes' || report.property.typeId !== 'App::PropertyIntegerSet' || !isDeepStrictEqual(report.initial, [1, 3])) fail('fixture identity changed')
const expected = new Map([
  ['float-item', { requested: '[1, 1.5]', message: 'type in list must be int, not float' }],
  ['string-item', { requested: "[1, '3']", message: 'type in list must be int, not str' }],
  ['set-object', { requested: '{1, 3}', message: 'type must be int or list of int, not set' }],
  ['none', { requested: 'None', message: 'type must be int or list of int, not NoneType' }],
])
if (report.failures?.length !== expected.size) fail('failure case count changed')
for (const entry of report.failures) {
  const contract = expected.get(entry.id)
  if (!contract || entry.requested !== contract.requested || entry.exception?.type !== 'TypeError' || entry.exception.message !== contract.message || entry.valuePreserved !== true || entry.objectsPreserved !== true || entry.polluted !== false || !isDeepStrictEqual(entry.before, [1, 3]) || !isDeepStrictEqual(entry.after, [1, 3]) || !isDeepStrictEqual(entry.beforeObjects, entry.afterObjects)) fail(`${entry.id} did not reject atomically`)
}
const disabled = report.disabled
if (disabled?.classification !== 'editor-read-only-and-python-immutable' || !isDeepStrictEqual(disabled.requested, [2, 4]) || !isDeepStrictEqual(disabled.status, ['Immutable', 'ReadOnly']) || !isDeepStrictEqual(disabled.editorMode, ['ReadOnly']) || disabled.exception?.type !== 'AttributeError' || disabled.exception.message !== "Object attribute 'Nodes' is read-only" || disabled.valuePreserved !== true || disabled.objectsPreserved !== true || !isDeepStrictEqual(disabled.before, [1, 3]) || !isDeepStrictEqual(disabled.after, [1, 3]) || !isDeepStrictEqual(disabled.restoredStatus, []) || !isDeepStrictEqual(disabled.restoredEditorMode, [])) fail('disabled state changed')
const transaction = report.transaction
if (transaction?.undoMode !== 1 || transaction.pendingAfterEdit !== true || transaction.pendingAfterAbort !== false || transaction.activeAfterEdit?.name !== 'property-integerset-cancel' || !(transaction.activeAfterEdit.id > 0) || transaction.activeAfterAbort?.name !== '' || transaction.activeAfterAbort.id !== 0 || transaction.restored !== true || transaction.objectsRestored !== true || !isDeepStrictEqual(transaction.before, [1, 3]) || !isDeepStrictEqual(transaction.edited, [2, 4]) || !isDeepStrictEqual(transaction.afterAbort, [1, 3]) || !isDeepStrictEqual(transaction.objectsBefore, transaction.objectsAfter)) fail('transaction abort changed')
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 incomplete')
if (report.documentIntegrity?.objectsPreserved !== true || !isDeepStrictEqual(report.documentIntegrity.initialObjects, report.documentIntegrity.finalObjects)) fail('document was polluted')
console.log(JSON.stringify({ status: 'freecad-property-integerset-failure-pass', failures: report.failures.map(({ id, exception }) => ({ id, exception })), disabled: { status: disabled.status, exception: disabled.exception }, transactionRestored: transaction.restored, cancellationBoundary: report.cancellationBoundary }, null, 2))

