~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
"""Direct contracts for the FastAPI Zotero router's failure boundaries.
The live smoke suite proves registration, authentication, and the fresh-user
path. These fast tests cover the branches that require an expired session,
configured Zotero account, typed client failure, or background thread without
opening an encrypted database or contacting Zotero.
"""
from contextlib import contextmanager, nullcontext
from json import loads
from types import SimpleNamespace
from unittest.mock import Mock, call, patch
import pytest
from loguru import logger as loguru_logger
from starlette.requests import Request
from local_deep_research.research_library.zotero import (
ZoteroAuthError,
ZoteroError,
ZoteroTransientError,
)
from local_deep_research.research_library.zotero.client import (
ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE,
)
from local_deep_research.research_library.zotero.sync_service import (
ZOTERO_GROUP_ID_REQUIRED_MESSAGE,
)
from local_deep_research.web.routers import zotero
def _request(session_id="sid"):
return Request(
{
"type": "http",
"method": "GET",
"path": "/library/api/zotero/test",
"headers": [],
"query_string": b"",
"session": {"session_id": session_id},
}
)
def _body(response):
return loads(response.body)
def _configured(**overrides):
values = {
"enabled": True,
"is_configured": True,
"library_type": "user",
"library_id": "123",
"collection_keys": ["ABC"],
"import_tags": True,
"import_items_without_pdf": False,
"import_annotations": True,
"pdf_storage_mode": "database",
"auto_sync_enabled": True,
"sync_interval_minutes": 60,
"use_local_api": False,
"api_key": "secret-key",
}
values.update(overrides)
return SimpleNamespace(**values)
@pytest.mark.parametrize(
"endpoint",
[
zotero.get_config,
zotero.test_connection,
zotero.list_collections,
zotero.list_groups,
zotero.sync_now,
zotero.get_status,
],
ids=["config", "test", "collections", "groups", "sync", "status"],
)
def test_expired_session_short_circuits_every_database_endpoint(endpoint):
service = Mock(side_effect=AssertionError("service constructed"))
with (
patch.object(
zotero.session_password_store,
"get_session_password",
return_value=None,
) as get_password,
patch.object(zotero, "ZoteroSyncService", service),
):
response = endpoint(_request(), username="alice")
assert response.status_code == 401
assert _body(response) == {
"success": False,
"error": "Session expired — please sign in again.",
}
get_password.assert_called_once_with("alice", "sid")
service.assert_not_called()
@pytest.mark.parametrize(
("exc", "status", "message"),
[
(
ZoteroAuthError("revoked key"),
401,
"Zotero authentication failed. Check the configured API key.",
),
(
ZoteroTransientError("rate limited"),
503,
"Zotero is temporarily unavailable. Please try again later.",
),
(
ZoteroError("bad library"),
400,
"Zotero request failed. Check the integration settings.",
),
],
ids=["auth", "transient", "client"],
)
def test_zotero_errors_map_to_stable_http_statuses(exc, status, message):
response = zotero._zotero_error_response(exc)
assert response.status_code == status
assert _body(response) == {"success": False, "error": message}
def test_zotero_error_response_forwards_curated_guidance():
"""The author-written HTTP 400 diagnostic must reach the browser.
Catches the revert that replaces every ``ZoteroError`` message with one
generic string. This text is the only place the product explains the
most common Zotero misconfiguration (a username in the library-ID
setting).
What this test does NOT catch: a revert to
``sanitize_error_for_client(str(exc))``. That scrubber leaves a curated
constant byte-for-byte unchanged, so this assertion passes either way.
``test_zotero_error_response_withholds_exception_text`` below is the leg
that fails on that revert -- it feeds a message that is not in the
allowlist and requires the generic fallback. The two are a pair: this one
pins that curated guidance survives, that one pins that nothing else does.
"""
response = zotero._zotero_error_response(
ZoteroError(ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE)
)
assert response.status_code == 400
assert _body(response) == {
"success": False,
"error": ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE,
}
assert "NUMERIC userID" in _body(response)["error"]
def test_zotero_error_response_withholds_exception_text():
secret = "sk-ABCDEFGHIJKLMNOPQRSTUV"
response = zotero._zotero_error_response(
ZoteroError(f"bad key {secret}\nforbidden control\x00" + "x" * 500)
)
error = _body(response)["error"]
assert error == "Zotero request failed. Check the integration settings."
assert secret not in error
assert "bad key" not in error
@contextmanager
def _capture_loguru(level="DEBUG"):
"""Collect this package's loguru records as text.
``caplog`` sees nothing here: loguru does not write through the stdlib
``logging`` handlers pytest installs, and the package is ``logger.disable``d
by default. Same pattern as
``tests/research_library/test_zotero_sync_contracts.py``.
"""
records = []
loguru_logger.enable("local_deep_research")
sink_id = loguru_logger.add(
lambda message: records.append(str(message)), level=level
)
try:
yield records
finally:
loguru_logger.remove(sink_id)
loguru_logger.disable("local_deep_research")
def test_zotero_error_response_logs_the_curated_text_it_withholds():
"""The generic body is only safe if the server still records WHICH
failure produced it.
``_zotero_error_response`` answers a non-allowlisted ``ZoteroError`` with
"Zotero request failed. Check the integration settings." -- a string that
is identical for an unroutable proxy, a deleted collection and a
malformed library id. The exception class and its author-written text are
the only thing left that tells those apart, so they must reach the log.
Deleting the ``logger.warning`` (the state this file's subject was in
before) leaves nothing on either side: the operator sees a generic body
and an empty log.
Note what is asserted about the traceback: this runs on the request path,
where the caller's frames hold ``cfg.api_key`` and the SQLCipher
password, so the site must stay ``logger.warning`` -- ``logger.exception``
would render those frames under loguru's ``diagnose``.
The call happens inside a real ``except`` block (raised and caught, not
invoked bare) so the traceback assertion is falsifiable: with no active
exception (``sys.exc_info() == (None, None, None)``), loguru 0.7.3's
``logger.exception(...)`` emits ``"NoneType: None"`` and no traceback
header at all, so a bare call to the helper would pass this assertion
whether the revert landed or not.
"""
detail = "collection ABC vanished between the listing and the fetch"
with _capture_loguru() as records:
try:
raise ZoteroTransientError(detail)
except ZoteroTransientError as exc:
response = zotero._zotero_error_response(exc)
text = "".join(records)
assert "ZoteroTransientError" in text, text
assert detail in text, text
# The client got the generic fallback; the log got the specific text.
assert (
_body(response)["error"]
== "Zotero is temporarily unavailable. Please try again later."
)
assert detail not in _body(response)["error"]
# A traceback would render the API-key-bearing frames under diagnose.
assert "Traceback (most recent call last)" not in text
def test_configured_summary_exposes_presence_but_never_the_api_key():
service = Mock()
service.get_config.return_value = _configured()
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", return_value=service) as cls,
):
result = zotero.get_config(_request(), username="alice")
assert result["success"] is True
assert result["configured"] is True
assert result["has_api_key"] is True
assert "api_key" not in result
assert "secret-key" not in str(result)
cls.assert_called_once_with("alice", "db-password")
@pytest.mark.parametrize(
("endpoint", "method", "response_key", "payload"),
[
(
zotero.list_collections,
"list_collections",
"collections",
[{"key": "A"}],
),
(zotero.list_groups, "list_groups", "groups", [{"id": 7}]),
],
ids=["collections", "groups"],
)
def test_configured_remote_lists_are_returned(
endpoint, method, response_key, payload
):
service = Mock()
service.get_config.return_value = _configured()
getattr(service, method).return_value = payload
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", return_value=service),
):
result = endpoint(_request(), username="alice")
assert result == {"success": True, response_key: payload}
getattr(service, method).assert_called_once_with()
@pytest.mark.parametrize(
("endpoint", "method"),
[
(zotero.list_collections, "list_collections"),
(zotero.list_groups, "list_groups"),
],
ids=["collections", "groups"],
)
@pytest.mark.parametrize(
("exc_type", "status", "message"),
[
(
ZoteroAuthError,
401,
"Zotero authentication failed. Check the configured API key.",
),
(
ZoteroTransientError,
503,
"Zotero is temporarily unavailable. Please try again later.",
),
(
ZoteroError,
400,
"Zotero request failed. Check the integration settings.",
),
],
ids=["auth", "transient", "client"],
)
def test_remote_list_endpoints_map_typed_client_failures(
endpoint, method, exc_type, status, message
):
service = Mock()
service.get_config.return_value = _configured()
getattr(service, method).side_effect = exc_type("private failure detail")
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", return_value=service),
):
response = endpoint(_request(), username="alice")
assert response.status_code == status
assert _body(response) == {"success": False, "error": message}
def _test_connection_response(service_result):
service = Mock()
service.test_connection.return_value = service_result
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", return_value=service),
):
return zotero.test_connection(_request(), username="alice")
@pytest.mark.parametrize(
("service_result", "status", "expected"),
[
(
{
"success": True,
"library_version": 42,
"collection_count": 3,
},
200,
{
"success": True,
"library_version": 42,
"collection_count": 3,
},
),
(
# The service's own guidance for a misconfigured group library.
# It is a module constant, not exception-derived, and the route
# must pass it through rather than replacing it: this is the
# only text that tells the user WHICH setting is wrong.
{"success": False, "error": ZOTERO_GROUP_ID_REQUIRED_MESSAGE},
400,
{"success": False, "error": ZOTERO_GROUP_ID_REQUIRED_MESSAGE},
),
],
ids=["success", "rejected"],
)
def test_connection_result_controls_http_status(
service_result, status, expected
):
response = _test_connection_response(service_result)
assert response.status_code == status
assert _body(response) == expected
@pytest.mark.parametrize(
("service_result", "status"),
[
(
{
"success": True,
"library_version": 42,
"collection_count": 3,
"debug": "sk-live-ABCDEFGHIJKLMNOPQRSTUV",
"api_key": "secret-key",
},
200,
),
(
{
"success": False,
"error": ZOTERO_GROUP_ID_REQUIRED_MESSAGE,
"debug": "sk-live-ABCDEFGHIJKLMNOPQRSTUV",
"api_key": "secret-key",
},
400,
),
],
ids=["success", "rejected"],
)
def test_connection_body_is_built_from_a_key_allowlist(service_result, status):
"""Only the four known keys may reach the browser.
Catches the revert to ``JSONResponse(result, status_code=...)``, which
forwarded the service dict wholesale -- so any field a future
``test_connection`` adds (a config echo, a resolved id, a debug blob)
would ship to the client with nobody deciding it should.
"""
response = _test_connection_response(service_result)
body = _body(response)
assert response.status_code == status
assert set(body) <= {
"success",
"error",
"library_version",
"collection_count",
}
assert "debug" not in body
assert "api_key" not in body
assert "sk-live-ABCDEFGHIJKLMNOPQRSTUV" not in response.body.decode()
assert "secret-key" not in response.body.decode()
def test_status_combines_stored_state_with_live_progress():
service = Mock()
service.get_status.return_value = [{"collection_key": "A", "version": 9}]
service_class = Mock(return_value=service)
service_class.get_sync_progress.return_value = {
"processed": 2,
"total": 5,
}
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", service_class),
):
result = zotero.get_status(_request(), username="alice")
assert result == {
"success": True,
"collections": [{"collection_key": "A", "version": 9}],
"progress": {"processed": 2, "total": 5},
}
service_class.get_sync_progress.assert_called_once_with("alice")
def test_sync_already_running_returns_without_creating_a_thread():
service = Mock()
service.get_config.return_value = _configured()
service_class = Mock(return_value=service)
service_class.is_user_syncing.return_value = True
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", service_class),
patch.object(zotero.threading, "Thread") as thread,
):
result = zotero.sync_now(_request(), username="alice")
assert result == {
"success": True,
"message": "A Zotero sync is already running.",
"already_running": True,
}
service_class.is_user_syncing.assert_called_once_with("alice")
thread.assert_not_called()
def test_sync_preflight_failure_uses_the_stable_api_error_boundary():
failure = RuntimeError("database unavailable")
service = Mock()
service.get_config.side_effect = failure
service_class = Mock(return_value=service)
handled = Mock(name="handled-response")
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", service_class),
patch.object(
zotero, "handle_api_error", return_value=handled
) as handle_error,
patch.object(zotero.threading, "Thread") as thread_class,
):
result = zotero.sync_now(_request(), username="alice")
assert result is handled
service_class.assert_called_once_with("alice", "db-password")
handle_error.assert_called_once_with("starting Zotero sync", failure)
thread_class.assert_not_called()
def test_manual_sync_thread_reprocesses_skips_and_cleans_up():
front = Mock()
front.get_config.return_value = _configured()
worker = Mock()
cleanup = Mock()
cleanup.__enter__ = Mock(return_value=None)
cleanup.__exit__ = Mock(return_value=False)
service_class = Mock(side_effect=[front, worker])
service_class.is_user_syncing.return_value = False
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", service_class),
patch.object(zotero, "thread_cleanup", return_value=cleanup),
patch.object(zotero.threading, "Thread") as thread_class,
):
result = zotero.sync_now(_request(), username="alice")
target = thread_class.call_args.kwargs["target"]
target()
assert result == {"success": True, "message": "Zotero sync started."}
assert thread_class.call_args.kwargs == {
"target": target,
"name": "zotero-sync-alice",
"daemon": True,
}
thread_class.return_value.start.assert_called_once_with()
assert service_class.call_args_list == [
call("alice", "db-password"),
call("alice", "db-password"),
]
worker.sync_all.assert_called_once_with(reprocess_skipped=True)
cleanup.__enter__.assert_called_once_with()
cleanup.__exit__.assert_called_once()
def test_manual_sync_thread_contains_worker_failure_and_still_cleans_up():
"""The background thread's failure log must not carry a traceback.
``front.get_config()`` returns a real ``api_key`` and the daemon thread's
own ``password`` closure variable is live in this frame -- both would
render under loguru's ``diagnose`` if the handler used
``logger.exception`` instead of the scrubbed ``logger.warning`` this
fixes it to. See ``_zotero_error_response`` and
``ZoteroSyncService._sync_one``'s bare-``Exception`` arm for the same
shape.
"""
front = Mock()
front.get_config.return_value = _configured()
worker = Mock()
worker.sync_all.side_effect = RuntimeError("sync failed")
service_class = Mock(side_effect=[front, worker])
service_class.is_user_syncing.return_value = False
with (
patch.object(zotero, "_db_password", return_value="db-password"),
patch.object(zotero, "ZoteroSyncService", service_class),
patch.object(
zotero, "thread_cleanup", return_value=nullcontext()
) as cleanup,
patch.object(zotero.threading, "Thread") as thread_class,
patch.object(zotero, "logger") as mock_logger,
):
zotero.sync_now(_request(), username="alice")
thread_class.call_args.kwargs["target"]()
assert service_class.call_args_list == [
call("alice", "db-password"),
call("alice", "db-password"),
]
cleanup.assert_called_once_with()
mock_logger.exception.assert_not_called()
mock_logger.warning.assert_called_once()
warning_text = mock_logger.warning.call_args.args[0]
assert warning_text.startswith("Zotero manual sync failed:")
assert "sync failed" in warning_text
assert "secret-key" not in warning_text