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-heatflux-success.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyHeatFlux success check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-heatflux-success' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d') fail('baseline is invalid')
if (report.object?.name !== 'HeatFluxProbe' || report.object.typeId !== 'Fem::ConstraintHeatflux' || report.property?.name !== 'DFlux' || report.property.typeId !== 'App::PropertyHeatFlux' || report.property.group !== 'ConstraintHeatflux') fail('native object/property identity changed')
if (report.diagnostics?.exception !== null || report.diagnostics.documentObjectCount !== 1 || report.cases?.length !== 7) fail('native success cases are incomplete')
const expectedValues = new Map([
  ['default', 0],
  ['nominal-watt-per-square-meter', 500],
  ['zero-boundary', 0],
  ['negative-boundary', -5],
  ['kilowatt-per-square-meter', 2000],
  ['watt-per-square-millimeter', 1000000],
  ['restored', 0],
])
const byId = new Map(report.cases.map((entry) => [entry.id, entry]))
for (const [id, expected] of expectedValues) {
  const entry = byId.get(id)
  if (!entry || entry.objectTypeId !== 'Fem::ConstraintHeatflux' || entry.propertyName !== 'DFlux' || entry.typeId !== 'App::PropertyHeatFlux' || entry.group !== 'ConstraintHeatflux' || JSON.stringify(entry.status) !== '[]' || JSON.stringify(entry.editorMode) !== '[]') fail(`${id} native property metadata changed`)
  if (entry.value?.numeric !== expected || typeof entry.value.unit !== 'string' || !entry.value.unit.includes('kg/s^3') || entry.shape?.applicable !== false || !Array.isArray(entry.objectState)) fail(`${id} heat flux value or object state is invalid`)
}
console.log(JSON.stringify({ status: 'freecad-property-heatflux-success-pass', cases: Object.fromEntries([...expectedValues].map(([id]) => [id, byId.get(id).value.numeric])), internalUnit: 'kg/s^3', wattsPerSquareMeterScale: 1 }, null, 2))

