"""{{ display_name }} MCP server — generated by mcpify. Do not hand-edit."""

from __future__ import annotations

import pytest

from {{ module_name }}.core.config import load_config
from {{ module_name }}.core.errors import ConfigurationError

_AUTH_METHOD_KEY = "{% if auth_methods | length > 0 %}{{ auth_methods.0.key }}{% else %}none{% endif %}"


def test_applies_built_in_defaults_when_nothing_else_is_set(monkeypatch) -> None:
    monkeypatch.delenv("{{ tool_prefix_env }}_LOG_LEVEL", raising=False)
    config = load_config({"url": "https://api.example.com", "auth_method": _AUTH_METHOD_KEY})
    assert config.log_level == "info"
    assert config.rate_limit == 100
    assert config.port == 3000


def test_cli_flags_win_over_everything_else() -> None:
    config = load_config(
        {
            "url": "https://api.example.com",
            "auth_method": _AUTH_METHOD_KEY,
            "log_level": "debug",
        }
    )
    assert config.log_level == "debug"


def test_env_vars_are_read_when_no_cli_flag_overrides_them(monkeypatch) -> None:
    monkeypatch.setenv("{{ tool_prefix_env }}_URL", "https://from-env.example.com")
    monkeypatch.setenv("{{ tool_prefix_env }}_AUTH_METHOD", _AUTH_METHOD_KEY)
    monkeypatch.setenv("{{ tool_prefix_env }}_LOG_LEVEL", "warning")

    config = load_config()

    assert config.url == "https://from-env.example.com"
    assert config.log_level == "warning"


def test_missing_required_fields_report_a_configuration_error() -> None:
    with pytest.raises(ConfigurationError):
        load_config({})
