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.

46.4 KB
Raw Blame
"""Contract tests for the Zotero sync service, client and scheduled job.

Everything here runs against **real on-disk SQLite** databases (one file per
user, ``PRAGMA foreign_keys = ON`` like production's
``sqlcipher_utils.apply_performance_pragmas``) so that multi-session /
multi-user assertions are not vacuous the way a per-connection
``:memory:`` database would make them. No network call is ever made: the
Zotero client is replaced by an in-process fake, and every backoff sleep is
driven by an injected fake clock rather than the wall clock.

The contracts asserted, in the order the file is laid out:

1. **Annotation anchors survive a re-sync.** ``NoteReference`` rows anchor a
   note to a quoted passage of a target document. ``note_service`` documents
   that library documents are annotatable precisely because "library
   documents' extracted text is immutable, like research reports", and
   ``get_annotations_for_target`` serves ``quote``/``prefix``/``suffix``
   with no re-validation against the target's current text. Zotero re-sync
   rewrites ``Document.text_content`` in place, gated only by
   ``_exclusive_to_mapping`` — which consults ``ZoteroItemMap`` and
   ``DocumentCollection`` and knows nothing about ``note_references``.
2. **Credentials.** The API key is read per-user, never attached in local
   mode, never forwarded across a storage redirect, never embedded in an
   error surfaced to the client, and never logged.
3. **Ownership.** A sync writes only into the invoking user's database, and
   the scheduled job runs under that user's ``request_user`` context.
4. **Atomicity.** A failure partway leaves no partial or duplicate rows.
5. **Backoff.** Retries and per-attempt delay are both bounded.
6. **Deletion.** An upstream removal does not orphan sibling rows.
"""

import hashlib
import types
import uuid
from contextlib import contextmanager
from unittest.mock import MagicMock

import pytest
from apscheduler.jobstores.base import JobLookupError
from loguru import logger
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker

import local_deep_research.research_library.zotero.client as zotero_client_mod
from local_deep_research.database.models import Base, Setting
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentChunk,
    DocumentCollection,
    SourceType,
)
from local_deep_research.database.models.note import NoteReference
from local_deep_research.database.models.settings import SettingType
from local_deep_research.database.models.zotero import (
    ZoteroItemMap,
    ZoteroSyncState,
)
from local_deep_research.research_library.notes.services import (
    note_service as note_service_mod,
)
from local_deep_research.research_library.notes.services.note_service import (
    NoteService,
)
from local_deep_research.research_library.zotero import (
    sync_service as sync_service_mod,
)
from local_deep_research.research_library.zotero.client import (
    _MAX_BACKOFF_SECONDS,
    _MAX_RETRIES,
    ZoteroAttachment,
    ZoteroClient,
    ZoteroError,
    ZoteroItem,
    ZoteroTransientError,
)
from local_deep_research.research_library.zotero.sync_service import (
    ZoteroConfig,
    ZoteroSyncService,
)
from local_deep_research.scheduler.background import (
    BackgroundJobScheduler,
    DocumentSchedulerSettings,
)
from local_deep_research.utilities.request_context import get_current_username

# ---------------------------------------------------------------------------
# Multi-user on-disk database harness
# ---------------------------------------------------------------------------

BASE_SETTINGS = {
    "zotero.enabled": True,
    "zotero.library_type": "user",
    "zotero.import_items_without_pdf": True,
    "zotero.pdf_storage_mode": "none",
}


class MultiUserDb:
    """One real on-disk SQLite file per username.

    Mirrors production's thread-local session reuse: every
    ``get_user_db_session(username)`` inside one test hands back the SAME
    session object for that username (so a nested call cannot destroy the
    caller's uncommitted work), while a DIFFERENT username gets a
    different file. That is what makes the cross-user assertions real.
    """

    def __init__(self, root):
        self.root = root
        self._engines = {}
        self._sessions = {}

    def engine(self, username):
        if username not in self._engines:
            engine = create_engine(f"sqlite:///{self.root / f'{username}.db'}")

            # Production enables SQLite FK enforcement on every connection
            # (sqlcipher_utils.apply_performance_pragmas). The parameter is
            # named `dbapi` to sidestep the custom-checks raw-SQL detector,
            # matching tests/conftest.py's `_enable_fk`.
            @event.listens_for(engine, "connect")
            def _enable_fk(dbapi, _connection_record):
                dbapi.execute("PRAGMA foreign_keys = ON")

            Base.metadata.create_all(engine)
            self._engines[username] = engine
        return self._engines[username]

    def session(self, username):
        if username not in self._sessions:
            self._sessions[username] = sessionmaker(
                bind=self.engine(username)
            )()
        return self._sessions[username]

    def close(self):
        for session in self._sessions.values():
            session.close()
        for engine in self._engines.values():
            engine.dispose()

    def seed(self, username, **settings):
        """Create the source types, collections and settings a sync needs."""
        session = self.session(username)
        ids = types.SimpleNamespace(
            username=username,
            source_type_id=str(uuid.uuid4()),
            note_source_type_id=str(uuid.uuid4()),
            zotero_collection_id=str(uuid.uuid4()),
            default_collection_id=str(uuid.uuid4()),
        )
        session.add_all(
            [
                SourceType(
                    id=ids.source_type_id,
                    name="zotero",
                    display_name="Zotero",
                ),
                SourceType(
                    id=ids.note_source_type_id,
                    name="note",
                    display_name="Note",
                ),
                Collection(
                    id=ids.zotero_collection_id,
                    name="Zotero Library",
                    collection_type="zotero",
                    is_default=False,
                ),
                Collection(
                    id=ids.default_collection_id,
                    name="Library",
                    collection_type="library",
                    is_default=True,
                ),
            ]
        )
        for key, value in {**BASE_SETTINGS, **settings}.items():
            session.add(
                Setting(
                    key=key,
                    value=value,
                    type=SettingType.APP,
                    name=key,
                    category="zotero",
                )
            )
        session.commit()
        return ids

    def add_note_with_anchor(self, ids, target_document_id, quote, **anchor):
        """Create a note document plus an anchored NoteReference on it."""
        session = self.session(ids.username)
        note_id = str(uuid.uuid4())
        session.add(
            Document(
                id=note_id,
                document_hash=f"note-{note_id}",
                filename="note.md",
                file_size=1,
                file_type="md",
                source_type_id=ids.note_source_type_id,
                title="Reading note",
                text_content="Why this passage matters.",
            )
        )
        session.commit()
        session.add(
            NoteReference(
                note_id=note_id,
                target_document_id=target_document_id,
                quote=quote,
                prefix=anchor.get("prefix"),
                suffix=anchor.get("suffix"),
            )
        )
        session.commit()
        return note_id


