gabriel / musehub public
test_repo_card_data_integrity.py python
336 lines 12.9 KB
Raw
sha256:c26749fee2b003dab376a2270ce659c1afcf70371a528352a2c40cbed3c0f0f6 feat(intel): one health score + freshness for Intel and Exp… Sonnet 5.5 minor ⚠ breaking 3 days ago
1 """
2 Explorer card data integrity (musehub#333).
3
4 Every metric on the enriched repo card must be either labelled for what it
5 measures or absent. These tests pin the four corrections:
6
7 Last commit
8 CARD_LC_01 last_commit_at is the newest commit timestamp
9 CARD_LC_02 a repo with no commits has last_commit_at None
10 CARD_LC_03 the card shows "Last commit" with the commit time, never created_at
11 CARD_LC_04 a repo with no commits shows "No commits yet"
12 CARD_LC_05 Explorer order (activity sort) matches the displayed last-commit order
13
14 Pulse
15 CARD_PU_01 pulse_total is the sum of the displayed daily buckets
16 CARD_PU_02 the card shows the total and says it counts commits, not pushes
17 CARD_PU_03 every day carries an accessible per-day count label
18
19 Health
20 CARD_HE_01 no analysis rows → status "none" and the card says "No analysis"
21 CARD_HE_02 a breakage-meta row or any dead row counts as analysis
22 CARD_HE_03 dismissed dead symbols are excluded from the active count
23 CARD_HE_04 existing risk / warn / clean classification is preserved
24 CARD_HE_05 the card never renders a 0-100 gauge
25
26 Agent attribution
27 CARD_AG_01 the card says "Agent-attributed commits", never "Autonomy"
28 CARD_AG_02 numerator, denominator and percentage are unchanged
29 CARD_AG_03 a repo with no commits shows no percentage
30
31 Visibility
32 CARD_PV_01 private repositories stay out of the Explorer
33 """
34 from __future__ import annotations
35
36 import re
37 import secrets
38 from datetime import date, datetime, timedelta, timezone
39
40 from httpx import AsyncClient
41 from sqlalchemy import text
42 from sqlalchemy.ext.asyncio import AsyncSession
43
44 from musehub.db.musehub_intel_models import MusehubIntelBreakageMeta, MusehubIntelDead
45 from musehub.services.repo_card_enrichment import (
46 RepoCardEnrichment,
47 _build_pulse,
48 enrich_repo_cards,
49 )
50 from tests.factories import create_commit, create_repo
51
52
53 # ---------------------------------------------------------------------------
54 # Helpers
55 # ---------------------------------------------------------------------------
56
57 def _now() -> datetime:
58 return datetime.now(tz=timezone.utc)
59
60
61 def _days_ago(n: float) -> datetime:
62 return _now() - timedelta(days=n)
63
64
65 async def _public_repo(
66 db: AsyncSession,
67 *,
68 created_days_ago: float | None = None,
69 visibility: str = "public",
70 ) -> tuple[str, str, str]:
71 """Seed a repo with a unique owner/slug; return (repo_id, owner, slug)."""
72 repo = await create_repo(db, visibility=visibility)
73 owner = "o" + secrets.token_hex(4)
74 slug = "s" + secrets.token_hex(4)
75 await db.execute(
76 text("UPDATE musehub_repos SET owner = :o, slug = :s, name = :s WHERE repo_id = :r"),
77 {"o": owner, "s": slug, "r": repo.repo_id},
78 )
79 if created_days_ago is not None:
80 await db.execute(
81 text("UPDATE musehub_repos SET created_at = :c WHERE repo_id = :r"),
82 {"c": _days_ago(created_days_ago), "r": repo.repo_id},
83 )
84 await db.commit()
85 return repo.repo_id, owner, slug
86
87
88 async def _agent_commit(db: AsyncSession, repo_id: str, *, when: datetime | None = None) -> None:
89 commit = await create_commit(db, repo_id, timestamp=when or _now())
90 await db.execute(
91 text("UPDATE musehub_commits SET agent_id = 'claude-code' WHERE commit_id = :c"),
92 {"c": commit.commit_id},
93 )
94 await db.commit()
95
96
97 async def _explore(client: AsyncClient) -> str:
98 resp = await client.get("/explore", params={"limit": 50})
99 assert resp.status_code == 200
100 return resp.text
101
102
103 def _card(html: str, owner: str, slug: str) -> str:
104 m = re.search(
105 rf'<a class="rc-card" href="/{owner}/{slug}">(.*?)</a>', html, re.S
106 )
107 assert m is not None, f"card for {owner}/{slug} not in Explorer HTML"
108 return m.group(1)
109
110
111 # ---------------------------------------------------------------------------
112 # Last commit
113 # ---------------------------------------------------------------------------
114
115 async def test_card_lc_01_last_commit_at_is_newest_commit(db_session: AsyncSession) -> None:
116 repo_id, _, _ = await _public_repo(db_session)
117 await create_commit(db_session, repo_id, timestamp=_days_ago(9))
118 newest = _days_ago(2)
119 await create_commit(db_session, repo_id, timestamp=newest)
120 await create_commit(db_session, repo_id, timestamp=_days_ago(40))
121
122 enc = (await enrich_repo_cards(db_session, [repo_id]))[repo_id]
123 assert enc.last_commit_at is not None
124 assert abs((enc.last_commit_at - newest).total_seconds()) < 1
125
126
127 async def test_card_lc_02_no_commits_means_no_last_commit(db_session: AsyncSession) -> None:
128 repo_id, _, _ = await _public_repo(db_session)
129 enc = (await enrich_repo_cards(db_session, [repo_id]))[repo_id]
130 assert enc.last_commit_at is None
131
132
133 async def test_card_lc_03_card_shows_commit_time_not_creation_time(
134 client: AsyncClient, db_session: AsyncSession
135 ) -> None:
136 repo_id, owner, slug = await _public_repo(db_session, created_days_ago=0)
137 commit_time = _days_ago(12)
138 await create_commit(db_session, repo_id, timestamp=commit_time)
139
140 card = _card(await _explore(client), owner, slug)
141 assert "Last commit" in card
142 assert f'datetime="{commit_time.date().isoformat()}' in card
143 assert _now().date().isoformat() not in re.findall(r'datetime="(\d{4}-\d{2}-\d{2})', card)
144
145
146 async def test_card_lc_04_empty_repo_says_no_commits_yet(
147 client: AsyncClient, db_session: AsyncSession
148 ) -> None:
149 _, owner, slug = await _public_repo(db_session, created_days_ago=5)
150 card = _card(await _explore(client), owner, slug)
151 assert "No commits yet" in card
152 assert "ago" not in card.split("rc-card__footer")[-1]
153
154
155 async def test_card_lc_05_explorer_order_matches_displayed_last_commit(
156 client: AsyncClient, db_session: AsyncSession
157 ) -> None:
158 # A is the oldest repo but has the newest commit; B was created last but is stale.
159 a_id, a_owner, a_slug = await _public_repo(db_session, created_days_ago=100)
160 b_id, b_owner, b_slug = await _public_repo(db_session, created_days_ago=1)
161 a_time, b_time = _days_ago(1), _days_ago(50)
162 await create_commit(db_session, a_id, timestamp=a_time)
163 await create_commit(db_session, b_id, timestamp=b_time)
164
165 html = await _explore(client)
166 assert html.index(f"/{a_owner}/{a_slug}") < html.index(f"/{b_owner}/{b_slug}")
167 assert f'datetime="{a_time.date().isoformat()}' in _card(html, a_owner, a_slug)
168 assert f'datetime="{b_time.date().isoformat()}' in _card(html, b_owner, b_slug)
169
170
171 # ---------------------------------------------------------------------------
172 # Pulse
173 # ---------------------------------------------------------------------------
174
175 def test_card_pu_01_pulse_total_sums_displayed_buckets() -> None:
176 today = date(2026, 11, 12)
177 buckets = _build_pulse({"2026-11-12": 3, "2026-11-01": 2, "2026-10-01": 99}, today)
178 enc = RepoCardEnrichment(repo_id="r", pulse_buckets=buckets)
179 # 2026-10-01 is outside the 30 displayed days and must not be counted.
180 assert enc.pulse_total == 5
181
182
183 async def test_card_pu_02_card_shows_total_and_says_commits_not_pushes(
184 client: AsyncClient, db_session: AsyncSession
185 ) -> None:
186 repo_id, owner, slug = await _public_repo(db_session)
187 for _ in range(3):
188 await create_commit(db_session, repo_id, timestamp=_now())
189 await create_commit(db_session, repo_id, timestamp=_days_ago(3))
190
191 card = _card(await _explore(client), owner, slug)
192 assert "4 commits" in card
193 assert "last 30 days" in card
194 assert "not pushes" in card
195
196
197 async def test_card_pu_03_every_day_has_an_accessible_count_label(
198 client: AsyncClient, db_session: AsyncSession
199 ) -> None:
200 repo_id, owner, slug = await _public_repo(db_session)
201 for _ in range(3):
202 await create_commit(db_session, repo_id, timestamp=_now())
203 await create_commit(db_session, repo_id, timestamp=_now() - timedelta(days=1))
204
205 card = _card(await _explore(client), owner, slug)
206 today = _now().date().isoformat()
207 yesterday = (_now().date() - timedelta(days=1)).isoformat()
208 assert f'aria-label="{today}: 3 commits"' in card
209 assert f'aria-label="{yesterday}: 1 commit"' in card
210 assert len(re.findall(r'aria-label="\d{4}-\d{2}-\d{2}: \d+ commits?"', card)) == 30
211 assert f"<title>{today}: 3 commits</title>" in card
212
213
214 # ---------------------------------------------------------------------------
215 # Health
216 # ---------------------------------------------------------------------------
217
218 def test_card_he_01_default_has_no_intel_status() -> None:
219 assert RepoCardEnrichment(repo_id="r").intel is None
220
221
222 async def test_card_he_01_card_says_no_analysis(
223 client: AsyncClient, db_session: AsyncSession
224 ) -> None:
225 _, owner, slug = await _public_repo(db_session)
226 card = _card(await _explore(client), owner, slug)
227 assert "No health score" in card
228 assert "Analysis: Not analyzed" in card
229 assert "no issues" not in card.lower()
230
231
232 async def test_card_he_02_breakage_meta_or_dead_row_is_not_a_health_score(
233 db_session: AsyncSession,
234 ) -> None:
235 """Only a published code.intel_snapshot yields a score; breakage/dead rows alone never look healthy."""
236 with_meta, _, _ = await _public_repo(db_session)
237 with_dead, _, _ = await _public_repo(db_session)
238 db_session.add(MusehubIntelBreakageMeta(
239 repo_id=with_meta, total_issues=0, error_count=0, warning_count=0, file_count=0, ref="main",
240 ))
241 db_session.add(MusehubIntelDead(
242 repo_id=with_dead, address="a.py::f", kind="function",
243 confidence="high", ref="main", dismissed=True,
244 ))
245 await db_session.commit()
246
247 res = await enrich_repo_cards(db_session, [with_meta, with_dead])
248 for rid in (with_meta, with_dead):
249 assert res[rid].intel.score is None and res[rid].intel.state == "not_analyzed"
250
251
252 async def test_card_he_03_dismissed_dead_symbols_are_not_active(
253 client: AsyncClient, db_session: AsyncSession
254 ) -> None:
255 repo_id, owner, slug = await _public_repo(db_session)
256 for addr, dismissed in (("a.py::one", True), ("a.py::two", True), ("a.py::three", False)):
257 db_session.add(MusehubIntelDead(
258 repo_id=repo_id, address=addr, kind="function",
259 confidence="high", ref="main", dismissed=dismissed,
260 ))
261 await db_session.commit()
262
263 enc = (await enrich_repo_cards(db_session, [repo_id]))[repo_id]
264 assert enc.dead_count == 1
265 card = _card(await _explore(client), owner, slug)
266 assert "1 dead" in card
267
268
269 async def test_card_he_05_card_without_a_snapshot_draws_no_fill_arc(
270 client: AsyncClient, db_session: AsyncSession
271 ) -> None:
272 ok_id, ok_owner, ok_slug = await _public_repo(db_session)
273 db_session.add(MusehubIntelBreakageMeta(
274 repo_id=ok_id, total_issues=2, error_count=2, warning_count=0, file_count=1, ref="main",
275 ))
276 await db_session.commit()
277
278 card = _card(await _explore(client), ok_owner, ok_slug)
279 assert "intel-health-track" in card # the empty ring is there ...
280 assert "stroke-dasharray" not in card # ... but nothing is filled: no score, no green ring
281 assert "2 errors" in card
282
283
284 # ---------------------------------------------------------------------------
285 # Agent attribution
286 # ---------------------------------------------------------------------------
287
288 async def test_card_ag_01_label_is_agent_attributed_commits(
289 client: AsyncClient, db_session: AsyncSession
290 ) -> None:
291 repo_id, owner, slug = await _public_repo(db_session)
292 await _agent_commit(db_session, repo_id)
293 card = _card(await _explore(client), owner, slug)
294 assert "Agent-attributed commits" in card
295 assert "utonomy" not in card
296
297
298 async def test_card_ag_02_numerator_denominator_percentage_unchanged(
299 client: AsyncClient, db_session: AsyncSession
300 ) -> None:
301 repo_id, owner, slug = await _public_repo(db_session)
302 for _ in range(3):
303 await _agent_commit(db_session, repo_id)
304 await create_commit(db_session, repo_id, timestamp=_now())
305
306 enc = (await enrich_repo_cards(db_session, [repo_id]))[repo_id]
307 assert (enc.agent_commits, enc.commit_total, enc.agent_attributed_pct) == (3, 4, 75)
308 card = _card(await _explore(client), owner, slug)
309 assert "75%" in card
310 assert "3 of 4 commits" in card
311
312
313 async def test_card_ag_03_no_commits_shows_no_percentage(
314 client: AsyncClient, db_session: AsyncSession
315 ) -> None:
316 _, owner, slug = await _public_repo(db_session)
317 card = _card(await _explore(client), owner, slug)
318 assert "Agent-attributed commits" in card
319 assert "0%" not in card
320
321
322 # ---------------------------------------------------------------------------
323 # Visibility
324 # ---------------------------------------------------------------------------
325
326 async def test_card_pv_01_private_repo_stays_out_of_explorer(
327 client: AsyncClient, db_session: AsyncSession
328 ) -> None:
329 pub_id, pub_owner, pub_slug = await _public_repo(db_session)
330 priv_id, priv_owner, priv_slug = await _public_repo(db_session, visibility="private")
331 await create_commit(db_session, pub_id, timestamp=_now())
332 await create_commit(db_session, priv_id, timestamp=_now())
333
334 html = await _explore(client)
335 assert f"/{pub_owner}/{pub_slug}" in html
336 assert f"/{priv_owner}/{priv_slug}" not in html
File History 3 commits
sha256:c26749fee2b003dab376a2270ce659c1afcf70371a528352a2c40cbed3c0f0f6 feat(intel): one health score + freshness for Intel and Exp… Sonnet 5.5 minor ⚠ 3 days ago
sha256:7bec3509d3f9dd83add7577c7c383bff78c2064f43ae33415c3fbedfa615b9a4 merge: Explorer card metrics say what they measure (#333) Sonnet 5.5 minor ⚠ 3 days ago
sha256:e9e8ca84a51b1eef245efa65f1eb96ade5daf89eebb3ab6d44f0a0ee84991fd4 fix(explore): make every repo card metric say what it measu… Sonnet 5.5 minor ⚠ 4 days ago