Files
FabledGrimoire/tests/test_lesson_claim.py
T
bvandeusenandClaude Opus 5.5 914f31359c
CI & Build / Python lint (push) Successful in 5s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / TypeScript typecheck (push) Successful in 57s
CI & Build / integration (push) Successful in 1m19s
CI & Build / Python tests (push) Successful in 2m2s
CI & Build / Build & push image (push) Successful in 15s
docs(6209): the remaining test files — lessons, reply moment and shape, retrieval specs and scopes, rule doors and judgments, services, snippets, search scope; five tests renamed to their property
Docstrings, section headers and assertion messages drop the record number,
milestone or step that introduced the behaviour, and incident accounts are
restated as the present-tense failure shape. A dead pointer to a design doc
that is not in the repo is removed.

Renamed to the property they check:
  test_the_old_shape_would_be_caught -> test_a_rank_then_scope_query_is_caught
  test_a_ledger_repeat_no_longer_produces_that_case_at_all -> test_a_ledger_repeat_is_counted_not_withheld
  test_the_header_no_longer_promises_once_per_session -> test_the_header_explains_the_seen_marker
  test_the_portal_case_no_longer_stamps_one_canon_onto_a_whole_file -> test_one_canon_is_not_stamped_onto_a_whole_file
  test_the_repos_own_prose_no_longer_defines_with -> test_the_repos_own_prose_does_not_define_with
  test_the_original_regression_create_note_with_content -> test_the_real_server_refuses_content_on_create_note

Fixture and example ids stay: "note 5" in a supersession chain, "rule 156"
in an illustrative question, `#2275` as the hex-versus-citation case,
`#3244` as the example of a bare number, fixed dates in date-filter tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 21:44:58 -04:00

126 lines
5.0 KiB
Python

"""A lesson's name is one claim on one line.
`what` is the title every listing and menu prints. Written as the incident
instead of the claim, it turns a menu line into a paragraph with the claim
somewhere in the middle. Two halves are pinned: the doors refuse a new
one, and the menu shows one line for a name stored before they did.
"""
from __future__ import annotations
import inspect
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
import pytest
from scribe.mcp._context import _user_id_ctx
from scribe.mcp.tools.lessons import create_lesson, update_lesson
from scribe.services import lessons as lessons_svc
from scribe.services.lessons import WHAT_MAX_CHARS, claim_line, require_claim
TRIGGER = "an existing call is about to double as a liveness signal"
CLAIM = ("A check-in piggybacked on an existing call inherits every backoff "
"that call is ever given")
STORY = (
"The operator reported their agent showing offline while it ran. "
+ "The lease poll backed off to 900s while the roster called 300s stopped. " * 6
).strip()
# ── The bound ──────────────────────────────────────────────────────────────
def test_a_one_line_claim_passes():
require_claim(CLAIM)
require_claim("x" * WHAT_MAX_CHARS)
@pytest.mark.parametrize("what", [
"x" * (WHAT_MAX_CHARS + 1),
"A claim.\n\nAnd then the story of how it was learned.",
], ids=["over the bound", "several lines"])
def test_a_story_in_the_name_is_refused_with_where_it_goes(what):
with pytest.raises(ValueError) as err:
require_claim(what)
message = str(err.value)
assert "Nothing was written" in message
assert "insight" in message, "the refusal must say where the story belongs"
# ── The doors ──────────────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_the_mcp_create_refuses_before_writing():
_user_id_ctx.set(7)
created = AsyncMock()
with patch.object(lessons_svc, "create_lesson", created):
with pytest.raises(ValueError):
await create_lesson(what=STORY, when_to_apply=TRIGGER)
created.assert_not_called()
@pytest.mark.asyncio
async def test_the_mcp_update_refuses_a_new_long_name():
_user_id_ctx.set(7)
updated = AsyncMock()
with patch.object(lessons_svc, "update_lesson", updated):
with pytest.raises(ValueError):
await update_lesson(lesson_id=1, what=STORY)
updated.assert_not_called()
@pytest.mark.asyncio
async def test_a_lesson_stored_with_a_long_name_can_still_be_edited():
"""Only a NEW name is checked. Refusing every edit of a lesson stored
before the bound would also refuse the edit that repairs it."""
_user_id_ctx.set(7)
note = SimpleNamespace(
id=1, title=STORY, body="b", tags=[], project_id=None,
note_type="lesson", data={"what": STORY}, arose_from_id=None,
created_at=None, updated_at=None,
)
updated = AsyncMock(return_value=note)
with patch.object(lessons_svc, "update_lesson", updated), \
patch("scribe.mcp.tools.lessons.systems_tools.attach_systems", AsyncMock()), \
patch("scribe.mcp.tools.lessons.lesson_rules_svc.attach_lesson_rules", AsyncMock()):
await update_lesson(lesson_id=1, when_to_apply=TRIGGER)
assert updated.await_args.kwargs["what"] is None
@pytest.mark.parametrize("handler", ["create_lesson_route", "update_lesson_route"])
def test_the_rest_door_refuses_too(handler):
"""The web form writes lessons as well, and a bound one door enforces is
a bound the other door walks around."""
from scribe.routes import lessons as routes
src = inspect.getsource(getattr(routes, handler))
write = "lessons_svc." + handler.removesuffix("_route") + "("
assert "require_claim(" in src
assert src.index("require_claim(") < src.index(write), \
"the check runs after the write it exists to prevent"
# ── The display ────────────────────────────────────────────────────────────
def test_a_short_name_is_shown_as_it_is():
assert claim_line(CLAIM) == CLAIM
def test_a_stored_story_shows_as_its_first_sentence_marked_cut():
line = claim_line(STORY)
assert line == "The operator reported their agent showing offline while it ran. …"
assert "\n" not in line
def test_a_first_sentence_past_the_bound_is_cut_at_a_word():
line = claim_line("word " * 100)
assert line.endswith(" …")
assert len(line) <= WHAT_MAX_CHARS + 2
def test_the_menu_names_a_long_lesson_in_one_line():
from scribe.services.plugin_context import _menu_name
name = _menu_name(STORY, "lesson", {"what": STORY, "when_to_apply": TRIGGER}, "")
assert name.endswith(" …")
assert len(name) <= WHAT_MAX_CHARS + 2