import { describe, it, expect } from 'vitest'
import { getEngineDigest } from '../engines.store'

// Generic markdown in the shape every engine-tagged wiki page must follow:
// intro paragraph, then "What You Get" as the first heading with bullets.
// Dummy text only — real content always comes from the wiki at runtime.
const SAMPLE_MARKDOWN = `
> Example Engine is a **sample** framework that turns markdown into landing cards.

# What You Get

- **First feature**: some \`inline code\` — with a longer explanation.
- **Second feature**: a [link](https://example.org) inside the text.
- Plain bullet without a bold lead.

# Later Section

- **Not picked up**: a list under a later heading.
`

describe('getEngineDigest', () => {
  it('extracts the intro from the leading blockquote with inline markdown stripped', () => {
    const digest = getEngineDigest(SAMPLE_MARKDOWN)
    expect(digest.intro).toBe(
      'Example Engine is a sample framework that turns markdown into landing cards.',
    )
  })

  it('takes the What You Get heading as the highlights title', () => {
    expect(getEngineDigest(SAMPLE_MARKDOWN).highlightsTitle).toBe('What You Get')
  })

  it('parses bold-lead bullets into title and text', () => {
    const [first, second] = getEngineDigest(SAMPLE_MARKDOWN).highlights
    expect(first).toEqual({
      title: 'First feature',
      text: 'some inline code — with a longer explanation.',
    })
    expect(second).toEqual({
      title: 'Second feature',
      text: 'a link inside the text.',
    })
  })

  it('keeps bullets without a bold lead as plain text', () => {
    const third = getEngineDigest(SAMPLE_MARKDOWN).highlights[2]
    expect(third).toEqual({ title: '', text: 'Plain bullet without a bold lead.' })
  })

  it('stops at the heading after the What You Get section', () => {
    expect(getEngineDigest(SAMPLE_MARKDOWN).highlights).toHaveLength(3)
  })

  it('matches the heading case-insensitively', () => {
    const digest = getEngineDigest('# WHAT you get\n- **A**: b.')
    expect(digest.highlights).toEqual([{ title: 'A', text: 'b.' }])
  })

  it('yields no highlights when the first heading is not What You Get', () => {
    const digest = getEngineDigest('Intro line.\n\n# Features\n- **A**: b.\n\n# What You Get\n- **C**: d.')
    expect(digest.intro).toBe('Intro line.')
    expect(digest.highlights).toEqual([])
  })

  it('caps the highlights at six items', () => {
    const many = '# What You Get\n' + Array.from({ length: 9 }, (_, i) => `- item ${i}`).join('\n')
    expect(getEngineDigest(many).highlights).toHaveLength(6)
  })

  it('uses the first paragraph as intro when there is no blockquote', () => {
    const digest = getEngineDigest('Just a plain paragraph.\n\n---\n\n# What You Get\n- one')
    expect(digest.intro).toBe('Just a plain paragraph.')
    expect(digest.highlights).toEqual([{ title: '', text: 'one' }])
  })

  it('returns an empty digest for empty content', () => {
    expect(getEngineDigest('')).toEqual({ intro: '', highlightsTitle: '', highlights: [] })
  })
})
