| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- # SPDX-License-Identifier: Apache-2.0
- """The secrets-from-day-one lesson is a short hub and parts, none of them long (issue #562).
- The lesson was one page of about 1,150 lines. It is now a hub that keeps the page name and URL, the intro and the
- banners, plus numbered part pages. The hub must list every part, each part must link back to the hub and stay under
- a line cap, the manifest must hold the pages as one series with the hub first, and every exercise the hub indexes must
- point at a part that really holds that heading. The text itself moved verbatim; that is checked against a record of the
- old page in tests/test_stale_sibling_statements.py.
- """
- import re
- import sys
- from pathlib import Path
- sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts"))
- import gen_wiki_nav as gwn
- from wikitext import page_text
- ROOT = Path(__file__).resolve().parent.parent
- WIKI = ROOT / "docs" / "wiki"
- STEM = "Secrets-From-Day-One"
- SERIES = "Secrets from day one"
- MAX_PART_LINES = 450
- PARTS = (
- f"{STEM}-Part-1-The-Rules-the-Tools-and-the-Threat-Model",
- f"{STEM}-Part-2-Free-Lab-Setup-and-Exercises-1-and-2",
- f"{STEM}-Part-3-Exercises-3-to-5-and-the-Optional-Doppler-Path",
- f"{STEM}-Part-4-Build-Break-Defend-the-Checkpoint-and-Claims",
- )
- LINK = re.compile(r"\]\(([A-Za-z0-9_-]+)\.md(?:#([^)]*))?\)")
- def part_names(wiki: Path) -> list[str]:
- return sorted(p.stem for p in wiki.glob(f"{STEM}-Part-*.md"))
- def split_problems(wiki: Path, cap: int = MAX_PART_LINES, expected: tuple[str, ...] = PARTS) -> list[str]:
- """Every way the split is broken: a part missing or extra, one the hub does not list, a long part, no way back."""
- parts = part_names(wiki)
- problems = []
- if tuple(parts) != tuple(sorted(expected)):
- problems.append(f"parts are {parts}, expected {sorted(expected)}")
- hub = page_text(wiki / f"{STEM}.md")
- linked = [name for name, _ in LINK.findall(hub)]
- for name in parts:
- if name not in linked:
- problems.append(f"the hub does not list {name}")
- text = page_text(wiki / f"{name}.md")
- lines = len(text.splitlines())
- if lines > cap:
- problems.append(f"{name} is {lines} lines, over {cap}")
- if f"]({STEM}.md)" not in text:
- problems.append(f"{name} does not link back to the hub")
- return problems
- def test_the_hub_lists_every_part_and_each_part_is_short():
- assert part_names(WIKI) == sorted(PARTS)
- assert split_problems(WIKI) == []
- def test_every_exercise_the_hub_indexes_is_a_heading_of_the_part_it_names():
- hub = page_text(WIKI / f"{STEM}.md")
- index = [(n, a) for n, a in LINK.findall(hub) if n.startswith(f"{STEM}-Part-") and a]
- assert len(index) >= 5, "the hub indexes fewer than the five exercises"
- for name, anchor in index:
- text = page_text(WIKI / f"{name}.md")
- assert anchor in {gwn.slug(h) for h in re.findall(r"^#{2,3} (.+)$", text, re.MULTILINE)}, (name, anchor)
- def test_every_link_into_the_lesson_names_the_part_that_holds_the_heading():
- """The banners, the exercise cross-references and every other page's links point at the part, never a stale anchor."""
- pages = [WIKI / f"{STEM}.md", *(WIKI / f"{n}.md" for n in PARTS)]
- headings = {}
- for name in PARTS:
- for h in re.findall(r"^#{2,3} (.+)$", page_text(WIKI / f"{name}.md"), re.MULTILINE):
- headings.setdefault(name, set()).add(gwn.slug(h))
- checked = 0
- for path in [*WIKI.glob("*.md")]:
- for name, anchor in LINK.findall(page_text(path)):
- if not anchor or not (name == STEM or name in PARTS):
- continue
- checked += 1
- assert name != STEM or anchor == "the-parts", (path.name, anchor)
- assert name == STEM or anchor in headings[name], (path.name, name, anchor)
- assert checked >= 20, "the guard collected too few links into the lesson"
- for path in pages[1:]:
- assert dangling_anchors(page_text(path), headings[path.stem]) == [], path.name
- def dangling_anchors(text: str, headings: set[str]) -> list[str]:
- """Same-page anchors that are not a heading of the page: after a split they must be links to the part instead."""
- return [a for a in re.findall(r"\]\(#([^)]+)\)", text) if a not in headings]
- def test_the_same_page_anchor_check_bites():
- assert dangling_anchors("see [x](#clean-up)", {"clean-up"}) == []
- assert dangling_anchors("see [x](#clean-up)", {"other"}) == ["clean-up"]
- def test_the_banners_send_the_claims_register_and_the_teardown_to_their_parts():
- claims = f"]({PARTS[3]}.md#claims-register)"
- teardown = f"]({PARTS[2]}.md#cost-and-teardown-read-first)"
- for name in (STEM, *PARTS):
- text = page_text(WIKI / f"{name}.md")
- assert claims in text, name
- assert teardown in text, name
- def test_the_manifest_holds_hub_and_parts_as_one_series_hub_first():
- m = gwn.read_manifest(ROOT)
- group = next(g["id"] for g in gwn.groups_of(m) if any(e["page"] == STEM for e in gwn.entries(m, g["id"])))
- rows = [e for e in gwn.entries(m, group) if e.get("series") == SERIES]
- assert [e["page"] for e in rows] == [STEM, *PARTS]
- assert all(len(e["title"]) <= 80 for e in rows[1:])
- def test_every_part_starts_with_a_title_then_a_section_heading():
- for name in PARTS:
- headings = re.findall(r"^(#{1,3}) ", page_text(WIKI / f"{name}.md"), re.MULTILINE)
- assert headings[:2] == ["#", "##"], name
- def test_the_split_check_bites(tmp_path):
- wiki = tmp_path
- for name in (STEM, *(f"{STEM}-Part-{i}-X" for i in (1, 2, 3))):
- body = "\n".join(["line"] * 10)
- (wiki / f"{name}.md").write_text(f"# t\n\n{body}\n[hub]({STEM}.md)\n", encoding="utf-8")
- hub = "\n".join(f"- [p]({STEM}-Part-{i}-X.md)" for i in (1, 2, 3))
- (wiki / f"{STEM}.md").write_text(f"# hub\n\n{hub}\n", encoding="utf-8")
- fake = tuple(f"{STEM}-Part-{i}-X" for i in (1, 2, 3))
- assert split_problems(wiki, expected=fake) == []
- assert any("expected" in p for p in split_problems(wiki, expected=(*fake, f"{STEM}-Part-4-X")))
- (wiki / f"{STEM}-Part-2-X.md").write_text("\n".join(["line"] * 500) + f"\n[hub]({STEM}.md)\n", encoding="utf-8")
- assert any("over" in p for p in split_problems(wiki, expected=fake))
- (wiki / f"{STEM}.md").write_text(f"# hub\n\n- [p]({STEM}-Part-1-X.md)\n", encoding="utf-8")
- assert any("does not list" in p for p in split_problems(wiki, expected=fake))
- (wiki / f"{STEM}-Part-3-X.md").write_text("# t\n", encoding="utf-8")
- assert any("does not link back" in p for p in split_problems(wiki, expected=fake))
- def test_part_3_keeps_the_original_peer_sections():
- """Part 3 holds the added Hands-on heading and the page's own Optional section, an H2 of its own before the split."""
- h2 = re.findall(r"^## (.+)$", page_text(WIKI / f"{PARTS[2]}.md"), re.MULTILINE)
- assert len(h2) == 2
- assert h2[0].startswith("Hands-on: exercises 3 to 5") and h2[1] == "Optional: your own Doppler account (free plan)"
|