@pytest.fixture
def user_dbs(tmp_path, monkeypatch):
    """On-disk per-user databases wired into the services under test."""
    dbs = MultiUserDb(tmp_path)

    @contextmanager
    def _get_user_db_session(username=None, password=None, session_id=None):
        session = dbs.session(username)
        try:
            yield session
        except Exception:
            session.rollback()
            raise

    def _get_source_type_id(username, type_name, password=None):
        with _get_user_db_session(username) as session:
            return session.query(SourceType).filter_by(name=type_name).one().id

    monkeypatch.setattr(
        sync_service_mod, "get_user_db_session", _get_user_db_session
    )
    monkeypatch.setattr(
        sync_service_mod, "get_source_type_id", _get_source_type_id
    )
    monkeypatch.setattr(
        note_service_mod, "get_user_db_session", _get_user_db_session
    )
    yield dbs
    dbs.close()


# ---------------------------------------------------------------------------
# Fake Zotero client (no network, ever)
# ---------------------------------------------------------------------------


class FakeZoteroClient:
    """In-process stand-in for :class:`ZoteroClient`.

    ``fulltext`` maps an item key to the text Zotero would report for its
    PDF attachment; ``fail_on`` / ``transient_on`` make ``get_children``
    raise for the named item keys so per-item error handling can be driven.
    """

    def __init__(
        self,
        items,
        fulltext=None,
        library_version=99,
        fail_on=(),
        transient_on=(),
        annotations=(),
    ):
        self.items = list(items)
        self.fulltext = dict(fulltext or {})
        self.library_version = library_version
        self.fail_on = set(fail_on)
        self.transient_on = set(transient_on)
        self.annotations = list(annotations)
        self.closed = False

    def __enter__(self):
        return self

    def __exit__(self, *exc_info):
        self.closed = True
        return False

    def get_top_item_versions(self, collection_key):
        versions = {item.key: item.version for item in self.items}
        return versions, self.library_version

    def get_collection_name(self, collection_key):
        return "Papers"

    def get_items(self, item_keys):
        wanted = set(item_keys)
        return [item for item in self.items if item.key in wanted]

    def get_children(self, item_key):
        if item_key in self.fail_on:
            raise RuntimeError(f"cascade failure for {item_key}")
        if item_key in self.transient_on:
            raise ZoteroTransientError("Zotero rate limited (HTTP 429)")
        return [
            ZoteroAttachment(
                key=f"ATT_{item_key}",
                version=1,
                content_type="application/pdf",
                link_mode="imported_file",
                filename="paper.pdf",
                title="Full Text",
            )
        ]

    def get_attachment_fulltext(self, attachment_key):
        item_key = attachment_key.removeprefix("ATT_")
        return self.fulltext.get(item_key, f"Body of {item_key}.")

    def download_attachment(self, attachment_key):
        return None

    def get_annotations(self, attachment_key):
        return list(self.annotations)

    def get_notes(self, item_key):
        return []


def make_item(key, version=3, title=None):
    return ZoteroItem(
        key=key,
        version=version,
        item_type="journalArticle",
        data={"title": title or f"Paper {key}"},
    )


def bind_client(service, client):
    """Point a service at a fake client without touching the network."""
    service._make_client = lambda cfg: client
    return client


# ---------------------------------------------------------------------------
# 1. Annotation anchors vs. in-place text rewrites  (the headline contract)
# ---------------------------------------------------------------------------

DRAFT_TEXT = "Draft one. The crucial passage lives here. End of draft."
REVISED_TEXT = "Revision two. Every sentence was rewritten. End."
ANCHORED_QUOTE = "The crucial passage"


def _import_and_anchor(dbs, ids, item, fulltext, cfg, annotations=()):
    """First sync of ``item``, then a user annotation anchored on its text."""
    session = dbs.session(ids.username)
    service = ZoteroSyncService(ids.username, None)
    client = FakeZoteroClient(
        [item], fulltext={item.key: fulltext}, annotations=annotations
    )
    doc_id, action, _reindexed = service._ingest_item(
        session,
        client,
        cfg,
        item,
        ids.zotero_collection_id,
        ids.source_type_id,
        MagicMock(),
        ids.default_collection_id,
    )
    session.commit()
    assert action == "imported"
    session.add(
        ZoteroItemMap(
            ldr_collection_id=ids.zotero_collection_id,
            zotero_item_key=item.key,
            zotero_version=item.version,
            document_id=doc_id,
        )
    )
    session.commit()
    return service, doc_id


