import { createHash } from 'node:crypto'
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const manifest = JSON.parse(await readFile(resolve(root, 'config/planegcs-wasm-artifact.json'), 'utf8'))
if (manifest.schemaVersion !== 1 || manifest.freecadVersion !== '1.1.1' || manifest.freecadCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || manifest.emscriptenVersion !== '3.1.69' || manifest.pthreadPoolSize !== 2) throw new Error('planegcs artifact baseline is invalid.')
const checked = []
for (const kind of ['javascript', 'wasm']) {
  const record = manifest[kind]
  const bytes = await readFile(resolve(root, record.path))
  const publicBytes = await readFile(resolve(root, record.publicPath))
  const hash = createHash('sha256').update(bytes).digest('hex')
  const publicHash = createHash('sha256').update(publicBytes).digest('hex')
  if (hash !== record.sha256 || publicHash !== record.sha256) throw new Error(`planegcs ${kind} artifact hash drifted.`)
  checked.push({ kind, bytes: bytes.byteLength, sha256: hash })
}
console.log(JSON.stringify({ status: 'planegcs-artifact-pass', freecadCommit: manifest.freecadCommit, pthreadPoolSize: manifest.pthreadPoolSize, artifacts: checked }, null, 2))
