"""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.in_([record_kinds.SETTING_KEY, record_kinds.REACH_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"]) assert await record_kinds.kinds_for(uid, "work.change") == {"snippet"} # Another door is untouched. assert (await record_kinds.kinds_for(uid, "message") == record_kinds.DOORS["message"].default) 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") == record_kinds.DOORS["message"].default 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"]} # The search by resemblance on an edit has its own row. assert cells["work.change.similar"] == {"snippet": {"delivered": 2, "opened": 1}} assert cells["work.change"] == {} assert cells["message"] == {"task:issue": {"delivered": 1, "opened": 0}} assert table["evidence_failed"] is False async def _reach_rows(uid: int) -> list[str]: async with async_session() as s: return list((await s.execute(select(Setting.value).where( Setting.user_id == uid, Setting.key == record_kinds.REACH_SETTING_KEY, ))).scalars()) async def test_the_reach_follows_its_default_until_the_user_changes_it(): uid = await _user("record_reach_owner") assert await record_kinds.reach_for(uid) == record_kinds.REACH_DEFAULT # Knowledge crosses by default; no task kind does. assert not any(k.startswith(record_kinds.TASK_PREFIX) for k in record_kinds.REACH_DEFAULT) table = await record_kinds.set_reach(uid, ["snippet", "task:issue"]) assert table["reach"] == { "kinds": ["snippet", "task:issue"], "default": sorted(record_kinds.REACH_DEFAULT, key=record_kinds.KINDS.index), "changed": True, } assert await record_kinds.reach_for(uid) == {"snippet", "task:issue"} # Nothing crossing is a real setting, not a reset. await record_kinds.set_reach(uid, []) assert await record_kinds.reach_for(uid) == frozenset() table = await record_kinds.set_reach(uid, None) assert table["reach"]["changed"] is False assert await _reach_rows(uid) == [] async def test_a_reach_equal_to_the_default_stores_no_change(): uid = await _user("record_reach_owner") await record_kinds.set_reach(uid, sorted(record_kinds.REACH_DEFAULT)) assert await _reach_rows(uid) == [] async def test_an_unknown_kind_in_the_reach_is_refused_and_nothing_is_stored(): uid = await _user("record_reach_owner") with pytest.raises(ValueError, match="unknown kind"): await record_kinds.set_reach(uid, ["snippet", "dossier"]) assert await _reach_rows(uid) == []