Files
FabledGrimoire/tests/test_integration_lesson_situations.py
T
bvandeusenandClaude Opus 5.5 c22e8e1844
CI & Build / Plugin hooks (push) Successful in 13s
CI & Build / Python lint (push) Successful in 2s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Failing after 1m41s
CI & Build / integration (push) Successful in 2m8s
CI & Build / Build & push image (push) Skipped
feat(7836): a lesson arrives at the situation confirmed for it, once per session
Each lesson can carry its situation: a moment of work (work.deliver,
work.finish, ...) or a typed trigger on the act (path, command, tool,
keyword, matched as rule triggers are). A pass proposes, a person judges:

- lesson_situations (migration 0137): one row per lesson, kind and value,
  suggested / confirmed / rejected; `none` records that no situation fits;
- MCP: lessons_to_situate, propose_lesson_situations,
  lesson_situation_proposals, judge_lesson_situations;
- Settings > Moments lists waiting proposals with Apply / Not this /
  Apply all; a lesson page lists its situations and lets a writer add one
  or take one off (kept as rejected, never proposed again).

Delivery: the moment doors (the plugin catch-all hook and MCP replies) add
the lessons whose confirmed moment the act reached or whose trigger it
matches. The plugin door shows each once per session, using the record
ledger, and records it as pushed (source lesson_situation); the tool filter
now includes the tools a lesson trigger listens on.

The edit door no longer finds lessons by meaning: lesson leaves the
work.change default kinds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 06:10:23 -05:00

103 lines
4.8 KiB
Python

"""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")