testsAndMisc/python_pkg/byox_ladder/tests/test_build_pages.py
Krzysztof kuhy Rudnicki 6342eaf0e2
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Python tests / test (push) Waiting to run
feat(byox_ladder): add build-your-own-x difficulty ladder generator
Ranks the codecrafters build-your-own-x projects by difficulty and emits two
offline, self-contained HTML pages: a per-guide ladder (~360 tutorials) and a
per-category ladder (30 categories), grouped into Beginner/Intermediate/Advanced/
Expert tiers plus an Unsorted bucket. A guide inherits its category's tier;
language only nudges order within a tier. Includes a Makefile pipeline
(fetch -> parse -> build) and 100%-branch-covered tests. Excludes the generated
guide page from codespell since it inlines upstream titles verbatim.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01QNRTPNYvoBTQoTEWkYtCJD
2026-07-17 18:31:21 +02:00

100 lines
3.3 KiB
Python

"""Unit tests for the standalone page builder."""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from python_pkg.byox_ladder.build_pages import (
build_category_page,
build_guide_page,
main,
standalone,
)
if TYPE_CHECKING:
from pathlib import Path
def _write(dirpath: Path, name: str, body: str) -> None:
(dirpath / name).write_text(body, encoding="utf-8")
def test_standalone_wraps_content() -> None:
out = standalone("<p>hi</p>")
assert out.startswith("<!doctype html>")
assert "<p>hi</p>" in out
assert out.rstrip().endswith("</html>")
def test_build_guide_page_injects_data(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
templates = tmp_path / "templates"
templates.mkdir()
_write(
templates,
"guide.template.html",
"<title>x</title><script>/*__GUIDES__*/</script>",
)
guides_json = tmp_path / "guides.json"
guides_json.write_text('[{"title": "A"}]', encoding="utf-8")
monkeypatch.setattr("python_pkg.byox_ladder.build_pages.TEMPLATES", templates)
monkeypatch.setattr("python_pkg.byox_ladder.build_pages.GUIDES_JSON", guides_json)
html = build_guide_page()
assert "const GUIDES = [" in html
assert html.startswith("<!doctype html>")
def test_build_guide_page_missing_marker_raises(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
templates = tmp_path / "templates"
templates.mkdir()
_write(templates, "guide.template.html", "<title>no marker</title>")
guides_json = tmp_path / "guides.json"
guides_json.write_text("[]", encoding="utf-8")
monkeypatch.setattr("python_pkg.byox_ladder.build_pages.TEMPLATES", templates)
monkeypatch.setattr("python_pkg.byox_ladder.build_pages.GUIDES_JSON", guides_json)
with pytest.raises(ValueError, match="marker"):
build_guide_page()
def test_build_category_page_wraps_template(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
templates = tmp_path / "templates"
templates.mkdir()
_write(templates, "category.html", "<title>cat</title>")
monkeypatch.setattr("python_pkg.byox_ladder.build_pages.TEMPLATES", templates)
html = build_category_page()
assert "<title>cat</title>" in html
assert html.startswith("<!doctype html>")
def test_main_missing_guides_returns_error(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(
"python_pkg.byox_ladder.build_pages.GUIDES_JSON", tmp_path / "nope.json"
)
assert main() == 1
def test_main_writes_both_pages(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
templates = tmp_path / "templates"
templates.mkdir()
_write(templates, "guide.template.html", "<script>/*__GUIDES__*/</script>")
_write(templates, "category.html", "<title>cat</title>")
guides_json = tmp_path / "guides.json"
guides_json.write_text("[]", encoding="utf-8")
monkeypatch.setattr("python_pkg.byox_ladder.build_pages.TEMPLATES", templates)
monkeypatch.setattr("python_pkg.byox_ladder.build_pages.GUIDES_JSON", guides_json)
monkeypatch.setattr("python_pkg.byox_ladder.build_pages.HERE", tmp_path)
assert main() == 0
assert (tmp_path / "guide-ladder.html").exists()
assert (tmp_path / "category-ladder.html").exists()