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.

10.5 KB
Raw Blame
"""Fixtures for the ``connected`` suite, ported from ``origin/main``.

These tests open REAL per-user encrypted (SQLCipher) databases through the
real registration/login flow and then drive production code against them.
That is the whole point: a ``MagicMock`` session cannot show that a claim
was atomic, that a reclaim skipped a live thread, or that a rejected
submission left no rows behind.

Port notes (plumbing only):

* ``flask.Flask`` / ``FlaskClient`` type hints -> the branch's ``client``
  fixture, which is a Flask-compat-shimmed Starlette ``TestClient``.
* ``web.routes.globals.get_active_research_ids`` ->
  ``web.research_state.get_active_research_ids`` (``routes/globals.py`` is
  now a re-export shim over it).
* Registration/login POSTs are state-changing and therefore go through
  ``CSRFMiddleware`` now, so :func:`register_connected_user` fetches a
  session CSRF token first and passes ``follow_redirects=False`` explicitly
  (httpx's TestClient follows redirects by default, Flask's did not — a
  ported ``== 302`` would otherwise silently see the followed 200).
"""

from __future__ import annotations

from collections.abc import Generator
from dataclasses import dataclass
from pathlib import Path
from uuid import uuid4

import pytest


def _sqlcipher_available() -> bool:
    """True if the SQLCipher backend (sqlcipher3[-binary]) is importable.

    The connected suite opens real per-user encrypted databases, so it must
    SKIP (not error) on a venv without the SQLCipher binary — matching the
    convention in tests/database/test_sqlcipher_integration.py.
    """
    try:
        from local_deep_research.database.sqlcipher_compat import (
            get_sqlcipher_module,
        )

        get_sqlcipher_module()
        return True
    except Exception:
        return False


def pytest_collection_modifyitems(config, items):  # noqa: ANN001
    """Stamp every connected test with the ``connected`` marker, and SKIP the
    whole suite (not error) when SQLCipher is unavailable.

    pytest calls this hook with the WHOLE session's collection, not just the
    items below this conftest's directory, so filter by path first: without
    that, merely loading this conftest in a mixed run stamped ``connected``
    onto every unrelated test, and skipped them all when SQLCipher was
    missing (#6301).
    """
    connected_dir = Path(__file__).resolve().parent
    connected_items = [
        item
        for item in items
        if getattr(item, "path", None) is not None
        and item.path.resolve().is_relative_to(connected_dir)
    ]
    if not connected_items:
        return
    skip_no_sqlcipher = pytest.mark.skip(
        reason="SQLCipher binary not available; connected suite needs encrypted DBs"
    )
    sqlcipher_ok = _sqlcipher_available()
    for item in connected_items:
        item.add_marker(pytest.mark.connected)
        if not sqlcipher_ok:
            item.add_marker(skip_no_sqlcipher)


@dataclass(frozen=True, slots=True)
class ConnectedUser:
    app: object
    client: object
    username: str
    password: str
    data_root: Path


def csrf_headers(client) -> dict[str, str]:
    """A session-bound CSRF token header for the given test client.

    Under Flask the test client bypassed CSRF entirely; under FastAPI
    ``CSRFMiddleware`` is always active, and register/login are no longer
    exempt.
    """
    resp = client.get("/auth/csrf-token")
    assert resp.status_code == 200, resp.text
    return {"X-CSRFToken": resp.json()["csrf_token"]}


def register_connected_user(case: ConnectedUser):
    """Register the fixture's user through the real auth flow.

    Returns the response so callers can assert the 302 the original tests
    asserted. ``follow_redirects=False`` is explicit: httpx follows by
    default and would turn the 302 into a 200.
    """
    response = case.client.post(
        "/auth/register",
        data={
            "username": case.username,
            "password": case.password,
            "confirm_password": case.password,
            "acknowledge": "true",
        },
        headers=csrf_headers(case.client),
        follow_redirects=False,
    )
    assert response.status_code == 302, response.text[:500]
    return response


