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-partdesign-failure-oracle.json'), 'utf8'))
const expected = [
  'PartDesign::Pad', 'PartDesign::Pocket', 'PartDesign::Revolution', 'PartDesign::Groove',
  'PartDesign::AdditiveLoft', 'PartDesign::SubtractiveLoft', 'PartDesign::AdditivePipe', 'PartDesign::SubtractivePipe',
  'PartDesign::Fillet', 'PartDesign::Chamfer', 'PartDesign::Draft', 'PartDesign::Thickness',
  'PartDesign::Mirrored', 'PartDesign::MultiTransform', 'PartDesign::LinearPattern', 'PartDesign::PolarPattern', 'PartDesign::Hole',
]
const acceptedEmpty = new Set(['PartDesign::Mirrored', 'PartDesign::MultiTransform', 'PartDesign::LinearPattern', 'PartDesign::PolarPattern'])
const fail = (message) => { throw new Error(`FreeCAD PartDesign failure oracle: ${message}`) }
if (report.schemaVersion !== 1 || report.baselineId !== 'freecad-1.1.1-partdesign-failure-oracle' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.status !== 'pass') fail('baseline or status is invalid.')
if (report.summary?.cases !== expected.length || report.summary.passed !== expected.length || report.summary.rejected !== expected.length - acceptedEmpty.size || report.summary.acceptedEmpty !== acceptedEmpty.size || report.summary.accepted !== 0 || !Array.isArray(report.cases) || report.cases.length !== expected.length) fail('summary is incomplete.')
const actual = report.cases.map((fixture) => fixture.typeId)
if (JSON.stringify(actual) !== JSON.stringify(expected) || new Set(actual).size !== expected.length) fail('TypeId coverage is incomplete or reordered.')
for (const fixture of report.cases) {
  const expectedOutcome = acceptedEmpty.has(fixture.typeId) ? 'accepted-empty' : 'rejected'
  if (fixture.expected !== expectedOutcome || fixture.observed !== expectedOutcome || fixture.passed !== true || fixture.inputClass !== 'missing-required-input') fail(`${fixture.typeId} does not match its native missing-input outcome.`)
  if (fixture.shapeNull !== true && typeof fixture.error !== 'string') fail(`${fixture.typeId} has neither a null Shape nor a native exception.`)
  if (typeof fixture.error === 'string' && /No module named|Unknown document object type/i.test(fixture.error)) fail(`${fixture.typeId} records an unavailable module instead of feature semantics.`)
}
console.log(JSON.stringify({ status: 'freecad-partdesign-failure-oracle-pass', cases: report.summary.cases, rejected: report.summary.rejected, acceptedEmpty: report.summary.acceptedEmpty, typeIds: actual }, null, 2))
