"""Regression test — decorative breadcrumb separator SVGs are aria-hidden.

The breadcrumb chevron is purely decorative (a `>` glyph between page-name
links). Without ``aria-hidden="true"`` screen readers announce a stray
'graphic' on every page. This test asserts every SVG with the canonical
``crumb-sep`` class (or the inline ``width="12" height="12"`` breadcrumb
chevron) carries the attribute.
"""
from __future__ import annotations

import re
from pathlib import Path

import pytest

_RIMS_TEMPLATES_DIR = Path(__file__).resolve().parents[1]
# Match the opening tag of an SVG element on a single line so we can assert
# aria-hidden on each occurrence.
_SVG_PATTERNS = [
    re.compile(r'<svg[^>]*\bclass="crumb-sep"[^>]*>'),
    re.compile(r'<svg[^>]*\bwidth="12"\s+height="12"[^>]*\bviewBox="0 0 24 24"[^>]*>'),
]


def _iter_template_files() -> list[Path]:
    return sorted(_RIMS_TEMPLATES_DIR.rglob("templates/**/*.html"))


@pytest.mark.parametrize("pattern", _SVG_PATTERNS)
def test_decorative_breadcrumb_svgs_are_aria_hidden(pattern):
    offenders: list[tuple[Path, int, str]] = []
    for path in _iter_template_files():
        try:
            text = path.read_text(encoding="utf-8")
        except (OSError, UnicodeDecodeError):
            continue
        for line_no, line in enumerate(text.splitlines(), start=1):
            for match in pattern.finditer(line):
                tag = match.group(0)
                if 'aria-hidden="true"' not in tag:
                    offenders.append((path, line_no, tag[:120]))
    if offenders:
        formatted = "\n".join(
            f"  {p.relative_to(_RIMS_TEMPLATES_DIR.parent.parent)}:{n}: {tag}"
            for p, n, tag in offenders
        )
        pytest.fail(
            f"{len(offenders)} decorative breadcrumb SVG(s) missing aria-hidden=\"true\":\n{formatted}"
        )
