test_wiki_navigation.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """Navigation checks for the wiki pages in docs/wiki/.
  2. A page nobody links to is a page nobody reads. These tests keep Contents (every page),
  3. the Reading-Order page and the "Next:" lines honest as pages are added.
  4. """
  5. import re
  6. from pathlib import Path
  7. WIKI = Path(__file__).resolve().parent.parent / "docs" / "wiki"
  8. PAGE_LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#[^)]*)?\)")
  9. EXEMPT = {"_Sidebar", "_Footer", "Home"}
  10. def pages() -> set[str]:
  11. return {p.stem for p in WIKI.glob("*.md")}
  12. def linked(name: str) -> list[str]:
  13. return PAGE_LINK.findall((WIKI / f"{name}.md").read_text())
  14. def test_every_page_is_in_contents_or_reading_order():
  15. reachable = set(linked("Contents")) | set(linked("Reading-Order"))
  16. missing = sorted(pages() - EXEMPT - reachable)
  17. assert not missing, f"not in Contents or Reading-Order: {missing}"
  18. def test_every_page_the_sidebar_links_exists():
  19. missing = sorted(set(linked("_Sidebar")) - pages())
  20. assert not missing, f"sidebar links to missing pages: {missing}"
  21. def test_every_page_named_in_a_next_line_exists():
  22. problems = []
  23. for page in sorted(WIKI.glob("*.md")):
  24. for line in page.read_text().splitlines():
  25. if "Next:" not in line:
  26. continue
  27. for target in PAGE_LINK.findall(line):
  28. if target not in pages():
  29. problems.append(f"{page.name}: {target}")
  30. assert not problems, f"Next lines name missing pages: {problems}"
  31. def test_reading_order_lists_no_page_twice_within_one_path():
  32. text = (WIKI / "Reading-Order.md").read_text()
  33. sections = re.split(r"^## ", text, flags=re.MULTILINE)[1:]
  34. checked = 0
  35. for section in sections:
  36. title = section.splitlines()[0]
  37. rows = [
  38. line
  39. for line in section.splitlines()
  40. if re.match(r"\|\s*\d+\s*\|", line)
  41. ]
  42. if not rows:
  43. continue
  44. checked += 1
  45. targets = [PAGE_LINK.search(row).group(1) for row in rows if PAGE_LINK.search(row)]
  46. dupes = sorted({t for t in targets if targets.count(t) > 1})
  47. assert not dupes, f"{title}: listed twice: {dupes}"
  48. assert checked >= 5, "expected the core path and at least four tracks"
  49. def test_reading_order_pages_all_exist():
  50. missing = sorted(set(linked("Reading-Order")) - pages())
  51. assert not missing, f"Reading-Order links to missing pages: {missing}"