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.

6.1 KB
Raw Blame
"""The no-JS settings form must not reject values it just rendered.

A <select> whose options include a null value -- "All Time" for
``search.engine.web.serper.default_params.time_period``, for instance -- has no
way to say None in an HTML form. The browser posts ``""``. Before this was
handled, ``validate_setting`` compared ``""`` against the allowed values
``[None, "day", "week", "month", "year"]``, found no match, and counted the
setting as failed -- so a JavaScript-disabled user who opened /settings/ and
pressed Save without touching anything was told their settings were "failing".

Found by the Puppeteer suite (``test_settings_form_post_fallback_ci.js``) on its
first run in four months, which reported "Saved with 3 setting(s) failing" where
it expected "Settings saved."; reproduced here through the same round-trip the
form performs. The JSON route is unaffected -- it sends a real null -- which is
why only the no-JS path showed it.
"""

import json


# allow: no-sut-import — this module drives the settings write path over HTTP
# through the `authenticated_client` fixture rather than importing a symbol from
# it. The subject is the round-trip itself: what a browser posts back for a
# rendered <select>, and whether the route accepts it. There is nothing to
# import and call directly — a unit call would have to fabricate the posted
# form, which is the exact thing that was wrong. Verified this exercises
# production code: removing the null-option coercion from
# web/routers/settings.py fails both tests here, naming the affected keys.


def _post_all_settings(client, caplog):
    """GET every setting, post them all straight back, return the failures."""
    data = client.get("/settings/api").json()
    settings = data.get("settings") if isinstance(data, dict) else data
    items = (
        settings.items()
        if isinstance(settings, dict)
        else [(s["key"], s) for s in settings]
    )

    form = {}
    for key, entry in items:
        value = entry.get("value") if isinstance(entry, dict) else entry
        if isinstance(value, bool):
            form[key] = "on" if value else ""
        elif value is None:
            form[key] = ""  # exactly what a browser posts for a null option
        elif isinstance(value, (list, dict)):
            # settings.js::processSettings serializes structured values as
            # JSON before rendering the inputs. Python repr is not the form
            # payload and must still be rejected by the strict list parser.
            form[key] = json.dumps(value, indent=2)
        else:
            form[key] = str(value)

    assert len(form) > 100, (
        f"only {len(form)} settings posted; the round-trip is not exercising "
        "the real form and any 'no failures' result would be vacuous"
    )

    with caplog.at_level("WARNING"):
        client.post(
            "/settings/save_settings", data=form, follow_redirects=False
        )

    return sorted(
        {
            record.getMessage()
            for record in caplog.records
            if "Validation failed for setting" in record.getMessage()
        }
    )


def _keys(failures):
    return {
        f.split("Validation failed for setting ")[1].split(":")[0]
        for f in failures
    }


class TestNullOptionSelectsRoundTrip:
    def test_null_valued_selects_are_not_reported_as_failures(
        self, authenticated_client, loguru_caplog_full
    ):
        """The two known null-option selects must survive an untouched Save."""
        failed = _keys(
            _post_all_settings(authenticated_client, loguru_caplog_full)
        )

        null_option_selects = {
            "search.engine.web.serper.default_params.time_period",
            "search.engine.web.sofya.default_params.freshness",
        }
        leaked = null_option_selects & failed
        assert not leaked, (
            f"{sorted(leaked)} were rejected by the no-JS Save path even though "
            "the user changed nothing. An HTML form posts '' for a null option; "
            "the POST path must map that back to None when None is an allowed "
            "option."
        )

    def test_no_setting_fails_a_round_trip_it_was_not_asked_to_change(
        self, authenticated_client, loguru_caplog_full
    ):
        """The whole property, now that both causes are fixed: opening
        /settings/ and pressing Save without touching anything must report
        nothing as failing.

        This replaces a narrower test that pinned ``app.theme`` as the single
        known remaining failure. ``app.theme`` shipped as ``"dark"`` while
        ``settings/manager.py`` replaces that setting's options at runtime from
        the theme registry, which has no ``dark`` entry — the shipped default
        named a theme that no longer existed. It was reset to ``"dark"`` in two
        further places in ``web/routers/settings.py``, so all three had to move
        together or a reset would restore the invalid value.

        Asserting zero, rather than a known count, is the point: a count that
        can be edited upward absorbs the next stale default silently, which is
        how this one survived long enough to be found by a browser test rather
        than by validation.
        """
        original = authenticated_client.get(
            "/settings/api/report.export_formats"
        )
        assert original.status_code == 200, original.text
        original_formats = original.json()["value"]
        assert isinstance(original_formats, list) and original_formats

        failures = _post_all_settings(authenticated_client, loguru_caplog_full)

        assert not failures, (
            "settings rejected by the no-JS Save path without the user "
            "changing anything:\n  "
            + "\n  ".join(f[:200] for f in failures)
            + "\n\nEach means a stored value does not validate against its own "
            "options — either a shipped default drifted from a "
            "dynamically-generated option list, or a type does not survive the "
            "HTML form round-trip."
        )

        saved = authenticated_client.get("/settings/api/report.export_formats")
        assert saved.status_code == 200, saved.text
        assert saved.json()["value"] == original_formats