"""The server half of the end-of-turn shape check: what the agent is asked at its next prompt, what the hook's ledger may say, and the outcome record.""" import json from unittest.mock import patch import pytest from tests.helpers import make_mock_session def test_the_ledger_lines_parse_and_garbage_is_dropped(): from grimoire.services.shape_check import parse_written raw = ("a.py\tsym\thelper\n" "a.py\tsym\thelper\n" # duplicate "web/A.svelte\tfile\tA\n" "x.py\tbogus\tthing\n" # unknown kind "only-two\tsym\n" # malformed "\tsym\tnameless-path\n") assert parse_written(raw) == [("a.py", "sym", "helper"), ("web/A.svelte", "file", "A")] assert len(parse_written("".join(f"p\tsym\tn{i}\n" for i in range(500)), cap=7)) == 7 def test_a_stop_notes_or_passes_and_the_stop_after_the_note_records_the_answer(): from grimoire.services.shape_check import outcome_for one = [{"path": "a", "kind": "sym", "symbol": "b"}] assert outcome_for("check", one) == "noted" assert outcome_for("check", []) == "passed" assert outcome_for("after", one) == "left_after_note" assert outcome_for("after", []) == "judged_after_note" def test_the_note_names_each_shape_its_evidence_and_the_one_call(): from grimoire.services.shape_check import note_text unjudged = [ {"path": "web/A.svelte", "kind": "file", "symbol": "A", "status": "new", "evidence": {}}, {"path": "src/x.py", "kind": "sym", "symbol": "load", "status": "unclassified", "evidence": {"looks_like": {"snippet_id": 12, "basis": "semantic", "score": 0.84}, "diverges_from": 9}}, ] reason = note_text(unjudged, project_id=3, repo="git.example.com/a/w") assert "last turn wrote 2 definitions" in reason assert "web/A.svelte · A (file) — new" in reason assert "looks like #12 0.84" in reason and "#9 is canon in this directory" in reason assert 'classify_shapes(project_id=3, repo="git.example.com/a/w"' in reason for verdict in ("create_snippet", "`instance`", "`variant`", "`exempt`"): assert verdict in reason, verdict def test_a_long_turn_is_listed_with_a_tail_not_dropped(): from grimoire.services.shape_check import _LISTED, note_text many = [{"path": "p.py", "kind": "sym", "symbol": f"f{i}", "status": "new", "evidence": {}} for i in range(_LISTED + 3)] reason = note_text(many, project_id=1, repo="") assert "… and 3 more" in reason assert "repo=" not in reason async def test_the_outcome_is_recorded_as_a_plugin_event(): from grimoire.services.shape_check import record_shape_check session = make_mock_session() with patch("grimoire.services.shape_check.async_session", return_value=session): await record_shape_check(7, "noted", written=5, unjudged=2, project_id=2) row = session.add.call_args.args[0] assert (row.category, row.action, row.user_id) == ("plugin", "shape_check", 7) assert json.loads(row.details) == {"outcome": "noted", "written": 5, "unjudged": 2, "project_id": 2} async def test_an_unknown_outcome_is_refused_before_anything_is_written(): from grimoire.services.shape_check import record_shape_check session = make_mock_session() with patch("grimoire.services.shape_check.async_session", return_value=session), \ pytest.raises(ValueError): await record_shape_check(7, "skipped", written=1, unjudged=1) session.add.assert_not_called() def test_the_window_counts_turns_asks_and_asks_walked_past(): from grimoire.services.shape_check import summarise assert summarise([]) == {"checked": 0, "asked": 0, "left": 0} got = summarise(["passed", "noted", "judged_after_note", "noted", "left_after_note"]) assert got == {"checked": 3, "asked": 2, "left": 1} # A window that spans the end-of-turn hold this check once was still sums. mixed = summarise(["blocked", "left_after_block", "noted", "judged_after_note"]) assert mixed == {"checked": 2, "asked": 2, "left": 1}