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.

5.6 KB
Raw Blame
"""The three theme helpers are reachable from every template.

Ported from ``tests/web/utils/test_theme_helper.py`` on main (deleted by the
FastAPI migration along with ``web/utils/theme_helper.py``).

``ThemeHelper`` was a Flask compatibility shim: a class whose entire job was
``app.jinja_env.globals[...] = ...`` in ``init_app``, plus two one-line
delegations to ``theme_registry``. The class has no FastAPI meaning and is
correctly gone — ``_setup_template_globals`` in ``web/fastapi_app.py`` now binds
the same three names directly. Its delegating methods
(``get_themes``/``clear_cache``) are already pinned on the registry itself by
``tests/web/themes/test_theme_registry.py``.

What had no successor is the registration half — main's
``test_init_app_registers_get_themes_global`` and its two siblings — and the
existing coverage is only partial:

* ``tests/web/test_template_environment_census.py`` demands that every name a
  template CALLS is present in ``env.globals``. ``get_themes_json`` and
  ``get_theme_metadata`` are called from ``base.html``, so they are covered.
  ``get_themes`` is registered but called from no checked-in template, so the
  census cannot see it at all — dropping that one line is invisible today.
* The census also only checks that the NAME exists. A name bound to the wrong
  callable satisfies it; main's tests asserted identity
  (``globals["get_themes"] == get_themes``), which is what actually matters,
  since ``get_themes`` and ``get_themes_json`` return different types and a
  swap would render ``base.html``'s inline ``var validThemes = ...`` as
  invalid JavaScript rather than raising anything server-side.

Both halves are restored here as identity assertions against the live
template environment the app serves from.
"""

import json
import re

import pytest

from local_deep_research.web import themes as themes_module
from local_deep_research.web.template_config import templates


THEME_GLOBALS = ["get_themes", "get_themes_json", "get_theme_metadata"]


@pytest.fixture(scope="module")
def env_globals():
    """The environment the app actually renders from.

    ``_setup_template_globals()`` is what installs the theme names, and it
    runs at ``web.fastapi_app`` import time — so importing the module is what
    makes ``templates.env.globals`` the real thing rather than a bare Jinja
    environment. Same premise as ``app_and_templates`` in
    ``tests/web/test_template_environment_census.py``.
    """
    import local_deep_research.web.fastapi_app  # noqa: F401  (import side effect)

    return templates.env.globals


@pytest.mark.parametrize("name", THEME_GLOBALS)
def test_the_theme_helper_is_registered_as_a_jinja_global(env_globals, name):
    """main: ``ThemeHelper.init_app`` bound all three. A missing name is an
    ``UndefinedError`` — a 500 — on every page that calls it."""
    assert name in env_globals, (
        f"'{name}' is not in the Jinja environment; templates calling it 500. "
        f"Registered theme names: {sorted(set(env_globals) & set(THEME_GLOBALS))}"
    )


@pytest.mark.parametrize("name", THEME_GLOBALS)
def test_the_global_is_the_registry_function_itself(env_globals, name):
    """main asserted identity, not mere presence
    (``jinja_env.globals["get_themes"] == get_themes``).

    The three are not interchangeable: ``get_themes`` returns a ``list[str]``,
    ``get_themes_json`` and ``get_theme_metadata`` return pre-rendered
    ``Markup``. ``base.html`` interpolates the latter two straight into a
    ``<script>`` block, so a crossed binding emits a Python repr where JSON
    was expected — broken theme switching, no server-side error.
    """
    expected = getattr(themes_module, name)
    assert env_globals[name] is expected, (
        f"jinja global '{name}' is bound to {env_globals[name]!r}, not "
        f"web.themes.{name}"
    )


def test_get_themes_returns_the_registrys_ids(env_globals):
    """The one theme global no checked-in template calls, so nothing else
    would notice if it were bound to something inert."""
    from local_deep_research.web.themes import theme_registry

    result = env_globals["get_themes"]()
    assert isinstance(result, list)
    assert result == theme_registry.get_theme_ids()
    assert result, (
        "the registry loaded no themes; the assertion above is vacuous"
    )


def test_default_theme_matches_the_registered_account_default(env_globals):
    from local_deep_research.defaults import get_default_theme
    from local_deep_research.settings import SettingsManager

    account_default = SettingsManager(db_session=None).default_settings[
        "app.theme"
    ]["value"]
    assert (
        env_globals["default_theme"] == get_default_theme() == account_default
    )
    assert themes_module.theme_registry.is_valid_theme(account_default)


# Hashed is the chosen dark default while light themes need refinement;
# Nord verifies that HTML and browser injection share other defaults too.
@pytest.mark.parametrize("theme", ["hashed", "nord"])
def test_initial_html_and_browser_share_the_injected_default(
    env_globals, theme
):
    source, _, _ = templates.env.loader.get_source(templates.env, "base.html")
    # Render the real initial HTML and startup script, before page assets.
    theme_script_end = source.index(
        "</script>", source.index("window.LDR_DEFAULT_THEME")
    )
    head = source[:theme_script_end] + "</script>"
    rendered = templates.env.from_string(head).render(default_theme=theme)

    assert f'data-theme="{theme}"' in rendered
    exported = re.search(r"window\.LDR_DEFAULT_THEME = (.+?);", rendered)
    assert exported is not None
    assert json.loads(exported.group(1)) == theme