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.

9.0 KB
Raw Blame
# allow: no-sut-import — imports the SUT inside isolated subprocess probes so import order is tested in a fresh interpreter (in-process imports would mask the cycle)
"""Regression tests for import-order parity between the settings service and
settings routes modules.

The service module must be importable on its own without pulling in the
routes module, regardless of which module is imported first. That property
still holds today, exactly as it did under Flask, and is what these tests
primarily guard.

Historical note (pre-FastAPI): under Flask, ``web/routes/settings_routes.py``
re-exported ``validate_setting``/``DYNAMIC_SETTINGS`` from
``web/services/settings_service.py``, so both modules bound the literal same
objects regardless of import order -- these tests originally asserted that
via ``is`` identity checks.

Under the FastAPI migration, ``web/routes/settings_routes.py`` was replaced
by ``web/routers/settings.py``. That module does *not* import
``validate_setting``/``DYNAMIC_SETTINGS`` from the service at all --
``web/services/settings_service.py`` (see its ``validate_setting`` docstring
comment, referencing #2898) now carries its own inlined copy of both,
specifically so the service module never needs to import the router (which
would reintroduce the cycle this test guards against). ``web/routers/settings.py``
independently defines its own ``DYNAMIC_SETTINGS``/``validate_setting`` too.
So the two symbols are, by design, no longer the same objects in either
import order -- the "same identity" guarantee genuinely no longer applies
under FastAPI, and asserting it would just permanently fail on an
intentional architectural change, not a regression.

What *does* still need to hold, and what these tests now check in its place:
  * No import cycle: importing the service module never pulls the routes
    module into ``sys.modules`` as a side effect (checked for the
    service-first order; the routes module legitimately imports the service
    module, so the reverse is expected and not asserted).
  * Behavioral/value parity between the two independent copies: since they
    are intentionally-duplicated code (not a shared reference), nothing
    stops them drifting apart silently. ``DYNAMIC_SETTINGS`` is compared by
    value, and ``validate_setting`` is compared by output across a small set
    of representative inputs (numeric range, select-with-options, and a
    dynamically-populated select key), in both import orders.

Each probe runs in a fresh subprocess (via sys.executable) so that
module-level state from the test process cannot mask a cycle.
"""

import subprocess
import sys
import textwrap


def _run_probe(script):
    """Run an import probe in a fresh interpreter; return its stdout text.

    The probe must exit zero and print key=value markers on stdout. Any
    non-zero exit (including ImportError) is reported with full diagnostics.
    """
    result = subprocess.run(
        [sys.executable, "-c", textwrap.dedent(script)],
        capture_output=True,
        text=True,
        timeout=60,
        check=False,
    )
    assert result.returncode == 0, (
        "import probe exited with code {code}.\n"
        "STDOUT:\n{stdout}\n"
        "STDERR:\n{stderr}\n".format(
            code=result.returncode,
            stdout=result.stdout,
            stderr=result.stderr,
        )
    )
    return result.stdout


def _parse_markers(stdout):
    """Parse key=value lines from probe stdout into a dict."""
    return dict(
        line.split("=", 1)
        for line in stdout.splitlines()
        if "=" in line and not line.startswith(" ")
    )