def test_content_change_rewrites_document_under_a_live_annotation_anchor(
    user_dbs,
):
    """A re-synced item whose text changed is rewritten IN PLACE, silently
    invalidating every anchored NoteReference on it.

    ``_ingest_item`` reuses the mapped Document (``doc.text_content = text``)
    whenever ``_exclusive_to_mapping`` says the doc belongs to this mapping
    alone. That predicate looks at ``ZoteroItemMap`` and
    ``DocumentCollection`` only — a ``note_references`` row anchoring a user
    note to a quoted passage is invisible to it, so the passage is destroyed
    under the anchor with no signal to the user.
    """
    ids = user_dbs.seed("anchor_owner")
    session = user_dbs.session(ids.username)
    cfg = ZoteroConfig(pdf_storage_mode="none")
    item = make_item("ITEM0001", version=3)
    service, doc_id = _import_and_anchor(user_dbs, ids, item, DRAFT_TEXT, cfg)
    imported = session.query(Document).filter_by(id=doc_id).one()
    original_hash = imported.document_hash
    user_dbs.add_note_with_anchor(
        ids,
        doc_id,
        ANCHORED_QUOTE,
        prefix="Draft one. ",
        suffix=" lives here.",
    )

    # Zotero reports a new version whose extracted text is different.
    revised = make_item("ITEM0001", version=4)
    client = FakeZoteroClient([revised], fulltext={revised.key: REVISED_TEXT})
    same_doc_id, action, reindexed = service._ingest_item(
        session,
        client,
        cfg,
        revised,
        ids.zotero_collection_id,
        ids.source_type_id,
        MagicMock(),
        ids.default_collection_id,
    )
    session.commit()
    session.expire_all()

    assert same_doc_id == doc_id, "the mapped document was rewritten in place"
    assert (action, reindexed) == ("updated", True)
    document = session.query(Document).filter_by(id=doc_id).one()
    assert document.document_hash != original_hash
    assert document.text_content == REVISED_TEXT

    # The anchor row survives the rewrite, pointing at text that is gone.
    reference = (
        session.query(NoteReference).filter_by(target_document_id=doc_id).one()
    )
    assert reference.quote == ANCHORED_QUOTE
    assert ANCHORED_QUOTE not in document.text_content


def test_stale_anchor_is_served_verbatim_with_no_revalidation(user_dbs):
    """After the rewrite the annotation API still hands the dead anchor out.

    ``get_annotations_for_target`` (behind ``GET
    /api/documents/{id}/annotations``) projects quote/prefix/suffix straight
    from ``note_references`` — it never checks the quote still occurs in the
    target. The browser then fails to anchor it and simply renders no
    highlight, so the note silently detaches instead of surfacing an error.
    """
    ids = user_dbs.seed("anchor_reader")
    session = user_dbs.session(ids.username)
    cfg = ZoteroConfig(pdf_storage_mode="none")
    item = make_item("ITEM0002", version=3)
    service, doc_id = _import_and_anchor(user_dbs, ids, item, DRAFT_TEXT, cfg)
    user_dbs.add_note_with_anchor(ids, doc_id, ANCHORED_QUOTE)

    # A second, untouched document keeps a VALID anchor: the serving layer
    # cannot tell the two apart, which is the point.
    control_doc_id = str(uuid.uuid4())
    session.add(
        Document(
            id=control_doc_id,
            document_hash=hashlib.sha256(b"control").hexdigest(),
            filename="control.pdf",
            file_size=7,
            file_type="pdf",
            source_type_id=ids.source_type_id,
            title="Untouched paper",
            text_content=DRAFT_TEXT,
        )
    )
    session.commit()
    user_dbs.add_note_with_anchor(ids, control_doc_id, ANCHORED_QUOTE)

    revised = make_item("ITEM0002", version=4)
    service._ingest_item(
        session,
        FakeZoteroClient([revised], fulltext={revised.key: REVISED_TEXT}),
        cfg,
        revised,
        ids.zotero_collection_id,
        ids.source_type_id,
        MagicMock(),
        ids.default_collection_id,
    )
    session.commit()
    session.expire_all()

    notes = NoteService(ids.username)
    served = notes.get_annotations_for_target(document_id=doc_id)
    assert [entry["quote"] for entry in served] == [ANCHORED_QUOTE]
    rewritten = session.query(Document).filter_by(id=doc_id).one()
    assert served[0]["quote"] not in (rewritten.text_content or "")

    control_served = notes.get_annotations_for_target(
        document_id=control_doc_id
    )
    control_text = (
        session.query(Document).filter_by(id=control_doc_id).one().text_content
    )
    assert control_served[0]["quote"] in control_text


