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.

21.1 KB
Raw Blame
"""Unit tests for the Zotero Web API client.

The client's HTTP layer (``SafeSession``) is replaced with a mock so these
tests exercise pagination, parsing, batching, retry and error handling
without any network access.
"""

from unittest.mock import MagicMock

import pytest

from local_deep_research.research_library.zotero.client import (
    ZoteroAuthError,
    ZoteroClient,
    ZoteroError,
    ZoteroItem,
    ZoteroTransientError,
)


class FakeResponse:
    def __init__(
        self, status_code=200, json_data=None, headers=None, content=b""
    ):
        self.status_code = status_code
        self._json = json_data
        self._has_json = json_data is not None
        self.headers = headers or {}
        self.content = content

    def json(self):
        # Mirror requests.Response: calling .json() on a non-JSON / empty body
        # raises rather than silently returning {}.
        if not self._has_json:
            raise ValueError("No JSON object could be decoded")
        return self._json


def make_client():
    client = ZoteroClient(
        api_key="KEY", library_type="user", library_id="12345"
    )
    client.session = MagicMock()
    return client


def test_init_validates_inputs():
    with pytest.raises(ValueError):
        ZoteroClient(api_key="", library_type="user", library_id="1")
    with pytest.raises(ValueError):
        ZoteroClient(api_key="k", library_type="bogus", library_id="1")
    with pytest.raises(ValueError):
        ZoteroClient(api_key="k", library_type="user", library_id="")


def test_prefix_user_vs_group():
    u = ZoteroClient(api_key="k", library_type="user", library_id="7")
    g = ZoteroClient(api_key="k", library_type="group", library_id="9")
    assert u._prefix == "/users/7"
    assert g._prefix == "/groups/9"


def test_local_mode_no_key_required_and_base_url():
    # Local mode: API key optional, base points at the desktop local API.
    c = ZoteroClient(
        api_key="", library_type="user", library_id="7", local=True
    )
    assert c._base == "http://localhost:23119/api"
    assert "Zotero-API-Key" not in c.session.headers
    # Cloud mode still requires a key.
    with pytest.raises(ValueError):
        ZoteroClient(api_key="", library_type="user", library_id="7")


def test_local_download_reads_file_url(tmp_path):
    f = tmp_path / "paper.pdf"
    f.write_bytes(b"%PDF-local-bytes")
    client = ZoteroClient(
        api_key="", library_type="user", library_id="7", local=True
    )
    client.session = MagicMock()
    client.session.get.return_value = FakeResponse(
        302, headers={"Location": f.as_uri()}
    )
    assert client.download_attachment("ATT1") == b"%PDF-local-bytes"


def test_local_download_rejects_non_pdf_file(tmp_path):
    """A file:// redirect target without a PDF magic number is not returned —
    the local API port is unauthenticated, so this must not become a generic
    local-file disclosure primitive."""
    f = tmp_path / "secrets.txt"
    f.write_bytes(b"root:x:0:0:than which nothing less pdf-like exists")
    client = ZoteroClient(
        api_key="", library_type="user", library_id="7", local=True
    )
    client.session = MagicMock()
    client.session.get.return_value = FakeResponse(
        302, headers={"Location": f.as_uri()}
    )
    assert client.download_attachment("ATT1") is None


def test_cloud_mode_ignores_file_redirect():
    client = make_client()  # cloud mode
    client.session.get.return_value = FakeResponse(
        302, headers={"Location": "file:///etc/passwd"}
    )
    assert client.download_attachment("ATT1") is None


def test_get_groups_empty_in_local_mode():
    client = ZoteroClient(
        api_key="", library_type="user", library_id="7", local=True
    )
    client.session = MagicMock()
    assert client.get_groups() == []
    client.session.get.assert_not_called()


def test_test_connection_returns_library_version():
    client = make_client()
    client.session.get.return_value = FakeResponse(
        200, json_data={}, headers={"Last-Modified-Version": "42"}
    )
    assert client.test_connection() == 42


