import { describe, it, expect } from 'vitest'
import { validateDescriptor } from '../react-pilot/src/demos/descriptors/schema'
import { progressDescriptor } from '../react-pilot/src/demos/descriptors/progress'
import { buttonDescriptor } from '../react-pilot/src/demos/descriptors/button'

describe('validateDescriptor', () => {
  it('flags missing component name', () => {
    expect(validateDescriptor({})).toContain('missing component name')
  })
  it('flags empty sections', () => {
    expect(validateDescriptor({ component: 'X', sections: [] })).toContain('sections must be a non-empty array')
  })
  it('flags bad title tuple', () => {
    const errs = validateDescriptor({ component: 'X', sections: [{ title: 'nope', cards: [{ type: 'prose', summary: ['a', 'b'] }] }] })
    expect(errs.some((e) => e.includes('title must be [en,zh]'))).toBe(true)
  })
  it('flags unknown card type', () => {
    const errs = validateDescriptor({ component: 'X', sections: [{ title: ['A', '甲'], cards: [{ type: 'bogus' }] }] })
    expect(errs.some((e) => e.includes('unknown'))).toBe(true)
  })
  it('accepts a minimal valid descriptor', () => {
    expect(validateDescriptor({ component: 'X', sections: [{ title: ['A', '甲'], cards: [{ type: 'prose', summary: ['a', 'b'] }] }] })).toEqual([])
  })
  it('progress descriptor is valid + has 6 sections', () => {
    expect(validateDescriptor(progressDescriptor)).toEqual([])
    expect(progressDescriptor.sections.map((s) => s.title[0])).toEqual([
      'Figma Coverage', 'Family Matrix', 'Runtime Value', 'Development Usage', 'Status Matrix', 'Interactive: showLabel',
    ])
  })
  it('button descriptor is valid + has 11 sections (2 info + 9 axis)', () => {
    expect(validateDescriptor(buttonDescriptor)).toEqual([])
    expect(buttonDescriptor.sections.map((s) => s.title[0])).toEqual([
      'Figma M Coverage', 'Development Usage',
      'Basic Usage', 'Color Buttons', 'Status Buttons', 'Disable Buttons', 'Icon Buttons',
      'Loading Buttons', 'Round Buttons', 'Button Sizes', 'Fixed Width Buttons',
    ])
  })
  it('button descriptor: 9 axis sections use layout "bare" with exactly one card', () => {
    const axisIds = ['Basic Usage', 'Color Buttons', 'Status Buttons', 'Disable Buttons', 'Icon Buttons',
      'Loading Buttons', 'Round Buttons', 'Button Sizes', 'Fixed Width Buttons']
    const axisSections = buttonDescriptor.sections.filter((s) => axisIds.includes(s.title[0]))
    expect(axisSections).toHaveLength(9)
    for (const s of axisSections) {
      expect(s.layout).toBe('bare')
      expect(s.cards).toHaveLength(1)
    }
  })
  it('button descriptor: loading-matrix has 40 members', () => {
    const loadingSection = buttonDescriptor.sections.find((s) => s.title[0] === 'Loading Buttons')!
    const card = loadingSection.cards[0] as any
    expect(card.type).toBe('loading-matrix')
    expect(card.members).toHaveLength(40)
  })
})
