"""Tests for composing-html's deterministic checker. Run with: python composing-html/tests/test_checker.py (no pytest needed) or python -m pytest composing-html/tests -q """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "scripts")) from checker import check_html, has_errors def _rules(html, **kw): return {f.rule for f in check_html(html, **kw)} _PASS = 0 _FAIL = 0 def ok(cond, label): global _PASS, _FAIL if cond: _PASS += 1 else: _FAIL += 1 print(f" FAIL: {label}") # --- clean fragment: no findings ------------------------------------------- # clean = """
OVERVIEW

Status

All good

v1
diagram
""" ok(_rules(clean, fragment=True) == set(), "clean fragment yields no findings") ok(not has_errors(check_html(clean, fragment=True)), "clean fragment has no errors") # --- chrome leak (fragment only) ------------------------------------------- # ok("chrome-leak" in _rules('

hi

', fragment=True), "chrome-leak fires on / in fragment") ok("chrome-leak" not in _rules('x'), "chrome-leak silent on a full artifact") # --- undefined token ------------------------------------------------------- # ok("undefined-token" in _rules('

typo

', fragment=True), "undefined-token catches var(--cley) typo") ok("undefined-token" not in _rules('

ok

', fragment=True), "undefined-token silent on valid token") ok("undefined-token" not in _rules( '', fragment=True), "undefined-token silent on --bind-* created by data-bind") ok("undefined-token" not in _rules( '
', fragment=True), "undefined-token silent on author-declared custom property") # --- hardcoded colour ------------------------------------------------------ # ok("hardcoded-color" in _rules('

red

', fragment=True), "hardcoded-color catches hex literal") ok("hardcoded-color" in _rules('

x

', fragment=True), "hardcoded-color catches rgb()") # --- inline typography ----------------------------------------------------- # ok("inline-typography" in _rules('

x

', fragment=True), "inline-typography catches font-family") ok("inline-typography" in _rules('

x

', fragment=True), "inline-typography catches font-size") # --- nested card ----------------------------------------------------------- # ok("nested-card" in _rules('
x
', fragment=True), "nested-card catches card-in-card") ok("nested-card" not in _rules('
x
y
', fragment=True), "nested-card silent on sibling cards") ok("nested-card" not in _rules('
x
', fragment=True), "nested-card does not match substring 'scorecard'") # --- broken tabs ----------------------------------------------------------- # broken_tabs = '
' ok("broken-tabs" in _rules(broken_tabs, fragment=True), "broken-tabs catches target with no panel") good_tabs = ('
' '
x
') ok("broken-tabs" not in _rules(good_tabs, fragment=True), "broken-tabs silent on wired tabs") # --- broken bind ----------------------------------------------------------- # ok("broken-bind" in _rules('', fragment=True), "broken-bind catches bind with no consumer") ok("broken-bind" in _rules('', fragment=True), "broken-bind catches orphan data-out") # --- broken sortable ------------------------------------------------------- # ok("broken-sortable" in _rules('
x
', fragment=True), "broken-sortable catches no draggable children") ok("broken-sortable" not in _rules( '
x
', fragment=True), "broken-sortable silent when draggable present") # --- heading skip ---------------------------------------------------------- # ok("heading-skip" in _rules('

a

b

', fragment=True), "heading-skip catches h1->h3") ok("heading-skip" not in _rules('

a

b

c

', fragment=True), "heading-skip silent on proper nesting") # --- img no alt ------------------------------------------------------------ # ok("img-no-alt" in _rules('', fragment=True), "img-no-alt catches missing alt") ok("img-no-alt" not in _rules('', fragment=True), "img-no-alt silent on empty alt") # --- severity contract ----------------------------------------------------- # ok(has_errors(check_html('

x

', fragment=True)), "undefined-token is error severity") ok(not has_errors(check_html('', fragment=True)), "img-no-alt alone is not error severity") print(f"\n{_PASS} passed, {_FAIL} failed") sys.exit(1 if _FAIL else 0)