gabriel / musehub public
test_intel_detect_refactor_gaps.py python
84 lines 4.6 KB
Raw
sha256:4abaf18ab6b2d8ce3ce7da0d026c83447280087b372f2bb12c21ab11fae7cd3d fix(intel): detect_refactor refuses to advance its watermar… Sonnet 5.5 minor ⚠ breaking 4 days ago
1 """``detect_refactor`` must not advance its watermark past commits it could not analyse — musehub#8 release gate.
2
3 The provider skipped a commit whose own or parent snapshot was unavailable with a bare ``continue``: the commit counted as neither analysed nor skipped, and the
4 published revision (the watermark the next run starts from) moved past it, so its refactoring events were lost for good.
5
6 Run ONLY this file, against a private Postgres (never the live hub database)::
7
8 TEST_DATABASE_URL=postgresql+asyncpg://musehub:musehub@<private-host>:<port>/musehub_test python -P -m pytest tests/test_intel_detect_refactor_gaps.py
9
10 G1 A commit whose snapshot is unavailable fails the job (retryable): no events, no result row, so the watermark stays where it was.
11 G2 Once the snapshot exists a retry analyses the commit and its events appear: nothing was lost.
12 G3 The honest bounds are unchanged: a root commit has nothing to diff (not a gap) and the 200-commit window still reports ``commits_skipped``.
13 """
14 from __future__ import annotations
15
16 import pytest
17 import pytest_asyncio
18 from sqlalchemy import func, select, update
19
20 from musehub.db.musehub_intel_models import MusehubIntelRefactorEvent, MusehubIntelResult
21 from musehub.db.musehub_repo_models import MusehubCommit
22 from musehub.services import musehub_intel_providers as P
23 from tests.factories import create_repo
24 from tests.test_intel_snapshot_providers import World
25
26 V1 = "def alpha(x):\n return x + 1\n\n\ndef beta():\n return 2\n"
27 V2 = "def alpha(x):\n return x + 100\n\n\ndef beta():\n return 2\n" # alpha's body changes: an 'implementation' refactoring event
28 V3 = "def alpha(x):\n return x + 1000\n\n\ndef beta():\n return 2\n"
29
30
31 @pytest_asyncio.fixture
32 async def session_factory(db_session, session_factory): # noqa: F811
33 return session_factory
34
35
36 async def count_events(s, repo_id: str) -> int:
37 return (await s.execute(select(func.count()).select_from(MusehubIntelRefactorEvent).where(MusehubIntelRefactorEvent.repo_id == repo_id))).scalar_one()
38
39
40 async def watermark(s, repo_id: str) -> str | None:
41 return (await s.execute(select(MusehubIntelResult.ref).where(MusehubIntelResult.repo_id == repo_id,
42 MusehubIntelResult.intel_type == "intel.code.detect_refactor"))).scalar_one_or_none()
43
44
45 @pytest.mark.asyncio
46 async def test_g1_g2_an_unavailable_snapshot_fails_the_job_and_a_retry_loses_nothing(session_factory) -> None:
47 async with session_factory() as s:
48 repo = await create_repo(s, owner="gabriel", slug="rf-gap")
49 w = World(s, repo.repo_id)
50 c1 = await w.commit("c1", {"m.py": V1}, minute=0)
51 c2 = await w.commit("c2", {"m.py": V2}, ["c1"], minute=1)
52 c3 = await w.commit("c3", {"m.py": V3}, ["c2"], minute=2)
53 c2_snapshot = (await s.get(MusehubCommit, c2)).snapshot_id
54 await s.execute(update(MusehubCommit).where(MusehubCommit.commit_id == c2).values(snapshot_id=None)) # c2's snapshot has not arrived yet
55 await s.commit()
56
57 with pytest.raises(P.IncompleteHistory, match="2 commit"):
58 await P.DetectRefactorProvider().compute(s, w.repo_id, c3, {})
59 await s.rollback()
60 assert await count_events(s, w.repo_id) == 0 and await watermark(s, w.repo_id) is None # nothing published, watermark did not move
61
62 await s.execute(update(MusehubCommit).where(MusehubCommit.commit_id == c2).values(snapshot_id=c2_snapshot)) # the snapshot arrives
63 await s.commit()
64 out = await P.DetectRefactorProvider().compute(s, w.repo_id, c3, {})
65 await s.commit()
66 data = out[0][1]
67 assert data["commits_analysed"] == 2 and data["commits_skipped"] == 0 and data["count"] == 2 # c2 and c3 each rewrote alpha: nothing lost
68 assert c1 # (c1 is the root: nothing to diff, and that is not a gap)
69
70
71 @pytest.mark.asyncio
72 async def test_g3_a_root_commit_is_not_a_gap_and_the_window_bound_is_still_reported(session_factory, monkeypatch) -> None:
73 monkeypatch.setattr(P.DetectRefactorProvider, "_MAX_COMMITS", 2)
74 async with session_factory() as s:
75 repo = await create_repo(s, owner="gabriel", slug="rf-window")
76 w = World(s, repo.repo_id)
77 prev = None
78 for i in range(5):
79 src = f"def alpha(x):\n return x + {i}\n"
80 prev = await w.commit(f"c{i}", {"m.py": src}, [f"c{i - 1}"] if i else None, minute=i)
81 await s.commit()
82 out = await P.DetectRefactorProvider().compute(s, w.repo_id, prev, {})
83 data = out[0][1]
84 assert data["commits_analysed"] == 2 and data["commits_skipped"] == 3 # bounded history, reported — not silent
File History 1 commit
sha256:4abaf18ab6b2d8ce3ce7da0d026c83447280087b372f2bb12c21ab11fae7cd3d fix(intel): detect_refactor refuses to advance its watermar… Sonnet 5.5 minor ⚠ 4 days ago