"""Real-Postgres tests for the kind×moment table (`record_kinds`): the shipped defaults, a user's change and its reset, the refusals, the SQL clause matching exactly the kinds named, and each cell's evidence from the ledger.""" from datetime import datetime, timezone import pytest from sqlalchemy import delete, select from grimoire.models import async_session from grimoire.models.note import Note from grimoire.models.record_delivery import RecordDelivery from grimoire.models.setting import Setting from grimoire.services import record_kinds from tests.helpers import ensure_user pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")] async def _user(name: str = "record_kinds_owner") -> int: async with async_session() as s: uid = (await ensure_user(s, name)).id await s.execute(delete(Setting).where( Setting.user_id == uid, Setting.key == record_kinds.SETTING_KEY)) await s.execute(delete(RecordDelivery).where(RecordDelivery.user_id == uid)) await s.commit() return uid async def test_a_door_follows_its_default_until_the_user_changes_it(): uid = await _user() assert await record_kinds.kinds_for(uid, "work.change") == record_kinds.DOORS["work.change"].default await record_kinds.set_kinds(uid, "work.change", ["snippet", "task:issue"]) assert await record_kinds.kinds_for(uid, "work.change") == {"snippet", "task:issue"} # Another door is untouched. assert await record_kinds.kinds_for(uid, "message") == frozenset(record_kinds.KINDS) table = await record_kinds.set_kinds(uid, "work.change", None) row = next(d for d in table["doors"] if d["door"] == "work.change") assert row["changed"] is False and row["kinds"] == row["default"] async def test_setting_the_default_stores_no_change(): uid = await _user() default = sorted(record_kinds.DOORS["work.change"].default) table = await record_kinds.set_kinds(uid, "work.change", default) assert not any(d["changed"] for d in table["doors"]) async def test_an_unknown_door_or_kind_is_refused_and_nothing_is_stored(): uid = await _user() with pytest.raises(ValueError, match="unknown door"): await record_kinds.set_kinds(uid, "work.nowhere", ["snippet"]) with pytest.raises(ValueError, match="unknown kind"): await record_kinds.set_kinds(uid, "message", ["snippet", "rule"]) assert await record_kinds.kinds_for(uid, "message") == frozenset(record_kinds.KINDS) async def test_the_clause_matches_exactly_the_kinds_named(): uid = await _user() async with async_session() as s: made = { "snippet": Note(user_id=uid, title="s", note_type="snippet"), "note": Note(user_id=uid, title="n", note_type="note"), "task:issue": Note(user_id=uid, title="i", status="todo", task_kind="issue"), "task:work": Note(user_id=uid, title="w", status="todo", task_kind="work"), } s.add_all(made.values()) await s.commit() ids = {k: n.id for k, n in made.items()} for kinds in (["snippet", "task:issue"], ["note"], ["task:work"], []): got = set((await s.execute( select(Note.id).where(Note.id.in_(ids.values()), record_kinds.kinds_clause(kinds)) )).scalars()) assert got == {ids[k] for k in kinds}, kinds async def test_each_cell_carries_the_lines_its_door_pushed_and_the_opens(): uid = await _user() async with async_session() as s: s.add_all([ RecordDelivery(user_id=uid, session_id="s", note_id=1, kind="snippet", way="pushed", source="write_path_semantic", plugin="p", opened_at=datetime.now(timezone.utc)), RecordDelivery(user_id=uid, session_id="s", note_id=2, kind="snippet", way="pushed", source="write_path_semantic", plugin="p"), RecordDelivery(user_id=uid, session_id="s", note_id=3, kind="task:issue", way="pushed", source="auto_inject", plugin="p"), ]) await s.commit() table = await record_kinds.table(uid, days=7) cells = {d["door"]: d["evidence"] for d in table["doors"]} assert cells["work.change"] == {"snippet": {"delivered": 2, "opened": 1}} assert cells["message"] == {"task:issue": {"delivered": 1, "opened": 0}} assert table["evidence_failed"] is False