sync update from LAN repo (source dhk-1 @ 3b1ec5b4)
This commit is contained in:
1 parent
da6521b6cb
commit
264e2200d6
995 files changed
+148105
-13399
No files matched your search
@@ -2,7 +2,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from collections.abc import Iterator
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
@@ -14,6 +17,40 @@ from app.gateway.auth.token_source import get_explicit_or_session_token
|
||||
from app.gateway.auth_middleware import PUBLIC_HEALTH_PATHS, AuthMiddleware
|
||||
|
||||
|
||||
def _effective_gateway_routes() -> Iterator[Any]:
|
||||
"""Expand FastAPI's lazy included routers into their effective routes."""
|
||||
for route in gateway_app.router.routes:
|
||||
route_contexts = getattr(route, "effective_route_contexts", None)
|
||||
if callable(route_contexts):
|
||||
yield from route_contexts()
|
||||
else:
|
||||
yield route
|
||||
|
||||
|
||||
def _endpoint_name(route: Any) -> str:
|
||||
endpoint = getattr(route, "endpoint", None)
|
||||
module = getattr(endpoint, "__module__", "<unknown>")
|
||||
name = getattr(endpoint, "__name__", repr(endpoint))
|
||||
return f"{module}.{name}"
|
||||
|
||||
|
||||
def _exact_routes(method: str, path: str) -> list[Any]:
|
||||
return [
|
||||
route
|
||||
for route in _effective_gateway_routes()
|
||||
if route.path == path and method in (getattr(route, "methods", None) or set())
|
||||
]
|
||||
|
||||
|
||||
def _first_matching_route(method: str, path: str) -> Any:
|
||||
for route in _effective_gateway_routes():
|
||||
methods = getattr(route, "methods", None) or set()
|
||||
path_regex = getattr(route, "path_regex", None)
|
||||
if method in methods and path_regex is not None and path_regex.fullmatch(path):
|
||||
return route
|
||||
raise AssertionError(f"No route matched {method} {path}")
|
||||
|
||||
|
||||
def _health_only_app() -> FastAPI:
|
||||
app = FastAPI()
|
||||
|
||||
@@ -89,6 +126,45 @@ def test_gateway_registers_every_public_health_path_for_get_and_head() -> None:
|
||||
assert {"GET", "HEAD"} <= methods_by_path[path]
|
||||
|
||||
|
||||
def test_gateway_method_path_pairs_are_unique() -> None:
|
||||
endpoints_by_method_path: dict[tuple[str, str], list[str]] = defaultdict(list)
|
||||
for route in _effective_gateway_routes():
|
||||
path = getattr(route, "path", None)
|
||||
if path is None:
|
||||
continue
|
||||
for method in getattr(route, "methods", None) or set():
|
||||
endpoints_by_method_path[(method, path)].append(_endpoint_name(route))
|
||||
|
||||
duplicates = {
|
||||
method_path: endpoints
|
||||
for method_path, endpoints in endpoints_by_method_path.items()
|
||||
if len(endpoints) > 1
|
||||
}
|
||||
|
||||
assert duplicates == {}
|
||||
|
||||
|
||||
def test_checkpoint_and_event_store_message_contracts_are_distinct() -> None:
|
||||
checkpoint_path = "/api/threads/{thread_id}/messages"
|
||||
event_store_path = "/api/threads/{thread_id}/run-messages"
|
||||
|
||||
checkpoint_routes = _exact_routes("GET", checkpoint_path)
|
||||
event_store_routes = _exact_routes("GET", event_store_path)
|
||||
|
||||
assert [_endpoint_name(route) for route in checkpoint_routes] == [
|
||||
"app.gateway.routers.threads.get_thread_messages"
|
||||
]
|
||||
assert [_endpoint_name(route) for route in event_store_routes] == [
|
||||
"app.gateway.routers.thread_runs.list_thread_run_messages"
|
||||
]
|
||||
assert _endpoint_name(
|
||||
_first_matching_route("GET", "/api/threads/thread-one/messages")
|
||||
) == "app.gateway.routers.threads.get_thread_messages"
|
||||
assert _endpoint_name(
|
||||
_first_matching_route("GET", "/api/threads/thread-one/run-messages")
|
||||
) == "app.gateway.routers.thread_runs.list_thread_run_messages"
|
||||
|
||||
|
||||
def test_non_health_route_still_requires_auth(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(auth_middleware, "is_auth_disabled", lambda: False)
|
||||
app = _health_only_app()
|
||||
|
||||
Reference in new issue
Block a user