"""Unit tests for OpenSourceMalware client."""

import unittest
from unittest.mock import patch
from registry.opensourcemalware.client import OpenSourceMalwareClient


class TestOpenSourceMalwareClient(unittest.TestCase):
    """Test cases for OpenSourceMalwareClient."""

    def setUp(self):
        """Set up test fixtures."""
        self.client = OpenSourceMalwareClient(
            api_token="test_token",
            base_url="https://api.opensourcemalware.com/functions/v1"
        )

    def test_eco_value_npm(self):
        """Test ecosystem normalization for npm."""
        self.assertEqual(self.client._eco_value("npm"), "npm")
        self.assertEqual(self.client._eco_value("NPM"), "npm")

    def test_eco_value_pypi(self):
        """Test ecosystem normalization for pypi."""
        self.assertEqual(self.client._eco_value("pypi"), "pypi")
        self.assertEqual(self.client._eco_value("PyPI"), "pypi")
        self.assertEqual(self.client._eco_value("python"), "pypi")

    def test_eco_value_maven(self):
        """Test ecosystem normalization for maven."""
        self.assertEqual(self.client._eco_value("maven"), "maven")
        self.assertEqual(self.client._eco_value("Maven"), "maven")
        self.assertEqual(self.client._eco_value("java"), "maven")

    @patch('registry.opensourcemalware.client.robust_get')
    def test_check_package_malicious(self, mock_get):
        """Test checking a malicious package."""
        import json
        response_data = {
            "malicious": True,
            "package_name": "evil-package",
            "ecosystem": "npm",
            "version": "1.0.0",
            "threat_count": 1,
            "details": {
                "description": "Cryptocurrency miner",
                "severity_level": "critical"
            }
        }
        mock_get.return_value = (
            200,
            {},
            json.dumps(response_data)
        )

        status, headers, data = self.client.check_package("npm", "evil-package", "1.0.0")
        self.assertEqual(status, 200)
        self.assertIsNotNone(data)
        self.assertTrue(data.get("malicious"))

    @patch('registry.opensourcemalware.client.robust_get')
    def test_check_package_clean(self, mock_get):
        """Test checking a clean package."""
        import json
        response_data = {
            "malicious": False,
            "package_name": "clean-package",
            "ecosystem": "npm",
            "version": "1.0.0",
            "message": "Package not found in malicious database"
        }
        mock_get.return_value = (
            200,
            {},
            json.dumps(response_data)
        )

        status, headers, data = self.client.check_package("npm", "clean-package", "1.0.0")
        self.assertEqual(status, 200)
        self.assertIsNotNone(data)
        self.assertFalse(data.get("malicious"))

    def test_auth_method_validation_header(self):
        """Test that valid 'header' auth_method is accepted."""
        client = OpenSourceMalwareClient(
            api_token="test_token",
            auth_method="header"
        )
        self.assertEqual(client.auth_method, "header")

    def test_auth_method_validation_query(self):
        """Test that valid 'query' auth_method is accepted."""
        client = OpenSourceMalwareClient(
            api_token="test_token",
            auth_method="query"
        )
        self.assertEqual(client.auth_method, "query")

    def test_auth_method_validation_case_insensitive(self):
        """Test that auth_method validation is case-insensitive."""
        client = OpenSourceMalwareClient(
            api_token="test_token",
            auth_method="HEADER"
        )
        self.assertEqual(client.auth_method, "header")

    def test_auth_method_validation_invalid_defaults_to_header(self):
        """Test that invalid auth_method defaults to 'header' with warning."""
        import logging
        with self.assertLogs(logger='registry.opensourcemalware.client', level='WARNING') as log:
            client = OpenSourceMalwareClient(
                api_token="test_token",
                auth_method="invalid_method"
            )
            self.assertEqual(client.auth_method, "header")
            self.assertIn("Invalid OpenSourceMalware auth_method", log.output[0])


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