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

import { CookieFindingList } from '../CookieLibraryPanel'

describe('CookieFindingList', () => {
  it('zeigt Befunde gruppiert nach Typ mit Severity + Library-Count', () => {
    const data = {
      summary: { checked: 10, in_library: 4, findings: 1 },
      findings: [{
        vendor: 'Salesforce', cookie: '_ga', type: 'tracker_as_necessary',
        severity: 'HIGH', declared: 'necessary',
        library_purpose: 'Besucher eindeutig unterscheiden',
        remediation: 'Als einwilligungspflichtig (§ 25 TDDDG) einstufen.', kind: 'finding',
      }],
    }
    render(<CookieFindingList data={data} />)
    expect(screen.getByText(/1 Befund/)).toBeInTheDocument()
    expect(screen.getByText('HIGH')).toBeInTheDocument()
    expect(screen.getByText(/Tracker als/)).toBeInTheDocument()       // Gruppen-Header
    expect(screen.getByText(/4\/10 Cookies/)).toBeInTheDocument()
  })

  it('zeigt grünen Hinweis bei 0 Befunden', () => {
    render(<CookieFindingList data={{ summary: { checked: 5, in_library: 2 }, findings: [] }} />)
    expect(screen.getByText(/Keine Abweichungen/)).toBeInTheDocument()
  })

  it('zeigt den Drift-Strip (Richtlinie vs. Browser-Realität)', () => {
    render(<CookieFindingList data={{
      summary: { checked: 31, in_library: 8, findings: 0 },
      drift: { declared_count: 0, browser_count: 31, high_findings: 31, low_findings: 0 },
      findings: [],
    }} />)
    expect(screen.getByText(/Richtlinie ↔ Realität/)).toBeInTheDocument()
    expect(screen.getByText(/31 undokumentiert geladen/)).toBeInTheDocument()
  })

  it('zeigt das Storage-Inventar (echte Cookies vs. andere)', () => {
    render(<CookieFindingList data={{
      summary: { checked: 100, in_library: 30, findings: 0 },
      storage_inventory: { total: 100, real_cookies: 60, other_storage: 40,
        by_type: { cookie: 60, framework_storage: 40 } },
      findings: [],
    }} />)
    expect(screen.getByText(/Storage-Inventar/)).toBeInTheDocument()
    expect(screen.getByText(/60 echte Cookies/)).toBeInTheDocument()
    expect(screen.getByText(/40 andere Endgeräte-Speicher/)).toBeInTheDocument()
  })
})
