feat(8247): a test case is accounted for by what it guards, and the coverage line counts the suite by it
CI & Build / TypeScript typecheck (push) Successful in 37s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Python tests (push) Successful in 2m24s
CI & Build / integration (push) Successful in 2m22s
CI & Build / Build & push image (push) Successful in 43s

A refresh now stamps a test case `test` when it is linked to something it
guards, and withdraws the stamp when its links go; a judgment always wins. A
test case linked to nothing stays in the todo, because nothing says what it is
for. Specs written as it()/test() calls get one ledger row per file, read
whole. The coverage line and card count tests linked, guarding nothing found,
and redundant, and the end-of-turn question no longer asks the writer to call
a test case a one-off.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 21:57:57 -05:00
co-authored by Claude Opus 5.5
parent ebd9d8fbb8
commit dc9e88aa6d
10 changed files with 288 additions and 11 deletions
+119
View File
@@ -0,0 +1,119 @@
"""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_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]
+1 -1
View File
@@ -483,7 +483,7 @@ async def test_coverage_measures_the_tree_exactly_and_caches(seeded):
assert coverage["unclassified"] == 2
assert coverage["counts"] == {
"canonical": 2, "instance": 0, "variant": 0, "exempt": 0,
"redundant": 0, "unused": 0, "scoped": 0,
"redundant": 0, "unused": 0, "scoped": 0, "test": 0,
}
assert coverage["estimate"] is True
assert coverage["repos"] == [{
+1 -1
View File
@@ -30,7 +30,7 @@ def test_the_todo_state_is_the_default():
assert "unclassified" in SHAPE_STATUSES
assert set(SHAPE_STATUSES) == {
"canonical", "instance", "variant", "exempt", "redundant", "unused",
"scoped", "unclassified",
"scoped", "test", "unclassified",
}
assert set(SHAPE_CLASSIFIERS) == {
"agent", "audit", "hook", "mechanical", "import", "person",
+28
View File
@@ -169,3 +169,31 @@ def test_the_coverage_line_names_the_removal_verdicts_and_the_test_groups():
})
assert "2 redundant" in line and "1 unused" in line
assert "3 test groups guarding the same thing" in line
def test_a_test_case_is_a_test_named_definition_or_a_call_style_spec_file():
assert tg.is_test_case("tests/test_a.py", "test_x", "sym")
assert not tg.is_test_case("tests/test_a.py", "_helper", "sym")
assert not tg.is_test_case("src/app/billing.py", "test_mode", "sym")
assert tg.is_test_case("web/src/a.spec.ts", "a.spec", "file")
def test_a_spec_written_as_calls_is_one_test_and_links_by_its_imports():
text = "import { charge } from '../app/billing'\n\nit('charges', () => {\n expect(charge(1)).toBe(1)\n})\n"
assert tg.is_call_style_spec("web/src/billing.spec.ts", text, [])
# A file that defines test-named functions is read per function instead.
assert not tg.is_call_style_spec("tests/test_a.py", "def test_x():\n pass\n", ["test_x"])
spec = tg.Def("web/src/billing.spec.ts", "billing.spec", text)
product = tg.Def("web/app/billing.ts", "charge", "export function charge(n) {\n return n\n}\n")
links = tg.link_tests([product], {spec.path: tg.read_test_file(text)}, file_tests=[spec])
assert links.shapes[(spec.path, "billing.spec")] == {("web/app/billing.ts", "charge")}
def test_the_coverage_line_counts_tests_by_what_they_guard():
line = coverage_line({
"accounted": 10, "total": 12, "unclassified": 2, "counts": {"test": 8},
"tests": {"linked": 8, "unlinked": 2, "redundant": 1},
"computed_at": "2026-12-01T00:00:00+00:00",
})
assert "8 test" in line
assert "tests: 8 linked · 2 guarding nothing found · 1 redundant" in line