# Shared behavioral-parity probe body. Builds a few representative fake
# ``Setting``-like objects (validate_setting only duck-types on
# key/ui_element/min_value/max_value/options) and compares
# ``validate_setting``'s output between the service and routes copies, plus
# ``DYNAMIC_SETTINGS`` by value. Printed as PARITY=same/different rather than
# IDENTITY, since these are intentionally-independent copies now (see module
# docstring) -- this is a drift check, not an object-identity check.
_PARITY_PROBE_BODY = """
        from types import SimpleNamespace

        same_dynamic_value = list(service_mod.DYNAMIC_SETTINGS) == list(
            routes_mod.DYNAMIC_SETTINGS
        )
        print("DYNAMIC_PARITY=" + ("same" if same_dynamic_value else "different"))

        cases = [
            SimpleNamespace(
                key="some.number", ui_element="number", value=5,
                min_value=1, max_value=10, options=None,
            ),
            SimpleNamespace(
                key="some.number", ui_element="number", value=50,
                min_value=1, max_value=10, options=None,
            ),
            SimpleNamespace(
                key="optional.number", ui_element="number", value=None,
                min_value=1, max_value=10, options=None,
            ),
            SimpleNamespace(
                key="optional.slider", ui_element="slider", value=None,
                min_value=1, max_value=10, options=None,
            ),
            SimpleNamespace(
                key="optional.range", ui_element="range", value=None,
                min_value=1, max_value=10, options=None,
            ),
            SimpleNamespace(
                key="invalid.number", ui_element="number", value="not-a-number",
                min_value=1, max_value=10, options=None,
            ),
            SimpleNamespace(
                key="invalid.range", ui_element="range", value="not-a-number",
                min_value=1, max_value=10, options=None,
            ),
            SimpleNamespace(
                key="invalid.slider", ui_element="slider", value="not-a-number",
                min_value=1, max_value=10, options=None,
            ),
            SimpleNamespace(
                key="some.select", ui_element="select", value="not-an-option",
                min_value=None, max_value=None,
                options=[{"value": "a"}, {"value": "b"}],
            ),
            SimpleNamespace(
                key="llm.provider", ui_element="select", value="anything",
                min_value=None, max_value=None, options=[{"value": "a"}],
            ),
        ]
        validate_parity = all(
            service_mod.validate_setting(c, c.value)
            == routes_mod.validate_setting(c, c.value)
            for c in cases
        )
        print("VALIDATE_PARITY=" + ("same" if validate_parity else "different"))
"""


def test_service_first_does_not_import_routes():
    """Importing the service alone must not load the routes module.

    After importing the service, routes must be absent from sys.modules; only
    then do we import routes and compare the two independently-defined
    ``DYNAMIC_SETTINGS``/``validate_setting`` copies for value/behavioral
    parity (see module docstring for why parity, not identity, is now the
    right check).
    """
    stdout = _run_probe(
        """
        import sys

        from local_deep_research.web.services import settings_service as service_mod

        routes_key = "local_deep_research.web.routers.settings"
        print("ROUTES_PRESENT=" + ("yes" if routes_key in sys.modules else "no"))

        from local_deep_research.web.routers import settings as routes_mod

        """
        + _PARITY_PROBE_BODY,
    )
    markers = _parse_markers(stdout)
    assert markers.get("ROUTES_PRESENT") == "no", (
        "routes module was imported as a side effect of importing the "
        "service module.\nProbe stdout:\n" + stdout
    )
    assert markers.get("DYNAMIC_PARITY") == "same", (
        "DYNAMIC_SETTINGS value differs between "
        "web.services.settings_service and web.routers.settings.\n"
        "Probe stdout:\n" + stdout
    )
    assert markers.get("VALIDATE_PARITY") == "same", (
        "validate_setting behavior differs between "
        "web.services.settings_service and web.routers.settings.\n"
        "Probe stdout:\n" + stdout
    )


def test_routes_first_binds_equivalent_behaviour():
    """Importing routes first must not change the parity outcome.

    Routes imports (part of) the service at module load; regardless of that
    import order, the two independently-defined
    ``DYNAMIC_SETTINGS``/``validate_setting`` copies must still agree in
    value/behavior (see module docstring for why this is a parity check
    rather than an object-identity check under FastAPI).
    """
    stdout = _run_probe(
        """
        from local_deep_research.web.routers import settings as routes_mod
        from local_deep_research.web.services import settings_service as service_mod

        """
        + _PARITY_PROBE_BODY,
    )
    markers = _parse_markers(stdout)
    assert markers.get("DYNAMIC_PARITY") == "same", (
        "DYNAMIC_SETTINGS value differs between "
        "web.services.settings_service and web.routers.settings when "
        "routes is imported first.\nProbe stdout:\n" + stdout
    )
    assert markers.get("VALIDATE_PARITY") == "same", (
        "validate_setting behavior differs between "
        "web.services.settings_service and web.routers.settings when "
        "routes is imported first.\nProbe stdout:\n" + stdout
    )