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

Exercises `call_operation`'s full validate -> execute -> validate
pipeline against a real (mocked-transport) HTTP dispatch, unlike
`tests/unit/tools/test_call_tool.py`'s `ApiClient.execute` monkeypatch —
this test only fakes the network boundary (`httpx.MockTransport`), not
`ApiClient` itself.
"""

from __future__ import annotations

import httpx
import pytest

import {{ module_name }}.services.api_client as api_client_module
from {{ module_name }}.auth.auth_manager import AuthManager
from {{ module_name }}.core.config import AuthMethod, Config
from {{ module_name }}.data.store import EndpointRecord
from {{ module_name }}.tools.call_tool import call_operation


@pytest.fixture
def mock_transport(monkeypatch: pytest.MonkeyPatch):
    def handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(200, json={"echo": str(request.url)})

    transport = httpx.MockTransport(handler)
    original_client = httpx.AsyncClient

    def fake_async_client(*args, **kwargs):
        kwargs["transport"] = transport
        return original_client(*args, **kwargs)

    monkeypatch.setattr(api_client_module.httpx, "AsyncClient", fake_async_client)
    return transport


async def test_call_operation_executes_a_real_http_dispatch_against_a_mocked_transport(
    mock_transport,
) -> None:
    config = Config(
        url="https://api.example.com",
        auth_method=AuthMethod.{% if auth_methods | length > 0 %}{{ auth_methods.0.class_name | upper }}{% else %}NONE{% endif %},
    )
    auth_manager = AuthManager(config.auth_method)
    # Seeded directly (bypassing the OS keychain) with whatever field(s)
    # the first-discovered method's own `validate_credentials` checks for
    # — each strategy checks a different key (see auth/strategies/*.py).
{%- if auth_methods | length == 0 %}
    auth_manager.set_credentials({})
{%- elif auth_methods.0.key == "oauth1" %}
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.primitives.asymmetric import rsa

    private_key_pem = (
        rsa.generate_private_key(public_exponent=65537, key_size=2048)
        .private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.PKCS8,
            encryption_algorithm=serialization.NoEncryption(),
        )
        .decode()
    )
    auth_manager.set_credentials({"consumer_key": "k", "private_key": private_key_pem})
{%- elif auth_methods.0.key == "apiKey" %}
    auth_manager.set_credentials({"api_key": "abc"})
{%- elif auth_methods.0.key == "pat" %}
    auth_manager.set_credentials({"token": "abc"})
{%- elif auth_methods.0.key == "oauth2" %}
    auth_manager.set_credentials({"access_token": "abc"})
{%- else %}
    auth_manager.set_credentials({"authorization_header": "Basic abc"})
{%- endif %}
    endpoint = EndpointRecord(
        operation_id="__unknown_operation__",
        path="/widgets",
        method="GET",
        summary=None,
        description=None,
        input_schema={},
        output_schema={},
        auth_scheme_ref=None,
    )

    result = await call_operation(endpoint, config, auth_manager, "__unknown_operation__", {})

    assert result["echo"] == "https://api.example.com/widgets"
