"""Phase 4 TDD: Mist JSON API router explicit registration in main.py. Tests are written RED first. Run before touching main.py and musehub/api/routes/musehub/__init__.py to confirm failure, then implement. The mists router must be: 1. Listed in _DIRECT_REGISTERED in __init__.py (excluded from auto-discovery). 2. Explicitly imported and included in main.py alongside api_identities_router, api_orgs_router, etc. 3. Reachable via the OpenAPI schema at /api/mists paths. These tests do not require DB state — they operate against the live app instance and its route table. """ from __future__ import annotations import pytest from httpx import AsyncClient class TestMistsRouterDirectRegistration: def test_mists_excluded_from_auto_discovery(self) -> None: """'mists' must appear in _DIRECT_REGISTERED so the package __init__ does not auto-include it.""" from musehub.api.routes.musehub import _DIRECT_REGISTERED assert "mists" in _DIRECT_REGISTERED, ( "'mists' must be in _DIRECT_REGISTERED to prevent double-registration" ) def test_mists_router_imported_in_main(self) -> None: """main.py must import the mists router explicitly.""" import musehub.main as _main src = open(_main.__file__).read() assert "mists" in src, ( "main.py must explicitly import or reference the mists router" ) def test_mists_router_registered_via_include_router(self) -> None: """main.py must call app.include_router with the mists router.""" import musehub.main as _main src = open(_main.__file__).read() # The module import line + the include_router call must both be present. assert "api_mists_router" in src or "mists_router" in src or ( "from musehub.api.routes.musehub.mists import router" in src ), "main.py must explicitly include the mists router by name" class TestMistsRouterOpenAPI: @pytest.mark.asyncio async def test_mists_paths_in_openapi_schema(self, client: AsyncClient) -> None: """GET /api/openapi.json must list /api/mists paths.""" r = await client.get("/api/openapi.json") assert r.status_code == 200 schema = r.json() paths = schema.get("paths", {}) mist_paths = [p for p in paths if "/mists" in p] assert len(mist_paths) > 0, ( f"No /mists paths found in OpenAPI schema. " f"Registered paths sample: {list(paths.keys())[:20]}" ) @pytest.mark.asyncio async def test_create_mist_operation_in_schema(self, client: AsyncClient) -> None: r = await client.get("/api/openapi.json") schema = r.json() paths = schema.get("paths", {}) assert "/api/mists" in paths or any( p.endswith("/mists") for p in paths ), f"POST /api/mists not found in schema paths: {[p for p in paths if 'mist' in p]}" @pytest.mark.asyncio async def test_explore_mists_operation_in_schema(self, client: AsyncClient) -> None: r = await client.get("/api/openapi.json") schema = r.json() paths = schema.get("paths", {}) assert any("mists/explore" in p for p in paths), ( f"GET /api/mists/explore not found in schema" ) class TestMistsRouterEndpoints: @pytest.mark.asyncio async def test_explore_endpoint_returns_200(self, client: AsyncClient) -> None: """GET /api/mists/explore must return 200, not 404.""" r = await client.get("/api/mists/explore") assert r.status_code == 200, ( f"Expected 200 from /api/mists/explore; got {r.status_code} — " "router may not be registered" ) @pytest.mark.asyncio async def test_explore_response_is_valid_json(self, client: AsyncClient) -> None: r = await client.get("/api/mists/explore") assert r.status_code == 200 body = r.json() assert "mists" in body @pytest.mark.asyncio async def test_get_nonexistent_mist_returns_404_not_422( self, client: AsyncClient ) -> None: """A valid mist_id that doesn't exist should return 404, not 422 (unprocessable entity from FastAPI if the route is missing or double-registered).""" r = await client.get("/api/mists/Abc123Xyz789") assert r.status_code in (404, 200), ( f"Expected 404 for unknown mist, got {r.status_code} — " "422 indicates a routing problem (possibly double-registration)" ) @pytest.mark.asyncio async def test_no_duplicate_operation_ids(self, client: AsyncClient) -> None: """Double-registration causes duplicate operationIds — FastAPI raises on startup.""" r = await client.get("/api/openapi.json") assert r.status_code == 200, ( "OpenAPI schema returned non-200 — likely duplicate operationId " "from double-registering the mists router" ) schema = r.json() # Collect all operationIds from mist-related paths. op_ids: list[str] = [] for path, methods in schema.get("paths", {}).items(): if "mist" not in path: continue for method_data in methods.values(): if isinstance(method_data, dict) and "operationId" in method_data: op_ids.append(method_data["operationId"]) assert len(op_ids) == len(set(op_ids)), ( f"Duplicate operationIds detected — router registered twice: " f"{[x for x in op_ids if op_ids.count(x) > 1]}" )