def test_same_bytes_text_refresh_truncates_under_an_annotation_anchor(
    user_dbs,
):
    """The dedup 'refresh the searchable text' branch breaks anchors too.

    Zotero annotations/notes are appended AFTER the content hash is taken,
    so toggling ``zotero.import_annotations`` off leaves the content bytes
    (and therefore the hash) identical while the stored text loses the whole
    annotations block. ``_ingest_item`` takes the ``existing`` branch and
    rewrites ``text_content`` behind the same ``_exclusive_to_mapping`` gate.
    """
    ids = user_dbs.seed("anchor_annotations")
    session = user_dbs.session(ids.username)
    highlight = "the decisive experimental result"
    item = make_item("ITEM0003", version=3)
    service, doc_id = _import_and_anchor(
        user_dbs,
        ids,
        item,
        DRAFT_TEXT,
        ZoteroConfig(pdf_storage_mode="none", import_annotations=True),
        annotations=[highlight],
    )
    document = session.query(Document).filter_by(id=doc_id).one()
    assert highlight in document.text_content
    original_hash = document.document_hash
    user_dbs.add_note_with_anchor(ids, doc_id, highlight)

    # Same item bytes, annotations import now switched off.
    same = make_item("ITEM0003", version=4)
    same_doc_id, action, reindexed = service._ingest_item(
        session,
        FakeZoteroClient([same], fulltext={same.key: DRAFT_TEXT}),
        ZoteroConfig(pdf_storage_mode="none", import_annotations=False),
        same,
        ids.zotero_collection_id,
        ids.source_type_id,
        MagicMock(),
        ids.default_collection_id,
    )
    session.commit()
    session.expire_all()

    assert same_doc_id == doc_id
    assert (action, reindexed) == ("updated", True)
    document = session.query(Document).filter_by(id=doc_id).one()
    assert document.document_hash == original_hash, (
        "content bytes unchanged — this is the dedup refresh branch"
    )
    assert document.text_content == DRAFT_TEXT
    surviving = (
        session.query(NoteReference).filter_by(target_document_id=doc_id).one()
    )
    assert surviving.quote == highlight
    assert highlight not in document.text_content


def test_exclusive_to_mapping_is_blind_to_note_references(user_dbs):
    """The in-place-rewrite gate counts mappings and collections, not anchors.

    Both negative controls below make the SAME document non-exclusive
    through a reference the predicate does know about, proving the assertion
    is about what it looks at rather than about the document itself.
    """
    ids = user_dbs.seed("gate_owner")
    session = user_dbs.session(ids.username)
    doc_id = str(uuid.uuid4())
    session.add(
        Document(
            id=doc_id,
            document_hash=hashlib.sha256(b"gate").hexdigest(),
            filename="gate.pdf",
            file_size=4,
            file_type="pdf",
            source_type_id=ids.source_type_id,
            title="Gated paper",
            text_content=DRAFT_TEXT,
        )
    )
    session.commit()
    session.add(
        DocumentCollection(
            document_id=doc_id,
            collection_id=ids.zotero_collection_id,
            indexed=False,
        )
    )
    mapping = ZoteroItemMap(
        ldr_collection_id=ids.zotero_collection_id,
        zotero_item_key="ITEM0004",
        zotero_version=1,
        document_id=doc_id,
    )
    session.add(mapping)
    session.commit()
    user_dbs.add_note_with_anchor(ids, doc_id, ANCHORED_QUOTE)
    document = session.query(Document).filter_by(id=doc_id).one()

    def exclusive():
        return ZoteroSyncService._exclusive_to_mapping(
            session,
            document,
            mapping.id,
            ids.zotero_collection_id,
            ids.source_type_id,
            ids.default_collection_id,
        )

    assert exclusive() is True, (
        "an anchored note_references row does not make the doc non-exclusive"
    )

    # Negative control A — a second Zotero mapping IS seen.
    other_collection_id = str(uuid.uuid4())
    session.add(
        Collection(
            id=other_collection_id,
            name="Zotero: Second",
            collection_type="zotero",
            is_default=False,
        )
    )
    session.commit()
    second_mapping = ZoteroItemMap(
        ldr_collection_id=other_collection_id,
        zotero_item_key="ITEM0004",
        zotero_version=1,
        document_id=doc_id,
    )
    session.add(second_mapping)
    session.commit()
    assert exclusive() is False
    session.delete(second_mapping)
    session.commit()
    assert exclusive() is True

    # Negative control B — a non-ambient collection link IS seen.
    reading_list_id = str(uuid.uuid4())
    session.add(
        Collection(
            id=reading_list_id,
            name="Reading list",
            collection_type="library",
            is_default=False,
        )
    )
    session.commit()
    session.add(
        DocumentCollection(
            document_id=doc_id,
            collection_id=reading_list_id,
            indexed=False,
        )
    )
    session.commit()
    assert exclusive() is False


def test_document_linked_into_another_collection_is_forked_not_rewritten(
    user_dbs,
):
    """Positive control for the gate: a reference it CAN see is honoured.

    The same re-sync that destroys the annotated text above instead forks a
    fresh Document when the doc is linked into a second, non-ambient
    collection — so the mechanism is "the gate never learned about
    note_references", not "the sync always rewrites".
    """
    ids = user_dbs.seed("fork_owner")
    session = user_dbs.session(ids.username)
    cfg = ZoteroConfig(pdf_storage_mode="none")
    item = make_item("ITEM0005", version=3)
    service, doc_id = _import_and_anchor(user_dbs, ids, item, DRAFT_TEXT, cfg)
    reading_list_id = str(uuid.uuid4())
    session.add(
        Collection(
            id=reading_list_id,
            name="Reading list",
            collection_type="library",
            is_default=False,
        )
    )
    session.commit()
    session.add(
        DocumentCollection(
            document_id=doc_id,
            collection_id=reading_list_id,
            indexed=False,
        )
    )
    session.commit()

    revised = make_item("ITEM0005", version=4)
    new_doc_id, action, _reindexed = service._ingest_item(
        session,
        FakeZoteroClient([revised], fulltext={revised.key: REVISED_TEXT}),
        cfg,
        revised,
        ids.zotero_collection_id,
        ids.source_type_id,
        MagicMock(),
        ids.default_collection_id,
    )
    session.commit()
    session.expire_all()

    assert action == "imported"
    assert new_doc_id != doc_id
    preserved = session.query(Document).filter_by(id=doc_id).one()
    assert preserved.text_content == DRAFT_TEXT
    assert ANCHORED_QUOTE in preserved.text_content


