"""Every generated figure carries the shared palette, and only it. Three generators draw into `docs/`: `crates/sds-core/examples/vignettes/render.rs` for the feature figures, `sds/labeldoc.py` for the label ones, and `crates/sds-core/examples/tactics/mod.rs` for the tactic ones. Each inlines the palette into every file it writes, because a figure is referenced as `![](x.svg)` and an `` document cannot load an external stylesheet. They used to hold a copy each, and the copies had drifted: the label figures were missing the `[data-theme="dark"]` block, so a reader's explicit choice moved the feature figures on the same page and not the label ones. Nothing could have caught that, because nothing compared the two. This does. It is deliberately not a test of either generator's output - the staleness tests own that - but of the one thing they have to agree on. """ from __future__ import annotations import re import unittest from pathlib import Path from sds.labeldoc import FIGURES_CSS, tokens ROOT = Path(__file__).resolve().parents[1] DIRS = ( ROOT / "docs" / "features", ROOT / "docs" / "labels", ROOT / "docs" / "tactics", ) def figures() -> list[Path]: return sorted(p for d in DIRS for p in d.glob("*.svg")) class TestSharedPalette(unittest.TestCase): def test_there_are_figures_to_check(self): # A glob that matches nothing would pass every test below in silence. self.assertGreater(len(figures()), 70) def test_every_figure_carries_the_shared_tokens(self): shared = tokens() missing = [p.name for p in figures() if shared not in p.read_text()] self.assertEqual(missing, [], "figures whose palette is not docs/figures.css") def test_no_figure_defines_a_token_outside_the_shared_block(self): # A generator that adds "just one more colour" locally is how the two # copies drifted the first time. shared = tokens() offenders = [] for path in figures(): body = path.read_text().replace(shared, "") declared = set(re.findall(r"(--[a-z-]+)\s*:", body)) if declared: offenders.append((path.name, sorted(declared))) self.assertEqual(offenders, []) def test_the_header_comment_is_not_shipped(self): # It is a page of prose explaining the file, and 78 copies of it would # be most of what the figures weigh. self.assertTrue(FIGURES_CSS.read_text().lstrip().startswith("/*")) for path in figures(): self.assertNotIn("docs/FEATURES.md", path.read_text()) def test_every_directory_has_figures_in_it(self): # A generator that stopped writing would otherwise pass every test # above by contributing nothing to check. for directory in DIRS: self.assertTrue(list(directory.glob("*.svg")), f"{directory.name} has no figures") def test_every_generator_is_reading_the_file(self): for source, expected in ( ("crates/sds-core/examples/vignettes/render.rs", '"../../../../docs/figures.css"'), ("crates/sds-core/examples/tactics/mod.rs", '"../../../../docs/figures.css"'), ("sds/labeldoc.py", "figures.css"), ): self.assertIn(expected, (ROOT / source).read_text(), source) if __name__ == "__main__": unittest.main()