"""Unit tests for OpenSourceMalware enrichment."""

import unittest
from unittest.mock import patch, MagicMock
from metapackage import MetaPackage
from registry.opensourcemalware.enrich import enrich_metapackage
from constants import Constants


class TestOpenSourceMalwareEnrichment(unittest.TestCase):
    """Test cases for OpenSourceMalware enrichment."""

    def setUp(self):
        """Set up test fixtures."""
        self.mp = MetaPackage("test-package", "npm")

    @patch('registry.opensourcemalware.enrich.OpenSourceMalwareClient')
    def test_enrich_malicious_package(self, mock_client_class):
        """Test enrichment of a malicious package."""
        # Mock client
        mock_client = MagicMock()
        mock_client_class.return_value = mock_client
        mock_client.base_url = "https://api.opensourcemalware.com/functions/v1"
        mock_client.check_package.return_value = (
            200,
            {},
            {
                "malicious": True,
                "package_name": "test-package",
                "ecosystem": "npm",
                "version": "1.0.0",
                "threat_count": 1,
                "details": {
                    "description": "Cryptocurrency miner",
                    "severity_level": "critical"
                }
            }
        )

        # Enable OSM
        Constants.OSM_ENABLED = True  # type: ignore[attr-defined]

        enrich_metapackage(self.mp, "npm", "test-package", "1.0.0", client=mock_client)

        self.assertTrue(self.mp.osm_checked)
        self.assertTrue(self.mp.osm_malicious)
        self.assertEqual(self.mp.osm_reason, "Cryptocurrency miner")
        self.assertEqual(self.mp.osm_threat_count, 1)
        self.assertEqual(self.mp.osm_severity, "critical")

    @patch('registry.opensourcemalware.enrich.OpenSourceMalwareClient')
    def test_enrich_clean_package(self, mock_client_class):
        """Test enrichment of a clean package."""
        # Mock client
        mock_client = MagicMock()
        mock_client_class.return_value = mock_client
        mock_client.base_url = "https://api.opensourcemalware.com/functions/v1"
        mock_client.check_package.return_value = (
            200,
            {},
            {
                "malicious": False,
                "package_name": "test-package",
                "ecosystem": "npm",
                "version": "1.0.0",
                "message": "Package not found in malicious database"
            }
        )

        # Enable OSM
        Constants.OSM_ENABLED = True  # type: ignore[attr-defined]

        enrich_metapackage(self.mp, "npm", "test-package", "1.0.0", client=mock_client)

        self.assertTrue(self.mp.osm_checked)
        self.assertFalse(self.mp.osm_malicious)
        self.assertIsNone(self.mp.osm_reason)
        self.assertEqual(self.mp.osm_threat_count, 0)

    def test_enrich_disabled(self):
        """Test that enrichment is skipped when OSM is disabled."""
        Constants.OSM_ENABLED = False  # type: ignore[attr-defined]

        enrich_metapackage(self.mp, "npm", "test-package", "1.0.0")

        self.assertIsNone(self.mp.osm_checked)


if __name__ == "__main__":
    unittest.main()
