import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import { compareGoldenResult, loadGoldenManifest, validateGoldenOperation } from '../scripts/freecad-golden-contract.mjs'

const manifestFile = resolve('fixtures/freecad-golden/manifest.json')

test('FreeCAD golden manifest loads unique validated scenarios', async () => {
  const manifest = await loadGoldenManifest(manifestFile)
  assert.equal(manifest.baselineId, 'freecad-1.1.1')
  assert.equal(manifest.scenarios.length, 100)
  assert.deepEqual(manifest.scenarios.slice(0, 5).map((entry) => entry.id), ['part-box', 'part-cylinder', 'part-sphere', 'part-cone', 'part-cut-through-hole'])
  assert.equal(manifest.scenarios.find((entry) => entry.id === 'part-cut-through-hole').scenario.expected.shapeType, 'Compound')
  assert.equal(manifest.scenarios.find((entry) => entry.id === 'part-cut-through-hole').scenario.expected.solids, 1)
})

test('FreeCAD failure corpus includes an invalid Common operand', async () => {
  const failureManifest = JSON.parse(await readFile(resolve('fixtures/freecad-golden/failures/manifest.json'), 'utf8'))
  assert.equal(failureManifest.failures.length, 51)
  const common = failureManifest.failures.find((entry) => entry.id === 'invalid-common-001')
  assert.ok(common)
  const fixture = JSON.parse(await readFile(resolve('fixtures/freecad-golden/failures', common.file), 'utf8'))
  assert.throws(() => validateGoldenOperation(fixture.operation, fixture.id), /must have a type/)
})

test('supplemental Part family corpus validates native solid primitives and failures', async () => {
  const supplemental = await loadGoldenManifest(resolve('fixtures/freecad-golden/feature-families/manifest.json'))
  assert.equal(supplemental.scenarios.length, 5)
  assert.deepEqual(supplemental.scenarios.map((entry) => entry.scenario.operation.type), ['ellipsoid', 'torus', 'prism', 'wedge', 'helix'])
  const failures = JSON.parse(await readFile(resolve('fixtures/freecad-golden/feature-families/failures/manifest.json'), 'utf8'))
  assert.equal(failures.failures.length, 5)
  for (const entry of failures.failures) {
    const fixture = JSON.parse(await readFile(resolve('fixtures/freecad-golden/feature-families/failures', entry.file), 'utf8'))
    assert.throws(() => validateGoldenOperation(fixture.operation, fixture.id))
  }
})

test('golden comparator applies linear and scalar tolerances only to declared oracle fields', async () => {
  const manifest = await loadGoldenManifest(manifestFile)
  const box = manifest.scenarios[0].scenario
  const actual = structuredClone(box.expected)
  actual.volume += box.tolerance.scalar / 2
  actual.boundingBox.max[0] += box.tolerance.linear / 2
  actual.untrackedRuntimeField = 'ignored'
  assert.deepEqual(compareGoldenResult(box, actual), [])
  actual.volume += box.tolerance.scalar * 2
  assert.match(compareGoldenResult(box, actual)[0], /volume/)
})

test('golden operation validation rejects malformed dimensions, placements and unknown operations', () => {
  assert.throws(() => validateGoldenOperation({ type: 'box', length: 0, width: 2, height: 3 }), /positive/)
  assert.throws(() => validateGoldenOperation({ type: 'sphere', radius: 2, placement: { rotation: { axis: [0, 0, 0], angle: 10 } } }), /non-zero/)
  assert.throws(() => validateGoldenOperation({ type: 'loft' }), /unsupported/)
})