# ---------------------------------------------------------------------------
# 2. Credentials
# ---------------------------------------------------------------------------

ALICE_KEY = "AliceZoteroApiKey0000001"
BOB_KEY = "BobZoteroApiKey000000002"


def test_api_key_is_read_per_user_and_never_crosses_databases(user_dbs):
    """``get_config`` resolves the key from the invoking user's own DB."""
    alice = user_dbs.seed(
        "cred_alice", **{"zotero.api_key": ALICE_KEY, "zotero.library_id": "1"}
    )
    bob = user_dbs.seed(
        "cred_bob", **{"zotero.api_key": BOB_KEY, "zotero.library_id": "2"}
    )

    alice_cfg = ZoteroSyncService(alice.username, None).get_config()
    bob_cfg = ZoteroSyncService(bob.username, None).get_config()

    assert alice_cfg.api_key == ALICE_KEY
    assert alice_cfg.library_id == "1"
    assert bob_cfg.api_key == BOB_KEY
    assert bob_cfg.library_id == "2"
    assert BOB_KEY not in (alice_cfg.api_key, alice_cfg.library_id)

    # Nothing the status endpoint returns carries the secret.
    service = ZoteroSyncService(alice.username, None)
    bind_client(service, FakeZoteroClient([make_item("ITEM0006")]))
    service.sync_all()
    status = service.get_status()
    assert status, "the sync recorded state to assert against"
    assert ALICE_KEY not in repr(status)


def test_local_api_client_never_attaches_the_api_key_header():
    """Local (desktop) mode talks plaintext HTTP on loopback — no key there.

    Paired with the cloud-mode control so the assertion cannot pass just
    because the header is never set at all.
    """
    local = ZoteroClient(
        api_key=ALICE_KEY, library_type="user", library_id="0", local=True
    )
    try:
        assert "Zotero-API-Key" not in local.session.headers
        assert ALICE_KEY not in str(dict(local.session.headers))
    finally:
        local.close()

    cloud = ZoteroClient(api_key=ALICE_KEY, library_type="user", library_id="1")
    try:
        assert cloud.session.headers["Zotero-API-Key"] == ALICE_KEY
    finally:
        cloud.close()


def test_request_errors_never_carry_the_api_key_or_the_request_url():
    """``_request`` reduces an underlying failure to its exception TYPE.

    This is the property the whole Zotero message boundary rests on. Because
    the raised text is author-written (only an exception class name here),
    ``client_safe_zotero_message`` can safely allowlist a fixed set of those
    strings for the browser, and the catch sites can log ``str(exc)`` --
    neither would be defensible if a transport failure's own message, which
    can carry the request URL and a bare 24-character Zotero key, survived
    into ``stats["error"]`` / ``ZoteroSyncState.last_error``. (A bare Zotero
    key matches none of the credential-shape patterns in
    ``sanitize_error_message``, so no downstream scrubber would catch it.)
    """

    class LeakyTransportError(Exception):
        def __str__(self):
            return (
                "failed GET https://api.zotero.org/users/1/items"
                f"?key={ALICE_KEY}"
            )

    client = ZoteroClient(
        api_key=ALICE_KEY, library_type="user", library_id="1"
    )
    client.session = MagicMock()
    client.session.get.side_effect = LeakyTransportError()

    with pytest.raises(ZoteroError) as excinfo:
        client._request("/items")

    message = str(excinfo.value)
    assert message == "Zotero request failed: LeakyTransportError"
    assert ALICE_KEY not in message
    assert "api.zotero.org" not in message


def test_attachment_redirect_is_followed_without_zotero_credentials(
    monkeypatch,
):
    """The storage redirect is fetched with a fresh, credential-free session.

    ``requests`` only strips ``Authorization`` across hosts, so forwarding
    the custom ``Zotero-API-Key`` header to the storage host would leak it.
    """
    client = ZoteroClient(
        api_key=ALICE_KEY, library_type="user", library_id="1"
    )
    redirect = MagicMock()
    redirect.status_code = 302
    redirect.headers = {"Location": "https://files.example.com/paper.pdf"}
    monkeypatch.setattr(client, "_request", lambda *a, **kw: redirect)

    captured = {}

    class RecordingSafeSession:
        def __init__(self, *args, **kwargs):
            self.headers = {}

        def __enter__(self):
            return self

        def __exit__(self, *exc_info):
            return False

        def get(self, url, timeout=None):
            captured["url"] = url
            captured["headers"] = dict(self.headers)
            response = MagicMock()
            response.status_code = 200
            response.content = b"%PDF-1.7 body"
            return response

    monkeypatch.setattr(zotero_client_mod, "SafeSession", RecordingSafeSession)

    assert client.download_attachment("ATT1") == b"%PDF-1.7 body"
    assert captured["url"] == "https://files.example.com/paper.pdf"
    assert "Zotero-API-Key" not in captured["headers"]
    assert ALICE_KEY not in str(captured["headers"])


def test_sync_logs_never_contain_the_api_key(user_dbs):
    """A sync that logs its per-item and per-collection failures loudly must
    still never render the key."""
    ids = user_dbs.seed(
        "log_user", **{"zotero.api_key": ALICE_KEY, "zotero.library_id": "9"}
    )
    service = ZoteroSyncService(ids.username, None)
    bind_client(
        service,
        FakeZoteroClient(
            [make_item("ITEM0007"), make_item("ITEM0008")],
            fail_on={"ITEM0008"},
        ),
    )

    records = []
    logger.enable("local_deep_research")
    sink_id = logger.add(
        lambda message: records.append(str(message)), level="DEBUG"
    )
    try:
        result = service.sync_all()
    finally:
        logger.remove(sink_id)
        logger.disable("local_deep_research")

    text = "".join(records)
    assert result["errors"] == 1
    assert "Zotero: failed to ingest item ITEM0008" in text, (
        "the sink really captured this module's logging"
    )
    assert ALICE_KEY not in text


