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 - the subject is browser JS with no module exports:
# settings.js is a single IIFE bound to `window`, so there is nothing to
# import and a runtime test would have to load the whole file with every
# global it touches stubbed. These read the file as DATA and assert the
# wiring the browser depends on. Each check carries a positive control so a
# selector that stops matching fails loudly instead of passing empty.
"""The Settings page's refresh buttons must be wired on first load (#6407).

Three shapes, all on the same path:

* ``setupRefreshButtons()`` had exactly ONE call site — the tab-switch
  listener — so on a fresh ``/settings/`` load the model refresh button was
  a total no-op until the user switched tabs at least once.
* ``#llm.provider-refresh`` is rendered by the template
  (``settings_form.html``, ``show_refresh=True``) and was bound nowhere.
* ``initializeModelDropdowns()`` carried a handler for
  ``#llm-model-refresh`` — with a HYPHEN — which is always ``null``: the
  template emits ``llm.model-refresh`` with a dot. Correcting the id there
  would have bound a second full handler and doubled the fetches per click,
  so the block had to go rather than be repaired.
"""

from pathlib import Path

import pytest

SETTINGS_JS = (
    Path(__file__).resolve().parents[2]
    / "src/local_deep_research/web/static/js/components/settings.js"
)


@pytest.fixture(scope="module")
def source() -> str:
    text = SETTINGS_JS.read_text(encoding="utf-8")
    # Positive control: a parser reading the wrong file would pass every
    # "is absent" assertion below for free.
    assert "function setupRefreshButtons()" in text, (
        "read the wrong file, or the function was renamed"
    )
    return text


def test_refresh_buttons_are_bound_outside_the_tab_switch_listener(source):
    """The initial-load render must bind them too, not only a tab switch."""
    call_sites = source.count("setupRefreshButtons();")

    assert call_sites >= 2, (
        "setupRefreshButtons() is called from one place only; on a fresh "
        "page load the refresh buttons are never bound"
    )


def test_the_binding_happens_where_the_markup_was_just_rendered(source):
    """Anchored on the render, not on a line number: the buttons live in the
    markup `renderSettingsByTab()` produces, so the call has to follow it."""
    anchor = "setupCustomDropdowns();"
    assert anchor in source
    after_render = source[source.index(anchor) : source.index(anchor) + 800]

    assert "setupRefreshButtons();" in after_render


def test_the_provider_refresh_button_is_bound(source):
    """The template renders it; nothing referenced it."""
    assert "llm.provider-refresh" in source


def _setup_refresh_buttons_body(source: str) -> str:
    """The text of `setupRefreshButtons()`, sliced on brace depth."""
    start = source.index("function setupRefreshButtons()")
    depth = 0
    for offset in range(start, len(source)):
        char = source[offset]
        if char == "{":
            depth += 1
        elif char == "}":
            depth -= 1
            if depth == 0:
                return source[start : offset + 1]
    raise AssertionError("setupRefreshButtons() is not brace-balanced")


def test_every_refresh_binding_is_idempotent(source):
    """`renderSettingsByTab()` rebuilds `#settings-content` via innerHTML, but
    a tab switch re-runs this function against buttons that are still live, so
    a second call must not double the fetches per click. `addEventListener`
    would; an `onclick` assignment is one handler per node by definition —
    that is the trap which kept the second call site from being added in the
    first place."""
    body = _setup_refresh_buttons_body(source)
    # Control: the slice really is the function, not an empty match.
    assert "getElementById('llm.model-refresh')" in body

    for button in (
        "providerRefreshBtn",
        "modelRefreshBtn",
        "searchEngineRefreshBtn",
    ):
        # Control: this is a button the function really handles.
        assert f"const {button} = document.getElementById(" in body, button

        assert f"{button}.onclick = function()" in body, (
            f"{button} must be bound by assignment"
        )
        assert f"{button}.addEventListener('click'" not in body, (
            f"{button} collects one more handler per call"
        )


def test_the_hyphenated_selector_is_gone(source):
    """`#llm-model-refresh` never matched anything: the template emits the id
    with a dot. It must not come back as a second handler for the same
    button."""
    assert "querySelector('#llm-model-refresh')" not in source
    assert 'querySelector("#llm-model-refresh")' not in source
    # Control: the id that DOES exist is still referenced.
    assert "getElementById('llm.model-refresh')" in source