"""Tests for cross-reference rewriting (no network).""" from codeberg_to_gitea_migrator.refs import NumberMap, rewrite def _ref(numbers: dict[int, int], text: str) -> str: nm = NumberMap() for src, tgt in numbers.items(): nm.add(src, tgt) return rewrite( text, nm, source_web_root="https://codeberg.org", target_web_root="https://git.babylondreams.de", ) def test_hash_ref_rewritten_when_mapped() -> None: assert _ref({289: 2}, "Fixes #289 and more") == "Fixes #2 and more" def test_unknown_hash_ref_left_alone() -> None: assert _ref({289: 2}, "see #999 here") == "see #999 here" def test_self_mapping_left_unchanged() -> None: # Numbers that map to themselves aren't rewritten (keeps diffs clean). assert _ref({5: 5}, "ref #5") == "ref #5" def test_markdown_heading_not_matched() -> None: assert _ref({}, "## 2 Section") == "## 2 Section" def test_anchor_not_matched() -> None: # `C#123` — the '#' is glued to a word char, so not a ref. assert _ref({123: 1}, "use C#123 framework") == "use C#123 framework" def test_url_repointed_and_renumbered() -> None: out = _ref({290: 3}, "see https://codeberg.org/AlexKucera/helm/issues/290") assert out == "see https://git.babylondreams.de/AlexKucera/helm/issues/3" def test_url_host_only_when_number_unknown() -> None: out = _ref({}, "see https://codeberg.org/AlexKucera/helm/issues/42") assert out == "see https://git.babylondreams.de/AlexKucera/helm/issues/42" def test_number_map_any_mismatch() -> None: nm = NumberMap() assert nm.any_mismatch is False nm.add(1, 1) assert nm.any_mismatch is False nm.add(2, 5) assert nm.any_mismatch is True assert len(nm) == 2 assert nm.get(2) == 5 assert nm.get(99) is None