import pytest
from cve_checker.risk_scorer import RiskScorer

@pytest.fixture
def risk_scorer():
    return RiskScorer()

@pytest.fixture
def sample_cve_data():
    return {
        'impact': {
            'baseMetricV3': {
                'cvssV3': {
                    'baseScore': 7.5,
                    'exploitabilityScore': 2.8
                }
            }
        }
    }

def test_calculate_risk_score(risk_scorer, sample_cve_data):
    risk_score = risk_scorer.calculate_risk_score(sample_cve_data)
    expected_score = round((7.5 * 0.6) + (2.8 * 0.4), 2)
    assert risk_score == expected_score

def test_prioritize_vulnerabilities(risk_scorer):
    vulnerabilities = [
        {
            'cve': {'CVE_data_meta': {'ID': 'CVE-2021-1234'}},
            'impact': {'baseMetricV3': {'cvssV3': {'baseScore': 7.5, 'exploitabilityScore': 2.8}}}
        },
        {
            'cve': {'CVE_data_meta': {'ID': 'CVE-2021-5678'}},
            'impact': {'baseMetricV3': {'cvssV3': {'baseScore': 9.1, 'exploitabilityScore': 3.5}}}
        }
    ]
    
    prioritized = risk_scorer.prioritize_vulnerabilities(vulnerabilities)
    assert len(prioritized) == 2
    assert prioritized[0]['cve']['CVE_data_meta']['ID'] == 'CVE-2021-5678'
    assert prioritized[1]['cve']['CVE_data_meta']['ID'] == 'CVE-2021-1234'
    assert prioritized[0]['risk_score'] > prioritized[1]['risk_score']

def test_missing_cvss_data(risk_scorer):
    cve_data = {'impact': {}}
    risk_score = risk_scorer.calculate_risk_score(cve_data)
    assert risk_score == 0
