import { execFileSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'

describe('KBX exception registry gate', () => {
  it('accepts the current empty approved registry', () => {
    const validator = join(process.cwd(), '..', 'scripts', 'validate-kbx-exceptions.mjs')
    const output = execFileSync(process.execPath, [validator, '--root', '.'], { cwd: process.cwd(), encoding: 'utf8' })
    expect(output).toContain('failures=0')
  })

  it('rejects an active exception with an expired review date', () => {
    const validator = join(process.cwd(), '..', 'scripts', 'validate-kbx-exceptions.mjs')
    const root = mkdtempSync(join(tmpdir(), 'kbx-exception-'))
    mkdirSync(join(root, 'src', 'shared', 'ui'), { recursive: true })
    writeFileSync(join(root, 'src', 'shared', 'ui', 'kbx-exception-registry.json'), JSON.stringify({ schemaVersion: '1.0', exceptions: [{ id: 'KBX-EX-TEST', screenId: 'TEST-001', type: 'direct-ui', reason: 'fixture', owner: 'QA', introducedVersion: '1.0.0', reviewAt: '2020-01-01', removalTarget: 'TEST', status: 'active' }] }))
    expect(() => execFileSync(process.execPath, [validator, '--root', root], { encoding: 'utf8' })).toThrow()
  })
})
