forked from retoor/devplacepy
Adds admin_sections registry, shared admin partials, WorkspaceList, admin CLAUDE.md spec, capped node/flag listings, and api/unit tests enforcing the spec. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
128 lines
4.2 KiB
Python
128 lines
4.2 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
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 "<h1" not in text
|
|
assert "<style" not in text
|
|
assert not re.search(r"\sstyle=", text)
|
|
assert not re.search(r"\son(click|change|submit|input|load)=", text)
|
|
assert "admin.css" not in text and "sidebar.css" not in text
|
|
assert not re.search(r"<th(?![a-z])(?![^>]*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"(?<![A-Za-z.])fetch\(", text)
|
|
assert "toLocaleString" not in text and "toLocaleTimeString" not in text
|
|
|
|
|
|
def test_a8_poll_floor_declared():
|
|
from devplacepy.routers.admin._shared import ADMIN_LIST_CAP, ADMIN_POLL_FLOOR_SECONDS
|
|
|
|
assert ADMIN_POLL_FLOOR_SECONDS == 15
|
|
assert ADMIN_LIST_CAP == 200
|