import { describe, it, expect } from 'vitest'
// @ts-expect-error — plain JS ESM CLI script, no types
import { checkComponentMapping } from '../scripts/design-kickoff.mjs'

// §9 table body: header + separator + rows. checkComponentMapping reads rows after
// the first two lines (tableRows slices(2)), cells = [element, source, evidence].
function section(rows: string[]) {
  return [
    '| Element / affordance | Source component / token / icon | Evidence |',
    '|---|---|---|',
    ...rows,
  ].join('\n')
}

describe('design:kickoff §9 — R-A5 substance gate (catalog-verified reuse decision)', () => {
  it('passes a fully evidenced row', () => {
    const findings: any[] = []
    checkComponentMapping(section(['| login button | Button (catalog §Verified) | search_design_system hit |']), findings)
    expect(findings).toEqual([])
  })

  it('flags a row that names an element but leaves evidence blank (impression)', () => {
    const findings: any[] = []
    checkComponentMapping(section(['| login button | Button | |']), findings)
    expect(findings.map(f => f.check)).toEqual(['component-mapping-unverified'])
    expect(findings[0].msg).toContain('catalog-verified evidence')
  })

  it('flags a row whose evidence is the empty token N/A', () => {
    const findings: any[] = []
    checkComponentMapping(section(['| status badge | Badge | N/A |']), findings)
    expect(findings.map(f => f.check)).toEqual(['component-mapping-unverified'])
  })

  it('flags a row missing both source and evidence', () => {
    const findings: any[] = []
    checkComponentMapping(section(['| mystery chip | | |']), findings)
    expect(findings).toHaveLength(1)
    expect(findings[0].msg).toContain('source component/token/icon')
    expect(findings[0].msg).toContain('catalog-verified evidence')
  })

  it('skips rows with no element (does not over-report empty trailing rows)', () => {
    const findings: any[] = []
    checkComponentMapping(section(['| | | |']), findings)
    expect(findings).toEqual([])
  })

  it('accepts an explicit self-draw 🟡 candidate as evidence', () => {
    const findings: any[] = []
    checkComponentMapping(section(['| custom rail | self-draw | 🟡 candidate: catalog+library confirmed missing |']), findings)
    expect(findings).toEqual([])
  })
})
