import pytest
import json
from cve_checker.report_generator import ReportGenerator

@pytest.fixture
def report_generator():
    return ReportGenerator()

@pytest.fixture
def sample_vulnerabilities():
    return [
        {
            'cve': {
                'CVE_data_meta': {'ID': 'CVE-2021-1234'},
                'description': {'description_data': [{'value': 'Test vulnerability'}]}
            },
            'impact': {'baseMetricV3': {'cvssV3': {'baseScore': 7.5}}},
            'configurations': {
                'nodes': [
                    {'cpe_match': [{'vulnerable': True, 'cpe23Uri': 'cpe:2.3:a:vendor:product:version:*:*:*:*:*:*:*'}]}
                ]
            },
            'risk_score': 8.2
        }
    ]

def test_generate_json_report(report_generator, sample_vulnerabilities):
    report = report_generator.generate_report(sample_vulnerabilities, 'json')
    report_data = json.loads(report)

    assert 'generated_at' in report_data
    assert report_data['total_vulnerabilities'] == 1
    assert len(report_data['vulnerabilities']) == 1
    
    vuln = report_data['vulnerabilities'][0]
    assert vuln['cve_id'] == 'CVE-2021-1234'
    assert vuln['description'] == 'Test vulnerability'
    assert vuln['cvss_score'] == 7.5
    assert vuln['risk_score'] == 8.2
    assert vuln['affected_packages'] == ['product']

def test_generate_html_report(report_generator, sample_vulnerabilities):
    report = report_generator.generate_report(sample_vulnerabilities, 'html')

    assert '<html>' in report
    assert '<table>' in report
    assert 'CVE-2021-1234' in report
    assert 'Test vulnerability' in report
    assert '7.5' in report
    assert '8.2' in report
    assert 'product' in report

def test_unsupported_format(report_generator, sample_vulnerabilities):
    with pytest.raises(ValueError):
        report_generator.generate_report(sample_vulnerabilities, 'xml')
