Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.7 KB
Raw Blame
# allow: no-sut-import — guardian; statically parses shipped CSS/templates
# for missing-control regressions rather than exercising Python code
"""Controls the light-theme pass restored must stay visible.

- Downloads and Library filter selects keep a native dropdown arrow (Bootstrap's
  ``.form-control`` removes it and nothing draws a replacement).
- The Downloads search input's left padding must beat the filter-group
  padding shorthand, or the search icon overlaps the placeholder.
- Font Awesome icon classes must not carry the ``ldr-`` prefix
  (``fa-ldr-star`` renders nothing); Metrics shows real stars.
"""

import re
from pathlib import Path

import pytest

WEB_ROOT = (
    Path(__file__).resolve().parents[2] / "src" / "local_deep_research" / "web"
)
CSS_ROOT = WEB_ROOT / "static" / "css"
TEMPLATES = WEB_ROOT / "templates"


def _strip_comments(css: str) -> str:
    return re.sub(r"/\*.*?\*/", "", css, flags=re.DOTALL)


def _declarations(block: str) -> dict[str, str]:
    decls = {}
    for part in block.split(";"):
        prop, sep, value = part.partition(":")
        if sep:
            decls[prop.strip().lower()] = " ".join(value.split())
    return decls


def _rules(css: str):
    """Yield (index, selector, declarations) for flat (non-nested) rules."""
    for index, match in enumerate(
        re.finditer(r"([^{}]*)\{([^{}]*)\}", _strip_comments(css))
    ):
        selector = " ".join(match.group(1).split())
        for part in selector.split(","):
            yield index, part.strip(), _declarations(match.group(2))


def _specificity(selector: str) -> tuple[int, int, int]:
    """Specificity of a simple selector (no :not()/:is() arguments)."""
    ids = len(re.findall(r"#[\w-]+", selector))
    classes = len(
        re.findall(r"\.[\w-]+|\[[^\]]*\]|(?<!:):(?!:)[\w-]+", selector)
    )
    stripped = re.sub(r"#[\w-]+|\.[\w-]+|\[[^\]]*\]|::?[\w-]+", " ", selector)
    elements = len(re.findall(r"(?<![\w-])[a-zA-Z][\w-]*", stripped))
    return ids, classes, elements


def _download_css() -> str:
    return (CSS_ROOT / "download_manager.css").read_text(encoding="utf-8")


@pytest.mark.parametrize(
    "template,stylesheet",
    [
        ("download_manager.html", "download_manager.css"),
        ("library.html", "library.css"),
    ],
)
def test_filter_selects_keep_native_arrow(template, stylesheet):
    markup = (TEMPLATES / "pages" / template).read_text(encoding="utf-8")
    # Premise: the filters are Bootstrap form-control selects inside
    # .ldr-filter-group, and the page loads the stylesheet checked below.
    assert re.search(r'<select id="filter-[\w-]+" class="form-control"', markup)
    assert f"/static/css/{stylesheet}" in markup

    appearance = [
        (index, decls["appearance"])
        for index, selector, decls in _rules(
            (CSS_ROOT / stylesheet).read_text(encoding="utf-8")
        )
        if "appearance" in decls
        and re.search(r"\.ldr-filter-group\b.*\bselect\b", selector)
    ]
    assert appearance, (
        f"{stylesheet} must restore the native arrow on "
        ".ldr-filter-group selects (appearance: auto)"
    )
    assert max(appearance)[1] == "auto"


def test_download_search_padding_clears_icon():
    padding_rules = [
        (_specificity(selector), index, selector, decls)
        for index, selector, decls in _rules(_download_css())
        if (
            ".ldr-search-wrapper" in selector or ".ldr-filter-group" in selector
        )
        and ("padding" in decls or "padding-left" in decls)
        and ("input" in selector or ".form-control" in selector)
        and "select" not in selector
    ]
    search = [
        rule for rule in padding_rules if ".ldr-search-wrapper" in rule[2]
    ]
    assert search, "no padding rule for the Downloads search input found"
    winner = max(padding_rules, key=lambda rule: (rule[0], rule[1]))
    assert ".ldr-search-wrapper" in winner[2], (
        f"{winner[2]!r} overrides the search input padding, so the search "
        "icon overlaps the placeholder"
    )
    left = winner[3].get("padding-left", "")
    assert left.endswith("px") and int(left[:-2]) >= 30, left


def test_no_ldr_prefixed_font_awesome_icons():
    offenders = []
    for path in list(TEMPLATES.rglob("*.html")) + list(
        (WEB_ROOT / "static" / "js").rglob("*.js")
    ):
        if "node_modules" in path.parts:
            continue
        text = path.read_text(encoding="utf-8", errors="ignore")
        if re.search(r"\bfa-ldr-[\w-]+", text):
            offenders.append(path.relative_to(WEB_ROOT).as_posix())
    assert not offenders, f"invalid fa-ldr-* icon classes in {offenders}"


def test_metrics_satisfaction_uses_star_icon():
    markup = (TEMPLATES / "pages" / "metrics.html").read_text(encoding="utf-8")
    assert len(re.findall(r'class="fas fa-star"', markup)) >= 2