Files
FabledSteward/tests/test_style_tokens.py
T
bvandeusenandClaude Opus 5.5 414c942cbf
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 1m5s
CI / unit (push) Successful in 1m19s
CI / publish (push) Successful in 57s
feat(plugins): Traefik problems first, UniFi/SNMP/fleet on shared shapes, audit names its entities, templates on tokens (#5126)
Traefik: routers are graded from their latest scrape (router_state: crit on
any real 5xx share, warn on p95 past the latency critical cutoff or a high
4xx share). Failing and slow routers lead as cards that say why; healthy
ones fold into one table that stays open or shut across polls. Proxy
counts are stat tiles; certificates are graded by Settings -> Thresholds
(tls_days).

UniFi: WAN health is a stat strip (latency graded by the thresholds),
devices carry status dots, event categories and firewall actions are
badges, the alarm count reuses .count-badge (renamed from .nav-count).
SNMP: each device wears a status badge and its readings are stat tiles.

Agent fleet: the table comes first, with a Status column (Reporting /
Silent / Never reported, by the host page's stale_after_seconds), silent
agents first, hosts linked, last seen as |ago.

Audit: entries name their entity (one query per type, steward/audit/
service.py) and link it; something deleted since keeps the name its entry
recorded and says "(deleted)".

Tokens: charts read colours through chart-colors.js (stewardColor resolves
a token to the rgb the browser computed; Chart.js defaults take the page's
text and border colours), so host, history, Traefik and SNMP charts drop
their hex. Template hex colours reach 0 (HEX_CEILING = 0), the last rgba
pills go, and heavy weights (600/700/bold, 60 of them) become 500, the
house pair being 400/500; a guard keeps templates there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 18:25:09 -04:00

82 lines
3.2 KiB
Python

"""Every colour a page draws comes from the design-system tokens (#5092).
- Every var(--x) a template or stylesheet uses is defined somewhere Steward
serves, so nothing silently falls back to the browser default.
- The base stylesheet states no colour or heavy weight of its own: values
are theme.css tokens (generated from design system 6) or aliases of them.
- Hex colours written into templates can only go down: each page moved onto
the tokens lowers HEX_CEILING (milestone 477 retires the rest).
"""
import pathlib
import re
import steward
from tests._templates import template_files as _templates
_REPO = pathlib.Path(steward.__file__).parent.parent
_STATIC = _REPO / "steward" / "static"
_COMMENTS = re.compile(r"\{#.*?#\}|<!--.*?-->|/\*.*?\*/", re.S)
_DEFINED = re.compile(r"(--[\w-]+)\s*:")
_USED = re.compile(r"var\(\s*(--[\w-]+)")
# A hex colour where a colour goes: after a CSS ':', '(' or ',', or as a quoted JS
# string -- but not an HTML attribute's value (href="#abc" is a link to an id).
_HEX = re.compile(r"""(?:[:(,]\s*|(?<![\w-]=)['"])#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6}|[0-9a-fA-F]{3})(?![0-9a-zA-Z_-])""")
HEX_CEILING = 0 # 75 at #5092, 58 at #5095, 56 at #5121, 54 at #5122, 47 at #5125, 0 with the charts on tokens (#5126)
def _sheets():
return [p for p in _STATIC.rglob("*.css") if "vendor" not in p.parts]
def _text(p):
return _COMMENTS.sub("", p.read_text())
def undefined_vars(texts) -> set[str]:
defined, used = set(), set()
for t in texts:
defined |= set(_DEFINED.findall(t))
used |= set(_USED.findall(t))
return used - defined
def hex_colours(text: str) -> list[str]:
return _HEX.findall(_COMMENTS.sub("", text))
def heavy_weights(text: str) -> list[str]:
"""Weights past the house pair (400, 500), outside comments."""
return re.findall(r"font-weight\s*:\s*(?:[6-9]00|bold)", _COMMENTS.sub("", text))
def test_the_guards_can_fail():
assert undefined_vars([":root { --a: 1; } .x { color: var(--a); background: var(--b); }"]) == {"--b"}
page = '<div style="color:#fff;background: rgb(0 0 0)" hx-target="#add-form"></div><script>c = "#4aa86a"</script>'
assert len(hex_colours(page)) == 2
assert hex_colours("{# color:#fff #}<a href=\"#abc\">") == []
assert heavy_weights('<b style="font-weight: 600">{# font-weight:700 #}</b>') == ["font-weight: 600"]
def test_every_css_variable_used_is_defined():
texts = [_text(p) for p in [*_templates(), *_sheets()]]
assert not undefined_vars(texts)
def test_the_base_stylesheet_states_no_colour_or_heavy_weight_of_its_own():
css = _text(_STATIC / "steward.css")
assert not re.findall(r"#[0-9a-fA-F]{3,8}\b|rgba?\(|hsla?\(", css)
assert not re.findall(r"font-weight\s*:\s*(?:[6-9]00|bold)", css)
def test_templates_use_the_two_house_weights():
"""The house style has two weights, 400 and 500 (design system 1)."""
heavy = {p.name: len(found) for p in _templates() if (found := heavy_weights(p.read_text()))}
assert not heavy, heavy
def test_hex_colours_in_templates_only_go_down():
count = sum(len(hex_colours(p.read_text())) for p in _templates())
assert count <= HEX_CEILING, (
f"{count} hex colours in templates, ceiling {HEX_CEILING}: use a token (theme.css / steward.css)")