Files
FabledGrimoire/tests/test_integration_record_kinds.py
T
bvandeusenandClaude Opus 5.5 4c69f9c163
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Failing after 12s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Failing after 1m39s
CI & Build / integration (push) Successful in 2m17s
CI & Build / Build & push image (push) Skipped
feat(7832): records reach an edit and a message by structure first; resemblance offers notes
The kind×moment table's first measured defaults (baseline note #7892):

- The edit door splits in two rows. work.change is what is recorded where
  the edit is (snippets beside the file, issues naming it or open in its
  area); work.change.similar is what the code resembles, notes by default.
  Issues and snippets no longer reach an edit by resemblance.
- Snippets recorded beside the file are named by id in an "Also recorded
  beside this file" pointer line instead of taking menu lines.
- The message door leaves lessons and issues out by default: a lesson
  arrives at its confirmed situation, an issue through its area or its id.
  Its lesson slot runs only for a user who turns lessons back on.
- The static context teaches the new pointer line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 12:17:29 -05:00

97 lines
4.4 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"])
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