def test_get_collections_paginates():
    client = make_client()

    def page(n, count):
        return FakeResponse(
            200,
            json_data=[
                {"key": f"K{n}_{i}", "data": {"name": f"C{n}_{i}"}}
                for i in range(count)
            ],
        )

    # First page full (100), second page short (5) -> stop.
    client.session.get.side_effect = [page(1, 100), page(2, 5)]
    collections = client.get_collections()
    assert len(collections) == 105
    assert collections[0]["key"] == "K1_0"
    assert collections[0]["name"] == "C1_0"


def test_get_top_item_versions_parses_object_and_library_version():
    client = make_client()
    client.session.get.side_effect = [
        FakeResponse(
            200,
            json_data={"AAA": 3, "BBB": 7},
            headers={"Last-Modified-Version": "7", "Total-Results": "2"},
        )
    ]
    versions, library_version = client.get_top_item_versions("COLLKEY")
    assert versions == {"AAA": 3, "BBB": 7}
    assert library_version == 7
    # Scoped to the collection path.
    called_url = client.session.get.call_args_list[0].args[0]
    assert "/collections/COLLKEY/items/top" in called_url


def test_get_items_batches_over_50():
    client = make_client()
    keys = [f"K{i:03d}" for i in range(60)]
    client.session.get.side_effect = [
        FakeResponse(
            200,
            json_data=[
                {"key": k, "version": 1, "data": {"itemType": "journalArticle"}}
                for k in keys[:50]
            ],
        ),
        FakeResponse(
            200,
            json_data=[
                {"key": k, "version": 1, "data": {"itemType": "journalArticle"}}
                for k in keys[50:]
            ],
        ),
    ]
    items = client.get_items(keys)
    assert len(items) == 60
    assert client.session.get.call_count == 2
    # First batch carries exactly 50 keys.
    first_params = client.session.get.call_args_list[0].kwargs["params"]
    assert len(first_params["itemKey"].split(",")) == 50


def test_get_children_filters_non_attachments():
    client = make_client()
    client.session.get.side_effect = [
        FakeResponse(
            200,
            json_data=[
                {
                    "key": "ATT1",
                    "version": 5,
                    "data": {
                        "itemType": "attachment",
                        "contentType": "application/pdf",
                        "linkMode": "imported_file",
                        "filename": "paper.pdf",
                        "title": "Full Text",
                    },
                },
                {
                    "key": "NOTE1",
                    "version": 6,
                    "data": {"itemType": "note"},
                },
            ],
        )
    ]
    children = client.get_children("PARENT")
    assert len(children) == 1
    att = children[0]
    assert att.key == "ATT1"
    assert att.is_downloadable_pdf is True


def test_get_notes_strips_html():
    client = make_client()
    client.session.get.side_effect = [
        FakeResponse(
            200,
            json_data=[
                {
                    "key": "NOTE1",
                    "data": {
                        "itemType": "note",
                        "note": "<p>First &amp; second</p><p>line two</p>",
                    },
                },
                {"key": "ATT", "data": {"itemType": "attachment"}},
            ],
        )
    ]
    notes = client.get_notes("ITEM")
    assert notes == ["First & second\nline two"]


def test_get_annotations_combines_text_and_comment():
    client = make_client()
    client.session.get.side_effect = [
        FakeResponse(
            200,
            json_data=[
                {
                    "key": "ANN1",
                    "data": {
                        "itemType": "annotation",
                        "annotationText": "important passage",
                        "annotationComment": "my thought",
                    },
                },
                {
                    "key": "ANN2",
                    "data": {
                        "itemType": "annotation",
                        "annotationType": "image",  # no text -> skipped
                    },
                },
            ],
        )
    ]
    anns = client.get_annotations("ATT")
    assert anns == ["important passage — [note] my thought"]


def test_download_attachment_returns_bytes():
    client = make_client()
    client.session.get.return_value = FakeResponse(200, content=b"%PDF-1.7 x")
    assert client.download_attachment("ATT1") == b"%PDF-1.7 x"


def test_403_raises_auth_error():
    client = make_client()
    client.session.get.return_value = FakeResponse(403)
    with pytest.raises(ZoteroAuthError):
        client.test_connection()