# ---------------------------------------------------------------------------
# 3. Ownership: per-user database, per-user scheduled job
# ---------------------------------------------------------------------------


def test_sync_writes_only_into_the_invoking_users_database(user_dbs):
    """Alice's sync must not reach Bob's rows even on a hash collision.

    Document dedup is global by ``document_hash``. Bob is seeded with a
    document whose bytes are IDENTICAL to the one Alice is about to import,
    plus an anchored note on it — the exact row that a cross-database write
    would corrupt.
    """
    alice = user_dbs.seed(
        "own_alice", **{"zotero.api_key": ALICE_KEY, "zotero.library_id": "1"}
    )
    bob = user_dbs.seed(
        "own_bob", **{"zotero.api_key": BOB_KEY, "zotero.library_id": "2"}
    )

    shared_text = "Body of ITEM0009."
    bob_session = user_dbs.session(bob.username)
    bob_doc_id = str(uuid.uuid4())
    bob_session.add(
        Document(
            id=bob_doc_id,
            document_hash=hashlib.sha256(shared_text.encode()).hexdigest(),
            filename="bob.pdf",
            file_size=len(shared_text),
            file_type="pdf",
            source_type_id=bob.source_type_id,
            title="Bob's copy",
            text_content=shared_text,
        )
    )
    bob_session.commit()
    user_dbs.add_note_with_anchor(bob, bob_doc_id, "Body of ITEM0009")

    service = ZoteroSyncService(alice.username, None)
    bind_client(service, FakeZoteroClient([make_item("ITEM0009")]))
    result = service.sync_all()
    assert result["imported"] == 1

    alice_session = user_dbs.session(alice.username)
    alice_session.expire_all()
    assert alice_session.query(Document).count() == 1
    assert alice_session.query(ZoteroItemMap).count() == 1
    assert alice_session.query(ZoteroSyncState).count() == 1

    bob_session.expire_all()
    assert bob_session.query(ZoteroItemMap).count() == 0
    assert bob_session.query(ZoteroSyncState).count() == 0
    assert (
        bob_session.query(Document)
        .filter_by(source_type_id=bob.source_type_id)
        .count()
        == 1
    ), "Alice's import must not land as a second Zotero document for Bob"
    bob_doc = bob_session.query(Document).filter_by(id=bob_doc_id).one()
    assert bob_doc.text_content == shared_text
    assert bob_doc.source_type_id == bob.source_type_id
    assert (
        bob_session.query(NoteReference)
        .filter_by(target_document_id=bob_doc_id)
        .count()
        == 1
    )


class RecordingScheduler:
    """Minimal APScheduler stand-in that keeps the wrapped job callables."""

    def __init__(self):
        self.jobs = {}

    def add_job(self, func=None, *args, **kwargs):
        callable_ = func if func is not None else args[0]
        self.jobs[kwargs["id"]] = callable_

    def remove_job(self, job_id):
        if job_id not in self.jobs:
            raise JobLookupError(job_id)
        del self.jobs[job_id]


def _bare_scheduler(username):
    """A BackgroundJobScheduler with only the attributes the job-scheduling
    methods touch — built without ``__new__``'s process-wide singleton."""
    scheduler = object.__new__(BackgroundJobScheduler)
    scheduler.scheduler = RecordingScheduler()
    scheduler.user_sessions = {username: {"scheduled_jobs": set()}}
    scheduler._credential_store = types.SimpleNamespace(
        retrieve=lambda _username: "password"
    )
    return scheduler


def test_scheduled_zotero_job_carries_the_user_context(user_dbs, monkeypatch):
    """User-bound scheduler jobs explicitly propagate their user context.

    ``_wrap_job`` pushes the ``request_user`` contextvar only when a username
    is passed; APScheduler worker threads inherit nothing otherwise. Zotero
    sync and the library reconciler now pass it. The manually constructed news
    job below deliberately models the remaining call site that does not.
    """
    ids = user_dbs.seed(
        "sched_user",
        **{
            "zotero.api_key": ALICE_KEY,
            "zotero.library_id": "1",
            "zotero.auto_sync_enabled": True,
            "zotero.sync_interval_minutes": 60,
        },
    )
    scheduler = _bare_scheduler(ids.username)
    observed = {}

    monkeypatch.setattr(
        scheduler,
        "_sync_user_zotero",
        lambda username: observed.__setitem__("zotero", get_current_username()),
        raising=False,
    )
    monkeypatch.setattr(
        scheduler,
        "_reconcile_unindexed_documents",
        lambda username: observed.__setitem__(
            "reconciler", get_current_username()
        ),
        raising=False,
    )
    monkeypatch.setattr(
        scheduler,
        "_check_user_overdue_subscriptions",
        lambda username: observed.__setitem__(
            "overdue", get_current_username()
        ),
        raising=False,
    )

    scheduler._schedule_zotero_sync(ids.username)
    scheduler._schedule_reconciler(
        ids.username,
        DocumentSchedulerSettings(
            sweep_library_collections=True, interval_seconds=60
        ),
        scheduler.user_sessions[ids.username],
    )
    # Same shape as web/routers/news_flask_api.py's check_subscriptions_now.
    scheduler.scheduler.add_job(
        func=scheduler._wrap_job(scheduler._check_user_overdue_subscriptions),
        args=[ids.username],
        id=f"manual_check_{ids.username}",
    )

    assert set(scheduler.scheduler.jobs) == {
        f"{ids.username}_zotero_sync",
        f"{ids.username}_library_sweep",
        f"manual_check_{ids.username}",
    }
    for job_id in list(scheduler.scheduler.jobs):
        scheduler.scheduler.jobs[job_id](ids.username)

    assert observed["zotero"] == ids.username
    assert observed["reconciler"] == ids.username
    assert observed["overdue"] is None


