"""Tests for blob deep-link infrastructure: symbol→lineno enrichment and #S: fragments. Covers: _enrich_with_linenos (unit) - test_enrich_adds_lineno_for_known_function - test_enrich_adds_lineno_for_class - test_enrich_skips_non_python_files - test_enrich_does_not_overwrite_existing_lineno - test_enrich_tolerates_syntax_error - test_enrich_ignores_symbol_not_in_ast _symbol_line_map (unit) - test_symbol_line_map_returns_display_name_to_lineno - test_symbol_line_map_excludes_symbols_without_lineno - test_symbol_line_map_excludes_falsy_lineno Issue detail SSR — #S: deep links - test_issue_detail_symbol_anchor_link_contains_hash_fragment - test_issue_detail_symbol_anchor_plain_file_no_fragment - test_issue_detail_multiple_symbol_anchors_all_linked Blob page SSR — symbolLines in page_json - test_blob_page_json_contains_symbol_lines_key """ from __future__ import annotations import pytest from httpx import AsyncClient from sqlalchemy.ext.asyncio import AsyncSession from datetime import datetime, timezone from musehub.api.routes.musehub.ui_blob import _enrich_with_linenos, _symbol_line_map from muse.core.types import now_utc_iso from musehub.core.genesis import compute_identity_id, compute_issue_id, compute_repo_id from musehub.db.musehub_models import MusehubIssue, MusehubRepo from musehub.types.json_types import JSONObject, StrDict # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- _PY_SRC = """\ class MyService: def run(self) -> None: pass async def compute(x: int) -> int: return x * 2 """ async def _make_repo(db: AsyncSession, owner: str = "blober", slug: str = "blobby") -> str: owner_id = compute_identity_id(owner.encode()) created_at = datetime.now(tz=timezone.utc) repo = MusehubRepo( repo_id=compute_repo_id(owner_id, slug, "code", created_at.isoformat()), name=slug, owner=owner, slug=slug, visibility="public", owner_user_id=owner_id, created_at=created_at, updated_at=created_at, ) db.add(repo) await db.commit() await db.refresh(repo) return str(repo.repo_id) async def _make_issue( db: AsyncSession, repo_id: str, *, number: int = 1, title: str = "Deep link test issue", symbol_anchors: list[str] | None = None, ) -> MusehubIssue: author_id = compute_identity_id(b"blober") issue = MusehubIssue( issue_id=compute_issue_id(repo_id, author_id, now_utc_iso()), repo_id=repo_id, number=number, title=title, body="", state="open", labels=[], author="blober", symbol_anchors=symbol_anchors or [], ) db.add(issue) await db.commit() await db.refresh(issue) return issue # --------------------------------------------------------------------------- # Unit: _enrich_with_linenos # --------------------------------------------------------------------------- def test_enrich_adds_lineno_for_known_function() -> None: syms: list[JSONObject] = [{"display_name": "compute"}] _enrich_with_linenos(syms, "service.py", _PY_SRC) assert syms[0]["lineno"] == 5 def test_enrich_adds_lineno_for_class() -> None: syms: list[JSONObject] = [{"display_name": "MyService"}] _enrich_with_linenos(syms, "service.py", _PY_SRC) assert syms[0]["lineno"] == 1 def test_enrich_skips_non_python_files() -> None: syms: list[JSONObject] = [{"display_name": "compute"}] _enrich_with_linenos(syms, "service.ts", _PY_SRC) assert "lineno" not in syms[0] def test_enrich_does_not_overwrite_existing_lineno() -> None: syms: list[JSONObject] = [{"display_name": "compute", "lineno": 99}] _enrich_with_linenos(syms, "service.py", _PY_SRC) assert syms[0]["lineno"] == 99 def test_enrich_tolerates_syntax_error() -> None: syms: list[JSONObject] = [{"display_name": "compute"}] _enrich_with_linenos(syms, "bad.py", "def compute(: pass") assert "lineno" not in syms[0] def test_enrich_ignores_symbol_not_in_ast() -> None: syms: list[JSONObject] = [{"display_name": "ghost_fn"}] _enrich_with_linenos(syms, "service.py", _PY_SRC) assert "lineno" not in syms[0] # --------------------------------------------------------------------------- # Unit: _symbol_line_map # --------------------------------------------------------------------------- def test_symbol_line_map_returns_display_name_to_lineno() -> None: syms: list[JSONObject] = [ {"display_name": "foo", "lineno": 3, "end_lineno": 8}, {"display_name": "bar", "lineno": 10, "end_lineno": 20}, ] result = _symbol_line_map(syms) assert result == {"foo": [3, 8], "bar": [10, 20]} def test_symbol_line_map_excludes_symbols_without_lineno() -> None: syms: list[JSONObject] = [ {"display_name": "foo", "lineno": 3, "end_lineno": 5}, {"display_name": "no_line"}, ] result = _symbol_line_map(syms) assert "no_line" not in result assert result["foo"] == [3, 5] def test_symbol_line_map_excludes_falsy_lineno() -> None: syms: list[JSONObject] = [{"display_name": "zero", "lineno": 0}] result = _symbol_line_map(syms) assert "zero" not in result # --------------------------------------------------------------------------- # Issue detail SSR — #S: deep links # --------------------------------------------------------------------------- async def test_issue_detail_symbol_anchor_link_contains_hash_fragment( client: AsyncClient, db_session: AsyncSession, auth_headers: StrDict ) -> None: repo_id = await _make_repo(db_session) await _make_issue(db_session, repo_id, symbol_anchors=["musehub/services/foo.py::compute"]) r = await client.get("/blober/blobby/issues/1") assert r.status_code == 200 assert "#S:compute" in r.text async def test_issue_detail_symbol_anchor_plain_file_no_fragment( client: AsyncClient, db_session: AsyncSession, auth_headers: StrDict ) -> None: repo_id = await _make_repo(db_session, slug="blobby2") await _make_issue(db_session, repo_id, number=1, symbol_anchors=["musehub/services/bar.py"]) r = await client.get("/blober/blobby2/issues/1") assert r.status_code == 200 # plain file anchor — no #S: fragment, just the blob URL assert "blob/main/musehub/services/bar.py" in r.text assert "#S:" not in r.text async def test_issue_detail_multiple_symbol_anchors_all_linked( client: AsyncClient, db_session: AsyncSession, auth_headers: StrDict ) -> None: repo_id = await _make_repo(db_session, slug="blobby3") await _make_issue( db_session, repo_id, symbol_anchors=[ "musehub/services/a.py::Alpha", "musehub/services/b.py::Beta", ], ) r = await client.get("/blober/blobby3/issues/1") assert r.status_code == 200 assert "#S:Alpha" in r.text assert "#S:Beta" in r.text # --------------------------------------------------------------------------- # Blob page SSR — symbolLines key present in page_json # --------------------------------------------------------------------------- async def test_blob_page_json_contains_symbol_lines_key( client: AsyncClient, db_session: AsyncSession ) -> None: """Even when no file is found the blob template must emit symbolLines in page_json.""" await _make_repo(db_session, owner="blober", slug="blobby4") r = await client.get("/blober/blobby4/blob/main/some/file.py") # May 200 or 404 depending on whether the file exists, but the template always renders. # We only care that the response contains "symbolLines" in the page_json script block. assert r.status_code in (200, 404) assert '"symbolLines"' in r.text