def test_404_raises_zotero_error():
    client = make_client()
    client.session.get.return_value = FakeResponse(404)
    with pytest.raises(ZoteroError):
        client.get_collections()


def test_retry_on_429_then_success(monkeypatch):
    monkeypatch.setattr(
        "local_deep_research.research_library.zotero.client.time.sleep",
        lambda *_a, **_k: None,
    )
    client = make_client()
    client.session.get.side_effect = [
        FakeResponse(429, headers={"Backoff": "0"}),
        FakeResponse(200, json_data=[], headers={"Last-Modified-Version": "1"}),
    ]
    # get_collections should retry past the 429 and succeed (empty list).
    assert client.get_collections() == []
    assert client.session.get.call_count == 2


def test_transient_error_raised_after_retry_exhaustion(monkeypatch):
    # 429 on every attempt exhausts the retries and must raise the *transient*
    # subclass, not a bare ZoteroError — so per-item callers can tell it apart
    # from a genuine "no file".
    monkeypatch.setattr(
        "local_deep_research.research_library.zotero.client.time.sleep",
        lambda *_a, **_k: None,
    )
    client = make_client()
    client.session.get.side_effect = [
        FakeResponse(429, headers={"Backoff": "0"}) for _ in range(4)
    ]
    with pytest.raises(ZoteroTransientError):
        client.get_collections()
    assert client.session.get.call_count == 4


def test_download_attachment_reraises_transient_error(monkeypatch):
    # A rate-limit blip on the /file request must propagate (so the sync skips
    # the item and retries next run) rather than be swallowed to None, which
    # would silently import the paper metadata-only forever.
    monkeypatch.setattr(
        "local_deep_research.research_library.zotero.client.time.sleep",
        lambda *_a, **_k: None,
    )
    client = make_client()
    client.session.get.side_effect = [FakeResponse(503) for _ in range(4)]
    with pytest.raises(ZoteroTransientError):
        client.download_attachment("ATT1")


def test_download_attachment_returns_none_on_404():
    # A genuine "this attachment has no downloadable file" (404) is NOT
    # transient — return None so the item imports metadata-only.
    client = make_client()
    client.session.get.return_value = FakeResponse(404)
    assert client.download_attachment("ATT1") is None


def test_get_attachment_fulltext_reraises_transient_error(monkeypatch):
    monkeypatch.setattr(
        "local_deep_research.research_library.zotero.client.time.sleep",
        lambda *_a, **_k: None,
    )
    client = make_client()
    client.session.get.side_effect = [
        FakeResponse(429, headers={"Backoff": "0"}) for _ in range(4)
    ]
    with pytest.raises(ZoteroTransientError):
        client.get_attachment_fulltext("ATT1")


def test_local_mode_omits_key_header_even_when_configured():
    # Security: the desktop local API is plaintext HTTP on loopback and needs
    # no key. Even if the user has an API key set, it must never be attached to
    # local-mode requests.
    c = ZoteroClient(
        api_key="SECRETKEY", library_type="user", library_id="7", local=True
    )
    assert "Zotero-API-Key" not in c.session.headers
    # Cloud mode DOES attach it.
    c2 = ZoteroClient(api_key="SECRETKEY", library_type="user", library_id="7")
    assert c2.session.headers.get("Zotero-API-Key") == "SECRETKEY"


def test_non_transient_error_is_not_retried():
    # SafeSession raises ValueError on SSRF / oversized body. That is not a
    # transient error and must NOT be retried 4x.
    client = make_client()
    client.session.get.side_effect = ValueError("possible SSRF")
    with pytest.raises(ZoteroError):
        client.get_collections()
    assert client.session.get.call_count == 1


def test_top_item_versions_paginates_without_total_results_header():
    # Regression: when Total-Results is absent and the first page is exactly
    # full, the loop must NOT stop early (which would silently drop items and
    # the sync would treat them as deletions).
    client = make_client()
    full_page = {f"K{i:03d}": 1 for i in range(100)}  # exactly _PAGE_LIMIT
    short_page = {"K100": 1, "K101": 1, "K102": 1}
    client.session.get.side_effect = [
        FakeResponse(200, json_data=full_page, headers={}),  # no Total-Results
        FakeResponse(200, json_data=short_page, headers={}),
    ]
    versions, _ = client.get_top_item_versions(None)
    assert len(versions) == 103
    assert client.session.get.call_count == 2


