Files
FabledGrimoire/tests/test_list_rows_brief.py
bvandeusenandClaude Opus 5.5 0f5ef0be63
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 56s
CI & Build / integration (push) Successful in 1m4s
CI & Build / Python tests (push) Successful in 1m55s
CI & Build / Build & push image (push) Successful in 44s
rename(547): the server is grimoire — package, image, DB defaults, backups
Step 2 of the Scribe -> Grimoire rename.

- src/scribe -> src/grimoire, and every import, patch target, module path,
  hypercorn entry point and `python -m grimoire.restore` with it; pyproject,
  uv.lock and the frontend package are named grimoire.
- Image git.fabledsword.com/bvandeusen/fabledgrimoire; CI's Postgres service
  runs as grimoire / grimoire_test.
- Compose files default the DB user, database and (prod) volume to grimoire,
  each overridable from .env, so an install created as Scribe keeps its data
  by setting POSTGRES_USER, POSTGRES_DB and PGDATA_VOLUME.
- Backups are written as grimoire-<ts>.tar with format grimoire-backup-tar.
  Archives named scribe-* with the scribe-backup-tar manifest still restore
  and are still counted at the destination (folder and S3), with tests.
- Exports, the MCP auth realm, the ASGI scope key and the persistent logger
  carry the new name.

Kept on purpose: the secret-sealing key label stays "scribe.secrets.v1",
because every stored secret was derived under it.

Plugin identity (plugin name, hooks, env vars, .scribe marker, skills) is
step 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 10:50:45 -04:00

94 lines
4.1 KiB
Python

"""List tools return rows that say what a record is, never what it says.
A list that returns every row's to_dict(), body included, runs a project's todo
list to 93-165k characters, past what an MCP client accepts as a tool result,
so the list arrives as a file to page through — the same failure enter_project
avoids by carrying a summary. A list is for choosing which record to open;
get_task / get_note read one in full.
"""
import json
from datetime import datetime, timezone
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
import pytest
from grimoire.mcp.tools.notes import list_notes
from grimoire.mcp.tools.systems import list_system_records
from grimoire.mcp.tools.tasks import list_tasks
from grimoire.services.notes import brief_row
pytestmark = pytest.mark.usefixtures("_bind_user")
STEP_PLAN = "A step plan paragraph long enough to matter. " * 110 # ~5k chars
WHEN = datetime(2026, 9, 15, tzinfo=timezone.utc)
def _row(nid: int, *, is_task: bool = True, milestone_id: int | None = 415) -> SimpleNamespace:
"""A real-shaped Note: every attribute brief_row or to_dict could read."""
return SimpleNamespace(
id=nid, title=f"Step {nid} — something worth doing", body=STEP_PLAN,
description=None, note_type="note", project_id=2, tags=["retrieval"],
updated_at=WHEN, created_at=WHEN, is_task=is_task,
status="todo" if is_task else None, task_kind="work", priority="high",
milestone_id=milestone_id if is_task else None, parent_id=None, due_date=None,
)
def test_a_task_row_names_what_it_is_and_where_it_sits_but_not_its_body():
row = brief_row(_row(7), {415: "An existing plan is found"})
assert "body" not in row
assert row["milestone_title"] == "An existing plan is found"
assert {"id", "title", "status", "task_kind", "priority", "milestone_id",
"tags", "updated_at", "project_id"} <= set(row)
# Empty optional fields are left off rather than sent as null.
assert not {"description", "parent_id", "due_date"} & set(row)
assert row["updated_at"] == WHEN.isoformat()
def test_a_note_row_carries_no_task_fields():
row = brief_row(_row(8, is_task=False))
assert "body" not in row and "status" not in row and "milestone_title" not in row
def test_a_task_outside_any_milestone_says_so_rather_than_inventing_a_title():
row = brief_row(_row(9, milestone_id=None), {})
assert row["milestone_id"] is None and "milestone_title" not in row
@pytest.mark.asyncio
async def test_a_full_page_of_long_tasks_fits_inline():
"""The ceiling. 100 rows (list_tasks' own limit cap) of ~5k-character step
plans: as to_dict() rows this was over half a million characters."""
rows = [_row(i) for i in range(100)]
with patch("grimoire.mcp.tools.tasks.notes_svc.list_notes",
AsyncMock(return_value=(rows, 100))), \
patch("grimoire.mcp.tools.tasks.milestones_svc.titles_for",
AsyncMock(return_value={415: "An existing plan is found"})) as titles:
out = await list_tasks(project_id=2, limit=100)
size = len(json.dumps(out))
assert size < 40_000, f"list_tasks page is {size} characters"
assert titles.await_args.args[0] == {415}
assert out["tasks"][0]["milestone_title"] == "An existing plan is found"
@pytest.mark.asyncio
async def test_list_notes_rows_leave_the_body_to_get_note():
rows = [_row(i, is_task=False) for i in range(3)]
with patch("grimoire.mcp.tools.notes.notes_svc.list_notes",
AsyncMock(return_value=(rows, 3))):
out = await list_notes(project_id=2)
assert all("body" not in r for r in out["notes"])
@pytest.mark.asyncio
async def test_list_system_records_rows_leave_the_body_to_the_getters():
rows = [_row(1), _row(2, is_task=False)]
with patch("grimoire.mcp.tools.systems.systems_svc.list_records_for_system",
AsyncMock(return_value=rows)), \
patch("grimoire.mcp.tools.systems.milestones_svc.titles_for",
AsyncMock(return_value={415: "Plan"})):
out = await list_system_records(system_id=3)
assert [r["id"] for r in out["records"]] == [1, 2]
assert all("body" not in r for r in out["records"])