import test from 'node:test'
import assert from 'node:assert/strict'
import { CAMOTICS_WASM_ARTIFACT_SHA256, CAMOTICS_WASM_SOURCE_REVISION, createCamoticsSweepKernel } from '../src/facade/camoticsWasm'

const calls: number[][] = []
const kernel = createCamoticsSweepKernel({
  memory: {} as WebAssembly.Memory,
  camotics_sweep_abi_version: () => 1,
  camotics_conic_depth: (...values: number[]) => { calls.push(values); return 1 },
  camotics_spheroid_depth: (...values: number[]) => { calls.push(values); return -1 },
  camotics_conic_bbox_count: (...values: number[]) => { calls.push(values); return 3 },
} as never)

test('CAMotics sweep adapter preserves upstream WASM identity and parser boundary', () => {
  assert.equal(kernel.backend, 'camotics-upstream-sweep-wasm')
  assert.equal(kernel.sourceRevision, CAMOTICS_WASM_SOURCE_REVISION)
  assert.equal(kernel.artifactSha256, CAMOTICS_WASM_ARTIFACT_SHA256)
  assert.equal(kernel.abiVersion, 1)
  assert.equal(kernel.fullCamoticsProgram, false)
  assert.equal(kernel.gcodeParserIncluded, false)
})

test('CAMotics sweep adapter validates and forwards native arguments', () => {
  assert.equal(kernel.conicDepth({ length: 5, topRadius: 2, start: [0, 0, 0], end: [10, 0, 0], point: [5, 0, 0] }), 1)
  assert.equal(kernel.spheroidDepth({ radius: 2, start: [0, 0, 0], end: [10, 0, 0], point: [5, 4, 0] }), -1)
  assert.equal(kernel.conicBoundingBoxCount({ length: 5, topRadius: 2, start: [0, 0, 0], end: [100, 0, 0] }), 3)
  assert.equal(calls.length, 3)
  assert.throws(() => kernel.conicDepth({ length: 0, topRadius: 2, start: [0, 0, 0], end: [1, 0, 0], point: [0, 0, 0] }), /greater than zero/)
  assert.throws(() => kernel.spheroidDepth({ radius: 2, start: [0, 0, 0], end: [1, Number.NaN, 0], point: [0, 0, 0] }), /must be finite/)
  assert.throws(() => kernel.conicBoundingBoxCount({ length: 5, topRadius: 2, start: [0, 0, 0], end: [1, 0, 0], tolerance: -1 }), /must not be negative/)
})

test('CAMotics sweep adapter rejects an unknown ABI', () => {
  assert.throws(() => createCamoticsSweepKernel({ camotics_sweep_abi_version: () => 2 } as never), /Unsupported CAMotics sweep WASM ABI/)
})
