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.

3.1 KB
Raw Blame
"""Phase attribution for ``connected_user_cleanup_probe`` (#5591).

The probe's active-research check runs only through ``connected_user``, so
an entry leaked by an earlier test that never requested this fixture used
to survive until the next ``connected_user`` teardown and be reported
against that unrelated test as ``active research leak after connected
test``. Probe setup now fails first when the registry is already dirty —
the contamination predates the current test — while teardown still
attributes entries created while the fixture consumer ran to that test.
"""

from __future__ import annotations

import pytest

from local_deep_research.web import research_state
from tests.connected.conftest import _assert_probe_cleanup

#: A distinctive id for registry entries seeded by these tests.
SEEDED_ID = "cleanup-probe-attribution-seed"


@pytest.fixture(autouse=True)
def _clean_active_research_registry():
    """Keep the registry free of this file's seeded entry, pass or fail.

    Every test here asserts on the registry's contents, so an entry leaked
    by an earlier test on the same worker would either make these tests
    pass vacuously or fail them with the wrong wording. Refuse to start on
    a dirty registry instead, naming the leak as pre-existing.
    """
    research_state.remove_active_research(SEEDED_ID)
    leaked = list(research_state.get_active_research_ids())
    assert not leaked, (
        f"active research registry already dirty before this test "
        f"(leak predates it, left by an earlier test): {leaked}"
    )
    yield
    research_state.remove_active_research(SEEDED_ID)


def test_setup_attributes_pre_existing_leak_to_an_earlier_test(request):
    """A dirty registry at probe setup must fail setup, not the next test.

    The seeded entry was ``set_active_research`` here — before the probe
    fixture even started — so the failure message must say the leak
    predates this test. ``getfixturevalue`` triggers probe setup inside
    the ``raises`` block; if the setup guard were missing, the fixture
    would instantiate cleanly and ``DID NOT RAISE`` fails this test.
    """
    research_state.set_active_research(SEEDED_ID, {"status": "running"})

    with pytest.raises(AssertionError, match="predates this test"):
        request.getfixturevalue("connected_user_cleanup_probe")


def test_teardown_still_attributes_entries_created_during_the_test():
    """An entry created after probe setup stays the consumer's fault.

    Drives the shared teardown half directly with the consumer's username
    registered, mirroring what pytest does when the fixture's consumer
    finishes. The existing failure wording is load-bearing triage text
    and must be retained.
    """
    research_state.set_active_research(SEEDED_ID, {"status": "running"})

    with pytest.raises(
        AssertionError, match="active research leak after connected test"
    ):
        _assert_probe_cleanup(["probe_attribution_user"])


def test_teardown_passes_with_an_empty_registry():
    """Sanity: a clean registry (and untouched per-user stores) is quiet."""
    _assert_probe_cleanup(["probe_attribution_user"])