"""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})), ("steps #5110–#5119 shipped", _refs(notes={5110, 5119})), ("(#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 { 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)