"""
Tests for html_analyzer.parse() against the hand-written sample HTML files.
Each test asserts a specific ParsedPage field rather than the full dict,
so failures are precise about what the parser got wrong.
"""
from __future__ import annotations
from pathlib import Path
from backend.analyzers.html_analyzer import parse
_SAMPLES = Path(__file__).parent.parent / "samples"
BAD_HTML = (_SAMPLES / "bad_ui.html").read_text(encoding="utf-8")
GOOD_HTML = (_SAMPLES / "good_ui.html").read_text(encoding="utf-8")
class TestBadUiTypography:
def test_body_font_size(self):
assert parse(BAD_HTML)["body_font_size_px"] == 11.0
def test_font_count_exceeds_limit(self):
fonts = parse(BAD_HTML)["fonts"]
assert len(fonts) >= 4, f"Expected ≥4 fonts, got {fonts}"
def test_line_height_tight(self):
lh = parse(BAD_HTML)["line_height"]
assert lh is not None and lh <= 1.2
def test_h1_size(self):
headings = parse(BAD_HTML)["headings"]
h1 = next((h for h in headings if h["level"] == 1), None)
assert h1 is not None and h1["font_size_px"] == 20.0
def test_h2_size(self):
headings = parse(BAD_HTML)["headings"]
h2 = next((h for h in headings if h["level"] == 2), None)
assert h2 is not None and h2["font_size_px"] == 18.0
def test_heading_hierarchy_weak(self):
headings = parse(BAD_HTML)["headings"]
h1 = next((h for h in headings if h["level"] == 1), None)
h2 = next((h for h in headings if h["level"] == 2), None)
assert h1 and h2
assert (h1["font_size_px"] / h2["font_size_px"]) < 1.2
class TestBadUiButtons:
def test_button_count(self):
btns = parse(BAD_HTML)["buttons"]
assert len(btns) >= 3, f"Expected ≥3 buttons, got {len(btns)}"
def test_button_padding_small(self):
btns = parse(BAD_HTML)["buttons"]
button_tags = [b for b in btns if b["text"] in ("Submit", "Cancel", "Delete")]
assert all(b["padding_top_px"] <= 3.0 for b in button_tags)
assert all(b["padding_left_px"] <= 6.0 for b in button_tags)
def test_button_height_small(self):
btns = parse(BAD_HTML)["buttons"]
button_tags = [b for b in btns if b["text"] in ("Submit", "Cancel", "Delete")]
assert all(b["height_px"] < 44.0 for b in button_tags)
def test_no_focus_styles(self):
btns = parse(BAD_HTML)["buttons"]
assert all(not b["has_focus_style"] for b in btns)
def test_distinct_backgrounds(self):
btns = parse(BAD_HTML)["buttons"]
# 3