"""Real-Postgres integration tests for the ledger rows of a repo that leaves a project. A coverage refresh walks only the repos bound to a project, so the rows of a repo that was unbound, or re-bound to another project, would never be matched against a tree again and would keep counting. Re-binding, unbinding and the refresh's own sweep stamp them vanished, with an `unbound` event and their judgment kept; a repo still bound stays live whether or not the forge served it this time. """ import pytest import pytest_asyncio from sqlalchemy import select from grimoire.models import async_session from grimoire.models.code_shape import CodeShape, CodeShapeEvent from grimoire.models.project import Project from grimoire.services import repo_bindings as svc from grimoire.services.shape_ledger import ( classify_shapes, live_rows, sync_repo_shapes, vanish_unbound, ) from tests.helpers import ensure_user pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")] MOVING = "https://git.example.com/alice/engine.git" STAYING = "https://git.example.com/alice/atlas.git" MOVING_KEY = "git.example.com/alice/engine" STAYING_KEY = "git.example.com/alice/atlas" @pytest_asyncio.fixture async def split(): """One project bound to two repos, each with a synced ledger, one row judged in the repo that is about to leave.""" async with async_session() as s: owner = await ensure_user(s, "shape_unbound_owner") first = Project(user_id=owner.id, title="Before the split") second = Project(user_id=owner.id, title="After the split") s.add_all([first, second]) await s.flush() ids = {"owner": owner.id, "first": first.id, "second": second.id} await s.commit() await svc.set_binding(ids["owner"], MOVING, ids["first"]) await svc.set_binding(ids["owner"], STAYING, ids["first"]) await sync_repo_shapes( ids["first"], MOVING_KEY, [("src/sim.py", "sym", "tick"), ("src/sim.py", "sym", "spawn")], seen_marker="c1", ) await sync_repo_shapes( ids["first"], STAYING_KEY, [("src/map.py", "sym", "render")], seen_marker="c1", ) await classify_shapes(ids["owner"], ids["first"], [ {"path": "src/sim.py", "symbol": "tick", "status": "exempt", "reason": "one caller"}, ]) yield ids await svc.delete_binding(ids["owner"], MOVING) await svc.delete_binding(ids["owner"], STAYING) async def _live_keys(pid: int) -> set[str]: return {r.repo_key for r in await live_rows(pid)} async def _rows(pid: int, key: str) -> list[CodeShape]: async with async_session() as s: rows = await s.execute( select(CodeShape).where(CodeShape.project_id == pid, CodeShape.repo_key == key) ) return list(rows.scalars()) async def _events(shape_ids: list[int]) -> list[str]: async with async_session() as s: rows = await s.execute( select(CodeShapeEvent.event).where(CodeShapeEvent.shape_id.in_(shape_ids)) ) return [e for (e,) in rows.all()] @pytest.mark.integration async def test_rebinding_a_repo_elsewhere_vanishes_its_rows_on_the_project_it_left(split): await svc.set_binding(split["owner"], MOVING, split["second"]) assert await _live_keys(split["first"]) == {STAYING_KEY} left = await _rows(split["first"], MOVING_KEY) assert len(left) == 2 and all(r.vanished_at is not None for r in left) assert {r.symbol: r.status for r in left} == {"tick": "exempt", "spawn": "unclassified"} assert (await _events([r.id for r in left])).count("unbound") == 2 staying = await _rows(split["first"], STAYING_KEY) assert [r.vanished_at for r in staying] == [None] @pytest.mark.integration async def test_unbinding_a_repo_vanishes_its_rows(split): assert await svc.delete_binding(split["owner"], MOVING) assert await _live_keys(split["first"]) == {STAYING_KEY} @pytest.mark.integration async def test_a_bound_repo_stays_live_though_no_sync_reached_it(split): # The refresh's sweep keys on bindings, not on which repos the forge # served: with every repo still bound, nothing is stamped. assert await vanish_unbound(split["first"]) == 0 assert await _live_keys(split["first"]) == {MOVING_KEY, STAYING_KEY} @pytest.mark.integration async def test_the_sweep_catches_rows_left_by_a_move_made_before_it_existed(split): # A binding moved by a plain UPDATE, as every re-bind did before the # sweep, leaves the rows live; the refresh's sweep is what clears them. from grimoire.models.repo_binding import RepoBinding async with async_session() as s: binding = (await s.execute( select(RepoBinding).where(RepoBinding.repo_key == MOVING_KEY) )).scalar_one() binding.project_id = split["second"] await s.commit() assert await _live_keys(split["first"]) == {MOVING_KEY, STAYING_KEY} assert await vanish_unbound(split["first"]) == 2 assert await _live_keys(split["first"]) == {STAYING_KEY} @pytest.mark.integration async def test_binding_the_repo_back_revives_its_rows_with_their_judgment(split): await svc.set_binding(split["owner"], MOVING, split["second"]) await svc.set_binding(split["owner"], MOVING, split["first"]) await sync_repo_shapes( split["first"], MOVING_KEY, [("src/sim.py", "sym", "tick"), ("src/sim.py", "sym", "spawn")], seen_marker="c2", ) back = {r.symbol: (r.vanished_at, r.status) for r in await _rows(split["first"], MOVING_KEY)} assert back == {"tick": (None, "exempt"), "spawn": (None, "unclassified")}