"""TDD spec for Phase 2 — /intel/dead route registration + empty state (issue #10). New route: GET /{owner}/{repo_slug}/intel/dead Query params: confidence (optional) — filter to one tier: high | medium | low show_dismissed (optional, default false) — include dismissed rows Handler queries musehub_intel_dead ordered by: confidence tier (high → medium → low), then address ascending. Template: musehub/templates/musehub/pages/intel_dead.html Layers: 1. Route — "intel/dead" registered in ui_intel router 2. Basic — GET /{owner}/{repo_slug}/intel/dead → 200 3. Not found — unknown repo → 404 4. Empty — zero dead rows → 200 with empty-state text 5. Filter — ?confidence=high filters to high tier only 6. Dismissed — ?show_dismissed=true includes dismissed rows 7. MIME — response Content-Type is text/html """ from __future__ import annotations import secrets import pytest import pytest_asyncio from httpx import AsyncClient from sqlalchemy.dialects.postgresql import insert as pg_insert from sqlalchemy.ext.asyncio import AsyncSession from muse.core.types import fake_id, long_id from musehub.db.musehub_intel_models import MusehubIntelDead from tests.factories import create_repo def _uid() -> str: return fake_id(secrets.token_hex(16)) _OWNER = "testuser" _SLUG = "deadrouterepo" async def _seed_dead( session: AsyncSession, repo_id: str, *, address: str, confidence: str = "high", dismissed: bool = False, ) -> None: stmt = ( pg_insert(MusehubIntelDead) .values( repo_id=repo_id, address=address, kind="function", confidence=confidence, reason="test reason", ref=long_id("a" * 64), dismissed=dismissed, ) .on_conflict_do_nothing() ) await session.execute(stmt) await session.flush() # --------------------------------------------------------------------------- # Fixtures # --------------------------------------------------------------------------- @pytest_asyncio.fixture async def dead_repo(db_session: AsyncSession) -> MusehubRepo: return await create_repo(db_session, owner=_OWNER, slug=_SLUG) # --------------------------------------------------------------------------- # Layer 1 — Route registration # --------------------------------------------------------------------------- class TestDeadRouteRegistration: def test_P2_01_dead_route_registered(self) -> None: from musehub.api.routes.musehub.ui_intel import router paths = [r.path for r in router.routes] assert any("intel/dead" in p for p in paths) # --------------------------------------------------------------------------- # Layer 2 — Basic response # --------------------------------------------------------------------------- class TestDeadRouteBasic: @pytest.mark.asyncio async def test_P2_02_get_dead_returns_200( self, client: AsyncClient, dead_repo: MusehubRepo ) -> None: resp = await client.get(f"/{_OWNER}/{_SLUG}/intel/dead") assert resp.status_code == 200 @pytest.mark.asyncio async def test_P2_03_unknown_repo_returns_404( self, client: AsyncClient ) -> None: resp = await client.get("/nobody/nonexistentrepo123/intel/dead") assert resp.status_code == 404 # --------------------------------------------------------------------------- # Layer 3 — Empty state # --------------------------------------------------------------------------- class TestDeadRouteEmptyState: @pytest.mark.asyncio async def test_P2_04_zero_rows_empty_state_text( self, client: AsyncClient, dead_repo: MusehubRepo ) -> None: resp = await client.get(f"/{_OWNER}/{_SLUG}/intel/dead") assert resp.status_code == 200 html = resp.text.lower() assert "no dead code" in html or "clean" in html or "no candidates" in html # --------------------------------------------------------------------------- # Layer 4 — Confidence filter # --------------------------------------------------------------------------- class TestDeadRouteFilter: @pytest.mark.asyncio async def test_P2_05_confidence_high_filters_to_high_only( self, client: AsyncClient, db_session: AsyncSession, dead_repo: MusehubRepo ) -> None: await db_session.commit() await _seed_dead(db_session, dead_repo.repo_id, address="pkg/a.py::high_fn", confidence="high") await _seed_dead(db_session, dead_repo.repo_id, address="pkg/b.py::low_fn", confidence="low") await db_session.commit() resp = await client.get(f"/{_OWNER}/{_SLUG}/intel/dead?confidence=high") assert resp.status_code == 200 assert "high_fn" in resp.text assert "low_fn" not in resp.text # --------------------------------------------------------------------------- # Layer 5 — Dismissed visibility # --------------------------------------------------------------------------- class TestDeadRouteDismissed: @pytest.mark.asyncio async def test_P2_06_show_dismissed_includes_dismissed_rows( self, client: AsyncClient, db_session: AsyncSession, dead_repo: MusehubRepo ) -> None: await db_session.commit() await _seed_dead(db_session, dead_repo.repo_id, address="pkg/c.py::dismissed_fn", dismissed=True) await db_session.commit() resp_default = await client.get(f"/{_OWNER}/{_SLUG}/intel/dead") assert "dismissed_fn" not in resp_default.text resp_show = await client.get(f"/{_OWNER}/{_SLUG}/intel/dead?show_dismissed=true") assert "dismissed_fn" in resp_show.text # --------------------------------------------------------------------------- # Layer 6 — MIME type # --------------------------------------------------------------------------- class TestDeadRouteMime: @pytest.mark.asyncio async def test_P2_07_content_type_is_html( self, client: AsyncClient, dead_repo: MusehubRepo ) -> None: resp = await client.get(f"/{_OWNER}/{_SLUG}/intel/dead") assert "text/html" in resp.headers.get("content-type", "")