Files
FabledGrimoire/tests/test_integration_record_kinds.py
T
bvandeusenandClaude Opus 5.5 9cbdfb205c
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 15s
CI & Build / TypeScript typecheck (push) Successful in 34s
CI & Build / Python tests (push) Failing after 1m35s
CI & Build / integration (push) Successful in 1m58s
CI & Build / Build & push image (push) Skipped
feat(7833): each moment declares which kinds of record its search may consider, editable in Settings > Moments
The kind x moment table (services/record_kinds.py):
- every record search fires at a door: the operator message, or a
  catalog moment (work.change for the write path);
- each door considers a set of kinds (snippet, note, lesson, process,
  task:issue/work/spike/plan); a kind left out is never searched there;
- the shipped defaults are today's behaviour; a user's change is stored
  per door, and putting a door back removes it.

Both note arms read it, replacing their fixed kind lists, and a reserved
slot whose kind is turned off is not reserved. The review re-run
searches the same kinds. Settings > Moments shows and edits the table,
with each cell's lines shown and opens from the record ledger.
list_moments returns it as record_kinds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 14:24:53 -05:00

94 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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