Files
FabledGrimoire/tests/test_record_ref_links.py
T
bvandeusenandClaude Opus 5.5 08fdabd5e9
CI & Build / Plugin hooks (push) Successful in 13s
CI & Build / Python lint (push) Successful in 2s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 1m50s
CI & Build / integration (push) Successful in 1m58s
CI & Build / Build & push image (push) Successful in 43s
test(7736): the range case no longer puts "steps" before the first id
"step" is in the other-numbering word list ("step #3" is a position in
a plan), so "steps #5110" correctly stays plain. The case now tests the
range alone, and a second case pins the "step #3" reading.

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

109 lines
4.1 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.
"""What a body cites, and that the web UI and the backend read it the same way.
A `#N` in prose names a note, task, lesson or snippet; `milestone N` and
`rule N` name records in their own id spaces. The backend reads these for
backlinks and graph edges (services/record_refs.referenced); the web UI links
them (frontend/src/utils/recordRefs.ts). A token made only of digits is never a
tag, on either side.
There is no frontend test runner, so the TS half is held to the Python half by
reading its source: the two patterns and the word list must be the same text.
"""
from __future__ import annotations
import re
from pathlib import Path
import pytest
from grimoire.services import record_refs
from grimoire.services.record_refs import referenced
from grimoire.utils.tags import extract_tags
FRONTEND = Path(__file__).resolve().parents[1] / "frontend" / "src"
REFS_TS = FRONTEND / "utils" / "recordRefs.ts"
TAGS_TS = FRONTEND / "utils" / "tags.ts"
def _refs(notes=(), milestones=(), rules=()):
return {"notes": set(notes), "milestones": set(milestones), "rules": set(rules)}
@pytest.mark.parametrize(("text", "expected"), [
("see #123", _refs(notes={123})),
("shipped #5110–#5119", _refs(notes={5110, 5119})),
# "step" names a position in a plan ("step #3"), the same reading the
# message parser uses, so a #N right after it stays plain.
("step #3 of the plan", _refs()),
("(#4286) and #4290.", _refs(notes={4286, 4290})),
("#v2 is a tag", _refs()),
("a colour #123abc", _refs()),
("inline `#12` is code", _refs()),
("```\nlog line #77\n```\nafter #78", _refs(notes={78})),
("PR #198 and run #4", _refs()),
("rule #34 and rule 10", _refs(rules={34, 10})),
("preference 12 holds", _refs(rules={12})),
("part of milestone 639", _refs(milestones={639})),
('Part of milestone 7 "Ship it"; next is #9 "Step two".', _refs(notes={9}, milestones={7})),
("an entity &#123; and a path a/#9", _refs()),
("", _refs()),
])
def test_referenced_reads_what_the_ui_links(text, expected):
assert referenced(text) == expected
def test_referenced_ignores_none_among_texts():
assert referenced(None, "#5", None) == _refs(notes={5})
@pytest.mark.parametrize(("body", "tags"), [
("#123", []),
("cites (#4286)", []),
("#5110–#5119", []),
("#v2 #123abc #2fa", ["123abc", "2fa", "v2"]),
("#123-notes and #12/x", ["12/x", "123-notes"]),
("#python #review", ["python", "review"]),
])
def test_a_token_of_digits_alone_is_never_a_tag(body, tags):
assert extract_tags(body) == tags
# ── The TS reader is the same text as the Python one ───────────────────────
def _ts_regex(src: str, name: str) -> str:
m = re.search(rf"export const {name} = /(.+?)/[gimsuy]*;", src)
assert m, f"{name} is no longer a regex literal in {REFS_TS.name}"
return m.group(1)
def test_the_ui_reads_note_references_with_the_backend_pattern():
src = REFS_TS.read_text()
assert _ts_regex(src, "NOTE_REF_RE") == record_refs.REF_RE.pattern
def test_the_ui_reads_milestone_and_rule_references_with_the_backend_pattern():
src = REFS_TS.read_text()
assert _ts_regex(src, "PROSE_REF_RE") == record_refs.PROSE_REF_RE.pattern
# Python compiles it IGNORECASE; the literal must carry `i` too.
assert re.search(r"export const PROSE_REF_RE = /.+?/[gmsuy]*i[gmsuy]*;", src)
assert record_refs.PROSE_REF_RE.flags & re.IGNORECASE
def test_the_ui_skips_the_same_other_numberings():
src = REFS_TS.read_text()
m = re.search(r"export const OTHER_SPACE: readonly string\[\] = \[(.*?)\];", src, re.S)
assert m, f"OTHER_SPACE is no longer a string-array literal in {REFS_TS.name}"
words = set(re.findall(r'"([a-z]+)"', m.group(1)))
assert words == set(record_refs._OTHER_SPACE)
def test_both_tag_patterns_leave_digits_alone_to_the_reference_reader():
from grimoire.utils import tags as tags_py
guard = r"#(?!\d+(?![\w/-]))"
assert guard in tags_py._TAG_RE.pattern
ts = TAGS_TS.read_text()
m = re.search(r"export const TAG_RE = /(.+?)/g;", ts)
assert m, f"TAG_RE is no longer a regex literal in {TAGS_TS.name}"
assert guard in m.group(1)