import { describe, it, expect } from 'vitest'
import { checkExportsFresh } from '../scripts/audit-token-exports.mjs'

describe('audit-token-exports', () => {
  it('passes when on-disk JSON matches freshly-built', () => {
    const fresh = '{"palette":{"a":{"$type":"color","$value":"#000"}}}'
    expect(checkExportsFresh(fresh, JSON.parse(fresh)).ok).toBe(true)
  })

  it('fails when on-disk drifts from source', () => {
    const built = { palette: { a: { $type: 'color', $value: '#000' } } }
    const stale = { palette: { a: { $type: 'color', $value: '#fff' } } }
    const r = checkExportsFresh(JSON.stringify(built), stale)
    expect(r.ok).toBe(false)
    expect(r.message).toMatch(/drift|mismatch/i)
  })

  it('is formatting-insensitive: pretty-printed vs compact JSON of the same structure still passes', () => {
    const built = { palette: { a: { $type: 'color', $value: '#000' } }, semantic: {} }
    // on-disk object has identical key order/content, just parsed from a compact
    // (no-whitespace) JSON string rather than the pretty-printed one emitJson produces.
    const onDisk = JSON.parse(JSON.stringify(built))
    const fresh = JSON.stringify(built, null, 2) + '\n' // pretty-printed, trailing newline
    const r = checkExportsFresh(fresh, onDisk)
    expect(r.ok).toBe(true)
  })
})