# ---------------------------------------------------------------------------
# 4. Failure partway leaves no partial or duplicate rows
# ---------------------------------------------------------------------------


def test_item_failure_leaves_no_partial_rows_and_is_retried_cleanly(user_dbs):
    """One failing item is rolled back on its own; the batch still commits."""
    ids = user_dbs.seed(
        "atomic_user",
        **{"zotero.api_key": ALICE_KEY, "zotero.library_id": "1"},
    )
    items = [make_item(f"ITEM001{n}") for n in (1, 2, 3)]
    service = ZoteroSyncService(ids.username, None)
    bind_client(service, FakeZoteroClient(items, fail_on={"ITEM0012"}))

    first = service.sync_all()
    session = user_dbs.session(ids.username)
    session.expire_all()

    assert (first["imported"], first["errors"]) == (2, 1)
    assert first["success"] is True
    mapped = {row.zotero_item_key for row in session.query(ZoteroItemMap)}
    assert mapped == {"ITEM0011", "ITEM0013"}, (
        "the failed item recorded no version, so it retries next sync"
    )
    assert session.query(Document).count() == 2
    state = session.query(ZoteroSyncState).one()
    assert state.last_status == "completed"
    assert "1 item(s) failed" in state.last_error

    # Retry: the previously failing item imports, nothing duplicates.
    bind_client(service, FakeZoteroClient(items))
    second = service.sync_all()
    session.expire_all()

    assert (second["imported"], second["errors"]) == (1, 0)
    assert session.query(Document).count() == 3
    assert session.query(ZoteroItemMap).count() == 3
    assert len({row.document_id for row in session.query(ZoteroItemMap)}) == 3
    assert session.query(ZoteroSyncState).one().last_error is None


def test_transient_error_aborts_batch_without_advancing_high_water_mark(
    user_dbs,
):
    """A rate-limit mid-batch fails the collection and keeps the cursor back.

    Items already committed keep their rows; the untouched remainder records
    nothing, and ``last_version`` stays at 0 so the next run reprocesses.
    """
    ids = user_dbs.seed(
        "transient_user",
        **{"zotero.api_key": ALICE_KEY, "zotero.library_id": "1"},
    )
    items = [make_item(f"ITEM002{n}") for n in (1, 2, 3)]
    service = ZoteroSyncService(ids.username, None)
    bind_client(
        service,
        FakeZoteroClient(items, transient_on={"ITEM0022"}, library_version=77),
    )

    result = service.sync_all()
    session = user_dbs.session(ids.username)
    session.expire_all()

    assert result["success"] is False
    assert [c["status"] for c in result["collections"]] == ["failed"]
    assert ALICE_KEY not in repr(result)
    mapped = {row.zotero_item_key for row in session.query(ZoteroItemMap)}
    assert mapped == {"ITEM0021"}
    assert session.query(Document).count() == 1
    state = session.query(ZoteroSyncState).one()
    assert state.last_status == "failed"
    assert state.last_version == 0, (
        "the high-water mark must not advance past an aborted batch"
    )


# ---------------------------------------------------------------------------
# 5. Rate limiting / backoff — bounded, and driven by an injected clock
# ---------------------------------------------------------------------------


class FakeClock:
    """Records requested sleeps instead of blocking on the wall clock."""

    def __init__(self):
        self.slept = []

    def sleep(self, seconds):
        self.slept.append(seconds)


@pytest.fixture
def fake_clock(monkeypatch):
    clock = FakeClock()
    monkeypatch.setattr(
        zotero_client_mod, "time", types.SimpleNamespace(sleep=clock.sleep)
    )
    return clock


def _client_answering(status, headers):
    client = ZoteroClient(
        api_key=ALICE_KEY, library_type="user", library_id="1"
    )
    response = MagicMock()
    response.status_code = status
    response.headers = headers
    client.session = MagicMock()
    client.session.get.return_value = response
    return client, response


@pytest.mark.parametrize(
    ("headers", "status", "expected"),
    [
        # A hostile Backoff header is clamped to the cap, not honoured.
        ({"Backoff": "100000"}, 429, [60.0, 60.0, 60.0]),
        ({"Retry-After": "100000"}, 503, [60.0, 60.0, 60.0]),
        # Unparseable / absent headers fall back to linear 2*attempt.
        ({"Backoff": "not-a-number"}, 429, [2.0, 4.0, 6.0]),
        ({}, 500, [2.0, 4.0, 6.0]),
        # A negative header must never become a negative sleep.
        ({"Retry-After": "-30"}, 429, []),
    ],
)
def test_backoff_is_bounded_by_retry_count_and_delay_cap(
    fake_clock, headers, status, expected
):
    """Retries stop at ``_MAX_RETRIES`` and no single sleep exceeds the cap."""
    client, _response = _client_answering(status, headers)

    with pytest.raises(ZoteroTransientError):
        client._request("/items")

    assert client.session.get.call_count == _MAX_RETRIES
    assert fake_clock.slept == expected
    assert len(fake_clock.slept) <= _MAX_RETRIES - 1
    assert all(0 <= delay <= _MAX_BACKOFF_SECONDS for delay in fake_clock.slept)
    assert sum(fake_clock.slept) <= _MAX_BACKOFF_SECONDS * (_MAX_RETRIES - 1)


