from packageurl import PackageURL

from .vulnerability import Vulnerability


def test_create_vulnerability():
    """Test creating a Vulnerability instance."""
    purl = PackageURL.from_string("pkg:pypi/example-package@1.0.0")
    vuln = Vulnerability(
        id="123",
        purl=purl,
        description="Malicious package detected",
        reference="https://malware-db.com/example",
        type="Malware"
    )

    assert vuln.id == "123"
    assert vuln.purl == purl
    assert vuln.description == "Malicious package detected"
    assert vuln.reference == "https://malware-db.com/example"
    assert vuln.type == "Malware"


def test_vulnerability_to_dict():
    """Test serializing a Vulnerability instance to a dictionary."""
    purl = PackageURL.from_string("pkg:pypi/example-package@1.0.0")
    vuln = Vulnerability(
        id="123",
        purl=purl,
        description="Malicious package detected",
        reference="https://malware-db.com/example",
        type="Malware"
    )

    vuln_dict = vuln.to_dict()

    assert isinstance(vuln_dict, dict)
    assert vuln_dict["purl"] == str(purl)
    assert vuln_dict["description"] == "Malicious package detected"
    assert vuln_dict["reference"] == "https://malware-db.com/example"
    assert vuln_dict["type"] == "Malware"


def test_vulnerability_from_dict():
    """Test deserializing a Vulnerability instance from a dictionary."""
    vuln_data = {
        "id": "123",
        "purl": "pkg:pypi/example-package@1.0.0",
        "description": "Malicious package detected",
        "reference": "https://malware-db.com/example",
        "type": "Malware"
    }

    vuln = Vulnerability.from_dict(vuln_data)

    assert isinstance(vuln, Vulnerability)
    assert vuln.id == "123"
    assert vuln.purl == "pkg:pypi/example-package@1.0.0"
    assert vuln.description == "Malicious package detected"
    assert vuln.reference == "https://malware-db.com/example"
    assert vuln.type == "Malware"


def test_vulnerability_default_type():
    """Test that the default type is Malware when not specified."""
    purl = PackageURL.from_string("pkg:pypi/example-package@1.0.0")
    vuln = Vulnerability(
        id="123",
        purl=purl,
        description="No type provided",
        reference="https://malware-db.com/example"
    )

    assert vuln.type == "Malware"
