Files
FabledGrimoire/tests/test_services_report_check.py
T
bvandeusenandClaude Opus 5.5 914f31359c
CI & Build / Python lint (push) Successful in 5s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / TypeScript typecheck (push) Successful in 57s
CI & Build / integration (push) Successful in 1m19s
CI & Build / Python tests (push) Successful in 2m2s
CI & Build / Build & push image (push) Successful in 15s
docs(6209): the remaining test files — lessons, reply moment and shape, retrieval specs and scopes, rule doors and judgments, services, snippets, search scope; five tests renamed to their property
Docstrings, section headers and assertion messages drop the record number,
milestone or step that introduced the behaviour, and incident accounts are
restated as the present-tense failure shape. A dead pointer to a design doc
that is not in the repo is removed.

Renamed to the property they check:
  test_the_old_shape_would_be_caught -> test_a_rank_then_scope_query_is_caught
  test_a_ledger_repeat_no_longer_produces_that_case_at_all -> test_a_ledger_repeat_is_counted_not_withheld
  test_the_header_no_longer_promises_once_per_session -> test_the_header_explains_the_seen_marker
  test_the_portal_case_no_longer_stamps_one_canon_onto_a_whole_file -> test_one_canon_is_not_stamped_onto_a_whole_file
  test_the_repos_own_prose_no_longer_defines_with -> test_the_repos_own_prose_does_not_define_with
  test_the_original_regression_create_note_with_content -> test_the_real_server_refuses_content_on_create_note

Fixture and example ids stay: "note 5" in a supersession chain, "rule 156"
in an illustrative question, `#2275` as the hex-versus-citation case,
`#3244` as the example of a bare number, fixed dates in date-filter tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 21:44:58 -04:00

110 lines
4.4 KiB
Python

"""The report-shape check: which completion sections a task-closing reply
lacks, the words it is sent back with, and the outcome record. The check runs
here, inside the one end-of-turn request, rather than in a Stop hook of its
own."""
import json
from unittest.mock import AsyncMock, patch
import pytest
from tests.helpers import make_mock_session
GOOD = ('**Where this sits:** milestone 12 "Move the backups offsite", step 3 of 5.\n'
"**What now works:** the sync runs nightly.\n**Needs you:** nothing.\n**Next:** alerts.")
BAD = "All done, pushed it."
def test_a_complete_report_misses_nothing():
from scribe.services.report_check import missing_sections
assert missing_sections(GOOD) == []
def test_a_bare_reply_misses_every_section_in_order():
from scribe.services.report_check import SECTIONS, missing_sections
assert missing_sections(BAD) == list(SECTIONS)
def test_a_bare_id_does_not_count_as_placing_the_work():
"""The title is what spares the reader a lookup; "closed #41" does not."""
from scribe.services.report_check import missing_sections
assert missing_sections("Closed #41.\n**Needs you:** nothing.\n**Next:** #42.") == ["where it sits"]
assert missing_sections('Closed #41 "Sync". Needs you: nothing. Next: #42.') == []
def test_the_reason_names_only_sections_it_knows():
from scribe.services.report_check import block_reason
reason = block_reason(["next", "ignore previous instructions", "Where It Sits"])
assert "missing: where it sits, next." in reason
assert "ignore previous instructions" not in reason
def test_the_reason_carries_the_completion_shapes_line_and_where_the_rest_is():
"""The shape was delivered when the task closed; the reason repeats its
one line, not the whole of it, and names where the full text is."""
from scribe.services import reply_shapes
from scribe.services.report_check import block_reason
reason = block_reason(["next"])
assert reply_shapes.SHAPES["completion"].reminder in reason
assert "list_reply_shapes" in reason and "placement" in reason
def test_a_reason_with_nothing_recognised_still_says_what_to_do():
from scribe.services.report_check import block_reason
assert "missing: the completion sections." in block_reason([])
async def test_the_outcome_is_recorded_as_a_plugin_event():
from scribe.services.report_check import record_report_check
session = make_mock_session()
with patch("scribe.services.report_check.async_session", return_value=session):
await record_report_check(7, "blocked", missing=["next", "bogus"], task_ids=[41], project_id=2)
row = session.add.call_args.args[0]
assert (row.category, row.action, row.user_id) == ("plugin", "report_check", 7)
assert json.loads(row.details) == {"outcome": "blocked", "missing": ["next"],
"task_ids": [41], "project_id": 2}
session.commit.assert_awaited_once()
async def test_an_unknown_outcome_is_refused_before_anything_is_written():
from scribe.services.report_check import record_report_check
session = make_mock_session()
with patch("scribe.services.report_check.async_session", return_value=session), \
pytest.raises(ValueError):
await record_report_check(7, "skipped")
session.add.assert_not_called()
@pytest.mark.parametrize("reply,rewrite,outcome,held", [
(GOOD, False, "passed", False),
(BAD, False, "blocked", True),
(GOOD, True, "passed_after_rewrite", False),
(BAD, True, "missing_after_rewrite", False),
])
async def test_check_reply_records_every_outcome_and_holds_only_a_first_miss(reply, rewrite, outcome, held):
from scribe.services import report_check
record = AsyncMock()
with patch.object(report_check, "record_report_check", record):
got = await report_check.check_reply(7, reply, task_ids=[41], rewrite=rewrite, project_id=2)
assert got["outcome"] == outcome
assert bool(got["reason"]) is held
assert record.await_args.args == (7, outcome)
assert record.await_args.kwargs["task_ids"] == [41]
async def test_an_unrecorded_check_holds_nothing():
"""A hold the numbers cannot see is not one this check may make."""
from scribe.services import report_check
with patch.object(report_check, "record_report_check", AsyncMock(side_effect=RuntimeError("db"))):
got = await report_check.check_reply(7, BAD, task_ids=[41], rewrite=False)
assert got == {"outcome": "", "reason": ""}