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.

10.2 KB
Raw Blame
# allow: no-sut-import — guardian; statically parses the shipped CSS files
# for a warning-ink regression rather than exercising Python production code
"""A filled warning surface must use the warning ink token, not a literal.

test_light_theme_contrast.py checks the *values* themes assign to
--warning-color and --text-on-warning, but the component CSS (news.css,
styles.css, ...) only ever writes the tokens, e.g.
``background: var(--warning-color); color: var(--text-on-warning);``. A
regression there -- reverting to a literal ``color: #000`` or ``color:
white`` -- can't be caught by computing contrast from theme files, because
the literal never appears in any theme; it appears in the component
stylesheet itself. This statically greps every component stylesheet under
``web/static/css`` for rules whose background is exactly
``var(--warning-color)`` and asserts the same rule pairs it with the warning
ink token. Warning surfaces styled from template ``<style>`` blocks or JS
inline styles are outside this sweep.

Selectors this repo already knows don't need (or can't yet have) that
pairing are excluded explicitly below, with a reason, instead of silently
skipping or asserting something unverified about them.
"""

import re
from pathlib import Path

import pytest

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

# Git-ignored ``themes.css``, written by the FastAPI lifespan
# (``web/fastapi_app.py``) from ``themes/*/*.css`` on every boot: it exists
# only once the app has started (e.g. in CI), and duplicates the theme
# sources out of scope. test_accent_ink_contrast guards that it is generated.
GENERATED_SHEETS = {"themes.css"}

WARNING_INK_TOKEN = "var(--text-on-warning)"

# (relative CSS path, selector) -> why it's excluded from the "must use
# var(--text-on-warning)" rule below, instead of just being silently
# skipped or force-asserted as correct.
ALLOWLIST: dict[tuple[str, str], str] = {
    # Pre-existing rules #6684 did not touch (its diff only reworked
    # news.css/styles.css warning surfaces). They use
    # `var(--bg-primary)` as a knockout ink, or have no text content at
    # all (a decorative dot/underline). Neither claim has been verified
    # against every theme's actual colors here, so they're excluded
    # rather than asserted as passing.
    (
        "collection_details.css",
        ".ldr-btn-warning",
    ): "pre-existing, out of scope for #6684",
    (
        "collection_details.css",
        ".ldr-stat-warning::before",
    ): "decorative, no text",
    (
        "collection_details.css",
        ".ldr-badge-warning",
    ): "pre-existing, out of scope for #6684",
    (
        "collection_details.css",
        ".ldr-btn-collections-warning",
    ): "pre-existing, out of scope for #6684",
    (
        "collection_details.css",
        ".ldr-btn-outline-warning:hover",
    ): "pre-existing, out of scope for #6684",
    (
        "benchmark.css",
        ".ldr-processing-status.ldr-processing",
    ): "pre-existing, out of scope for #6684",
    (
        "subscriptions.css",
        ".ldr-status-indicator.ldr-checking",
    ): "decorative dot, no text",
    (
        "collections.css",
        ".ldr-btn-collections-warning",
    ): "pre-existing, out of scope for #6684",
    (
        "collections.css",
        ".ldr-btn-outline-warning:hover",
    ): "pre-existing, out of scope for #6684",
    (
        "download_manager.css",
        ".ldr-download-buttons .btn.btn-warning",
    ): "pre-existing, out of scope for #6684",
    (
        "download_manager.css",
        ".ldr-download-buttons .btn.btn-warning:hover:not(:disabled)",
    ): "no color override in this rule",
    (
        "download_manager.css",
        ".ldr-research-actions .btn.btn-warning",
    ): "pre-existing, out of scope for #6684",
    (
        "download_manager.css",
        ".ldr-research-actions .btn.btn-warning:hover:not(:disabled)",
    ): "no color override in this rule",
    (
        "document_details.css",
        ".ldr-badge-warning",
    ): "pre-existing, out of scope for #6684",
    # #6684 reverted this rule's ink back to its pre-PR literal because
    # `.ldr-library-status-bar .badge-warning` (no `ldr-` prefix) is never
    # emitted by any template or JS -- the rendered badge is
    # `ldr-badge ldr-badge-warning`, styled by styles.css instead.
    (
        "library.css",
        ".ldr-library-status-bar .badge-warning",
    ): "dead selector (see styles.css .ldr-badge-warning for the live rule)",
}

# Selectors this test must positively confirm use the warning ink token --
# the ones #6684 fixed. If any of these silently drops out of the sweep
# (e.g. the rule is deleted or the selector text changes) the coverage
# check below fails loudly instead of the sweep just going quiet.
REQUIRED_SELECTORS: set[tuple[str, str]] = {
    ("news.css", ".ldr-priority-alert"),
    ("news.css", ".ldr-history-status.ldr-status-in_progress"),
    ("styles.css", ".ldr-metrics-btn-overflow .ldr-badge-overflow"),
    # news.js currently emits the unprefixed `impact-medium`, so this rule
    # is not rendered yet (#6821); it is inked now so fixing the class name
    # cannot ship dark text on the darkened light-theme warning fill.
    ("news.css", ".ldr-impact-medium"),
    # benchmark.html renders "<n> results" in this badge; the base rule's
    # white ink was ~1.1-3.2:1 on the dark themes' bright warning fill.
    ("benchmark.css", ".ldr-search-count-badge.ldr-warning"),
}


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


