"""
Test FastMCP tool registration.

This module verifies that the FastMCP server is properly configured
and that the search_web tool is correctly registered.
"""

import inspect
import pytest
import os
import sys

# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


def test_fastmcp_instance_exists():
    """Verify that the mcp instance exists in server.py."""
    try:
        from server import mcp
        assert mcp is not None, "mcp instance should not be None"
    except ImportError:
        pytest.skip("server.py not yet created")
    except AttributeError:
        pytest.skip("mcp instance not yet defined in server.py")


def test_search_web_function_exists():
    """Verify that the search_web function exists in server.py."""
    try:
        from server import search_web
        assert search_web is not None, "search_web function should not be None"
        assert callable(search_web), "search_web should be callable"
    except ImportError:
        pytest.skip("server.py not yet created")
    except AttributeError:
        pytest.skip("search_web function not yet defined in server.py")


def test_tool_decorator_applied():
    """Verify that @mcp.tool decorator is applied to search_web."""
    try:
        from server import mcp, search_web

        # Check if the function is registered as a tool
        # FastMCP stores tools in mcp._tools
        if hasattr(mcp, '_tools'):
            tool_names = list(mcp._tools.keys())
            assert 'search_web' in tool_names, "search_web should be registered as a tool"
        else:
            pytest.skip("mcp instance does not have _tools attribute yet")

    except ImportError:
        pytest.skip("server.py not yet created")
    except (AttributeError, ImportError) as e:
        pytest.skip(f"mcp or search_web not yet defined: {e}")


def test_server_can_be_imported():
    """Verify that the server module can be imported."""
    try:
        import server
        assert server is not None
    except ImportError:
        pytest.skip("server.py not yet created")
    except Exception as e:
        pytest.skip(f"server.py cannot be imported: {e}")
