gabriel / musehub public
test_intel_pages_callgraph.py python
81 lines 4.4 KB
Raw
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3 feat(intel): every grammar on the hub, and every intel job … Opus 5.5 minor ⚠ breaking 1 day ago
1 """muse#251 — the Intel pages describe the call-graph data the jobs now store (the CLI's definitions).
2
3 PG_01 blast risk: the CLI's three tiers (high / medium / low); no "critical" tier anywhere; the alert counts "high".
4 PG_02 dead code: two confidence tiers that say what the evidence is (references, imports); no co-change wording.
5 PG_03 gravity: direct callers and transitive dependents, not "changed together".
6 """
7 from __future__ import annotations
8
9 import re
10
11 import pytest
12 from sqlalchemy.ext.asyncio import AsyncSession
13
14 from musehub.db.musehub_intel_models import MusehubIntelBlastRisk, MusehubIntelDead, MusehubSymbolIntel
15 from musehub.services.musehub_intel_alerts import HIGH_BLAST_TIERS
16 from tests.test_intel_alerts_reconciliation import _repo
17 from tests.test_intel_health_status import H, _publish
18
19
20 def _text(html: str) -> str:
21 return re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", html))
22
23
24 @pytest.mark.asyncio
25 async def test_PG_01_blast_risk_has_the_cli_tiers(client, db_session: AsyncSession) -> None:
26 assert HIGH_BLAST_TIERS == ("high",)
27 rid, repo = await _repo(db_session)
28 await _publish(db_session, rid, H)
29 for addr, risk, score in (("a.py::hi", "high", 80), ("a.py::med", "medium", 40), ("a.py::lo", "low", 5)):
30 db_session.add(MusehubIntelBlastRisk(repo_id=rid, address=addr, kind="function", risk=risk, risk_score=score, ref=H))
31 await db_session.commit()
32 page = (await client.get(f"/{repo.owner}/{repo.slug}/intel/blast-risk")).text
33 assert all(a in page for a in ("a.py::hi", "a.py::med", "a.py::lo"))
34 assert "critical" not in _text(page).lower()
35 r = await client.get(f"/{repo.owner}/{repo.slug}/intel/blast-risk/detail", params={"address": "a.py::hi"})
36 assert r.status_code == 200 and "a.py::hi" in r.text
37 assert "co-changed" not in _text(r.text) and "files that reference it" in _text(r.text)
38
39
40 @pytest.mark.asyncio
41 async def test_PG_02_dead_code_tiers_say_what_the_evidence_is(client, db_session: AsyncSession) -> None:
42 rid, repo = await _repo(db_session)
43 await _publish(db_session, rid, H)
44 for addr, conf in (("a.py::orphan", "high"), ("b.py::exported", "medium")):
45 db_session.add(MusehubIntelDead(repo_id=rid, address=addr, kind="function", confidence=conf, reason="r", ref=H))
46 await db_session.commit()
47 page = _text((await client.get(f"/{repo.owner}/{repo.slug}/intel/dead")).text)
48 assert "HIGH CONFIDENCE" in page and "MEDIUM CONFIDENCE" in page and "LOW CONFIDENCE" not in page
49 assert "No file imports its module" in page and "may be exported API" in page
50 assert "blast radius" not in page and "changed together" not in page
51
52
53 @pytest.mark.asyncio
54 async def test_PG_03_gravity_is_callers_not_co_changes(client, db_session: AsyncSession) -> None:
55 rid, repo = await _repo(db_session)
56 await _publish(db_session, rid, H)
57 db_session.add(MusehubSymbolIntel(repo_id=rid, address="a.py::core", symbol_kind="function", op="insert", gravity_pct=50.0,
58 gravity_direct_dependents=1, gravity_transitive_dependents=2, gravity_max_depth=2,
59 gravity_depth_distribution={"1": 1, "2": 1}))
60 await db_session.commit()
61 page = (await client.get(f"/{repo.owner}/{repo.slug}/intel/gravity")).text
62 assert "a.py::core" in page and "changed together" not in page and "call" in _text(page).lower()
63 r = await client.get(f"/{repo.owner}/{repo.slug}/intel/gravity/detail", params={"address": "a.py::core"})
64 assert r.status_code == 200
65 detail = _text(r.text)
66 assert "co-changed" not in detail and "direct callers" in detail.lower()
67
68
69 @pytest.mark.asyncio
70 async def test_PG_04_stable_ranks_by_commits_unchanged(client, db_session: AsyncSession) -> None:
71 """muse code stable's ranking: commits since the last change (``unchanged_for``), not calendar days."""
72 from musehub.db.musehub_intel_models import MusehubIntelStable
73
74 rid, repo = await _repo(db_session)
75 await _publish(db_session, rid, H)
76 db_session.add(MusehubIntelStable(repo_id=rid, address="a.py::old_by_days", days_stable=400, unchanged_for=2, since_start=False, ref=H))
77 db_session.add(MusehubIntelStable(repo_id=rid, address="a.py::old_by_commits", days_stable=10, unchanged_for=50, since_start=False, ref=H))
78 await db_session.commit()
79 page = (await client.get(f"/{repo.owner}/{repo.slug}/intel/stable")).text
80 assert page.index("old_by_commits") < page.index("old_by_days")
81 assert "50 commits" in _text(page)
File History 1 commit
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3 feat(intel): every grammar on the hub, and every intel job … Opus 5.5 minor ⚠ 1 day ago