81 lines
3.1 KiB
Python
81 lines
3.1 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]+)")
|
|
STANDARD_INDEX_HEADER = (
|
|
"| 名称 | 相对地址 | 内容描述 | 使用场景 | 使用要求 |\n"
|
|
"|------|----------|----------|----------|----------|"
|
|
)
|
|
|
|
|
|
class MarkdownLinkTest(unittest.TestCase):
|
|
def test_agent_content_directories_have_standard_indexes(self) -> None:
|
|
agent_root = ROOT / ".agent"
|
|
root_index = (agent_root / "目录.md").read_text(encoding="utf-8")
|
|
missing: list[str] = []
|
|
invalid_headers: list[str] = []
|
|
undiscoverable: list[str] = []
|
|
|
|
for directory in sorted(path for path in agent_root.iterdir() if path.is_dir()):
|
|
index = directory / "目录.md"
|
|
relative = index.relative_to(ROOT).as_posix()
|
|
if not index.is_file():
|
|
missing.append(relative)
|
|
continue
|
|
if not index.read_text(encoding="utf-8").startswith(STANDARD_INDEX_HEADER):
|
|
invalid_headers.append(relative)
|
|
if f"{directory.name}/目录.md" not in root_index:
|
|
undiscoverable.append(relative)
|
|
|
|
self.assertEqual(missing, [], "缺少一级目录索引:\n" + "\n".join(missing))
|
|
self.assertEqual(
|
|
invalid_headers,
|
|
[],
|
|
"一级目录索引未使用固定五列表头:\n" + "\n".join(invalid_headers),
|
|
)
|
|
self.assertEqual(
|
|
undiscoverable,
|
|
[],
|
|
"一级目录索引未从 .agent/目录.md 登记:\n"
|
|
+ "\n".join(undiscoverable),
|
|
)
|
|
|
|
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
|
|
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()
|