def _assert_probe_cleanup(usernames: list[str]) -> None:
    """Teardown half of ``connected_user_cleanup_probe``.

    Lives at module level so tests can drive both probe phases directly
    (see ``tests/connected/test_cleanup_probe_attribution.py``): every
    per-user store the fixture's consumer could have touched must be
    empty for the consumer's username, and the process-wide
    active-research registry must not leak (#5591).
    """
    assert len(usernames) == 1
    username = usernames[0]

    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.database.session_passwords import (
        session_password_store,
    )
    from local_deep_research.database.temp_auth import temp_auth_store
    from local_deep_research.database.thread_local_session import (
        thread_session_manager,
    )
    from local_deep_research.web.auth.session_manager import session_manager
    from local_deep_research.web.research_state import get_active_research_ids

    with session_password_store._lock:
        leaked_session_passwords = [
            key for key in session_password_store._store if key[0] == username
        ]
    assert not leaked_session_passwords, (
        f"session_password_store leak for {username}"
    )

    with temp_auth_store._lock:
        leaked_temp_auth = [
            token
            for token, entry in temp_auth_store._store.items()
            if entry["username"] == username
        ]
    assert not leaked_temp_auth, f"temp_auth_store leak for {username}"

    with session_manager._lock:
        leaked_sessions = [
            sid
            for sid, data in session_manager.sessions.items()
            if data["username"] == username
        ]
    assert not leaked_sessions, f"session_manager leak for {username}"

    with thread_session_manager._lock:
        leaked_thread_credentials = [
            tid
            for tid, creds in thread_session_manager._thread_credentials.items()
            if creds[0] == username
        ]
    assert not leaked_thread_credentials, (
        f"thread_session_manager leak for {username}"
    )

    # No absence assertion for ``_user_init_locks``: those locks deliberately
    # retain stable process-lifetime identity. Removing one during teardown can
    # race a caller that looked it up but has not acquired it yet, creating two
    # concurrent initialisation gates for the same user.

    with db_manager._connections_lock:
        assert username not in db_manager.connections, (
            f"db connection leak for {username}"
        )
        # No assertion on db_manager._init_locks: close_user_database
        # deliberately keeps the per-user init lock (see its docstring and
        # tests/database/test_concurrent_user_db_open.py); the autouse
        # cleanup_database_connections sweep clears the whole mapping.

    leaked_active_research = get_active_research_ids()
    assert not leaked_active_research, (
        f"active research leak after connected test: {leaked_active_research}"
    )


@pytest.fixture
def connected_user_cleanup_probe() -> Generator[list[str], None, None]:
    # The probe only runs through ``connected_user``, so an entry leaked by
    # an earlier test that never requested this fixture would survive until
    # the next ``connected_user`` teardown and be attributed to that
    # unrelated test (#5591). Fail setup instead: contamination visible
    # here predates the current test.
    from local_deep_research.web.research_state import get_active_research_ids

    preexisting_active_research = get_active_research_ids()
    assert not preexisting_active_research, (
        f"active research registry not empty before test start; the leak "
        f"predates this test (left by an earlier test): "
        f"{preexisting_active_research}"
    )

    usernames: list[str] = []
    yield usernames

    _assert_probe_cleanup(usernames)


@pytest.fixture
def connected_user(
    app,
    client,
    temp_data_dir: Path,
    connected_user_cleanup_probe: list[str],
) -> Generator[ConnectedUser, None, None]:
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.database.library_init import pop_user_init_lock
    from local_deep_research.database.session_passwords import (
        session_password_store,
    )
    from local_deep_research.database.temp_auth import temp_auth_store
    from local_deep_research.database.thread_local_session import (
        thread_session_manager,
    )
    from local_deep_research.web.auth.session_manager import session_manager

    # Save the raw override (not the resolved property value) so the restore
    # puts the manager back into lazy resolution when no override was set.
    original_data_dir_override = db_manager._data_dir_override
    isolated_data_root = temp_data_dir
    username = f"pytest_user_{uuid4().hex[:12]}"
    connected_user_cleanup_probe.append(username)

    try:
        db_manager.data_dir = isolated_data_root / "encrypted_databases"
        yield ConnectedUser(
            app=app,
            client=client,
            username=username,
            password="ConnectedPass123!",
            data_root=isolated_data_root,
        )
    finally:
        session_password_store.clear_all_for_user(username)
        _ = session_manager.destroy_all_user_sessions(username)

        # Collect this user's temp-auth tokens under the store lock, then
        # clear each entry outside the lock (clear_entry re-acquires it).
        with temp_auth_store._lock:
            temp_tokens = [
                token
                for token, entry in temp_auth_store._store.items()
                if entry["username"] == username
            ]
        for token in temp_tokens:
            temp_auth_store.clear_entry(token)

        # Collect thread IDs bound to this user under the manager lock, then
        # clean each thread outside the lock (cleanup_thread re-acquires it).
        with thread_session_manager._lock:
            user_thread_ids = [
                tid
                for tid, creds in thread_session_manager._thread_credentials.items()
                if creds[0] == username
            ]
        for tid in user_thread_ids:
            thread_session_manager.cleanup_thread(tid)

        pop_user_init_lock(username)
        db_manager.close_user_database(username)
        db_manager._data_dir_override = original_data_dir_override