# retoor import re from pathlib import Path import pytest from devplacepy.admin_sections import ADMIN_SECTION_KEYS, ADMIN_SECTIONS ROOT = Path(__file__).resolve().parents[2] / "devplacepy" TEMPLATES = ROOT / "templates" ROUTERS = ROOT / "routers" / "admin" STATIC_JS = ROOT / "static" / "js" ADMIN_TEMPLATES = sorted( path for path in TEMPLATES.glob("*.html") if re.search(r'{%\s*extends\s+"admin_base.html"\s*%}', path.read_text()) ) FORBIDDEN_CLASSES = ("btn", "btn-primary", "btn-danger", "btn-secondary", "hint-text", "empty-state", "admin-header") CLASS_ATTR = re.compile(r'class="([^"]*)"') ADMIN_SCRIPT_REF = re.compile(r"/static/js/([A-Za-z0-9_]+\.js)") CHART_SRC = re.compile(r"""src="([^"]*chart[^"]*\.js)\"""") CANONICAL_CHART = "/static/vendor/chartjs/chart.umd.min.js" def ids(paths): return [path.name for path in paths] def test_admin_templates_discovered(): assert len(ADMIN_TEMPLATES) >= 25 @pytest.mark.parametrize("path", ADMIN_TEMPLATES, ids=ids(ADMIN_TEMPLATES)) def test_a1_no_loading_placeholder(path): assert not re.search(r"Loading[^<]{0,40}\.\.\.", path.read_text()) @pytest.mark.parametrize("path", ADMIN_TEMPLATES, ids=ids(ADMIN_TEMPLATES)) def test_a3_anatomy(path): text = path.read_text() assert "]*scope=)", text) @pytest.mark.parametrize("path", ADMIN_TEMPLATES, ids=ids(ADMIN_TEMPLATES)) def test_a4_class_vocabulary(path): for attribute in CLASS_ATTR.findall(path.read_text()): for name in attribute.split(): assert name not in FORBIDDEN_CLASSES, f"{path.name}: forbidden class {name}" @pytest.mark.parametrize("path", ADMIN_TEMPLATES, ids=ids(ADMIN_TEMPLATES)) def test_a5_instants_use_local_dt(path): assert "format_date(" not in path.read_text() assert "time_ago(" not in path.read_text() @pytest.mark.parametrize("path", ADMIN_TEMPLATES, ids=ids(ADMIN_TEMPLATES)) def test_a9_single_chart_path(path): for src in CHART_SRC.findall(path.read_text()): assert CANONICAL_CHART in src, f"{path.name}: chart loaded from {src}" @pytest.mark.parametrize("path", ADMIN_TEMPLATES, ids=ids(ADMIN_TEMPLATES)) def test_a10_no_hand_written_nav(path): assert 'class="sidebar-link' not in path.read_text() def test_a2_routes_use_respond(): offenders = [ leaf.name for leaf in ROUTERS.glob("*.py") if "TemplateResponse" in leaf.read_text() ] assert offenders == [] def test_a2_admin_section_keys_registered(): used = set() for leaf in ROUTERS.glob("*.py"): used.update(re.findall(r'"admin_section":\s*"([a-z-]+)"', leaf.read_text())) assert used assert used <= ADMIN_SECTION_KEYS def test_a10_every_section_has_a_page(): used = set() for leaf in ROUTERS.glob("*.py"): used.update(re.findall(r'"admin_section":\s*"([a-z-]+)"', leaf.read_text())) assert {section.key for section in ADMIN_SECTIONS} <= used def test_a10_registry_is_unique_and_well_formed(): keys = [section.key for section in ADMIN_SECTIONS] hrefs = [section.href for section in ADMIN_SECTIONS] assert len(keys) == len(set(keys)) assert len(hrefs) == len(set(hrefs)) assert all(href.startswith("/admin/") for href in hrefs) def admin_scripts() -> list[Path]: names = set() for path in ADMIN_TEMPLATES: names.update(ADMIN_SCRIPT_REF.findall(path.read_text())) return sorted(STATIC_JS / name for name in names if (STATIC_JS / name).exists()) @pytest.mark.parametrize("path", admin_scripts(), ids=ids(admin_scripts())) def test_a8_admin_scripts_use_the_shared_poller_and_http(path): text = path.read_text() assert "setInterval" not in text assert not re.search(r"(?