"""Real-Postgres tests for `lesson_situations`: a pass proposes when a lesson applies, a person confirms or rejects it, and only a confirmed situation delivers its lesson — at the moment it names, or on an act its trigger matches, to a reader who may read the lesson.""" import uuid import pytest from grimoire.models import async_session from grimoire.models.note import Note from grimoire.services import lesson_situations as svc from tests.helpers import ensure_user pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")] async def _world() -> dict: tag = uuid.uuid4().hex[:8] async with async_session() as s: owner = await ensure_user(s, f"situations_owner_{tag}") stranger = await ensure_user(s, f"situations_stranger_{tag}") made = { key: Note(user_id=owner.id, note_type="lesson", title=title, body="") for key, title in ( ("deploy", "A green build is not a running deploy"), ("migrate", "Migrations need their constraint in the same change"), ("plain", "Name things for a stranger"), ) } s.add_all(made.values()) await s.commit() return {"owner": owner.id, "stranger": stranger.id, **{k: int(n.id) for k, n in made.items()}} async def test_a_proposal_waits_and_delivers_only_once_confirmed(): w = await _world() out = await svc.propose(w["owner"], [ {"lesson_id": w["deploy"], "moments": ["work.deliver"], "why": "about shipping"}, {"lesson_id": w["migrate"], "triggers": [{"kind": "path", "pattern": "alembic/versions/*.py"}], "why": "about writing migrations"}, {"lesson_id": w["plain"], "why": "about naming, whatever the act"}, {"lesson_id": w["plain"], "moments": ["not.a.moment"]}, ]) assert out["written"] == 3 and len(out["refused"]) == 1 # Suggested rows deliver nothing. assert await svc.for_act(w["owner"], ["work.deliver"]) == [] waiting = await svc.pending(w["owner"]) ids = {p["lesson_id"]: p["proposals"][0]["id"] for p in waiting["lessons"]} assert set(ids) == {w["deploy"], w["migrate"]}, "a `none` answer waits on nobody" await svc.judge(w["owner"], [{"id": ids[w["deploy"]], "verdict": "confirm"}, {"id": ids[w["migrate"]], "verdict": "confirm"}]) at_deliver = await svc.for_act(w["owner"], ["work.deliver"]) assert [a.lesson_id for a in at_deliver] == [w["deploy"]] assert "moment `work.deliver`" in svc.line(at_deliver[0]) on_migration = await svc.for_act( w["owner"], ["work.change"], tool="Edit", tool_input={"file_path": "/repo/alembic/versions/0001_init.py"}, ) assert [a.lesson_id for a in on_migration] == [w["migrate"]] assert await svc.for_act(w["owner"], ["work.change"], tool="Edit", tool_input={"file_path": "/repo/src/app.py"}) == [] # Another user's lesson never reaches them. assert await svc.for_act(w["stranger"], ["work.deliver"]) == [] # The session already has it: left out. assert await svc.for_act(w["owner"], ["work.deliver"], exclude=frozenset({w["deploy"]})) == [] async def test_a_rejected_value_is_never_proposed_again_and_the_lesson_returns_to_the_queue(): w = await _world() await svc.propose(w["owner"], [{"lesson_id": w["deploy"], "moments": ["work.finish"]}]) pid = (await svc.pending(w["owner"]))["lessons"][0]["proposals"][0]["id"] await svc.judge(w["owner"], [{"id": pid, "verdict": "reject", "note": "too broad"}]) queue = await svc.to_situate(w["owner"], limit=100) entry = next(item for item in queue["lessons"] if item["id"] == w["deploy"]) assert entry["rejected"] == [{"describes": "moment `work.finish`", "why": "too broad"}] again = await svc.propose(w["owner"], [{"lesson_id": w["deploy"], "moments": ["work.finish"]}]) assert again == {"written": 0, "skipped": 1, "refused": []} async def test_a_person_adds_and_takes_off_a_situation_and_the_counts_follow(): w = await _world() await svc.propose(w["owner"], [{"lesson_id": w["plain"], "why": "none fits"}]) added = await svc.add(w["owner"], w["plain"], trigger={"kind": "command", "pattern": "git push"}) assert added["state"] == "confirmed" and added["source"] == "edit" live = await svc.for_lesson(w["owner"], w["plain"]) assert [s["describes"] for s in live] == ["command `git push`"] moments, tools = await svc.reachable(w["owner"]) assert "bash" in tools counts = await svc.counts(w["owner"]) assert counts["situated"] == 1 and counts["none_fits"] == 0, "adding one clears `none`" assert counts["unsituated"] == 2 await svc.remove(w["owner"], added["id"]) assert await svc.for_lesson(w["owner"], w["plain"]) == [] with pytest.raises(ValueError): await svc.add(w["stranger"], w["plain"], moment="work.deliver")