def test_download_attachment_does_not_leak_key_on_redirect(monkeypatch):
    # The /file endpoint 302-redirects to storage. We must follow that with a
    # fresh credential-free session, never the authenticated one.
    client = make_client()
    client.session.get.return_value = FakeResponse(
        302, headers={"Location": "https://files.example.com/abc"}
    )

    clean = MagicMock()
    clean.get.return_value = FakeResponse(200, content=b"PDFBYTES")
    clean.__enter__ = MagicMock(return_value=clean)
    clean.__exit__ = MagicMock(return_value=False)
    monkeypatch.setattr(
        "local_deep_research.research_library.zotero.client.SafeSession",
        lambda *a, **k: clean,
    )

    result = client.download_attachment("ATT1")
    assert result == b"PDFBYTES"
    # Initial request used allow_redirects=False on the AUTHENTICATED session.
    assert client.session.get.call_args.kwargs.get("allow_redirects") is False
    # The redirect target was fetched by a DIFFERENT (credential-free) session.
    assert clean.get.call_args.args[0] == "https://files.example.com/abc"
    assert clean is not client.session


def test_download_attachment_direct_200_no_redirect():
    client = make_client()
    client.session.get.return_value = FakeResponse(200, content=b"DIRECT")
    assert client.download_attachment("ATT1") == b"DIRECT"


def test_download_attachment_reraises_transient_from_storage(monkeypatch):
    # A transient failure (5xx) fetching the REDIRECTED storage URL must
    # propagate so the sync retries next run, not be swallowed to None (which
    # would import the paper metadata-only forever).
    client = make_client()
    client.session.get.return_value = FakeResponse(
        302, headers={"Location": "https://files.example.com/abc"}
    )
    clean = MagicMock()
    clean.get.return_value = FakeResponse(503)
    clean.__enter__ = MagicMock(return_value=clean)
    clean.__exit__ = MagicMock(return_value=False)
    monkeypatch.setattr(
        "local_deep_research.research_library.zotero.client.SafeSession",
        lambda *a, **k: clean,
    )
    with pytest.raises(ZoteroTransientError):
        client.download_attachment("ATT1")


def test_get_groups_resolves_user_then_lists():
    client = make_client()
    client.session.get.side_effect = [
        FakeResponse(200, json_data={"userID": 99, "username": "x"}),
        FakeResponse(
            200,
            json_data=[{"id": 123, "data": {"name": "Lab", "type": "Private"}}],
        ),
    ]
    groups = client.get_groups()
    assert groups == [{"id": "123", "name": "Lab", "type": "Private"}]
    # First call hits the non-prefixed /keys/current endpoint.
    assert (
        client.session.get.call_args_list[0].args[0].endswith("/keys/current")
    )
    # Second call hits /users/99/groups (also non-prefixed).
    assert "/users/99/groups" in client.session.get.call_args_list[1].args[0]


def test_get_groups_empty_when_no_user():
    client = make_client()
    client.session.get.return_value = FakeResponse(200, json_data={})
    assert client.get_groups() == []


def test_get_attachment_fulltext_returns_content():
    client = make_client()
    client.session.get.return_value = FakeResponse(
        200, json_data={"content": "hello world", "indexedChars": 11}
    )
    assert client.get_attachment_fulltext("ATT1") == "hello world"


def test_get_attachment_fulltext_none_when_not_indexed():
    client = make_client()
    client.session.get.return_value = FakeResponse(404)  # no indexed text
    assert client.get_attachment_fulltext("ATT1") is None


def test_get_attachment_fulltext_none_when_empty():
    client = make_client()
    client.session.get.return_value = FakeResponse(
        200, json_data={"content": ""}
    )
    assert client.get_attachment_fulltext("ATT1") is None


