refactor: render pages from a PageMeta in tests

This commit is contained in:
Abdessamad Derraz committed 2026-11-17 09:34:59 +01:00
1 parent c7d89627fb
commit ec3a6ab8af
5 files changed
+67 -73

No files matched your search

-23
View File
@@ -556,29 +556,6 @@ class PageMeta:
words_by_page: Mapping[str, set[str]] | None = None
def finalize_page(
rel: str,
markdown: str,
*,
level: str,
weight: float | None,
browser_title: str,
description: str,
structured: dict,
terms: dict[str, str],
search: bool = True,
logo: str = "",
parent: dict | None = None,
authored: dict | None = None,
words_by_page: Mapping[str, set[str]] | None = None,
) -> tuple[str, str]:
"""render_page with the fields of PageMeta as keywords."""
return render_page(rel, markdown, PageMeta(
level=level, weight=weight, browser_title=browser_title, description=description,
structured=structured, terms=terms, search=search, logo=logo, parent=parent,
authored=authored, words_by_page=words_by_page))
def render_page(rel: str, markdown: str, meta: PageMeta) -> tuple[str, str]:
"""The Starlight file for a page body: its path under the content
collection and its text. *parent* ({label, link}, the link written
+13 -4
View File
@@ -256,10 +256,19 @@ class TheRules(unittest.TestCase):
def test_the_logo_reaches_the_front_matter(self):
import sitestarlight
_target, text = sitestarlight.finalize_page(
"platforms/x.md", "# X\n\nBody.\n", level="setup", weight=None,
browser_title="X", description="d", structured={}, terms={},
logo="assets/logos/x.svg")
_target, text = sitestarlight.render_page(
"platforms/x.md",
"# X\n\nBody.\n",
sitestarlight.PageMeta(
level="setup",
weight=None,
browser_title="X",
description="d",
structured={},
terms={},
logo="assets/logos/x.svg",
),
)
self.assertIn("logo: assets/logos/x.svg", text)
def test_a_large_data_directory_is_listed_on_pages_of_its_own(self):
+16 -18
View File
@@ -99,10 +99,18 @@ class NotFoundPage(unittest.TestCase):
self.assertIn("/404", side["options"]["topics"]["start-here"])
def test_the_404_has_no_contents_no_index_and_no_structured_data(self):
_target, text = sitestarlight.finalize_page(
"404.md", "# Page not found\n\nGone.\n", level="", weight=None,
browser_title="Page not found", description="d",
structured={"@type": "TechArticle"}, terms={})
_target, text = sitestarlight.render_page(
"404.md",
"# Page not found\n\nGone.\n",
sitestarlight.PageMeta(
level="",
weight=None,
browser_title="Page not found",
description="d",
structured={"@type": "TechArticle"},
terms={},
),
)
front = generate_site._front_matter(text)
self.assertIs(front["tableOfContents"], False)
self.assertIs(front["prev"], False)
@@ -212,9 +220,7 @@ class OneNextPerPage(unittest.TestCase):
def test_card_pages_turn_pagination_off(self):
for rel in ("systems/sony.md", "emulators/duckstation.md", "platforms/batocera.md"):
_target, text = sitestarlight.finalize_page(
rel, "# T\n\nBody.\n", level="explore", weight=None, browser_title="T",
description="d", structured={}, terms={})
_target, text = sitestarlight.render_page(rel, "# T\n\nBody.\n", sitestarlight.PageMeta(level="explore", weight=None, browser_title="T", description="d", structured={}, terms={}))
front = generate_site._front_matter(text)
with self.subTest(page=rel):
self.assertIs(front["prev"], False)
@@ -223,16 +229,12 @@ class OneNextPerPage(unittest.TestCase):
def test_the_next_link_is_the_first_of_next_steps(self):
body = ("# Troubleshooting\n\nText.\n\n## Next steps\n\n"
"- [Common questions](faq.md)\n- [Glossary](glossary.md)\n")
_target, text = sitestarlight.finalize_page(
"wiki/troubleshooting.md", body, level="start", weight=None,
browser_title="Troubleshooting", description="d", structured={}, terms={})
_target, text = sitestarlight.render_page("wiki/troubleshooting.md", body, sitestarlight.PageMeta(level="start", weight=None, browser_title="Troubleshooting", description="d", structured={}, terms={}))
front = generate_site._front_matter(text)
self.assertEqual(front["next"], {"link": "/retrobios/wiki/faq/", "label": "Common questions"})
def test_a_page_without_next_steps_keeps_the_reading_order(self):
_target, text = sitestarlight.finalize_page(
"wiki/faq.md", "# FAQ\n\nText.\n", level="start", weight=None,
browser_title="FAQ", description="d", structured={}, terms={})
_target, text = sitestarlight.render_page("wiki/faq.md", "# FAQ\n\nText.\n", sitestarlight.PageMeta(level="start", weight=None, browser_title="FAQ", description="d", structured={}, terms={}))
self.assertNotIn("next", generate_site._front_matter(text))
@@ -565,11 +567,7 @@ class MovedPlatformPages(unittest.TestCase):
self.assertRegex(front, r"parent:\n label: .+\n link: /retrobios/platforms/[a-z0-9_-]+/\n")
def test_parent_link_resolves_against_the_page(self) -> None:
_target, text = sitestarlight.finalize_page(
"platforms/retroarch-sony-playstation.md", "# RetroArch: Sony PlayStation\n\nBody.\n",
level="reference", weight=0.5, browser_title="x", description="d",
structured={}, terms={}, parent={"label": "RetroArch", "link": "retroarch.md"},
)
_target, text = sitestarlight.render_page("platforms/retroarch-sony-playstation.md", "# RetroArch: Sony PlayStation\n\nBody.\n", sitestarlight.PageMeta(level="reference", weight=0.5, browser_title="x", description="d", structured={}, terms={}, parent={"label": "RetroArch", "link": "retroarch.md"}))
self.assertIn("parent:\n label: RetroArch\n link: /retrobios/platforms/retroarch/\n", text)
def test_the_schema_accepts_parent(self) -> None:
+31 -25
View File
@@ -61,9 +61,19 @@ def _gate_queries() -> dict:
def _finalize(rel: str, body: str, authored: dict) -> dict:
_target, text = sitestarlight.finalize_page(
rel, body, level="", weight=None, browser_title="T", description="d",
structured={}, terms={}, authored=authored)
_target, text = sitestarlight.render_page(
rel,
body,
sitestarlight.PageMeta(
level="",
weight=None,
browser_title="T",
description="d",
structured={},
terms={},
authored=authored,
),
)
return {"front": _front(text), "text": text}
@@ -391,11 +401,7 @@ class ConsoleWordsReachTheConsole(unittest.TestCase):
["SNES BIOS", "Super Famicom BIOS"])
def test_the_terms_field_carries_them(self):
_target, text = sitestarlight.finalize_page(
"systems/nintendo-snes.md", "# SNES\n\nNo file needed.\n", level="0",
weight=None, browser_title="SNES", description="d",
structured={"@type": "TechArticle"}, terms={},
authored={"consoles": ["SNES"]})
_target, text = sitestarlight.render_page("systems/nintendo-snes.md", "# SNES\n\nNo file needed.\n", sitestarlight.PageMeta(level="0", weight=None, browser_title="SNES", description="d", structured={"@type": "TechArticle"}, terms={}, authored={"consoles": ["SNES"]}))
self.assertIn('data-pagefind-meta="consoles:SNES"', text)
self.assertIn('data-pagefind-meta="terms:SNES BIOS"', text)
@@ -434,19 +440,22 @@ class ANameAnotherPageCovers(unittest.TestCase):
def test_names_another_page_covers(self):
covered = {page: sitestarlight.covered_names(names, page, self.words())
for page, names in self.PAGES.items()}
self.assertEqual(covered, {
"systems/sony-playstation": ["PlayStation"],
"systems/sony-playstation-2": [],
# Every word of PlayStation 3 is on the PlayStation 2 page, but
# not "3" itself: no page covers it.
"systems/sony": [],
"systems/nintendo-nintendo-ds": ["Nintendo DS"],
"systems/nintendo-nintendo-dsi": [],
"systems/nintendo": [],
# Nintendo, Super and System, each in another name.
"systems/arcade-nintendo-super-system": ["Nintendo Super System"],
"systems/sega-saturn": [],
})
self.assertEqual(
covered,
{
"systems/sony-playstation": ["PlayStation"],
"systems/sony-playstation-2": [],
# Every word of PlayStation 3 is on the PlayStation 2 page, but
# not "3" itself: no page covers it.
"systems/sony": [],
"systems/nintendo-nintendo-ds": ["Nintendo DS"],
"systems/nintendo-nintendo-dsi": [],
"systems/nintendo": [],
# Nintendo, Super and System, each in another name.
"systems/arcade-nintendo-super-system": ["Nintendo Super System"],
"systems/sega-saturn": [],
},
)
def test_words_of_a_decorated_page(self):
text = '<span data-pagefind-meta="consoles:Nintendo DS" hidden></span>'
@@ -457,10 +466,7 @@ class ANameAnotherPageCovers(unittest.TestCase):
words = self.words()
def text(page: str) -> str:
return sitestarlight.finalize_page(
f"{page}.md", "# T\n\nBody.\n", level="0", weight=None,
browser_title="T", description="d", structured={"@type": "TechArticle"},
terms={}, authored={"consoles": self.PAGES[page]}, words_by_page=words)[1]
return sitestarlight.render_page(f"{page}.md", "# T\n\nBody.\n", sitestarlight.PageMeta(level="0", weight=None, browser_title="T", description="d", structured={"@type": "TechArticle"}, terms={}, authored={"consoles": self.PAGES[page]}, words_by_page=words))[1]
self.assertIn('data-pagefind-meta="exact:PlayStation"', text("systems/sony-playstation"))
for page in ("systems/sony-playstation-2", "systems/sega-saturn"):
+7 -3
View File
@@ -157,9 +157,13 @@ class TheTableOfContentsOfACardPageStopsAtLevelTwo(unittest.TestCase):
import yaml
body = "# T\n\n" + "".join(f"## S{i}\n\n### card\n\n" for i in range(sections))
_target, text = sitestarlight.finalize_page(
rel, body, level="", weight=None, browser_title="T", description="d",
structured={}, terms={})
_target, text = sitestarlight.render_page(
rel,
body,
sitestarlight.PageMeta(
level="", weight=None, browser_title="T", description="d", structured={}, terms={}
),
)
return yaml.safe_load(text.split("---\n")[1]).get("tableOfContents", "default")
def test_cards_leave_the_toc_and_wiki_pages_keep_level_three(self):