import { test } from 'node:test'
import assert from 'node:assert/strict'
import { createSecondaryFormats, type SecondaryFormat } from '../src/facade/secondaryFormats'

const formats: SecondaryFormat[] = ['DXF', 'SVG', 'OBJ', 'PLY', 'STL', 'PDF', 'IFC', 'CSV']

test('secondary format adapters cover 2D, mesh, BIM and data with byte-exact proxy round-trips', () => {
  const api = createSecondaryFormats()
  for (const format of formats) {
    const bytes = api.exportBytes(format, { label: 'Fixture' })
    assert.equal(api.detect(format, bytes), true)
    const record = api.importBytes({ id: format, format, bytes })
    assert.equal(record.detected, true)
    assert.equal(record.byteLength, bytes.byteLength)
    assert.deepEqual(api.exportRecord(format), bytes)
  }
  assert.deepEqual(new Set(api.descriptors().map((entry) => entry.category)), new Set(['2d', 'mesh', 'bim', 'data']))
  assert.equal(api.snapshot().records.length, formats.length)
  assert.deepEqual(api.exportBytes('SVG'), api.exportBytes('SVG'))
})

test('every secondary format rejects an invalid signature and records remain isolated', () => {
  const api = createSecondaryFormats()
  const invalid = new TextEncoder().encode('invalid secondary payload')
  for (const format of formats) assert.throws(() => api.importBytes({ id: `bad-${format}`, format, bytes: invalid }), new RegExp(`${format} payload signature`))
  const bytes = api.exportBytes('PDF')
  api.importBytes({ id: 'pdf', format: 'PDF', bytes })
  assert.throws(() => api.importBytes({ id: 'pdf', format: 'PDF', bytes }), /already exists/)
  api.remove('pdf')
  assert.throws(() => api.exportRecord('pdf'), /does not exist/)
})