def test_top_item_versions_raises_on_incomplete_map():
    # Total-Results says 5 items exist, but the (short) page returns only 2.
    # Returning that partial map would make the sync treat 3 items as
    # deletions — so the client must raise instead.
    client = make_client()
    client.session.get.return_value = FakeResponse(
        200, json_data={"A": 1, "B": 1}, headers={"Total-Results": "5"}
    )
    with pytest.raises(ZoteroError):
        client.get_top_item_versions(None)


def test_top_item_versions_complete_map_ok():
    client = make_client()
    client.session.get.return_value = FakeResponse(
        200,
        json_data={"A": 1, "B": 2},
        headers={"Total-Results": "2", "Last-Modified-Version": "9"},
    )
    versions, lib = client.get_top_item_versions(None)
    assert versions == {"A": 1, "B": 2}
    assert lib == 9


def test_pagination_has_a_hard_safety_cap(monkeypatch):
    # A server that returns a FULL page forever (ignores `start`, no
    # Total-Results) must not loop unbounded — the page cap aborts it.
    monkeypatch.setattr(
        "local_deep_research.research_library.zotero.client._MAX_PAGES", 2
    )
    client = make_client()
    full = [{"key": f"K{i}", "data": {"name": "n"}} for i in range(100)]
    client.session.get.return_value = FakeResponse(200, json_data=full)
    with pytest.raises(ZoteroError):
        client.get_collections()


# ---------------------------------------------------------------------------
# Standalone PDF attachments (a PDF dropped into Zotero with no parent item)
# ---------------------------------------------------------------------------


def _standalone_data(**overrides):
    data = {
        "title": "paper.pdf",
        "filename": "paper.pdf",
        "contentType": "application/pdf",
        "linkMode": "imported_file",
    }
    data.update(overrides)
    return data


def test_is_standalone_pdf_attachment():
    yes = ZoteroItem("K1", 3, "attachment", _standalone_data())
    assert yes.is_standalone_pdf_attachment is True

    # linked_file points at the user's local disk — not servable by the API.
    linked = ZoteroItem(
        "K2", 3, "attachment", _standalone_data(linkMode="linked_file")
    )
    assert linked.is_standalone_pdf_attachment is False

    # A child attachment has a parent — the parent is what gets imported.
    child = ZoteroItem(
        "K3", 3, "attachment", _standalone_data(parentItem="PARENT01")
    )
    assert child.is_standalone_pdf_attachment is False

    non_pdf = ZoteroItem(
        "K4", 3, "attachment", _standalone_data(contentType="text/html")
    )
    assert non_pdf.is_standalone_pdf_attachment is False

    note = ZoteroItem("K5", 3, "note", {})
    assert note.is_standalone_pdf_attachment is False


def test_as_attachment_maps_item_fields():
    item = ZoteroItem("KEY9", 7, "attachment", _standalone_data())
    att = item.as_attachment()
    assert att.key == "KEY9"
    assert att.version == 7
    assert att.content_type == "application/pdf"
    assert att.link_mode == "imported_file"
    assert att.filename == "paper.pdf"
    assert att.is_downloadable_pdf is True


# ---------------------------------------------------------------------------
# /keys/current + clearer HTTP 400
# ---------------------------------------------------------------------------


def test_get_current_key_info():
    client = make_client()
    client.session.get.return_value = FakeResponse(
        200, json_data={"userID": 20971466, "username": "someone"}
    )
    info = client.get_current_key_info()
    assert info["userID"] == 20971466
    # Non-prefixed endpoint: the library prefix must NOT be in the URL.
    url = client.session.get.call_args.args[0]
    assert url.endswith("/keys/current")
    assert "/users/" not in url


def test_get_current_key_info_local_mode_is_empty():
    client = ZoteroClient(
        api_key="", library_type="user", library_id="0", local=True
    )
    client.session = MagicMock()
    assert client.get_current_key_info() == {}
    client.session.get.assert_not_called()


def test_request_400_mentions_numeric_library_id():
    client = make_client()
    client.session.get.return_value = FakeResponse(400)
    with pytest.raises(ZoteroError, match="NUMERIC"):
        client.test_connection()