def _declarations(decl_block: str) -> dict[str, str]:
    """Parse `prop: value;` pairs from one rule body into a dict."""
    decls = {}
    for part in decl_block.split(";"):
        part = part.strip()
        if not part or ":" not in part:
            continue
        prop, _, value = part.partition(":")
        decls[prop.strip().lower()] = " ".join(value.split())
    return decls


def _iter_rules(css: str):
    """Yield (selector, declarations) for every leaf `sel { decls }` block.

    Repeatedly peels off the innermost (no nested `{`) block so arbitrary
    nesting (e.g. an `@media` wrapper) doesn't confuse a flat regex: once a
    leaf is removed, its wrapper's own text becomes a (harmless,
    declaration-less) leaf on the next pass.
    """
    leaf = re.compile(r"([^{}]*)\{([^{}]*)\}")
    remaining = css
    while True:
        match = leaf.search(remaining)
        if not match:
            return
        selector = " ".join(match.group(1).split())
        yield selector, _declarations(match.group(2))
        remaining = remaining[: match.start()] + remaining[match.end() :]


def _iter_warning_background_rules():
    """Yield (relative_path, selector, declarations) for every rule whose
    background/background-color is exactly `var(--warning-color)`."""
    for path in sorted(CSS_ROOT.rglob("*.css")):
        if path.relative_to(CSS_ROOT).as_posix() in GENERATED_SHEETS:
            continue
        css = _strip_comments(path.read_text(encoding="utf-8"))
        for selector, decls in _iter_rules(css):
            if not selector:
                continue
            background = decls.get("background") or decls.get(
                "background-color"
            )
            if background == "var(--warning-color)":
                yield path.relative_to(CSS_ROOT).as_posix(), selector, decls


WARNING_BACKGROUND_RULES = list(_iter_warning_background_rules())


def test_found_warning_background_rules():
    """Sanity check the parser itself still finds rules to check."""
    assert WARNING_BACKGROUND_RULES, (
        "no `background: var(--warning-color)` rule found under "
        f"{CSS_ROOT}; either the parser regressed or every such rule was "
        "removed (update this test either way)"
    )


@pytest.mark.parametrize(
    "path,selector,decls",
    WARNING_BACKGROUND_RULES,
    ids=[f"{p}:{s}" for p, s, _ in WARNING_BACKGROUND_RULES],
)
def test_warning_background_pairs_with_warning_ink(path, selector, decls):
    """Every non-allowlisted `background: var(--warning-color)` rule must
    also set `color: var(--text-on-warning)` in the same rule."""
    reason = ALLOWLIST.get((path, selector))
    if reason is not None:
        pytest.skip(reason)
    assert decls.get("color") == WARNING_INK_TOKEN, (
        f"{path}: {selector!r} sets background: var(--warning-color) but "
        f"color is {decls.get('color')!r}, not {WARNING_INK_TOKEN!r}. "
        "If this is intentional (dead code, decorative, or a "
        "pre-existing/out-of-scope pattern), add it to ALLOWLIST with a "
        "reason instead of a literal color."
    )


def test_required_selectors_are_covered_and_passing():
    """The selectors #6684 fixed stay both present and correctly inked."""
    found = {
        (path, selector): decls
        for path, selector, decls in WARNING_BACKGROUND_RULES
    }
    missing = REQUIRED_SELECTORS - found.keys()
    assert not missing, (
        f"expected warning-background rules not found: {missing}"
    )
    for key in REQUIRED_SELECTORS:
        assert found[key].get("color") == WARNING_INK_TOKEN, (
            f"{key} must set color: {WARNING_INK_TOKEN}"
        )


def test_allowlist_entries_are_not_stale():
    """Each allowlisted selector must still be a warning-background rule, so
    a renamed or deleted rule cannot leave a silent exemption behind."""
    found = {(path, selector) for path, selector, _ in WARNING_BACKGROUND_RULES}
    stale = sorted(set(ALLOWLIST) - found)
    assert not stale, (
        "ALLOWLIST entries no longer match any `background: "
        f"var(--warning-color)` rule; remove or update them: {stale}"
    )


def test_priority_alert_link_inherits_alert_ink():
    """`.ldr-priority-alert a` must inherit its parent's ink, not hardcode
    a fixed color that could drift out of sync with the alert background."""
    css = _strip_comments((CSS_ROOT / "news.css").read_text(encoding="utf-8"))
    rules = dict(_iter_rules(css))
    decls = rules.get(".ldr-priority-alert a")
    assert decls is not None, ".ldr-priority-alert a rule not found in news.css"
    assert decls.get("color") == "inherit", (
        ".ldr-priority-alert a must set color: inherit so it always "
        f"matches .ldr-priority-alert's ink; got {decls.get('color')!r}"
    )