import pytest
from unittest.mock import AsyncMock, patch
from cve_checker.vulnerability_checker import VulnerabilityChecker
from cve_checker.cve_data_fetcher import CVEDataFetcher

@pytest.fixture
def mock_cve_data():
    return [
        {
            'cve': {
                'CVE_data_meta': {'ID': 'CVE-2021-1234'},
                'description': {'description_data': [{'value': 'Test vulnerability'}]},
                'affects': {
                    'vendor': {
                        'vendor_data': [
                            {
                                'product': {
                                    'product_data': [
                                        {
                                            'product_name': 'test_software',
                                            'version': {
                                                'version_data': [
                                                    {'version_value': '1.0.0'}
                                                ]
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        }
    ]

@pytest.fixture
def vulnerability_checker():
    mock_fetcher = AsyncMock(spec=CVEDataFetcher)
    return VulnerabilityChecker(mock_fetcher)

@pytest.mark.asyncio
async def test_check_vulnerabilities(vulnerability_checker, mock_cve_data):
    vulnerability_checker.cve_data_fetcher.fetch_cve_data.return_value = mock_cve_data

    vulnerabilities = await vulnerability_checker.check_vulnerabilities('test_software', '1.0.0')

    assert len(vulnerabilities) == 1
    assert vulnerabilities[0]['cve_id'] == 'CVE-2021-1234'
    assert vulnerabilities[0]['description'] == 'Test vulnerability'
    assert vulnerabilities[0]['affected_version']['version_value'] == '1.0.0'

@pytest.mark.asyncio
async def test_check_vulnerabilities_no_match(vulnerability_checker, mock_cve_data):
    vulnerability_checker.cve_data_fetcher.fetch_cve_data.return_value = mock_cve_data

    vulnerabilities = await vulnerability_checker.check_vulnerabilities('other_software', '1.0.0')

    assert len(vulnerabilities) == 0

@pytest.mark.asyncio
async def test_check_vulnerabilities_version_mismatch(vulnerability_checker, mock_cve_data):
    vulnerability_checker.cve_data_fetcher.fetch_cve_data.return_value = mock_cve_data

    vulnerabilities = await vulnerability_checker.check_vulnerabilities('test_software', '2.0.0')

    assert len(vulnerabilities) == 0
