import { describe, it, expect, vi, afterEach } from 'vitest'
import { render, screen } from '@testing-library/react'

import { AuditReportTab } from '../AuditReportTab'

function mockFetch(body: unknown) {
  return vi.fn(async () => ({ ok: true, status: 200, json: async () => body })) as unknown as typeof fetch
}

describe('AuditReportTab', () => {
  afterEach(() => { vi.restoreAllMocks() })

  it('rendert Sektionen + Download-Buttons', async () => {
    const data = {
      report: {
        sections: [
          { title: 'Einleitung', level: 2, body: 'Dieser Bericht fasst die Analyse zusammen.' },
          { title: 'Empfohlene Maßnahmen', level: 2, body: '- **[Hoch]** Tracking erst nach Consent laden.' },
        ],
      },
      markdown: '# Bericht\n',
    }
    vi.stubGlobal('fetch', mockFetch(data))
    render(<AuditReportTab snapshotId="abc" />)
    expect(await screen.findByText('Einleitung')).toBeInTheDocument()
    expect(screen.getByText('Empfohlene Maßnahmen')).toBeInTheDocument()
    expect(screen.getByText(/Tracking erst nach Consent/)).toBeInTheDocument()
    expect(screen.getByText('PDF herunterladen')).toBeInTheDocument()
    expect(screen.getByText('Markdown herunterladen')).toBeInTheDocument()
  })
})
