import { describe, it, expect } from 'vitest'
import { chapters, getAllFiles, getChapterById } from '../data/chapters.js'

describe('chapters data', () => {
  it('should have 6 chapters', () => {
    expect(chapters.length).toBe(6)
  })

  it('each chapter should have required fields', () => {
    for (const ch of chapters) {
      expect(ch.id).toBeTruthy()
      expect(ch.title).toBeTruthy()
      expect(ch.subtitle).toBeTruthy()
      expect(ch.description).toBeTruthy()
      expect(ch.icon).toBeTruthy()
      expect(Array.isArray(ch.topics)).toBe(true)
    }
  })

  it('chapter ids should be ch1 through ch6', () => {
    const ids = chapters.map(ch => ch.id)
    expect(ids).toEqual(['ch1', 'ch2', 'ch3', 'ch4', 'ch5', 'ch6'])
  })

  it('getChapterById should find correct chapter', () => {
    const ch3 = getChapterById('ch3')
    expect(ch3.title).toContain('动态规划')
    expect(getChapterById('nonexistent')).toBeUndefined()
  })

  it('each chapter should have at least one topic', () => {
    for (const ch of chapters) {
      expect(ch.topics.length).toBeGreaterThan(0)
    }
  })
})

describe('getAllFiles', () => {
  it('should return all files flattened', () => {
    const files = getAllFiles()
    expect(files.length).toBeGreaterThan(0)
  })

  it('each file should have path, name, label', () => {
    const files = getAllFiles()
    for (const f of files) {
      expect(f.path).toBeTruthy()
      expect(f.name).toBeTruthy()
      expect(f.label).toBeTruthy()
      expect(f.chapterId).toBeTruthy()
    }
  })
})
