import { describe, it, expect } from 'vitest'
import { render } from '@testing-library/react'
import { Markdown } from './Markdown'

describe('Markdown', () => {
  it('renders headings, bold and bullet lists (not raw markdown markers)', () => {
    const { container } = render(
      <Markdown
        content={'## Pflichten\n\nDer **Verantwortliche** muss:\n\n- ein Verzeichnis fuehren\n- Risiken bewerten'}
      />,
    )
    expect(container.querySelector('h4')?.textContent).toBe('Pflichten')
    expect(container.querySelector('strong')?.textContent).toBe('Verantwortliche')
    expect(container.querySelectorAll('li')).toHaveLength(2)
    expect(container.textContent).not.toContain('##')
    expect(container.textContent).not.toContain('**')
  })

  it('renders ordered lists and inline code', () => {
    const { container } = render(<Markdown content={'1. Erst `init`\n2. Dann `build`'} />)
    expect(container.querySelector('ol')).not.toBeNull()
    expect(container.querySelectorAll('li')).toHaveLength(2)
    expect(container.querySelectorAll('code')).toHaveLength(2)
  })

  it('renders fenced code blocks', () => {
    const { container } = render(<Markdown content={'```\nconst x = 1\n```'} />)
    expect(container.querySelector('pre')).not.toBeNull()
    expect(container.textContent).toContain('const x = 1')
  })

  it('only allows http(s) links', () => {
    const { container } = render(
      <Markdown content={'[ok](https://example.test) and [bad](javascript:alert(1))'} />,
    )
    const links = container.querySelectorAll('a')
    expect(links).toHaveLength(1)
    expect(links[0].getAttribute('href')).toBe('https://example.test')
  })
})
