muse-agent-example/tests/architecture/test_markdown_links.py

49 lines
1.7 KiB
Python

"""活入口、SoT 与框架文档的 Markdown 链接死链门。"""
from __future__ import annotations
import pathlib
import re
import unittest
import urllib.parse
ROOT = next(
parent
for parent in (pathlib.Path(__file__).resolve().parent, *pathlib.Path(__file__).resolve().parents)
if (parent / "AGENTS.md").is_file() and (parent / ".git").exists()
)
LINK = re.compile(r"\[[^\]\n]*\]\(([^)\s]+)")
class MarkdownLinkTest(unittest.TestCase):
def test_active_markdown_links_resolve(self) -> None:
roots = [
ROOT / "AGENTS.md",
ROOT / "README.md",
ROOT / ".agent",
ROOT / "framework",
ROOT / "muse",
ROOT / "docs",
]
missing: list[str] = []
for root in roots:
files = [root] if root.is_file() else list(root.rglob("*.md"))
for source in files:
relative = source.relative_to(ROOT).as_posix()
if relative.startswith("docs/plans/") or "/artifacts/" in relative:
continue
if relative.startswith("framework/catalog/"):
continue
for raw_target in LINK.findall(source.read_text(encoding="utf-8")):
target = urllib.parse.unquote(raw_target.strip("<>").split("#", 1)[0])
if not target or target.startswith(("#", "http://", "https://", "mailto:", "/")):
continue
if not (source.parent / target).resolve().exists():
missing.append(f"{relative}: {raw_target}")
self.assertEqual(missing, [], "Markdown 链接目标不存在:\n" + "\n".join(missing))
if __name__ == "__main__":
unittest.main()