~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
import functools
import logging
import os
import sys
import tempfile
import types
import shutil
import uuid
from contextlib import contextmanager, suppress
from pathlib import Path
from unittest.mock import Mock, patch
import platformdirs
import pytest
from loguru import logger
from sqlalchemy import create_engine, event
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import sessionmaker, Session
import local_deep_research.utilities.db_utils as db_utils_module
from local_deep_research.database.models import Base
from local_deep_research.database.auth_db import (
dispose_auth_engine,
init_auth_database,
)
try:
from local_deep_research.web.app_factory import create_app
except ImportError:
create_app = None # Flask app_factory removed in FastAPI migration
from local_deep_research.settings.manager import (
SettingsManager,
)
# The document and collection deletion services bind
# ``database.session_context.get_user_db_session`` when they are first
# imported. Several code paths import the deletion package lazily
# (``get_collection_documents``, the indexing routes and workers,
# ``LibraryService.delete_document`` ...), and many tests patch that helper
# around them. Whichever such test imported the package first would leave its
# mock in the services for the rest of the process, and later tests that
# delete through the real services would fail depending on test order.
# Import the package here, before any test can patch the helper.
import local_deep_research.research_library.deletion # noqa: F401
# Import our mock fixtures
try:
from .mock_fixtures import (
get_mock_arxiv_response,
get_mock_error_responses,
get_mock_findings,
get_mock_google_pse_response,
get_mock_ollama_response,
get_mock_pubmed_article,
get_mock_pubmed_response,
get_mock_research_history,
get_mock_search_results,
get_mock_semantic_scholar_response,
get_mock_settings,
get_mock_wikipedia_response,
)
except ImportError:
# Mock fixtures not yet created, skip for now
pass
def generate_unique_test_username(prefix: str = "pytest_user") -> str:
"""Generate unique username using UUID instead of timestamp.
This ensures no collisions when running tests in parallel with pytest-xdist.
"""
return f"{prefix}_{uuid.uuid4().hex[:12]}"
@pytest.fixture(autouse=True)
def reset_all_singletons():
"""Reset all singletons before and after each test.
This ensures proper isolation when running with pytest-xdist (-n auto).
Without this, singleton state can leak between tests running in parallel,
causing intermittent failures.
"""
def _reset():
# SocketIOService
try:
from local_deep_research.web.services.socket_service import (
SocketIOService,
)
SocketIOService._instance = None
except ImportError:
pass
# BackgroundJobScheduler (both class and module-level)
try:
from local_deep_research.scheduler import background
# Stop any running scheduler BEFORE dropping the singleton
# reference so its APScheduler thread doesn't emit logs to a
# closed pytest stderr sink during teardown.
if background.BackgroundJobScheduler._instance is not None:
try:
background.BackgroundJobScheduler._instance.stop()
except Exception:
# Never fail fixture teardown on scheduler edge cases.
pass
background.BackgroundJobScheduler._instance = None
# Also reset module-level global if it exists
if hasattr(background, "_scheduler_instance"):
background._scheduler_instance = None
except ImportError:
pass
# QueueProcessorV2 (module-level singleton thread). Like the
# scheduler above, create_app() starts this background thread, but
# nothing stopped it between tests — so the first test's processor
# ran for the whole worker, looping through SettingsManager ->
# SQLCipher connection opens on the shared db_manager concurrently
# with every later test (and emitting logs to a closed pytest
# stderr sink at teardown). Stop it here so each test starts clean;
# the next create_app() restarts it (stop() clears the running
# guard). Tests that exercise the queue patch this object, so
# stopping the real thread does not affect them.
try:
from local_deep_research.web.queue.processor_v2 import (
queue_processor,
)
queue_processor.stop()
except ImportError:
pass
except Exception:
# Never fail fixture teardown on queue-processor edge cases.
pass
# ProviderDiscovery - reset both class-level and module-level state
try:
from local_deep_research.llm.providers import auto_discovery
from local_deep_research.llm.providers.auto_discovery import (
ProviderDiscovery,
)
# Reset class-level singleton
ProviderDiscovery._instance = None
ProviderDiscovery._providers = {}
if hasattr(ProviderDiscovery, "_discovered"):
ProviderDiscovery._discovered = False
# Reset the module-level singleton instance's state
if hasattr(auto_discovery, "provider_discovery"):
auto_discovery.provider_discovery._discovered = False
auto_discovery.provider_discovery._providers = {}
# Re-register the built-in providers. get_llm() has no fallback
# construction path anymore, so a test that called
# clear_llm_registry() would otherwise break every later test
# that uses a built-in provider (order-dependent failures).
from local_deep_research.llm.providers import discover_providers
discover_providers(force_refresh=True)
except ImportError:
pass
# AccountLockoutManager singleton
try:
from local_deep_research.security import account_lockout
account_lockout._manager = None
except ImportError:
pass
# slowapi limiter buckets.
#
# The limiter is a module-level singleton whose in-memory storage
# accumulates hits across every test in the process. A test that
# merely logs in a few times can therefore push an unrelated later
# test over a per-IP limit and turn its 200 into a 429 — a failure
# that reproduces only under full-suite ordering and vanishes when
# the file is run alone, which is the worst possible shape for a
# CI signal. Individual suites already call limiter.reset()
# by hand (e.g. tests/web/routers/test_auth_rate_limits.py); doing
# it here makes the isolation uniform instead of opt-in.
try:
from local_deep_research.web.dependencies.rate_limit import (
limiter,
)
limiter.reset()
except (ImportError, AttributeError):
pass
# AdaptiveRateLimitTracker: no singleton to reset — get_tracker()
# returns a fresh instance each call.
_reset()
yield
_reset()
# NOTE: this fixture must NOT take a ``monkeypatch`` dependency. pytest
# registers autouse fixtures in alphabetical order (conftest attributes are
# scanned via ``dir()``), and a fixture's dependencies are set up alongside
# it while finalization is LIFO over setup order. A ``monkeypatch``
# dependency here would set up ``monkeypatch`` before
# ``reset_all_singletons`` (this fixture's name sorts first) and defer
# ``monkeypatch.undo()`` to AFTER the singleton-reset teardown, which then
# calls ``discover_providers(force_refresh=True)`` through whatever doubles
# the test left installed — "ERROR at teardown" for every such test. The
# manual save/restore below leaves ``monkeypatch`` to test-level doubles
# only, so their undo runs before the reset teardown as it did before this
# fixture existed. ``tests/test_conftest_fixture_ordering.py`` pins both
# properties.
@pytest.fixture(autouse=True)
def isolate_server_process_dump_policy():
"""Server startup tests must not change the pytest process's OS policy.
Helper tests receive the real function and restore it only after
replacing its resource/ctypes boundaries. Startup wiring tests replace
this stub with their own recorder or failure. Fresh interpreter probes
can apply the real policy without affecting the pytest process.
"""
from local_deep_research.security import process_dump_policy
real_enforcer = process_dump_policy.enforce_process_dump_policy
process_dump_policy.enforce_process_dump_policy = lambda: True
try:
yield real_enforcer
finally:
process_dump_policy.enforce_process_dump_policy = real_enforcer
@pytest.fixture(autouse=True)
def database_operation_timeout():
"""Set shorter timeouts for database operations in tests.
This helps tests fail fast instead of hanging indefinitely when
database contention occurs.
"""
original = os.environ.get("LDR_DB_BUSY_TIMEOUT")
os.environ["LDR_DB_BUSY_TIMEOUT"] = "5000" # 5 seconds in tests
yield
if original:
os.environ["LDR_DB_BUSY_TIMEOUT"] = original
else:
os.environ.pop("LDR_DB_BUSY_TIMEOUT", None)
def pytest_configure(config):
"""Configure pytest with custom markers."""
config.addinivalue_line(
"markers",
"requires_llm: mark test as requiring a real LLM (skipped when LDR_TESTING_WITH_MOCKS=true)",
)
config.addinivalue_line(
"markers",
"integration: mark test as an integration test requiring live network access (skipped when LDR_TESTING_WITH_MOCKS=true)",
)
# In CI, LDR_TESTING_WITH_MOCKS is set via Docker environment variables
# For local testing, set it here if not already set
if not os.environ.get("LDR_TESTING_WITH_MOCKS"):
os.environ["LDR_TESTING_WITH_MOCKS"] = "true"
# Unless AUTO_DOWNLOAD_NLTK holds a value other than "true", importing
# unstructured.nlp.tokenize, as unstructured's text partitioners do,
# downloads the NLTK data they classify text with if any of it is
# missing. That download would write the data directory all xdist
# workers share without nltk_parser_data's lock, so tests get the data
# from that autouse fixture alone.
os.environ["AUTO_DOWNLOAD_NLTK"] = "false"
@pytest.fixture(scope="session", autouse=True)
def no_readability_node_subprocess():
"""Keep Readability.js (a Node subprocess) out of the test suite.
readabilipy shells out to node for every extraction — ~2.9s per call,
with no timeout of its own. Content-extraction tests exercise our
parsing branches, not Readability.js, and under xdist one slow
subprocess costs the whole worker at the 180s pytest-timeout rather
than failing a single test (#6209).
A budget of 0 skips both the per-page node call and the availability
probe — itself an unbounded ``node -v``, plus an ``npm install`` on
every install, since the wheel ships no ``node_modules`` of its own; it
is not a first-run edge case. Tests that need the Node path restore the
budget themselves; the probe then does run — cached for the rest of the
process in general, though ``test_readability_node_timeout.py``'s own
autouse fixture resets that cache before and after each of its tests, so
within that module the probe can run again per test.
"""
from local_deep_research.research_library.downloaders.extraction import (
readability_extractor,
)
original = readability_extractor.NODE_TIMEOUT_SECONDS
readability_extractor.NODE_TIMEOUT_SECONDS = 0
yield
readability_extractor.NODE_TIMEOUT_SECONDS = original
# Run by nltk_parser_data in a separate interpreter. The check tokenizes and
# tags a sentence, as unstructured's text classification does, so a package
# that an interrupted download left incomplete counts as missing. The exit
# status is 0 only if the data passed that check. When it fails, the script
# downloads the packages named as arguments, if any: nltk.download compares
# each package with the size and checksum in its index and downloads it
# again if they differ.
_INSTALL_NLTK_PARSER_DATA = """
import sys
import nltk
def usable():
try:
sentence = nltk.sent_tokenize("Otters build dams.")[0]
nltk.pos_tag(nltk.word_tokenize(sentence))
except Exception:
return False
return True
if not usable():
for package in sys.argv[1:]:
nltk.download(package, quiet=True)
sys.exit(1)
"""
@contextmanager
def _file_lock(path, *, exclusive):
"""Hold a lock on *path* that other processes wait for.
A shared lock waits only for an exclusive one; an exclusive lock waits
for every other lock on the file.
"""
try:
import fcntl
except ImportError: # Windows: no flock; processes there do not wait
yield
return
with open(path, "a") as handle:
fcntl.flock(handle, fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH)
try:
yield
finally:
fcntl.flock(handle, fcntl.LOCK_UN)
@pytest.fixture(scope="session", autouse=True)
def nltk_parser_data(tmp_path_factory):
"""Install the NLTK data that unstructured classifies text with.
The parser child never downloads NLTK data (AUTO_DOWNLOAD_NLTK=false,
see security/isolated_loader.py), and pytest_configure turns
unstructured's import-time download off in every test process too. So
this fixture, which installs the data as an operator would, is the only
way tests get it. It is autouse and session-scoped: each test process
(each xdist worker, or the one process without xdist) runs it once,
while setting up its first test, so every test under tests/ runs after
it, whether its parse runs in a parser child or in this process.
The xdist workers of a session share one data directory (by default
~/nltk_data) and one lock file. Each worker first checks the data while
holding the lock shared, so the checks run at the same time and wait
only for a worker that holds it exclusively. If the data is usable, that
check, a few seconds spent mostly importing nltk, is all this fixture
does. Otherwise the worker takes the lock exclusively, checks again and
downloads what is missing, so no two workers write the directory at once
and no check reads a package another worker is still unpacking. Checks
and downloads run in a separate interpreter, so this process does not
load the data. pytest-timeout counts this fixture, waits included,
against each worker's first test, so a check has 20 seconds and a check
with a download 75. Without network, parses that need the data fail:
in a parser child with ParserDataMissingError, in this process with
LookupError.
"""
import subprocess
# The pytest-xdist recipe: every worker's base temporary directory is
# inside this one, which the whole session shares.
lock = tmp_path_factory.getbasetemp().parent / "nltk_parser_data.lock"
env = {
**os.environ,
"AUTO_DOWNLOAD_NLTK": "false",
"SCARF_NO_ANALYTICS": "true",
}
def data_usable(*download, timeout):
# Run the script, which downloads the packages in *download* if the
# data fails its check. True only if the data passed the check.
try:
completed = subprocess.run(
[sys.executable, "-c", _INSTALL_NLTK_PARSER_DATA, *download],
env=env,
check=False,
capture_output=True,
timeout=timeout,
)
except (OSError, subprocess.TimeoutExpired):
return False
return completed.returncode == 0
with _file_lock(lock, exclusive=False):
if data_usable(timeout=20):
return
from local_deep_research.security.isolated_loader import (
NLTK_DATA_PACKAGES,
)
# Another worker may have installed the data while this one waited for
# the lock, so the script checks again before it downloads anything.
with _file_lock(lock, exclusive=True):
data_usable(*NLTK_DATA_PACKAGES, timeout=75)
@pytest.fixture(autouse=True)
def skip_if_no_real_llm(request):
"""Skip tests marked with @pytest.mark.requires_llm when running with mocks."""
if request.node.get_closest_marker("requires_llm"):
if os.environ.get("LDR_TESTING_WITH_MOCKS", "").lower() == "true":
pytest.skip("Test requires real LLM but running with mocks")
@pytest.fixture(autouse=True)
def skip_integration_in_mock_mode(request):
"""Skip integration tests when running with mocks (CI default)."""
if request.node.get_closest_marker("integration"):
if os.environ.get("LDR_TESTING_WITH_MOCKS", "true").lower() == "true":
pytest.skip(
"Integration test skipped in mock mode "
"(set LDR_TESTING_WITH_MOCKS=false to run)"
)
@pytest.fixture(scope="session", autouse=True)
def post_login_task_policy():
"""Keep the real post-login task body out of ordinary request tests.
Many FastAPI test modules construct their own module-scoped clients and
therefore bypass the function-scoped ``app`` fixture. Install the guard
once, before any such client can log in, and deliberately leave it in place
until the pytest process exits. The login thread resolves this module global
only when it starts running; restoring the production callable during
teardown would let a starved thread wake late and race a deleted test DB.
The route still creates its daemon thread, whose patched target returns
immediately. Dedicated tests receive the captured production callable from
``real_post_login_tasks`` and invoke it directly, without ever reopening a
process-wide race window.
"""
from local_deep_research.web.routers import auth as auth_routes
production_callable = auth_routes._perform_post_login_tasks
auth_routes._perform_post_login_tasks = lambda *_args, **_kwargs: None
return production_callable
@pytest.fixture
def real_post_login_tasks(post_login_task_policy):
"""Return the captured production worker without restoring its global."""
return post_login_task_policy
@pytest.fixture(scope="session", autouse=True)
def backup_status_store_policy():
"""Keep backup attempt reporting off the developer's real user databases.
Backup tests often pin only service path helpers. Allow status access
only when its actual data root is outside the platform's real root;
otherwise skip writes and return no attempt. This also covers an already
connected developer database without changing its credentials or sessions.
Installed once because test executor threads can outlive their fixture.
"""
try:
from local_deep_research.config.paths import get_data_directory
from local_deep_research.database.backup import backup_status
except ImportError:
# A broken install fails in the tests that need these modules; this
# guard must not take every unrelated test down with it.
yield
return
def resolves_to_real_data_dir() -> bool:
try:
real = Path(
platformdirs.user_data_dir("local-deep-research")
).resolve()
return get_data_directory().resolve().is_relative_to(real)
except Exception:
# An unusable LDR_DATA_DIR (relative, control characters) or a
# stubbed path: the real store fails on its own without touching
# anything, and the guard itself must never fail a test.
return False
real_record = backup_status.record_backup_result
real_read = backup_status.get_backup_status
real_flush = backup_status.persist_pending_backup_status
@functools.wraps(real_record)
def guarded_record(*args, **kwargs):
if resolves_to_real_data_dir():
return None
return real_record(*args, **kwargs)
@functools.wraps(real_read)
def guarded_read(*args, **kwargs):
if resolves_to_real_data_dir():
return {"status_available": True, "last_attempt": None}
return real_read(*args, **kwargs)
@functools.wraps(real_flush)
def guarded_flush(*args, **kwargs):
if resolves_to_real_data_dir():
return None
return real_flush(*args, **kwargs)
guards = {
"record_backup_result": (real_record, guarded_record),
"get_backup_status": (real_read, guarded_read),
"persist_pending_backup_status": (real_flush, guarded_flush),
}
with pytest.MonkeyPatch.context() as patcher:
for module in list(sys.modules.values()):
module_name = getattr(module, "__name__", "")
if not (
isinstance(module_name, str)
and module_name.startswith("local_deep_research")
):
continue
namespace = getattr(module, "__dict__", {})
for name, (real, guard) in guards.items():
if namespace.get(name) is real:
patcher.setattr(module, name, guard)
yield
@pytest.fixture
def temp_data_dir():
"""Create a temporary data directory for testing."""
temp_dir = tempfile.mkdtemp()
yield Path(temp_dir)
# Use ignore_errors=True because SQLite WAL/SHM files may still be held
# open by database connections that are disposed later in teardown order
# (cleanup_database_connections autouse fixture runs after this fixture).
shutil.rmtree(temp_dir, ignore_errors=True)
@pytest.fixture(autouse=True)
def cleanup_database_connections():
"""Clean up database connections before and after each test.
This fixture ensures proper cleanup of database connections with
logging for debugging CI issues.
"""
# Import here to avoid circular imports
from local_deep_research.database.encrypted_db import db_manager
# Close all connections before test.
#
# Deliberately does NOT clear session_manager.sessions, though it used to.
# That clear was inconsistent with the rest of the teardown: the credential
# itself (session_password_store) is never cleared here, so DatabaseMiddleware
# simply reopens the database on the next request and `is_user_connected`
# recovers — while the server-side session record, once dropped, was gone for
# good. Nothing noticed because require_auth did not consult session_manager.
#
# It does now (revocation has to mean something — see
# tests/web/dependencies/test_session_revocation.py), so clearing sessions
# here would log out every module-scoped client that legitimately registered
# and logged in during module setup, from its second test onward. Sessions
# are keyed by a random session_id, so leaving them in place leaks nothing
# between tests; singleton isolation is handled by reset_all_singletons.
db_manager.close_all_databases()
# Dispose auth engine so it will be recreated with correct path
dispose_auth_engine()
try:
from local_deep_research.utilities.db_utils import (
cleanup_cached_user_sessions_current_thread,
_get_cached_user_session,
)
cleanup_cached_user_sessions_current_thread()
if hasattr(_get_cached_user_session, "cache"):
_get_cached_user_session.cache.clear()
except (ImportError, AttributeError):
pass
yield
# Close all connections after test
db_manager.close_all_databases()
# Dispose auth engine after test
dispose_auth_engine()
try:
from local_deep_research.utilities.db_utils import (
cleanup_cached_user_sessions_current_thread,
_get_cached_user_session,
)
cleanup_cached_user_sessions_current_thread()
if hasattr(_get_cached_user_session, "cache"):
_get_cached_user_session.cache.clear()
except (ImportError, AttributeError):
pass
@pytest.fixture(autouse=True)
def _legacy_bare_username_auth(request):
"""Keep the legacy test idiom authenticating.
Many route tests authenticate by putting a bare ``username`` in the session
(via ``session_transaction`` or by patching the decorator's ``session``
object) with a mocked ``db_manager``, and never create a server-side
session. After the session-id revocation fix, ``login_required`` /
``inject_current_user`` validate the cookie's ``session_id`` via
``session_manager`` and would reject those requests. This autouse fixture
relaxes the decorator's server-side-session gate so the legacy
"username present == authenticated" behaviour is restored without editing
each test.
``_server_session_valid`` is only ever called *after* the caller has already
confirmed a username is present, so accepting unconditionally is exactly the
pre-revocation contract — and works regardless of how the test built its
session (real Flask session, patched ``decorators.session`` fake, etc.).
Tests that must prove a destroyed / invalid session IS rejected opt out with
``@pytest.mark.real_session_check`` so they exercise the real gate. The
fixture only relaxes the HTTP decorator path (``_server_session_valid``);
the WebSocket handshake validates ``session_manager`` directly and is
unaffected, so the socket revocation test keeps using the real check.
"""
if request.node.get_closest_marker("real_session_check"):
# Security tests: leave the real server-side-session check in place.
yield
return
def _accept(_request, _username):
return True
# main patched web.auth.decorators._server_session_valid; that Flask
# decorator is gone, and the equivalent seam here is the same-named
# helper behind require_auth.
with patch(
"local_deep_research.web.dependencies.auth._server_session_valid",
_accept,
):
yield
# Last epoch projected per username under the _durable_epoch_projection
# shim -- see the fixture for why stability across failures matters.
_LAST_EPOCH_PROJECTION: dict[str, str] = {}
@pytest.fixture(autouse=True)
def _durable_epoch_projection(request):
"""Keep fabricated test sessions working under durable-epoch binding.
``SessionManager.create_session`` binds the user's durable rekey epoch
(read from disk) so a password change in ANY worker fences every
stale session. In production a session is only ever created at login,
right after ``open_user_database`` proved the credential against the
epoch-fenced database, so the projection always resolves. The test
fleet, however, fabricates sessions and logins against mocked
databases for users with no on-disk state; the projection would raise
``RekeyRecoveryRequiredError`` for them.
This autouse fixture relaxes the seam the same way
``_legacy_bare_username_auth`` relaxes the server-side-session gate:
sessions fabricated under tests bind a deterministic per-username
epoch. Tests that must prove the real fencing contract (binding,
stale-epoch removal, transient-vs-ambiguous failure discipline) opt
out with ``@pytest.mark.real_durable_epoch`` and run against real
epoch files.
"""
if request.node.get_closest_marker("real_durable_epoch"):
# Fencing tests: leave the real epoch projection in place.
yield
return
# A shared monkeypatch dependency would outlive singleton teardown and
# leave unrelated tests' provider mocks active during that reset.
with pytest.MonkeyPatch.context() as epoch_patch:
_install_durable_epoch_projection(epoch_patch)
yield
def _install_durable_epoch_projection(monkeypatch):
import local_deep_research.database.encrypted_db as _encrypted_db
from local_deep_research.database.rekey_epoch import (
read_user_authorization_epoch as _real_projection,
)
def _fabricated(username):
return _LAST_EPOCH_PROJECTION.setdefault(
username, f"test-epoch-{username}"
)
def _project_through_or_fabricate(username):
# Sessions created against real databases (module-scoped client
# fixtures register + log in real users) keep their REAL epochs,
# and any failure projecting them propagates exactly as it would
# in production. Only two cases get a deterministic stand-in:
#
# * ``db_manager`` itself is a test double (not a DatabaseManager,
# or one whose path resolution is mocked), so there is nothing
# durable to project;
# * the resolved path holds NO durable epoch state at all (neither
# ``.rekey.epoch`` nor ``.rekey.pending``): a fabricated login
# against a mocked database, or a real user whose data dir a
# module fixture un-pinned or deleted.
#
# STABILITY cache: a username's last real projection is remembered
# and reused for the no-state case, so a create/validate pair that
# straddles a data-dir pin/restore does not mismatch-destroy the
# record.
manager = _encrypted_db.db_manager
if not isinstance(manager, _encrypted_db.DatabaseManager):
return _fabricated(username)
db_path = manager._get_user_db_path(username)
if not isinstance(db_path, Path):
return _fabricated(username)
has_state = (
db_path.with_name(f"{db_path.name}.rekey.epoch").exists()
or db_path.with_name(f"{db_path.name}.rekey.pending").exists()
)
if not has_state:
return _fabricated(username)
projected = _real_projection(username)
_LAST_EPOCH_PROJECTION[username] = projected
return projected
monkeypatch.setattr(
"local_deep_research.web.auth.session_manager."
"read_user_authorization_epoch",
_project_through_or_fabricate,
)
# The login route refuses (503) when the open recorded no epoch proof
# for the user. A test that swaps the login route's ``db_manager`` (or
# its ``open_user_database``) for a double records none, so for that
# case only the legacy "no proof, no straddle check" path is kept
# (``create_session(proven_epoch=None)``). A real open that records no
# proof still refuses exactly as in production.
_real_proven_epoch_for = _encrypted_db.LoginProof.proven_epoch_for
def _proven_epoch_for_or_double(self, username):
try:
return _real_proven_epoch_for(self, username)
except _encrypted_db.MissingLoginProofError:
router = sys.modules.get("local_deep_research.web.routers.auth")
manager = getattr(router, "db_manager", None)
real_open = _encrypted_db.DatabaseManager.open_user_database
is_double = (
not isinstance(manager, _encrypted_db.DatabaseManager)
or "open_user_database" in vars(manager)
or type(manager).open_user_database is not real_open
)
if is_double:
return None
raise
monkeypatch.setattr(
_encrypted_db.LoginProof,
"proven_epoch_for",
_proven_epoch_for_or_double,
)
@pytest.fixture
def app(temp_data_dir, monkeypatch):
"""Create an app configured for testing."""
# Override data directory
monkeypatch.setenv("LDR_DATA_DIR", str(temp_data_dir))
# Production default (256000) makes PBKDF2 dominate wall-clock in
# fixtures that create real encrypted users. Keep function-scoped app
# clients cheap under xdist; custom higher-scoped fixtures must set their
# own test value. sqlcipher_utils declares MIN_KDF_ITERATIONS_TESTING=1
# specifically to support this.
monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
if create_app is not None:
# Flask path (legacy)
app, _ = create_app()
app.config["TESTING"] = True
app.config["WTF_CSRF_ENABLED"] = False
app.config["WTF_CSRF_CHECK_DEFAULT"] = False
app.config["SESSION_COOKIE_SECURE"] = False
app.config["PREFERRED_URL_SCHEME"] = "http"
else:
# FastAPI path
from local_deep_research.web.fastapi_app import app
# Initialize auth database in test directory
init_auth_database()
return app
def _test_client_response_classes():
"""Every response class a test client in this suite can hand back.
``starlette.testclient`` does ``import httpx2 as httpx`` and only falls
back to plain ``httpx`` when httpx2 is not installed. Both packages are
present in the Docker test image (``openai>=3.3`` requires ``httpx2``,
while ``ollama`` and the app itself pull in ``httpx``), so the module a
TestClient response comes from is decided by what happens to be
installed — not by anything this suite controls. Return both, deduped by
identity, so the compat accessors below land on whichever one is live.
"""
classes = []
try:
from starlette import testclient as _starlette_testclient
classes.append(_starlette_testclient.httpx.Response)
except Exception: # pragma: no cover - starlette always importable here
pass
try:
import httpx as _httpx
classes.append(_httpx.Response)
except Exception: # pragma: no cover - httpx always importable here
pass
out = []
for cls in classes:
if not any(cls is seen for seen in out):
out.append(cls)
return out
def _install_flask_response_compat():
"""Add Flask's response accessors to the test client's response class.
Ported tests read ``response.data`` (bytes), ``.is_json``,
``.get_data()`` and ``.get_json()`` — Flask's API, which a Starlette
``TestClient`` response does not have. We add them to the response class
itself, once per worker process.
Two things about this function are load-bearing, and both were bugs:
* **Which class gets patched.** This used to ``import httpx`` directly
and decorate ``httpx.Response``. When httpx2 is installed,
``starlette.testclient`` binds *that* module instead, so the patch
landed on a class no test-client response is an instance of and every
ported ``.data`` read raised ``AttributeError``. It passed locally
(httpx only) and failed in the Docker image (httpx and httpx2), which
is the worst possible shape for a CI signal. Resolving the class from
``starlette.testclient`` means the patch cannot disagree with the
client about which httpx is in play.
* **When it runs.** This used to be inline in
``_make_flask_compat_client``, i.e. installed by a function-scoped
fixture. A class attribute is process-global state, so that made every
test reading ``.data`` depend on some *earlier* test in the same xdist
worker having built a compat client first — an ordering dependency
that holds or breaks according to how xdist happens to shard the
suite. Calling it at conftest import time gives every worker the
accessors before it runs anything.
Idempotent: each accessor is only added when absent.
"""
for response_cls in _test_client_response_classes():
if not hasattr(response_cls, "data"):
def _data(self):
return self.content
response_cls.data = property(_data)
if not hasattr(response_cls, "is_json"):
def _is_json(self):
ct = self.headers.get("content-type", "")
return "json" in ct.lower()
response_cls.is_json = property(_is_json)
if not hasattr(response_cls, "get_data"):
def _get_data(self, as_text=False):
return self.text if as_text else self.content
response_cls.get_data = _get_data
# Flask `Response.get_json()` returns the parsed JSON body or None;
# httpx has `.json()` but it raises on non-JSON. Match Flask's
# tolerant variant so tests using `.get_json()` keep working.
if not hasattr(response_cls, "get_json"):
def _get_json(self, force=False, silent=False):
try:
return self.json()
except Exception:
if silent:
return None
raise
response_cls.get_json = _get_json
# Installed at import time, not from a fixture — see the docstring above.
_install_flask_response_compat()
def _make_flask_compat_client(app):
"""Wrap a FastAPI TestClient to accept Flask test-client kwargs.
The migration from Flask removed `app.test_client()`; downstream
tests call `client.post(..., content_type="application/json")`,
`client.get(...).data` (bytes), `.is_json`, etc. — Starlette's
TestClient doesn't speak that API. We sub-class TestClient and
translate the differences in-place so the bulk of the api_tests/
and security/ tests can keep their Flask-style calls without a
file-by-file rewrite. Tests that need Starlette-only features
(httpx parameters, follow_redirects, etc.) keep working since
we only intercept Flask-specific kwargs.
"""
from fastapi.testclient import TestClient
from starlette.testclient import _RequestData # noqa: F401 (compat)
class _FlaskCompatClient(TestClient):
@staticmethod
def _translate(kwargs):
# Flask: `query_string={"limit": 10}` → httpx: `params={...}`
qs = kwargs.pop("query_string", None)
if qs is not None:
kwargs.setdefault("params", qs)
# Flask: `content_type="application/json"` →
# Starlette: `headers={"Content-Type": "application/json"}`
ct = kwargs.pop("content_type", None)
# Never forward a bare multipart/form-data content type — it has
# no boundary, so the server can't parse the body. httpx sets the
# correct multipart Content-Type (with boundary) itself when
# `files=` is present; when there are no files we just let it send
# the data normally.
if ct is not None and "multipart" not in ct.lower():
hdrs = dict(kwargs.get("headers") or {})
hdrs.setdefault("Content-Type", ct)
kwargs["headers"] = hdrs
# Flask-style multipart file uploads:
# data={"field": (fileobj, filename)} (or a werkzeug MultiDict
# of such (field, (fileobj, filename)) pairs)
# → httpx `files=[(field, (filename, bytes))]`. Only triggers when
# a value is a file tuple (its first element is file-like), so
# plain string form-data is untouched. httpx must set the
# multipart Content-Type itself, so the explicit override is
# dropped when files are present.
data = kwargs.get("data")
if (
data is not None
and "files" not in kwargs
and hasattr(data, "items")
):
try:
pairs = list(data.items(multi=True)) # werkzeug MultiDict
except TypeError:
pairs = list(data.items())
file_items, form_items = [], []
for k, v in pairs:
if isinstance(v, tuple) and v and hasattr(v[0], "read"):
fileobj = v[0]
filename = v[1] if len(v) > 1 else ""
file_items.append((k, (filename, fileobj.read())))
else:
form_items.append((k, v))
if file_items:
kwargs["files"] = file_items
if form_items:
kwargs["data"] = dict(form_items)
else:
kwargs.pop("data", None)
hdrs = {
hk: hv
for hk, hv in (kwargs.get("headers") or {}).items()
if hk.lower() != "content-type"
}
kwargs["headers"] = hdrs
return kwargs
def get(self, url, **kwargs):
return super().get(url, **self._translate(kwargs))
def post(self, url, **kwargs):
return super().post(url, **self._translate(kwargs))
def put(self, url, **kwargs):
return super().put(url, **self._translate(kwargs))
def patch(self, url, **kwargs):
return super().patch(url, **self._translate(kwargs))
def delete(self, url, **kwargs):
return super().delete(url, **self._translate(kwargs))
client = _FlaskCompatClient(app, raise_server_exceptions=False)
# The Flask-style response accessors (`.data`, `.is_json`,
# `.get_data()`, `.get_json()`) are installed on the response class at
# conftest import time — see `_install_flask_response_compat`. Called
# again here (it is idempotent) only to cover the case where an earlier
# import raced ahead of `starlette.testclient` picking its httpx.
_install_flask_response_compat()
# Flask `client.set_cookie(server_name, key, value)` — Starlette
# uses `client.cookies.set(key, value)`. Provide the Flask shape.
if not hasattr(client, "set_cookie"):
def _set_cookie(self, *args, **kwargs):
# Flask 1.x: set_cookie(server_name, key, value, ...)
# Flask 2.x: set_cookie(key, value, ...)
if len(args) >= 3:
_, key, value = args[0], args[1], args[2]
elif len(args) >= 2:
key, value = args[0], args[1]
else:
key = kwargs.get("key")
value = kwargs.get("value")
self.cookies.set(key, value)
client.set_cookie = _set_cookie.__get__(client)
return client
@pytest.fixture
def client(app):
"""Create a test client.
Returns a Flask test client when running against the legacy Flask
runtime (`app.test_client()`); returns a Flask-compat-shimmed
Starlette TestClient when running against the FastAPI app, so the
majority of pre-migration tests (which use Flask kwargs +
response accessors) keep working without per-file rewrites.
"""
if hasattr(app, "test_client"):
return app.test_client()
return _make_flask_compat_client(app)
@pytest.fixture
def app_with_csrf(app):
"""App with CSRF protection ENABLED.
Under Flask this was a dedicated app built with CSRF on (the default
`app` disabled it for ergonomic testing). Under FastAPI the
``CSRFMiddleware`` is ALWAYS active on the app (fastapi_app.py adds it
unconditionally), so this fixture is just an alias of the standard
`app` fixture — kept so CSRF-enforcement tests read intent-clearly and
to ease a future re-split if CSRF ever becomes opt-out in tests.
"""
return app
def _cleanup_authenticated_test_client(client, username):
"""Best-effort auth cleanup, closing clients that expose ``close``."""
try:
with suppress(Exception):
client.post("/auth/logout", follow_redirects=False)
with suppress(Exception):
from local_deep_research.web.auth.session_manager import (
session_manager,
)
session_manager.destroy_all_user_sessions(username)
with suppress(Exception):
from local_deep_research.database.session_passwords import (
session_password_store,
)
session_password_store.clear_all_for_user(username)
with suppress(Exception):
from local_deep_research.database.thread_local_session import (
clear_user_credentials,
)
clear_user_credentials(username)
finally:
close = getattr(client, "close", None)
if close is not None:
close()
@pytest.fixture
def authenticated_client(app, temp_data_dir, request):
"""Create a test client with an authenticated user."""
# Create unique test username using UUID to avoid conflicts in parallel tests
test_username = generate_unique_test_username()
test_password = "TestPass123"
# Clear any existing user database
encrypted_db_dir = temp_data_dir / "encrypted_databases"
if encrypted_db_dir.exists():
import shutil
try:
shutil.rmtree(encrypted_db_dir)
except Exception as e:
logger.warning(f"Could not remove encrypted_db_dir: {e}")
# Create a test client (FastAPI or legacy Flask)
if hasattr(app, "test_client"):
client = app.test_client()
_decode_body = lambda r: r.data.decode() # noqa: E731
else:
client = _make_flask_compat_client(app)
_decode_body = lambda r: r.text # noqa: E731
# Each authenticated_client fixture gets a unique X-Forwarded-For
# so tests don't share the slowapi rate-limit bucket. Without
# this, registrations from one shared IP accumulate in a single
# bucket and later tests can hit 429 from /auth/register's per-IP
# rate limit. TRUST_PROXY_HEADERS isn't
# required because the slowapi key uses the direct client peer
# for non-private IPs by default — but the testclient peer is
# treated as private, so X-Forwarded-For IS honored.
import uuid as _uuid
_fwd_ip = (
f"10.{_uuid.uuid4().int % 254 + 1}.{_uuid.uuid4().int % 254 + 1}.1"
)
client.headers.update({"X-Forwarded-For": _fwd_ip})
# Register the finalizer before bootstrap. Pytest runs it even if a later
# registration/auth assertion raises during fixture setup.
request.addfinalizer(
lambda: _cleanup_authenticated_test_client(client, test_username)
)
def _csrf():
# Stamp the session with a CSRF token, then fetch it as a string.
# Post-Wave-9, register and login are no longer in the CSRF
# exempt list — both forms must POST a real token.
client.get("/auth/login")
return client.get("/auth/csrf-token").json()["csrf_token"]
# Register through the normal flow. FastAPI registration auto-authenticates
# the new user, so posting immediately to /auth/login would rotate the
# cookie onto a second server session and orphan the first one.
with client:
# Register new unique user
register_response = client.post(
"/auth/register",
data={
"username": test_username,
"password": test_password,
"confirm_password": test_password,
"acknowledge": "true",
"csrf_token": _csrf()
if not hasattr(app, "test_client")
else "",
},
follow_redirects=False,
)
expected_register_statuses = (
{200, 302} if hasattr(app, "test_client") else {302}
)
if register_response.status_code not in expected_register_statuses:
raise Exception(
f"Registration failed with status {register_response.status_code}: "
f"{_decode_body(register_response)[:500]}"
)
if hasattr(app, "test_client"):
# Legacy Flask registration did not guarantee auto-login.
login_response = client.post(
"/auth/login",
data={
"username": test_username,
"password": test_password,
"csrf_token": "",
},
follow_redirects=False,
)
if login_response.status_code not in [200, 302]:
raise Exception(
f"Login failed with status {login_response.status_code}: "
f"{_decode_body(login_response)[:500]}"
)
else:
auth_check = client.get("/auth/check")
auth_body = auth_check.json()
if (
auth_check.status_code != 200
or auth_body.get("authenticated") is not True
or auth_body.get("username") != test_username
):
raise Exception(
"Registration did not establish the expected authenticated "
f"session: {auth_check.status_code} {auth_body!r}"
)
# Attach the session's CSRF token as a default header on the
# FastAPI test client so subsequent state-changing requests pass
# the post-Wave-2 fail-closed CSRFMiddleware. Flask's test_client
# bypasses CSRF in TESTING mode and doesn't need this.
if not hasattr(app, "test_client"):
try:
tok = client.get("/auth/csrf-token").json().get("csrf_token")
if tok:
client.headers.update({"X-CSRFToken": tok})
except Exception:
pass
return client
@pytest.fixture()
def setup_database_for_all_tests(
tmp_path_factory, mocker
): # Use function-scoped mocker so patches don't leak to other tests
"""
Provides a database setup for a temporary SQLite file database for the entire test session.
It patches db_utils.get_db_session and db_utils.get_settings_manager to use this test DB.
"""
# Call cache_clear on the functions from db_utils_module.
# This ensures any pre-existing cached instances are gone.
# We must ensure db_utils_module is imported before this point.
try:
if hasattr(db_utils_module.get_db_session, "cache_clear"):
db_utils_module.get_db_session.cache_clear()
if hasattr(db_utils_module.get_settings_manager, "cache_clear"):
db_utils_module.get_settings_manager.cache_clear()
# get_setting_from_db_main_thread has been removed
except Exception as e:
logger.warning(f"Failed to clear db_utils caches aggressively: {e}")
# This shouldn't prevent test run, but indicates a problem with cache_clear
# Debug tmp_path_factory behavior
temp_dir = tmp_path_factory.mktemp("db_test_data")
db_file = temp_dir / "test_settings.db"
db_url = f"sqlite:///{db_file}"
engine = None
try:
engine = create_engine(db_url)
except Exception:
logger.exception("Failed to create SQLAlchemy engine")
raise
# Enable SQLite FK enforcement on every connection. Mirrors production
# (sqlcipher_utils.apply_performance_pragmas) and the FK-aware
# fixtures in tests/database/test_research_strategy_fk_regression.py
# and tests/database/test_chat_models.py. Fixture is function-scoped
# so the listener is registered fresh per test — no leakage.
# NB: parameter is named `dbapi` (not `dbapi_connection`) to sidestep
# the custom-checks raw-SQL detector, which uses an unanchored regex
# `conn.execute` that flags ANY identifier ending in "conn.execute".
# The PRAGMA is the canonical way to enable FK enforcement on SQLite
# and has no ORM equivalent.
@event.listens_for(engine, "connect")
def _enable_fk(dbapi, _connection_record):
dbapi.execute("PRAGMA foreign_keys = ON")
try:
Base.metadata.create_all(engine)
except SQLAlchemyError:
logger.exception("SQLAlchemyError during Base.metadata.create_all")
raise
except Exception:
logger.exception("Unexpected error during Base.metadata.create_all")
raise
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
temp_session = SessionLocal()
temp_settings_manager = SettingsManager(db_session=temp_session)
try:
temp_settings_manager.load_from_defaults_file(commit=True)
except Exception:
logger.exception("Failed to load default settings")
temp_session.rollback() # Rollback if default loading fails
raise # Re-raise to fail the test if default loading is critical
finally:
temp_session.close() # Close the temporary session used for loading defaults
# Clear caches and patch. Post-FastAPI-migration the session cache moved
# from get_db_session (formerly lru_cache-wrapped) down to the inner
# _get_cached_user_session, which is wrapped by cachetools.cached and so
# exposes cache_clear(). get_db_session itself is now a plain function
# with no cache_clear, so guard before calling.
if hasattr(db_utils_module.get_db_session, "cache_clear"):
db_utils_module.get_db_session.cache_clear()
if hasattr(db_utils_module._get_cached_user_session, "cache_clear"):
db_utils_module._get_cached_user_session.cache_clear()
mock_get_db_session = mocker.patch(
"local_deep_research.utilities.db_utils.get_db_session"
)
mock_get_db_session.side_effect = SessionLocal
mock_get_settings_manager = mocker.patch(
"local_deep_research.utilities.db_utils.get_settings_manager"
)
def _settings_with_maybe_fake_db(
db_session: Session | None = None, *_, **__
) -> SettingsManager:
if db_session is None:
# Use the mock.
db_session = mock_get_db_session()
return SettingsManager(db_session=db_session)
mock_get_settings_manager.side_effect = _settings_with_maybe_fake_db
yield SessionLocal # Yield the SessionLocal class for individual tests to create sessions
if engine:
engine.dispose() # Dispose the engine to close all connections
# tmp_path_factory handles deleting the temporary directory and its contents
@pytest.fixture
def mock_db_session(mocker):
return mocker.MagicMock()
@pytest.fixture
def mock_logger(mocker):
return mocker.patch("local_deep_research.settings.manager.logger")
# ============== LLM and Search Mock Fixtures (inspired by scottvr) ==============
@pytest.fixture
def mock_llm():
"""Create a mock LLM for testing."""
mock = Mock()
mock.invoke.return_value = Mock(content="Mocked LLM response")
return mock
@pytest.fixture
def mock_search():
"""Create a mock search engine for testing."""
mock = Mock()
mock.run.return_value = get_mock_search_results()
return mock
@pytest.fixture
def mock_search_system():
"""Create a mock search system for testing."""
mock = Mock()
mock.analyze_topic.return_value = get_mock_findings()
mock.all_links_of_system = [
{"title": "Source 1", "link": "https://example.com/1"},
{"title": "Source 2", "link": "https://example.com/2"},
]
return mock
# ============== API Response Mock Fixtures ==============
@pytest.fixture
def mock_wikipedia_response():
"""Mock response from Wikipedia API."""
return get_mock_wikipedia_response()
@pytest.fixture
def mock_arxiv_response():
"""Mock response from arXiv API."""
return get_mock_arxiv_response()
@pytest.fixture
def mock_pubmed_response():
"""Mock response from PubMed API."""
return get_mock_pubmed_response()
@pytest.fixture
def mock_pubmed_article():
"""Mock PubMed article detail."""
return get_mock_pubmed_article()
@pytest.fixture
def mock_semantic_scholar_response():
"""Mock response from Semantic Scholar API."""
return get_mock_semantic_scholar_response()
@pytest.fixture
def mock_google_pse_response():
"""Mock response from Google PSE API."""
return get_mock_google_pse_response()
@pytest.fixture
def mock_ollama_response():
"""Mock response from Ollama API."""
return get_mock_ollama_response()
# ============== Data Structure Mock Fixtures ==============
@pytest.fixture
def mock_search_results():
"""Sample search results for testing."""
return get_mock_search_results()
@pytest.fixture
def mock_findings():
"""Sample research findings for testing."""
return get_mock_findings()
@pytest.fixture
def mock_error_responses():
"""Collection of error responses for testing."""
return get_mock_error_responses()
# ============== Environment and Module Mock Fixtures ==============
@pytest.fixture
def mock_env_vars(monkeypatch):
"""Set up mock environment variables for testing."""
monkeypatch.setenv("LDR_LLM__PROVIDER", "test_provider")
monkeypatch.setenv("LDR_LLM__MODEL", "test_model")
monkeypatch.setenv("LDR_SEARCH__TOOL", "test_tool")
monkeypatch.setenv("LDR_SEARCH__ITERATIONS", "2")
yield
@pytest.fixture
def mock_llm_config(monkeypatch):
"""Create and patch a mock llm_config module."""
# Create a mock module
mock_module = types.ModuleType("mock_llm_config")
# Add necessary functions and variables
def get_llm(*args, **kwargs):
mock = Mock()
mock.invoke.return_value = Mock(content="Mocked LLM response")
return mock
mock_module.get_llm = get_llm
# Patch the module
monkeypatch.setitem(
sys.modules, "local_deep_research.config.llm_config", mock_module
)
monkeypatch.setattr("local_deep_research.config.llm_config", mock_module)
return mock_module
# ============== Test Database Fixtures ==============
@pytest.fixture
def temp_db_path():
"""Create a temporary database file for testing."""
fd, path = tempfile.mkstemp(suffix=".db")
os.close(fd)
yield path
os.unlink(path)
@pytest.fixture
def mock_research_history():
"""Mock research history entries."""
return get_mock_research_history()
@pytest.fixture
def mock_settings():
"""Mock settings configuration."""
return get_mock_settings()
# ============== Loguru Logging Fixtures ==============
class PropagateHandler(logging.Handler):
def emit(self, record):
logging.getLogger(record.name).handle(record)
@pytest.fixture
def loguru_caplog(caplog):
"""Make pytest caplog work with loguru.
Standard pytest caplog doesn't capture loguru logs out of the box.
This fixture propagates loguru logs to the standard logging module
so they can be captured by pytest's caplog fixture.
Note: The local_deep_research package disables loguru logging by default
(see src/local_deep_research/__init__.py). This fixture re-enables it
for the duration of the test.
See: https://loguru.readthedocs.io/en/stable/resources/migration.html
Usage:
def test_something(loguru_caplog):
with loguru_caplog.at_level("WARNING"):
# ... code that uses loguru logging ...
assert "expected message" in loguru_caplog.text
"""
# Re-enable logging for local_deep_research (disabled in __init__.py)
logger.enable("local_deep_research")
handler_id = logger.add(
PropagateHandler(),
format="{message}",
level="DEBUG",
# diagnose=False: loguru defaults to True, which dumps repr() of
# every traceback frame's local on exception. The propagating handler
# forwards those into pytest's stdlib log capture, where they end up
# in CI logs. Hygiene companion to #4185 / #4384.
diagnose=False,
)
yield caplog
logger.remove(handler_id)
# Re-disable logging to restore original state
logger.disable("local_deep_research")
@pytest.fixture
def loguru_caplog_full(caplog):
"""Like ``loguru_caplog`` but captures the exception block too.
Use this in security tests that assert a credential never appears in
log output. The default ``loguru_caplog`` fixture uses
``format="{message}"``, which excludes the rendered exception block
that ``logger.exception()`` (and ``exc_info=True``) emit — so a leak
that lives only in the traceback would false-pass.
This fixture appends ``{exception}`` to the format and enables
``backtrace=True`` so the cause chain (``__cause__`` / ``__context__``)
is rendered. ``diagnose`` stays off to match production
(``utilities/log_utils.py`` runs ``diagnose=False`` unless
``LDR_APP_DEBUG`` is set).
"""
logger.enable("local_deep_research")
handler_id = logger.add(
PropagateHandler(),
format="{message}\n{exception}",
level="DEBUG",
backtrace=True,
diagnose=False,
)
yield caplog
logger.remove(handler_id)
logger.disable("local_deep_research")