"""
Behavioral tests for web/routes/route_registry module.

Tests the route registry data structure, route lookups, and filtering functions.
"""


class TestRouteRegistryStructure:
    """Tests for ROUTE_REGISTRY constant."""

    def test_is_dict(self):
        """ROUTE_REGISTRY is a dictionary."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        assert isinstance(ROUTE_REGISTRY, dict)

    def test_is_non_empty(self):
        """ROUTE_REGISTRY is non-empty."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        assert len(ROUTE_REGISTRY) > 0

    def test_contains_research_blueprint(self):
        """Contains research blueprint."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        assert "research" in ROUTE_REGISTRY

    def test_contains_settings_blueprint(self):
        """Contains settings blueprint."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        assert "settings" in ROUTE_REGISTRY

    def test_contains_history_blueprint(self):
        """Contains history blueprint."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        assert "history" in ROUTE_REGISTRY

    def test_contains_metrics_blueprint(self):
        """Contains metrics blueprint."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        assert "metrics" in ROUTE_REGISTRY

    def test_contains_api_v1_blueprint(self):
        """Contains api_v1 blueprint."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        assert "api_v1" in ROUTE_REGISTRY

    def test_each_blueprint_has_routes(self):
        """Each blueprint has a routes list."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        for name, info in ROUTE_REGISTRY.items():
            assert "routes" in info, f"Blueprint {name!r} missing 'routes'"
            assert isinstance(info["routes"], list), (
                f"Blueprint {name!r} routes is not a list"
            )

    def test_each_blueprint_has_url_prefix(self):
        """Each blueprint has a url_prefix key."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        for name, info in ROUTE_REGISTRY.items():
            assert "url_prefix" in info, (
                f"Blueprint {name!r} missing 'url_prefix'"
            )

    def test_route_tuples_have_four_elements(self):
        """Route tuples have (method, path, endpoint, description)."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        for name, info in ROUTE_REGISTRY.items():
            for route in info["routes"]:
                assert len(route) == 4, (
                    f"Route in {name!r} has {len(route)} elements, expected 4"
                )

    def test_route_methods_are_valid_http(self):
        """Route methods are valid HTTP methods."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        valid_methods = {"GET", "POST", "PUT", "DELETE", "PATCH"}
        for name, info in ROUTE_REGISTRY.items():
            for method, path, endpoint, desc in info["routes"]:
                assert method in valid_methods, (
                    f"Invalid HTTP method {method!r} in {name!r}"
                )

    def test_route_paths_start_with_slash(self):
        """Route paths start with /."""
        from local_deep_research.web.routes.route_registry import ROUTE_REGISTRY

        for name, info in ROUTE_REGISTRY.items():
            for method, path, endpoint, desc in info["routes"]:
                assert path.startswith("/"), (
                    f"Path {path!r} in {name!r} doesn't start with /"
                )


class TestGetAllRoutes:
    """Tests for get_all_routes function."""

    def test_returns_list(self):
        """Returns a list."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        result = get_all_routes()
        assert isinstance(result, list)

    def test_returns_non_empty(self):
        """Returns non-empty list."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        result = get_all_routes()
        assert len(result) > 0

    def test_each_route_has_method(self):
        """Each route dict has 'method' key."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        for route in get_all_routes():
            assert "method" in route

    def test_each_route_has_path(self):
        """Each route dict has 'path' key."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        for route in get_all_routes():
            assert "path" in route

    def test_each_route_has_endpoint(self):
        """Each route dict has 'endpoint' key."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        for route in get_all_routes():
            assert "endpoint" in route

    def test_each_route_has_description(self):
        """Each route dict has 'description' key."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        for route in get_all_routes():
            assert "description" in route

    def test_each_route_has_blueprint(self):
        """Each route dict has 'blueprint' key."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        for route in get_all_routes():
            assert "blueprint" in route

    def test_endpoint_includes_blueprint_prefix(self):
        """Endpoint includes blueprint name as prefix."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        for route in get_all_routes():
            assert "." in route["endpoint"], (
                f"Endpoint {route['endpoint']!r} missing blueprint prefix"
            )

    def test_api_v1_routes_have_prefix(self):
        """API v1 routes have /api/v1 prefix in path."""
        from local_deep_research.web.routes.route_registry import get_all_routes

        api_routes = [r for r in get_all_routes() if r["blueprint"] == "api_v1"]
        for route in api_routes:
            assert route["path"].startswith("/api/v1"), (
                f"API route {route['path']!r} missing /api/v1 prefix"
            )


class TestGetRoutesByBlueprint:
    """Tests for get_routes_by_blueprint function."""

    def test_returns_list_for_valid_blueprint(self):
        """Returns list for valid blueprint name."""
        from local_deep_research.web.routes.route_registry import (
            get_routes_by_blueprint,
        )

        result = get_routes_by_blueprint("research")
        assert isinstance(result, list)

    def test_returns_empty_for_invalid_blueprint(self):
        """Returns empty list for invalid blueprint name."""
        from local_deep_research.web.routes.route_registry import (
            get_routes_by_blueprint,
        )

        result = get_routes_by_blueprint("nonexistent_blueprint")
        assert result == []

    def test_research_has_routes(self):
        """Research blueprint has routes."""
        from local_deep_research.web.routes.route_registry import (
            get_routes_by_blueprint,
        )

        result = get_routes_by_blueprint("research")
        assert len(result) > 0

    def test_settings_has_routes(self):
        """Settings blueprint has routes."""
        from local_deep_research.web.routes.route_registry import (
            get_routes_by_blueprint,
        )

        result = get_routes_by_blueprint("settings")
        assert len(result) > 0

    def test_routes_have_method_key(self):
        """Returned routes have 'method' key."""
        from local_deep_research.web.routes.route_registry import (
            get_routes_by_blueprint,
        )

        for route in get_routes_by_blueprint("research"):
            assert "method" in route

    def test_routes_have_path_key(self):
        """Returned routes have 'path' key."""
        from local_deep_research.web.routes.route_registry import (
            get_routes_by_blueprint,
        )

        for route in get_routes_by_blueprint("research"):
            assert "path" in route


class TestFindRoute:
    """Tests for find_route function."""

    def test_finds_api_routes(self):
        """Finds routes matching /api pattern."""
        from local_deep_research.web.routes.route_registry import find_route

        result = find_route("/api")
        assert len(result) > 0

    def test_returns_empty_for_no_match(self):
        """Returns empty list when nothing matches."""
        from local_deep_research.web.routes.route_registry import find_route

        result = find_route("/xyz_nonexistent_route_abc")
        assert result == []

    def test_case_insensitive_matching(self):
        """Matching is case-insensitive."""
        from local_deep_research.web.routes.route_registry import find_route

        lower_result = find_route("/api")
        upper_result = find_route("/API")
        assert len(lower_result) == len(upper_result)

    def test_finds_settings_routes(self):
        """Finds settings-related routes."""
        from local_deep_research.web.routes.route_registry import find_route

        result = find_route("settings")
        assert len(result) > 0

    def test_finds_history_routes(self):
        """Finds history-related routes."""
        from local_deep_research.web.routes.route_registry import find_route

        result = find_route("history")
        assert len(result) > 0

    def test_result_items_are_dicts(self):
        """Result items are dictionaries."""
        from local_deep_research.web.routes.route_registry import find_route

        result = find_route("/api")
        for item in result:
            assert isinstance(item, dict)
