Files
FabledGrimoire/tests/test_services_record_refs.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

93 lines
3.4 KiB
Python

"""Guessed record ids are refused; `{{ref:N}}` placeholders resolve.
The failure: a session predicts the ids its next creates will get and writes
them into a body before the records exist; any other session's create takes
those numbers. These pin the recogniser — what counts as a `#N`, where a guess
can sit relative to the highest id — and the placeholder rewrite the batch
create uses instead of guessing.
"""
from unittest.mock import AsyncMock, patch
import pytest
from scribe.services import record_refs
from scribe.services.record_refs import (
GUESS_WINDOW,
cited_ids,
placeholder_keys,
resolve_placeholders,
)
@pytest.mark.parametrize(("text", "expected"), [
("see #12 and (#34)", {12, 34}),
("#7 at the start", {7}),
("an entity &#123; is not a reference", set()),
("a fragment page#12 is not one", set()),
("a colour #123abc is not one", set()),
("path/#9 is not one", set()),
("", set()),
])
def test_cited_ids_reads_standalone_hash_numbers_only(text, expected):
assert cited_ids(text) == expected
def test_cited_ids_ignores_none_among_texts():
assert cited_ids(None, "#5", None) == {5}
async def test_a_number_just_above_the_highest_id_is_a_guess():
with patch.object(record_refs, "_max_note_id", AsyncMock(return_value=100)):
assert await record_refs.guessed_ids("next is #101, then #102") == [101, 102]
async def test_existing_ids_and_far_numbers_are_not_guesses():
"""Below the max is a real record; far above it is not a Scribe id at all
(a PR, a forge issue) — neither may be refused."""
far = 100 + GUESS_WINDOW + 1
with patch.object(record_refs, "_max_note_id", AsyncMock(return_value=100)):
assert await record_refs.guessed_ids(f"#1 #100 #{far}") == []
async def test_the_window_edge_is_still_a_guess():
edge = 100 + GUESS_WINDOW
with patch.object(record_refs, "_max_note_id", AsyncMock(return_value=100)):
assert await record_refs.guessed_ids(f"#{edge}") == [edge]
async def test_text_without_references_never_queries():
"""Most writes cite nothing; they must not pay a query for it."""
probe = AsyncMock(return_value=100)
with patch.object(record_refs, "_max_note_id", probe):
await record_refs.refuse_guessed_ids("no numbers here", None, "")
probe.assert_not_awaited()
async def test_refusal_names_the_guess_and_the_fix():
with patch.object(record_refs, "_max_note_id", AsyncMock(return_value=100)):
with pytest.raises(ValueError) as exc:
await record_refs.refuse_guessed_ids("steps #101 and #103")
message = str(exc.value)
assert "#101, #103" in message
assert "create_records" in message and "{{ref:N}}" in message
def test_placeholder_keys_tolerates_spacing():
assert placeholder_keys("{{ref:1}} {{ ref: 2 }} {{ref:milestone}}") == {"1", "2", "milestone"}
def test_resolve_placeholders_rewrites_every_occurrence():
refs = {"1": '#40 "first"', "milestone": 'milestone 9 "plan"'}
out = resolve_placeholders("{{ref:1}} then {{ref:1}} in {{ref:milestone}}", refs)
assert out == '#40 "first" then #40 "first" in milestone 9 "plan"'
def test_resolve_placeholders_passes_empty_through():
assert resolve_placeholders(None, {}) is None
assert resolve_placeholders("", {}) == ""
def test_an_unknown_key_is_a_caller_bug_not_a_silent_leftover():
with pytest.raises(KeyError):
resolve_placeholders("{{ref:3}}", {"1": "#1"})