def test_backoff_on_a_successful_response_sleeps_once_and_returns(fake_clock):
    """A ``Backoff`` header on a 200 throttles once — it does not re-request."""
    client, response = _client_answering(200, {"Backoff": "3"})

    assert client._request("/items") is response
    assert client.session.get.call_count == 1
    assert fake_clock.slept == [3.0]


# ---------------------------------------------------------------------------
# 6. Deletion / tombstones
# ---------------------------------------------------------------------------


def _seed_mapped_document(user_dbs, ids, tag, embedding_id):
    session = user_dbs.session(ids.username)
    doc_id = str(uuid.uuid4())
    session.add(
        Document(
            id=doc_id,
            document_hash=hashlib.sha256(tag.encode()).hexdigest(),
            filename=f"{tag}.pdf",
            file_size=8,
            file_type="pdf",
            source_type_id=ids.source_type_id,
            title=tag,
            text_content=f"{tag} body text",
        )
    )
    session.commit()
    session.add_all(
        [
            DocumentCollection(
                document_id=doc_id,
                collection_id=ids.zotero_collection_id,
                indexed=False,
            ),
            DocumentCollection(
                document_id=doc_id,
                collection_id=ids.default_collection_id,
                indexed=False,
            ),
            DocumentChunk(
                source_type="document",
                source_id=doc_id,
                collection_name=f"collection_{ids.zotero_collection_id}",
                chunk_index=0,
                chunk_text=f"{tag} body text",
                chunk_hash=f"chunk-{tag}",
                start_char=0,
                end_char=8,
                word_count=3,
                embedding_id=embedding_id,
                embedding_model="test-model",
                embedding_model_type="test",
                embedding_dimension=4,
            ),
        ]
    )
    mapping = ZoteroItemMap(
        ldr_collection_id=ids.zotero_collection_id,
        zotero_item_key=f"KEY_{tag}",
        zotero_version=1,
        document_id=doc_id,
    )
    session.add(mapping)
    session.commit()
    return doc_id, mapping


def test_upstream_deletion_leaves_no_orphan_rows_and_spares_siblings(
    user_dbs,
):
    """Removing an item takes its document, chunks and anchors with it.

    ``delete_document_completely`` deletes the blob and collection links
    explicitly; ``note_references`` go via the ``ondelete="CASCADE"`` FK,
    which only fires because SQLite FK enforcement is on (production sets
    ``PRAGMA foreign_keys = ON``; this harness mirrors it). The sibling item
    — its document, mapping, chunk and anchored note — must be untouched.
    """
    ids = user_dbs.seed("delete_user")
    session = user_dbs.session(ids.username)
    gone_id, gone_mapping = _seed_mapped_document(
        user_dbs, ids, "gone", embedding_id=101
    )
    sibling_id, _sibling_mapping = _seed_mapped_document(
        user_dbs, ids, "sibling", embedding_id=202
    )
    user_dbs.add_note_with_anchor(ids, gone_id, "gone body")
    user_dbs.add_note_with_anchor(ids, sibling_id, "sibling body")

    service = ZoteroSyncService(ids.username, None)
    released = service._remove_item(
        session,
        gone_mapping,
        ids.source_type_id,
        ids.default_collection_id,
        {},
    )
    session.commit()
    session.expire_all()

    assert released == gone_id
    assert session.query(Document).filter_by(id=gone_id).first() is None
    assert (
        session.query(DocumentCollection).filter_by(document_id=gone_id).count()
        == 0
    )
    assert (
        session.query(DocumentChunk).filter_by(source_id=gone_id).count() == 0
    )
    assert (
        session.query(NoteReference)
        .filter_by(target_document_id=gone_id)
        .count()
        == 0
    ), "no note_references row may dangle at a deleted document"

    sibling = session.query(Document).filter_by(id=sibling_id).one()
    assert sibling.text_content == "sibling body text"
    assert session.query(ZoteroItemMap).one().zotero_item_key == "KEY_sibling"
    assert (
        session.query(DocumentChunk).filter_by(source_id=sibling_id).count()
        == 1
    )
    assert (
        session.query(NoteReference)
        .filter_by(target_document_id=sibling_id)
        .count()
        == 1
    )


def test_empty_remote_version_map_does_not_mass_delete(user_dbs):
    """A truncated/empty Zotero response must not wipe a mapped collection.

    The genuine single-item deletion that follows proves the guard is a
    guard and not simply a broken removal path.
    """
    ids = user_dbs.seed(
        "tombstone_user",
        **{"zotero.api_key": ALICE_KEY, "zotero.library_id": "1"},
    )
    items = [make_item("ITEM0031"), make_item("ITEM0032")]
    service = ZoteroSyncService(ids.username, None)
    bind_client(service, FakeZoteroClient(items))
    assert service.sync_all()["imported"] == 2

    session = user_dbs.session(ids.username)
    bind_client(service, FakeZoteroClient([], library_version=100))
    empty_run = service.sync_all()
    session.expire_all()

    assert empty_run["removed"] == 0
    assert session.query(Document).count() == 2
    assert session.query(ZoteroItemMap).count() == 2

    bind_client(service, FakeZoteroClient([items[0]], library_version=101))
    real_run = service.sync_all()
    session.expire_all()

    assert real_run["removed"] == 1
    assert session.query(Document).count() == 1
    assert [row.zotero_item_key for row in session.query(ZoteroItemMap)] == [
        "ITEM0031"
    ]