Files
FabledSteward/tests/test_breadcrumbs.py
T
bvandeusenandClaude Opus 5.5 24700ee608
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 56s
CI / unit (push) Successful in 1m24s
CI / publish (push) Successful in 1m9s
feat(settings): Settings brought into line with the rest of Steward (milestone 493)
The last page of #5214's walk. Settings still described Steward as it was
before monitors were unified and SNMP was rebuilt, drew its tabs below the
breadcrumb, and showed the report schedule in UTC.

- Tabs (#5377): settings/_tabs.html is a settings_tabs() macro called in
  each page's section_tabs block, above the trail, as Ansible's are. The
  breadcrumb guard also fails any page that {% include %}s a tab strip.
- Report time (#5378): reports.schedule_tz keeps the zone of whoever saved
  the schedule; check_and_send compares in it (report_due), and the form shows
  the day and hour on the reader's clock (schedule_in) under their zone's
  name. "Last sent" uses |when.
- Wording (#5379): the report contents list what the report holds (monitors,
  not ping-enabled hosts); "Ping latency" is "Response time" and says what it
  grades; plugin detail says enabling loads now and disabling needs a
  restart; the monitor interval says it applies on the next check (the
  scheduler reads it every pass); the dead list-config branch is gone.
- Traefik (#5380): the 5xx and 4xx cutoffs are settings
  (traefik.threshold.error_5xx_pct 0.1, error_4xx_pct 1.0) in a "Traefik
  routers" group on Thresholds while the plugin runs, read by router_state
  through error_cutoffs(). The Traefik page has a Thresholds button for
  admins, to /settings/thresholds/#traefik.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 13:28:43 -04:00

102 lines
4.7 KiB
Python

"""Every page below a top-level section shows its trail back up (rule 134).
The breadcrumb block is empty by default, so a nested page that forgets it
ships with no way back up and nothing says so. Every full page (one that
extends base.html) must fill the block, unless it is a section's own top
page, a logged-out page or an error page: those are named below, so adding
one is a decision someone makes in this file.
"""
import re
from tests._templates import REPO, template_files
TOP_LEVEL = {
"steward/templates/dashboard/index.html",
"steward/templates/hosts/list.html",
"steward/templates/monitors/index.html",
"steward/templates/alerts/list.html",
"steward/templates/ansible/index.html",
"steward/templates/audit/list.html",
"steward/templates/ai_access/index.html",
"steward/templates/account/index.html",
"plugins/docker/templates/docker/index.html",
"plugins/snmp/templates/snmp/index.html",
"plugins/traefik/templates/traefik/index.html",
"plugins/unifi/templates/unifi/index.html",
# Logged out, or no page to go back up from.
"steward/templates/auth/login.html",
"steward/templates/auth/setup.html",
"steward/templates/errors/404.html",
"steward/templates/errors/500.html",
"steward/templates/errors/database_unavailable.html",
}
_EXTENDS_BASE = re.compile(r"""\{%-?\s*extends\s+["']base\.html["']""")
_BLOCK = re.compile(r"\{%-?\s*block\s+breadcrumb\s*-?%\}(.*?)\{%-?\s*endblock", re.S)
def needs_crumbs_and_lacks_them(text: str) -> bool:
block = _BLOCK.search(text)
return bool(_EXTENDS_BASE.search(text)) and not (block and "crumbs(" in block.group(1))
def test_the_guard_can_fail():
assert needs_crumbs_and_lacks_them('{% extends "base.html" %}{% block content %}x{% endblock %}')
assert needs_crumbs_and_lacks_them('{% extends "base.html" %}{% block breadcrumb %}{% endblock %}')
assert not needs_crumbs_and_lacks_them(
'{% extends "base.html" %}{% block breadcrumb %}{{ crumbs([("Hosts", "/hosts/"), ("x", "")]) }}{% endblock %}')
assert not needs_crumbs_and_lacks_them("<tr><td>a fragment</td></tr>")
def test_every_nested_page_has_a_trail():
pages = {str(p.relative_to(REPO)): p for p in template_files()}
assert TOP_LEVEL <= set(pages), sorted(TOP_LEVEL - set(pages)) # a renamed page leaves no stale entry
missing = sorted(name for name, p in pages.items()
if name not in TOP_LEVEL and needs_crumbs_and_lacks_them(p.read_text()))
assert not missing, f"add {{% block breadcrumb %}}{{{{ crumbs([...]) }}}} (rule 134): {missing}"
# A section's tabs go in base.html's section_tabs block, above the trail, so the
# strip holds still whether or not the page has a breadcrumb (#5214). Drawn in
# the content block instead, they sit under the trail and jump down a line on
# every nested page.
_TABS_BLOCK = re.compile(r"\{%-?\s*block\s+section_tabs\s*-?%\}(.*?)\{%-?\s*endblock", re.S)
_TABS_CALL = re.compile(r"\{\{-?\s*\w*_tabs\(")
def tabs_outside_their_block(text: str) -> bool:
calls = len(_TABS_CALL.findall(text))
inside = sum(len(_TABS_CALL.findall(b)) for b in _TABS_BLOCK.findall(text))
return calls > inside
def test_the_tabs_guard_can_fail():
assert tabs_outside_their_block('{% block content %}{{ ansible_tabs("runs") }}{% endblock %}')
assert not tabs_outside_their_block('{% block section_tabs %}{{ ansible_tabs("runs") }}{% endblock %}')
def test_section_tabs_sit_above_the_trail():
base = (REPO / "steward/templates/base.html").read_text()
assert base.index("{% block section_tabs %}") < base.index("{% block breadcrumb %}")
pages = [p for p in template_files() if _EXTENDS_BASE.search(p.read_text())]
tabbed = [p for p in pages if _TABS_CALL.search(p.read_text())]
assert len(tabbed) >= 18, [str(p) for p in tabbed] # the ten Ansible pages and eight Settings pages
wrong = sorted(str(p.relative_to(REPO)) for p in tabbed if tabs_outside_their_block(p.read_text()))
assert not wrong, f"move the tabs into {{% block section_tabs %}}: {wrong}"
# A tab strip pulled in with {% include %} lands wherever the include sits, which
# is how Settings drew its tabs under the trail unnoticed (#5377). Tabs come only
# from a *_tabs() macro call, which the guard above places.
_TABS_INCLUDE = re.compile(r"\{%-?\s*include\s+[\"'][^\"']*_tabs\.html")
def test_the_include_guard_can_fail():
assert _TABS_INCLUDE.search('{% include "settings/_tabs.html" %}')
assert not _TABS_INCLUDE.search('{% from "settings/_tabs.html" import settings_tabs %}')
def test_no_page_includes_a_tab_strip():
included = sorted(str(p.relative_to(REPO)) for p in template_files() if _TABS_INCLUDE.search(p.read_text()))
assert not included, f"call the section's *_tabs() macro in {{% block section_tabs %}}: {included}"