Files
FabledGrimoire/tests/test_integration_test_guards.py
T
bvandeusenandClaude Opus 5.5 f4f036c8a7
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 14s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Successful in 2m7s
CI & Build / integration (push) Successful in 2m41s
CI & Build / Build & push image (push) Successful in 25s
fix(8249): a redundant verdict can name a cover the sync has not read yet
A test is usually rewritten in the same change that removes the one it
replaces, so the cover is new to the ledger when the verdict is recorded,
and the next refresh retires the removed test. classify_shapes refused such
a cover as matching no live shape. With a bound `repo` the cover now
resolves the way the judged shape already does, as a provisional row the
next sync adopts; without one it is refused as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 02:58:35 -05:00

147 lines
6.1 KiB
Python

"""Real-Postgres tests for test-guard links: the `test` stamp, the groups,
a redundant verdict, and a kept group.
What the pure tests can't prove: that a sync stamps a linked test case and
withdraws the stamp when its links go, never over a judgment, and that the
group readout honours verdicts and reviews held in the database. Ledger rows
are seeded through the same sync the coverage walk uses; no forge needed.
"""
import pytest
import pytest_asyncio
from sqlalchemy import select
from grimoire.models import async_session
from grimoire.models.code_shape import CodeShape
from grimoire.models.project import Project
from grimoire.services import test_guards as tg
from grimoire.services.shape_ledger import classify_shapes, sync_repo_shapes
from tests.helpers import ensure_user
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
REPO = "git.example.com/alice/widget"
CHARGE = ("src/billing.py", "charge")
A = ("tests/test_a.py", "test_charges")
B = ("tests/test_b.py", "test_also_charges")
LONE = ("tests/test_c.py", "test_reads_nothing")
SHAPES = [(p, "sym", s) for p, s in (CHARGE, A, B, LONE)]
def _links(*, a_links: bool = True) -> tg.Links:
shapes = {A: {CHARGE} if a_links else set(), B: {CHARGE}, LONE: set()}
return tg.Links(shapes, {k: set() for k in shapes}, {k: [] for k in shapes}, set())
@pytest_asyncio.fixture
async def seeded():
async with async_session() as s:
owner = await ensure_user(s, "guards_owner")
project = Project(user_id=owner.id, title="Guards target")
s.add(project)
await s.flush()
ids = {"owner": owner.id, "pid": project.id}
await s.commit()
await sync_repo_shapes(ids["pid"], REPO, SHAPES, seen_marker="main")
return ids
async def _rows(pid: int) -> dict[tuple[str, str], CodeShape]:
async with async_session() as s:
rows = (await s.execute(select(CodeShape).where(CodeShape.project_id == pid))).scalars().all()
return {(r.path, r.symbol): r for r in rows}
@pytest.mark.integration
async def test_a_linked_test_case_is_stamped_and_loses_the_stamp_with_its_links(seeded):
pid = seeded["pid"]
await tg.sync_repo_guards(pid, REPO, _links())
rows = await _rows(pid)
assert rows[A].status == "test" and rows[A].classified_by == "mechanical"
# Linked to nothing: still the todo, because nothing says what it is for.
assert rows[LONE].status == "unclassified"
assert await tg.test_summary(pid) == {"linked": 2, "unlinked": 1, "redundant": 0}
await tg.sync_repo_guards(pid, REPO, _links(a_links=False))
assert (await _rows(pid))[A].status == "unclassified"
@pytest.mark.integration
async def test_the_stamp_never_overrides_a_judgment(seeded):
pid, owner = seeded["pid"], seeded["owner"]
await classify_shapes(owner, pid, [{"path": A[0], "symbol": A[1], "status": "exempt",
"reason": "a smoke check kept on purpose"}])
await tg.sync_repo_guards(pid, REPO, _links())
assert (await _rows(pid))[A].status == "exempt"
@pytest.mark.integration
async def test_a_group_leaves_the_readout_once_a_test_is_judged_redundant(seeded):
pid, owner = seeded["pid"], seeded["owner"]
await tg.sync_repo_guards(pid, REPO, _links())
groups = await tg.guard_groups(pid)
assert groups["count"] == 1
assert {(t["path"], t["symbol"]) for t in groups["groups"][0]["tests"]} == {A, B}
await classify_shapes(owner, pid, [{
"path": A[0], "symbol": A[1], "status": "redundant",
"covered_by": {"path": B[0], "symbol": B[1]},
"reason": "test_also_charges makes the same call and checks the total too",
}])
rows = await _rows(pid)
assert rows[A].covered_by_id == rows[B].id
assert (await tg.guard_groups(pid))["count"] == 0
assert (await tg.test_summary(pid))["redundant"] == 1
@pytest.mark.integration
async def test_a_kept_group_leaves_the_readout_until_its_members_change(seeded):
pid, owner = seeded["pid"], seeded["owner"]
await tg.sync_repo_guards(pid, REPO, _links())
rows = await _rows(pid)
await tg.review_group(owner, pid, [rows[A].id, rows[B].id],
reason="one checks the amount, the other the receipt", by="agent")
assert (await tg.guard_groups(pid))["count"] == 0
# A third test joins: a new group, back in front of a reviewer.
links = _links()
links.shapes[LONE] = {CHARGE}
await tg.sync_repo_guards(pid, REPO, links)
assert (await tg.guard_groups(pid))["count"] == 1
@pytest.mark.integration
async def test_a_redundant_verdict_can_name_a_cover_the_sync_has_not_read_yet(seeded):
"""The replacement test lands in the same change as the removal, so its
row does not exist yet: with a bound repo the cover becomes a provisional
row, and the sync that reads it keeps the link. Without repo the verdict
is refused, as a cover nobody can check."""
from grimoire.services import repo_bindings as repo_bindings_svc
pid, owner = seeded["pid"], seeded["owner"]
await repo_bindings_svc.set_binding(owner, REPO, pid)
new = ("tests/test_d.py", "test_charges_and_counts")
verdict = [{"path": A[0], "symbol": A[1], "kind": "sym", "status": "redundant",
"reason": "the rewrite checks the charge and the count",
"covered_by": {"path": new[0], "symbol": new[1]}}]
with pytest.raises(ValueError, match="matches no live shape"):
await classify_shapes(owner, pid, verdict)
out = await classify_shapes(owner, pid, verdict, repo=REPO)
assert out["provisional"] == 1
await sync_repo_shapes(pid, REPO, SHAPES + [(new[0], "sym", new[1])], seen_marker="c2")
rows = await _rows(pid)
assert rows[A].status == "redundant"
assert rows[A].covered_by_id == rows[new].id
assert rows[new].vanished_at is None
@pytest.mark.integration
async def test_the_end_of_turn_question_asks_about_product_code_not_test_cases(seeded):
from grimoire.services.shape_ledger import unjudged_shapes
asked = await unjudged_shapes(seeded["pid"], [
(A[0], "sym", A[1]), (CHARGE[0], "sym", CHARGE[1]),
])
assert [(x["path"], x["symbol"]) for x in asked] == [CHARGE]