gabriel / musehub public
test_intel_health_status.py python
378 lines 19.1 KB
Raw
sha256:0bed760b925ef8ad45036955346e241552d231ea5045477a59c2a6917d0ac84b fix(intel): a repository with no code symbols is 'empty', n… Sonnet 5.5 minor ⚠ breaking 3 days ago
1 """Health score, ring geometry and analysis freshness: one definition shared by the Intel page and the Explorer card.
2
3 Pure tests (no DB): ring arc, provider/repo state folding, label bands, the shared ring macro.
4 DB tests: ``load_intel_status`` transitions driven by real published rows and job rows, and Explorer/Intel agreement.
5 """
6 from __future__ import annotations
7
8 import re
9 from datetime import datetime, timezone
10
11 import pytest
12 from jinja2 import Environment, FileSystemLoader
13 from sqlalchemy.ext.asyncio import AsyncSession
14
15 from musehub.services.musehub_intel import health_label
16 from musehub.services.musehub_intel_status import (
17 REQUIRED_JOB_TYPES,
18 STATE_LABELS,
19 IntelStatus,
20 provider_state,
21 repo_state,
22 ring_arc,
23 short_ref,
24 )
25
26 _TEMPLATES = "musehub/templates"
27 H = "sha256:" + "a" * 64
28 OLD = "sha256:" + "b" * 64
29
30
31 # ── labels ────────────────────────────────────────────────────────────────────
32
33 @pytest.mark.parametrize("score,label", [(100, "Excellent"), (95, "Excellent"), (90, "Excellent"), (89, "Good"), (75, "Good"), (74, "Fair"), (55, "Fair"), (54, "Poor"), (35, "Poor"), (34, "Critical"), (0, "Critical")])
34 def test_health_label_bands(score: int, label: str) -> None:
35 assert health_label(score) == label
36 assert IntelStatus(head=H, state="current", score=score, score_ref=H).label == label
37
38
39 def test_no_score_has_no_label() -> None:
40 assert IntelStatus(head=H, state="not_analyzed").label is None
41
42
43 # ── ring geometry ─────────────────────────────────────────────────────────────
44
45 @pytest.mark.parametrize("score", [0, 25, 50, 75, 90, 95, 100])
46 def test_ring_arc_is_exactly_the_score_fraction(score: int) -> None:
47 arc = ring_arc(score)
48 assert arc.dash + arc.gap == 100.0
49 assert arc.dash == float(score)
50 assert arc.visible is (score > 0)
51
52
53 def test_ring_arc_missing_and_out_of_range() -> None:
54 assert not ring_arc(None).visible
55 assert not ring_arc(-3).visible
56 assert ring_arc(140).dash == 100.0
57
58
59 def _render(status: IntelStatus, size: str = "lg") -> str:
60 env = Environment(loader=FileSystemLoader(_TEMPLATES))
61 return env.from_string('{% from "musehub/partials/health_ring.html" import health_ring %}{{ health_ring(s, size) }}').render(s=status, size=size)
62
63
64 @pytest.mark.parametrize("score", [0, 25, 50, 75, 90, 95, 100])
65 def test_rendered_ring_arc_matches_score(score: int) -> None:
66 svg = _render(IntelStatus(head=H, state="current", score=score, score_ref=H))
67 fills = re.findall(r'<circle class="intel-health-fill[^>]*>', svg)
68 if score == 0:
69 assert fills == []
70 return
71 (fill,) = fills
72 assert 'pathLength="100"' in fill
73 assert "dashoffset" not in fill.lower() # the old bug: an offset removed 25% of every arc
74 dash, gap = re.search(r'stroke-dasharray="([\d.]+) ([\d.]+)"', fill).groups()
75 assert float(dash) == score and float(dash) + float(gap) == 100.0
76 assert 'transform="rotate(-90 50 50)"' in fill # starts at 12 o'clock, clockwise
77 assert f">{score}</text>" in svg # the number is printed: readable without colour
78 assert f"intel-health-fill--{health_label(score).lower()}" in fill
79
80
81 def test_rendered_ring_100_is_a_closed_circle_and_95_is_not() -> None:
82 full = _render(IntelStatus(head=H, state="current", score=100, score_ref=H))
83 ninety_five = _render(IntelStatus(head=H, state="current", score=95, score_ref=H))
84 assert 'stroke-dasharray="100.0 0.0"' in full
85 assert 'stroke-dasharray="95.0 5.0"' in ninety_five
86
87
88 @pytest.mark.parametrize("state", ["analyzing", "outdated", "failed"])
89 def test_stale_score_is_muted_never_a_severity_colour(state: str) -> None:
90 svg = _render(IntelStatus(head=H, state=state, score=95, score_ref=OLD), "sm")
91 assert "intel-health-fill--stale" in svg
92 assert "intel-health-fill--excellent" not in svg
93
94
95 def test_missing_score_draws_track_only_and_a_dash() -> None:
96 svg = _render(IntelStatus(head=H, state="not_analyzed"), "sm")
97 assert "intel-health-track" in svg and "intel-health-fill" not in svg
98 assert ">—</text>" in svg
99 assert "No health score" in svg
100
101
102 def test_both_sizes_share_geometry() -> None:
103 st = IntelStatus(head=H, state="current", score=95, score_ref=H)
104 for size in ("lg", "sm"):
105 assert 'stroke-dasharray="95.0 5.0"' in _render(st, size)
106
107
108 # ── provider / repo state ─────────────────────────────────────────────────────
109
110 def test_provider_state_precedence() -> None:
111 assert provider_state(H, H, []) == "current"
112 assert provider_state(H, H, ["running"]) == "current" # published beats any job row
113 assert provider_state(OLD, H, ["running"]) == "analyzing"
114 assert provider_state(OLD, H, ["failed", "pending"]) == "queued" # a retry in flight outranks an earlier failure (queued: nothing running yet)
115 assert provider_state(OLD, H, ["pending", "running"]) == "analyzing" # running wins over pending
116 assert provider_state(OLD, H, ["done", "failed"]) == "outdated" # newest job finished but published nothing for the head: not an analysis of it
117 assert provider_state(None, H, ["done"]) == "never" # a finished job that published nothing is never a "current zero"
118 assert provider_state(OLD, H, ["failed", "done"]) == "failed" # newest job failed
119 assert provider_state(OLD, H, ["quarantined"]) == "failed"
120 assert provider_state(OLD, H, ["blocked"]) == "failed"
121 assert provider_state(OLD, H, ["skipped"]) == "not_run"
122 assert provider_state(OLD, H, []) == "outdated"
123 assert provider_state(None, H, []) == "never"
124
125
126 def _all(state: str, **over: str) -> dict[str, str]:
127 d = {t: state for t in REQUIRED_JOB_TYPES}
128 d.update(over)
129 return d
130
131
132 def test_repo_state_current_requires_every_provider() -> None:
133 assert repo_state(_all("current")) == "current"
134 assert repo_state(_all("current", **{"intel.code.dead": "never"})) == "outdated" # a done-but-unpublished provider no longer counts as current
135 # some providers finished, others still running: never "Current"
136 assert repo_state(_all("current", **{"intel.code.gravity": "analyzing", "intel.code.type": "analyzing"})) == "analyzing"
137 # all finished but one never ran: not Current either
138 assert repo_state(_all("current", **{"intel.code.type": "never"})) == "outdated"
139 assert repo_state(_all("current", **{"intel.code.type": "not_run"})) == "outdated"
140
141
142 def test_repo_state_previous_revision_with_new_one_analyzing_or_failing() -> None:
143 assert repo_state(_all("outdated", **{"intel.code": "analyzing"})) == "analyzing"
144 assert repo_state(_all("outdated", **{"intel.code": "failed"})) == "failed"
145 assert repo_state(_all("outdated")) == "outdated"
146
147
148 def test_repo_state_analyzing_outranks_failed() -> None:
149 assert repo_state(_all("current", **{"intel.code.type": "failed", "intel.code.dead": "analyzing"})) == "analyzing"
150
151
152 def test_repo_state_nothing_published() -> None:
153 assert repo_state(_all("never")) == "not_analyzed"
154 assert repo_state(_all("not_run")) == "not_analyzed"
155 assert repo_state(_all("never", **{"intel.code": "failed"})) == "failed"
156 assert repo_state(_all("never", **{"intel.code": "analyzing"})) == "analyzing"
157
158
159 def test_required_provider_set_is_the_code_intel_jobs() -> None:
160 assert "intel.code" in REQUIRED_JOB_TYPES and "intel.code.gravity" in REQUIRED_JOB_TYPES
161 assert not any(t.startswith("intel.structural") or t == "gc" for t in REQUIRED_JOB_TYPES)
162 assert len(REQUIRED_JOB_TYPES) == 15
163 assert set(STATE_LABELS) == {"current", "analyzing", "queued", "not_applicable", "empty", "outdated", "failed", "not_analyzed"}
164
165
166 def test_short_ref() -> None:
167 assert short_ref(H) == "aaaaaaaa" and short_ref(None) is None and short_ref("") is None
168
169
170 # ── DB: load_intel_status against real rows ───────────────────────────────────
171
172 async def _publish(db: AsyncSession, repo_id: str, ref: str, score: int = 95, only: set[str] | None = None) -> None:
173 import json
174 from musehub.db.musehub_intel_models import MusehubIntelResult
175 from musehub.services.musehub_intel_providers import published_types
176 types = {"code.intel_snapshot"} | {t for j in REQUIRED_JOB_TYPES for t in published_types(j)}
177 for t in sorted(types):
178 job = next((j for j in REQUIRED_JOB_TYPES if t in published_types(j)), "intel.code")
179 if only is not None and job not in only:
180 continue
181 data = {"health_score": score, "health_label": health_label(score)} if t == "code.intel_snapshot" else {"count": 0}
182 await db.merge(MusehubIntelResult(
183 result_id=f"{repo_id}|{t}", repo_id=repo_id, intel_type=t, domain="code", ref=ref,
184 data_json=json.dumps(data), schema_version=1, computed_at=datetime.now(tz=timezone.utc),
185 ))
186 await db.commit()
187
188
189 async def _job(db: AsyncSession, repo_id: str, job_type: str, head: str, status: str) -> None:
190 from uuid import uuid4
191 from musehub.db.musehub_jobs_models import MusehubBackgroundJob
192 db.add(MusehubBackgroundJob(job_id=uuid4().hex, repo_id=repo_id, job_type=job_type, payload={"head": head}, status=status))
193 await db.commit()
194
195
196 async def _repo_with_head(db: AsyncSession, head: str) -> str:
197 from tests.factories import create_branch, create_repo # noqa: PLC0415
198 repo = await create_repo(db, visibility="public")
199 await create_branch(db, repo.repo_id, name=repo.default_branch, head_commit_id=head)
200 return repo.repo_id
201
202
203 @pytest.mark.asyncio
204 async def test_status_current_after_every_provider_published_the_head(db_session: AsyncSession) -> None:
205 from musehub.services.musehub_intel_status import load_intel_status
206 rid = await _repo_with_head(db_session, H)
207 await _publish(db_session, rid, H, score=95)
208 st = (await load_intel_status(db_session, [rid]))[rid]
209 assert (st.state, st.score, st.label, st.score_ref, st.score_is_current) == ("current", 95, "Excellent", H, True)
210
211
212 @pytest.mark.asyncio
213 async def test_status_partial_publication_is_not_current(db_session: AsyncSession) -> None:
214 from musehub.services.musehub_intel_status import load_intel_status
215 rid = await _repo_with_head(db_session, H)
216 await _publish(db_session, rid, OLD, score=80)
217 await _publish(db_session, rid, H, score=95, only={"intel.code", "intel.code.dead"})
218 for j in REQUIRED_JOB_TYPES:
219 if j not in {"intel.code", "intel.code.dead"}:
220 await _job(db_session, rid, j, H, "running" if j.endswith("gravity") else "pending")
221 st = (await load_intel_status(db_session, [rid]))[rid]
222 assert st.state == "analyzing"
223 assert st.score == 95 and st.score_is_current # the snapshot itself is at the head; the set is not complete
224
225
226 @pytest.mark.asyncio
227 async def test_status_new_push_outdated_then_analyzing_then_failed_then_current(db_session: AsyncSession) -> None:
228 from musehub.services.musehub_intel_status import load_intel_status
229 rid = await _repo_with_head(db_session, OLD)
230 await _publish(db_session, rid, OLD, score=95)
231 assert (await load_intel_status(db_session, [rid]))[rid].state == "current"
232
233 # push: the branch head moves; nothing analysed yet
234 from sqlalchemy import update
235 from musehub.db.musehub_repo_models import MusehubBranch
236 await db_session.execute(update(MusehubBranch).where(MusehubBranch.repo_id == rid).values(head_commit_id=H))
237 await db_session.commit()
238 st = (await load_intel_status(db_session, [rid]))[rid]
239 assert st.state == "outdated" and st.score == 95 and not st.score_is_current and st.score_ref == OLD
240
241 await _job(db_session, rid, "intel.code", H, "running")
242 assert (await load_intel_status(db_session, [rid]))[rid].state == "analyzing"
243
244 await db_session.execute(__import__("sqlalchemy").text("DELETE FROM musehub_background_jobs WHERE repo_id=:r"), {"r": rid})
245 await _job(db_session, rid, "intel.code", H, "failed")
246 st = (await load_intel_status(db_session, [rid]))[rid]
247 assert st.state == "failed" and st.score == 95 and not st.score_is_current # last good score stays visible, muted
248
249 await _publish(db_session, rid, H, score=90)
250 st = (await load_intel_status(db_session, [rid]))[rid]
251 assert (st.state, st.score, st.label) == ("current", 90, "Excellent")
252
253
254 @pytest.mark.asyncio
255 async def test_status_non_default_branch_push_changes_nothing(db_session: AsyncSession) -> None:
256 from musehub.services.musehub_intel_status import load_intel_status
257 rid = await _repo_with_head(db_session, H)
258 await _publish(db_session, rid, H)
259 other = "sha256:" + "c" * 64
260 for j in REQUIRED_JOB_TYPES:
261 await _job(db_session, rid, j, other, "running") # jobs for a feature branch tip
262 assert (await load_intel_status(db_session, [rid]))[rid].state == "current"
263
264
265 @pytest.mark.asyncio
266 async def test_status_retry_in_flight_outranks_earlier_failure(db_session: AsyncSession) -> None:
267 from musehub.services.musehub_intel_status import load_intel_status
268 rid = await _repo_with_head(db_session, H)
269 await _publish(db_session, rid, OLD)
270 await _job(db_session, rid, "intel.code", H, "failed")
271 await _job(db_session, rid, "intel.code", H, "pending")
272 assert (await load_intel_status(db_session, [rid]))[rid].state == "queued"
273
274
275 @pytest.mark.asyncio
276 async def test_status_without_snapshot_has_no_score(db_session: AsyncSession) -> None:
277 from musehub.services.musehub_intel_status import load_intel_status
278 rid = await _repo_with_head(db_session, H)
279 st = (await load_intel_status(db_session, [rid]))[rid]
280 assert (st.state, st.score, st.label) == ("not_analyzed", None, None)
281 await _job(db_session, rid, "intel.code", H, "failed")
282 st = (await load_intel_status(db_session, [rid]))[rid]
283 assert (st.state, st.score) == ("failed", None)
284
285
286 @pytest.mark.asyncio
287 async def test_status_ignores_a_corrupt_score(db_session: AsyncSession) -> None:
288 from musehub.services.musehub_intel_status import load_intel_status
289 rid = await _repo_with_head(db_session, H)
290 await _publish(db_session, rid, H, score=250)
291 st = (await load_intel_status(db_session, [rid]))[rid]
292 assert st.score is None
293
294
295 # ── Explorer and Intel page agree ─────────────────────────────────────────────
296
297 @pytest.mark.asyncio
298 async def test_explorer_and_intel_page_show_the_same_score(client, db_session: AsyncSession) -> None:
299 from sqlalchemy import select
300 from musehub.db.musehub_repo_models import MusehubRepo
301 rid = await _repo_with_head(db_session, H)
302 await _publish(db_session, rid, H, score=95)
303 repo = (await db_session.execute(select(MusehubRepo).where(MusehubRepo.repo_id == rid))).scalar_one()
304
305 explore = (await client.get("/explore")).text
306 start = explore.index(f'href="/{repo.owner}/{repo.slug}"')
307 card = explore[start:explore.index("</a>", start)]
308 page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text
309
310 for html in (card, page):
311 assert 'aria-label="Health 95 of 100, Excellent, analysis current"' in html
312 assert 'stroke-dasharray="95.0 5.0"' in html
313 assert "Analysis: Current" in html
314 assert "aaaaaaaa" in html or html is card
315 assert ">95</text>" in card and ">95</text>" in page
316
317
318 @pytest.mark.asyncio
319 async def test_intel_page_without_snapshot_shows_not_analyzed_not_healthy(client, db_session: AsyncSession) -> None:
320 from sqlalchemy import select
321 from musehub.db.musehub_repo_models import MusehubRepo
322 rid = await _repo_with_head(db_session, H)
323 repo = (await db_session.execute(select(MusehubRepo).where(MusehubRepo.repo_id == rid))).scalar_one()
324 html = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text
325 assert "Analysis: Not analyzed" in html
326 assert "Excellent" not in html and "intel-health-fill" not in html
327 assert "intel-alert-list" not in html # no "0 alerts ✓" for an unanalysed repo
328 assert "Not analysed yet. Push a commit to populate." in html
329
330
331 # ── Truthful empty states and the gravity panel ───────────────────────────────
332
333 async def _intel_html(client, db: AsyncSession, rid: str) -> str:
334 from sqlalchemy import select
335 from musehub.db.musehub_repo_models import MusehubRepo
336 repo = (await db.execute(select(MusehubRepo).where(MusehubRepo.repo_id == rid))).scalar_one()
337 return (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text
338
339
340 @pytest.mark.asyncio
341 async def test_completed_zero_finding_providers_say_so(client, db_session: AsyncSession) -> None:
342 rid = await _repo_with_head(db_session, H)
343 await _publish(db_session, rid, H)
344 html = await _intel_html(client, db_session, rid)
345 assert "No breaking changes detected." in html
346 assert "No duplicate symbol clusters detected." in html
347 assert "No entangled symbol pairs detected." in html
348 assert "No refactoring events detected." in html
349 assert "Push a commit to populate" not in html
350
351
352 @pytest.mark.asyncio
353 async def test_empty_state_follows_each_providers_own_state(client, db_session: AsyncSession) -> None:
354 rid = await _repo_with_head(db_session, H)
355 await _publish(db_session, rid, H, only={j for j in REQUIRED_JOB_TYPES if j not in {"intel.code.breakage", "intel.code.clones", "intel.code.entangle"}})
356 await _job(db_session, rid, "intel.code.breakage", H, "running")
357 await _job(db_session, rid, "intel.code.clones", H, "failed")
358 html = await _intel_html(client, db_session, rid)
359 assert "Analysis is running for the current revision" in html # breakage
360 assert "Analysis failed for the current revision." in html # clones
361 assert "Not analysed yet. Push a commit to populate." in html # entangle: never ran
362 assert "No breaking changes detected." not in html and "No duplicate symbol clusters detected." not in html
363 assert "Analysis: Analyzing" in html
364
365
366 @pytest.mark.asyncio
367 async def test_gravity_panel_bar_value_and_order_use_the_same_metric(client, db_session: AsyncSession) -> None:
368 from musehub.db.musehub_intel_models import MusehubSymbolIntel
369 rid = await _repo_with_head(db_session, H)
370 await _publish(db_session, rid, H)
371 # the screenshot case: transitive-heavy symbol with no direct dependents next to ones with direct dependents
372 db_session.add(MusehubSymbolIntel(repo_id=rid, address="a.py::summary", gravity_pct=100.0, gravity_direct_dependents=0, gravity_transitive_dependents=11))
373 db_session.add(MusehubSymbolIntel(repo_id=rid, address="a.py::Rect", gravity_pct=90.9, gravity_direct_dependents=7, gravity_transitive_dependents=10))
374 await db_session.commit()
375 html = await _intel_html(client, db_session, rid)
376 assert ">100.0%</span>" in html and ">90.9%</span>" in html
377 assert "0 direct · 11 transitive dependents" in html and "7 direct · 10 transitive dependents" in html
378 assert html.index("a.py::summary") < html.index("a.py::Rect")
File History 4 commits
sha256:0bed760b925ef8ad45036955346e241552d231ea5045477a59c2a6917d0ac84b fix(intel): a repository with no code symbols is 'empty', n… Sonnet 5.5 minor ⚠ 3 days ago
sha256:545266da3f09ffbfe65a21bfc233bb91fe37f83ab714dd2d07db1fee98c3988b feat(intel): always-on code intelligence for every reposito… Sonnet 5.5 minor ⚠ 3 days ago
sha256:f9cda821d29cd59f4b95c1cf9cd667e1af3a8e40de55f505bcf50fdd0dc52b1d fix(intel): bounded failure recording, terminal resource-li… Sonnet 5.5 minor ⚠ 3 days ago
sha256:c26749fee2b003dab376a2270ce659c1afcf70371a528352a2c40cbed3c0f0f6 feat(intel): one health score + freshness for Intel and Exp… Sonnet 5.5 minor ⚠ 3 days ago