"""Tests for configuration module."""

import os
from unittest.mock import patch


from crustdata_mcp.config import Config, get_token


class TestConfig:
    """Tests for Config class."""

    def test_default_values(self):
        """Test that default configuration values are set correctly."""
        config = Config()
        assert config.api_url == "https://api.crustdata.com"
        assert config.request_timeout == 30.0
        assert config.log_level == "INFO"
        assert config.http_host == "127.0.0.1"
        assert config.http_port == 8000
        assert config.http_path == "/mcp"

    def test_from_env_with_defaults(self):
        """Test Config.from_env() with no environment variables set."""
        with patch.dict(os.environ, {}, clear=True):
            config = Config.from_env()
            assert config.api_url == "https://api.crustdata.com"
            assert config.request_timeout == 30.0
            assert config.log_level == "INFO"

    def test_from_env_with_custom_values(self):
        """Test Config.from_env() with custom environment variables."""
        env_vars = {
            "CRUSTDATA_API_URL": "https://custom.api.com",
            "CRUSTDATA_REQUEST_TIMEOUT": "60.0",
            "CRUSTDATA_LOG_LEVEL": "DEBUG",
        }
        with patch.dict(os.environ, env_vars, clear=True):
            config = Config.from_env()
            assert config.api_url == "https://custom.api.com"
            assert config.request_timeout == 60.0
            assert config.log_level == "DEBUG"

    def test_from_env_partial_override(self):
        """Test Config.from_env() with partial environment variable override."""
        env_vars = {
            "CRUSTDATA_LOG_LEVEL": "WARNING",
        }
        with patch.dict(os.environ, env_vars, clear=True):
            config = Config.from_env()
            assert config.api_url == "https://api.crustdata.com"
            assert config.request_timeout == 30.0
            assert config.log_level == "WARNING"

    def test_http_config_from_env(self):
        """Test HTTP transport configuration from environment variables."""
        env_vars = {
            "CRUSTDATA_HTTP_HOST": "0.0.0.0",
            "CRUSTDATA_HTTP_PORT": "9000",
            "CRUSTDATA_HTTP_PATH": "/api/mcp",
        }
        with patch.dict(os.environ, env_vars, clear=True):
            config = Config.from_env()
            assert config.http_host == "0.0.0.0"
            assert config.http_port == 9000
            assert config.http_path == "/api/mcp"


class TestGetToken:
    """Tests for get_token function."""

    def test_get_token_from_crustdata_token(self):
        """Test get_token returns CRUSTDATA_TOKEN when set."""
        with patch.dict(os.environ, {"CRUSTDATA_TOKEN": "new-token"}, clear=True):
            assert get_token() == "new-token"

    def test_get_token_returns_empty_when_not_set(self):
        """Test get_token returns empty string when no token is set."""
        with patch.dict(os.environ, {}, clear=True):
            assert get_token() == ""
