|
| 1 | +"""Tests for scripts/culture_completeness.py. |
| 2 | +
|
| 3 | +Locks two contracts: |
| 4 | +
|
| 5 | +1. ``complete_countries()`` returns ONLY sovereign-state cultures |
| 6 | + (depth 3). build_zips and build_pdfs rely on this signature for |
| 7 | + their per-country zip / PDF loop; widening it would double-build |
| 8 | + sub-national content. |
| 9 | +2. ``complete_cultures()`` returns BOTH depths (sovereign + sub- |
| 10 | + national) so the auto-release bump diff and the registry-alignment |
| 11 | + cross-check see the full set. Pre-#536 these two consumers were |
| 12 | + running on ``complete_countries()``, which meant a sub-national |
| 13 | + like Schleswig-Holstein got patch-bumped instead of minor-bumped |
| 14 | + and primed a latent alignment-test block on the next promotion. |
| 15 | +""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import sys |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 24 | +sys.path.insert(0, str(REPO_ROOT / "scripts")) |
| 25 | + |
| 26 | +import culture_completeness as cc # noqa: E402 |
| 27 | +from culture_completeness import ( # noqa: E402 |
| 28 | + complete_countries, |
| 29 | + complete_cultures, |
| 30 | + is_complete_culture, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +def _make_complete(d: Path) -> None: |
| 35 | + """Drop the five files is_complete_culture requires into ``d``.""" |
| 36 | + d.mkdir(parents=True, exist_ok=True) |
| 37 | + (d / "culture_x_position.md").write_text("pos", encoding="utf-8") |
| 38 | + (d / "culture_x_persona_one.md").write_text("p1", encoding="utf-8") |
| 39 | + (d / "culture_x_persona_two.md").write_text("p2", encoding="utf-8") |
| 40 | + (d / "culture_x_place_a.md").write_text("place", encoding="utf-8") |
| 41 | + (d / "culture_x_piece_b.md").write_text("piece", encoding="utf-8") |
| 42 | + (d / "README.md").write_text("readme", encoding="utf-8") |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def fake_regions(tmp_path, monkeypatch): |
| 47 | + regions = tmp_path / "regions" |
| 48 | + regions.mkdir() |
| 49 | + monkeypatch.setattr(cc, "_REGIONS", regions) |
| 50 | + return regions |
| 51 | + |
| 52 | + |
| 53 | +class TestIsCompleteCulture: |
| 54 | + def test_complete_folder(self, tmp_path): |
| 55 | + _make_complete(tmp_path / "x") |
| 56 | + assert is_complete_culture(tmp_path / "x") |
| 57 | + |
| 58 | + def test_missing_position(self, tmp_path): |
| 59 | + _make_complete(tmp_path / "x") |
| 60 | + (tmp_path / "x" / "culture_x_position.md").unlink() |
| 61 | + assert not is_complete_culture(tmp_path / "x") |
| 62 | + |
| 63 | + def test_only_one_persona(self, tmp_path): |
| 64 | + _make_complete(tmp_path / "x") |
| 65 | + (tmp_path / "x" / "culture_x_persona_two.md").unlink() |
| 66 | + assert not is_complete_culture(tmp_path / "x") |
| 67 | + |
| 68 | + def test_legacy_persona_name(self, tmp_path): |
| 69 | + """``persona_<name>.md`` (the pre-rename form) counts.""" |
| 70 | + d = tmp_path / "x" |
| 71 | + _make_complete(d) |
| 72 | + (d / "culture_x_persona_one.md").unlink() |
| 73 | + (d / "culture_x_persona_two.md").unlink() |
| 74 | + (d / "persona_alice.md").write_text("a", encoding="utf-8") |
| 75 | + (d / "persona_bob.md").write_text("b", encoding="utf-8") |
| 76 | + assert is_complete_culture(d) |
| 77 | + |
| 78 | + def test_non_dir(self, tmp_path): |
| 79 | + f = tmp_path / "file.md" |
| 80 | + f.write_text("x", encoding="utf-8") |
| 81 | + assert not is_complete_culture(f) |
| 82 | + |
| 83 | + |
| 84 | +class TestCompleteCountriesIsSovereignOnly: |
| 85 | + """``complete_countries`` MUST stay sovereign-only -- build_zips |
| 86 | + and build_pdfs rely on this depth contract for their per-country |
| 87 | + loop. A sub-national must NOT appear here.""" |
| 88 | + |
| 89 | + def test_returns_sovereigns_only(self, fake_regions): |
| 90 | + _make_complete(fake_regions / "europe" / "germany") |
| 91 | + _make_complete(fake_regions / "europe" / "germany" / "schleswig_holstein") |
| 92 | + names = [path.name for _r, path in complete_countries()] |
| 93 | + assert "germany" in names |
| 94 | + assert "schleswig_holstein" not in names |
| 95 | + |
| 96 | + def test_skeleton_country_not_included(self, fake_regions): |
| 97 | + """A region folder without the full chapter set is filtered out.""" |
| 98 | + (fake_regions / "europe" / "atlantis").mkdir(parents=True) |
| 99 | + (fake_regions / "europe" / "atlantis" / "README.md").write_text("stub") |
| 100 | + names = [path.name for _r, path in complete_countries()] |
| 101 | + assert "atlantis" not in names |
| 102 | + |
| 103 | + def test_skips_hidden_folders(self, fake_regions): |
| 104 | + _make_complete(fake_regions / ".hidden_region" / "x") |
| 105 | + _make_complete(fake_regions / "europe" / ".hidden") |
| 106 | + names = [path.name for _r, path in complete_countries()] |
| 107 | + assert ".hidden" not in names |
| 108 | + assert "x" not in names |
| 109 | + |
| 110 | + |
| 111 | +class TestCompleteCulturesWalksBothDepths: |
| 112 | + """``complete_cultures`` returns sovereign AND sub-national folders |
| 113 | + at every depth. This is what auto-release and the alignment |
| 114 | + cross-check consume after #536.""" |
| 115 | + |
| 116 | + def test_includes_sub_national(self, fake_regions): |
| 117 | + _make_complete(fake_regions / "europe" / "germany") |
| 118 | + _make_complete(fake_regions / "europe" / "germany" / "schleswig_holstein") |
| 119 | + names = [path.name for _r, path in complete_cultures()] |
| 120 | + assert "germany" in names |
| 121 | + assert "schleswig_holstein" in names |
| 122 | + |
| 123 | + def test_subnational_without_complete_parent(self, fake_regions): |
| 124 | + """A complete sub-national surfaces even when its parent is |
| 125 | + only a skeleton -- the parent's incompleteness shouldn't hide |
| 126 | + a shippable sub-culture (it has its own zip).""" |
| 127 | + (fake_regions / "europe" / "germany").mkdir(parents=True) |
| 128 | + (fake_regions / "europe" / "germany" / "README.md").write_text("stub") |
| 129 | + _make_complete(fake_regions / "europe" / "germany" / "schleswig_holstein") |
| 130 | + names = [path.name for _r, path in complete_cultures()] |
| 131 | + assert "schleswig_holstein" in names |
| 132 | + assert "germany" not in names |
| 133 | + |
| 134 | + def test_skeleton_subnational_not_included(self, fake_regions): |
| 135 | + _make_complete(fake_regions / "europe" / "germany") |
| 136 | + (fake_regions / "europe" / "germany" / "bavaria").mkdir() |
| 137 | + (fake_regions / "europe" / "germany" / "bavaria" / "README.md").write_text("stub") |
| 138 | + names = [path.name for _r, path in complete_cultures()] |
| 139 | + assert "germany" in names |
| 140 | + assert "bavaria" not in names |
| 141 | + |
| 142 | + def test_no_double_count_of_sovereign(self, fake_regions): |
| 143 | + """A sovereign is returned once, not duplicated as its own |
| 144 | + sub-self -- the per-depth loops never re-enter.""" |
| 145 | + _make_complete(fake_regions / "europe" / "germany") |
| 146 | + rows = complete_cultures() |
| 147 | + germany_rows = [r for r in rows if r[1].name == "germany"] |
| 148 | + assert len(germany_rows) == 1 |
| 149 | + |
| 150 | + def test_ordering_sovereigns_then_subnationals(self, fake_regions): |
| 151 | + """Sovereigns come first (sorted by region, slug), then sub- |
| 152 | + nationals (sorted by region, parent, sub). Stable across runs |
| 153 | + so the auto-release BEFORE/AFTER comparison is deterministic.""" |
| 154 | + _make_complete(fake_regions / "europe" / "germany") |
| 155 | + _make_complete(fake_regions / "europe" / "denmark") |
| 156 | + _make_complete(fake_regions / "europe" / "germany" / "schleswig_holstein") |
| 157 | + names = [path.name for _r, path in complete_cultures()] |
| 158 | + assert names.index("denmark") < names.index("germany") |
| 159 | + assert names.index("germany") < names.index("schleswig_holstein") |
| 160 | + |
| 161 | + |
| 162 | +class TestCompleteCulturesAgainstRealRepo: |
| 163 | + """The live regions/ tree must produce a sane complete-cultures |
| 164 | + set: every sovereign is present, schleswig_holstein is there as |
| 165 | + the only sub-national so far.""" |
| 166 | + |
| 167 | + def test_germany_present(self): |
| 168 | + names = [path.name for _r, path in complete_cultures()] |
| 169 | + assert "germany" in names |
| 170 | + |
| 171 | + def test_schleswig_holstein_present(self): |
| 172 | + names = [path.name for _r, path in complete_cultures()] |
| 173 | + assert "schleswig_holstein" in names |
| 174 | + |
| 175 | + def test_superset_of_complete_countries(self): |
| 176 | + countries = {path.name for _r, path in complete_countries()} |
| 177 | + cultures = {path.name for _r, path in complete_cultures()} |
| 178 | + assert countries.issubset(cultures) |
0 commit comments