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.

115 KB
Raw Blame
"""Unit tests for the Zotero sync service.

The pure metadata helpers are tested directly. Document ingestion is tested
against an in-memory SQLite database by calling ``_ingest_item`` with a fake
client and a real ORM session — no encrypted DB / network required.
"""

import hashlib
import uuid
from contextlib import contextmanager
from datetime import date, datetime, UTC
from unittest.mock import MagicMock

import pytest
from loguru import logger as loguru_logger
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentCollection,
    DocumentStatus,
    SourceType,
)
from local_deep_research.database.models.zotero import (
    ZoteroItemMap,
    ZoteroSyncState,
)
from local_deep_research.research_library.zotero.client import (
    ZoteroAttachment,
    ZoteroItem,
)
from local_deep_research.research_library.zotero.sync_service import (
    ZoteroConfig,
    ZoteroSyncService,
)


# ---------------------------------------------------------------------------
# Pure helpers
# ---------------------------------------------------------------------------


def test_config_targets_and_configured():
    cfg = ZoteroConfig(enabled=True, api_key="k", library_id="1")
    assert cfg.is_configured is True
    assert cfg.targets == [""]  # whole library when no keys
    cfg2 = ZoteroConfig(
        enabled=True, api_key="k", library_id="1", collection_keys=["A", "B"]
    )
    assert cfg2.targets == ["A", "B"]
    assert (
        ZoteroConfig(enabled=False, api_key="k", library_id="1").is_configured
        is False
    )


def test_parse_date():
    p = ZoteroSyncService._parse_date
    assert p("2020-05-01") == date(2020, 5, 1)
    assert p("2020-05") == date(2020, 5, 1)
    assert p("2019") == date(2019, 1, 1)
    assert p("May 2018") == date(2018, 1, 1)
    assert p("") is None
    assert p(None) is None
    assert p("no-year-here") is None


def test_authors():
    item = ZoteroItem(
        key="K",
        version=1,
        item_type="journalArticle",
        data={
            "creators": [
                {"firstName": "Ada", "lastName": "Lovelace"},
                {"name": "OpenAI"},
                {"lastName": "Onlylast"},
            ]
        },
    )
    assert ZoteroSyncService._authors(item) == [
        "Ada Lovelace",
        "OpenAI",
        "Onlylast",
    ]


def test_source_url_prefers_url_then_doi():
    svc = ZoteroSyncService
    with_url = ZoteroItem("K", 1, "x", {"url": "http://e.com", "DOI": "10.1/x"})
    assert svc._source_url(with_url) == "http://e.com"
    with_doi = ZoteroItem("K", 1, "x", {"DOI": "10.1/x"})
    assert svc._source_url(with_doi) == "https://doi.org/10.1/x"
    assert svc._source_url(ZoteroItem("K", 1, "x", {})) is None


def test_filename_for_uses_title_then_key():
    item = ZoteroItem("ABCD1234", 1, "x", {"title": "A Nice Title"})
    assert ZoteroSyncService._filename_for(item, "pdf").endswith(".pdf")
    blank = ZoteroItem("ABCD1234", 1, "x", {})
    assert ZoteroSyncService._filename_for(blank, "text") == "ABCD1234.text"


# ---------------------------------------------------------------------------
# Ingestion against an in-memory database
# ---------------------------------------------------------------------------


@pytest.fixture
def db_session():
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    yield session
    session.close()
    engine.dispose()


@pytest.fixture
def seeded(db_session):
    source_type_id = str(uuid.uuid4())
    collection_id = str(uuid.uuid4())
    db_session.add(
        SourceType(
            id=source_type_id,
            name="zotero",
            display_name="Zotero",
        )
    )
    db_session.add(
        Collection(
            id=collection_id,
            name="Zotero: Test",
            collection_type="zotero",
            is_default=False,
        )
    )
    db_session.commit()
    return source_type_id, collection_id


def _pdf_item(key="ITEM0001", title="A Paper"):
    return ZoteroItem(
        key=key,
        version=3,
        item_type="journalArticle",
        data={"title": title, "DOI": "10.1234/x", "date": "2021"},
    )


def _fake_client(pdf_bytes=None, fulltext=None):
    client = MagicMock()
    # Default: no Zotero-side extracted full text (so tests exercise the
    # download path unless they opt in).
    client.get_attachment_fulltext.return_value = fulltext
    if pdf_bytes is not None or fulltext is not None:
        att = ZoteroAttachment(
            key="ATT1",
            version=2,
            content_type="application/pdf",
            link_mode="imported_file",
            filename="paper.pdf",
            title="Full Text",
        )
        client.get_children.return_value = [att]
        client.download_attachment.return_value = pdf_bytes
    else:
        client.get_children.return_value = []
    return client


def test_ingest_pdf_item_creates_document(db_session, seeded):
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=b"%PDF-1.7 not-a-real-pdf")

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert action == "imported"
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert doc.file_type == "pdf"
    assert doc.title == "A Paper"
    assert doc.doi == "10.1234/x"
    assert doc.published_date == date(2021, 1, 1)
    link = (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id, collection_id=collection_id)
        .one()
    )
    assert link is not None


def test_resync_pdf_to_text_removes_stale_blob(db_session, seeded):
    """Regression: an item imported with a database-stored PDF that later has
    no usable PDF must have its orphaned DocumentBlob deleted on re-sync.

    Otherwise the doc is marked text-only (storage_mode="none") while a stale
    encrypted PDF blob lingers, and PDFStorageManager.load_pdf() (which keys
    off document_id and ignores file_type) would keep serving it.
    """
    from local_deep_research.database.models.library import DocumentBlob

    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    item = _pdf_item(key="ITEMBLOB1")
    cfg = ZoteroConfig(
        pdf_storage_mode="database", import_items_without_pdf=True
    )

    # First sync: a valid PDF in database mode -> document stored as PDF.
    doc_id, action, _ = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-1.7 body"),
        cfg,
        item,
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()
    assert action == "imported"
    assert (
        db_session.query(Document).filter_by(id=doc_id).one().storage_mode
        == "database"
    )

    # Simulate the blob the real PDFStorageManager would have written (the
    # test injects a MagicMock storage) and the item mapping the sync loop
    # records, so the re-sync reuses this document.
    db_session.add(
        DocumentBlob(document_id=doc_id, pdf_binary=b"%PDF-1.7 body")
    )
    db_session.add(
        ZoteroItemMap(
            ldr_collection_id=collection_id,
            zotero_item_key=item.key,
            zotero_version=3,
            document_id=doc_id,
        )
    )
    db_session.commit()
    assert (
        db_session.query(DocumentBlob).filter_by(document_id=doc_id).count()
        == 1
    )

    # Re-sync: same item, PDF now gone -> reuse doc, downgrade to text-only.
    doc_id2, action2, _ = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=None),
        cfg,
        item,
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert doc_id2 == doc_id
    assert action2 == "updated"
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert doc.file_type == "text"
    assert doc.storage_mode == "none"
    # The stale encrypted PDF blob must be gone.
    assert (
        db_session.query(DocumentBlob).filter_by(document_id=doc_id).count()
        == 0
    )


def test_ingest_metadata_only_when_enabled(db_session, seeded):
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=None)

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(title="No PDF Here"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert action == "imported"
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert doc.file_type == "text"
    assert "No PDF Here" in (doc.text_content or "")


def test_ingest_metadata_only_skipped_when_disabled(db_session, seeded):
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=False)
    client = _fake_client(pdf_bytes=None)

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    assert doc_id is None
    assert action == "skipped"
    assert db_session.query(Document).count() == 0


def test_ingest_dedup_identical_content(db_session, seeded):
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=b"%PDF-same-bytes")

    doc_id1, _, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="A"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()
    doc_id2, action2, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="A"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert doc_id1 == doc_id2
    assert action2 == "updated"
    assert db_session.query(Document).count() == 1


def test_upsert_and_remove_map(db_session, seeded):
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=b"%PDF-x")
    item = _pdf_item(key="ITEMX")

    doc_id, _, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        item,
        collection_id,
        source_type_id,
        MagicMock(),
    )
    svc._upsert_map(db_session, collection_id, item, doc_id)
    db_session.commit()

    row = (
        db_session.query(ZoteroItemMap)
        .filter_by(ldr_collection_id=collection_id, zotero_item_key="ITEMX")
        .one()
    )
    assert row.document_id == doc_id
    assert row.zotero_version == 3
    assert row.has_pdf is True

    # Removal deletes both the map row and the document.
    svc._remove_item(db_session, row, source_type_id)
    db_session.commit()
    assert (
        db_session.query(ZoteroItemMap)
        .filter_by(ldr_collection_id=collection_id, zotero_item_key="ITEMX")
        .first()
        is None
    )
    assert db_session.query(Document).filter_by(id=doc_id).first() is None


def test_ingest_does_not_overwrite_foreign_document(db_session, seeded):
    """A byte-identical doc from another source must not be mutated/owned."""
    source_type_id, collection_id = seeded
    foreign_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(
            id=foreign_type_id,
            name="user_upload",
            display_name="User Upload",
        )
    )
    shared = b"%PDF-1.7 SHARED-BYTES"
    foreign = Document(
        id=str(uuid.uuid4()),
        source_type_id=foreign_type_id,
        document_hash=hashlib.sha256(shared).hexdigest(),
        file_size=len(shared),
        file_type="pdf",
        title="User Title",
        tags=["mine"],
        status=DocumentStatus.COMPLETED,
        processed_at=datetime.now(UTC),
    )
    db_session.add(foreign)
    db_session.commit()

    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=shared)
    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(title="Zotero Title"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert doc_id == foreign.id
    assert action == "updated"
    db_session.refresh(foreign)
    assert foreign.title == "User Title"  # NOT overwritten
    assert foreign.tags == ["mine"]
    # Linked into the Zotero collection, but no duplicate created.
    assert (
        db_session.query(DocumentCollection)
        .filter_by(document_id=foreign.id, collection_id=collection_id)
        .first()
        is not None
    )
    assert db_session.query(Document).count() == 1


def test_remove_item_keeps_document_shared_with_another_collection(
    db_session, seeded
):
    """Removing an item must not destroy a document referenced elsewhere."""
    from local_deep_research.research_library.utils import (
        ensure_in_collection,
    )

    source_type_id, collection_id = seeded
    coll2 = str(uuid.uuid4())
    db_session.add(
        Collection(
            id=coll2,
            name="Zotero: Other",
            collection_type="zotero",
            is_default=False,
        )
    )
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type_id,
        document_hash="shared-hash",
        file_size=1,
        file_type="pdf",
        status=DocumentStatus.COMPLETED,
        processed_at=datetime.now(UTC),
    )
    db_session.add(doc)
    db_session.flush()
    # Capture the id as a plain string: after the full delete below, the ORM
    # instance is stale and touching doc.id raises ObjectDeletedError.
    doc_id = doc.id
    ensure_in_collection(db_session, doc_id, collection_id)
    ensure_in_collection(db_session, doc_id, coll2)
    row1 = ZoteroItemMap(
        ldr_collection_id=collection_id,
        zotero_item_key="K",
        zotero_version=1,
        document_id=doc_id,
    )
    row2 = ZoteroItemMap(
        ldr_collection_id=coll2,
        zotero_item_key="K",
        zotero_version=1,
        document_id=doc_id,
    )
    db_session.add_all([row1, row2])
    db_session.commit()

    svc = ZoteroSyncService("user", None)

    # Remove from collection 1 — shared, so only the link + map row go.
    svc._remove_item(db_session, row1, source_type_id)
    db_session.commit()
    assert db_session.query(Document).filter_by(id=doc_id).first() is not None
    assert (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id, collection_id=collection_id)
        .first()
        is None
    )
    assert (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id, collection_id=coll2)
        .first()
        is not None
    )

    # Remove from the last collection — now unshared, so the doc is deleted.
    svc._remove_item(db_session, row2, source_type_id)
    db_session.commit()
    assert db_session.query(Document).filter_by(id=doc_id).first() is None


def test_sync_one_imports_then_reconciles(db_session, monkeypatch, tmp_path):
    """End-to-end _sync_one: import, then a second pass that removes one item
    and updates another, advancing the stored library version."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session  # shared, never closed/committed by the CM

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    client = MagicMock()
    client.get_children.return_value = []  # metadata-only ingest
    client.get_top_item_versions.side_effect = [
        ({"A": 1, "B": 1}, 10),
        ({"A": 2}, 20),  # B removed, A bumped
    ]
    client.get_items.side_effect = [
        [item("A", 1, "Paper A"), item("B", 1, "Paper B")],
        [item("A", 2, "Paper A")],
    ]

    stats1 = svc._sync_one(client, cfg, "")
    assert stats1["status"] == "completed"
    assert stats1["imported"] == 2
    assert db_session.query(Document).count() == 2
    coll = db_session.query(Collection).filter_by(name="Zotero Library").one()
    assert (
        db_session.query(ZoteroItemMap)
        .filter_by(ldr_collection_id=coll.id)
        .count()
        == 2
    )
    state = db_session.query(ZoteroSyncState).one()
    assert state.last_version == 10

    stats2 = svc._sync_one(client, cfg, "")
    assert stats2["removed"] == 1
    assert stats2["updated"] == 1
    assert stats2["imported"] == 0
    assert db_session.query(Document).count() == 1  # B gone
    a_row = db_session.query(ZoteroItemMap).filter_by(zotero_item_key="A").one()
    assert a_row.zotero_version == 2
    db_session.refresh(state)
    assert state.last_version == 20


def test_sync_one_cleans_removed_vectors_even_when_additions_fail(
    db_session, monkeypatch, tmp_path
):
    """Regression: a removal commits, then the additions phase fails (a Zotero
    5xx from get_items). The removed doc's FAISS vectors MUST still be purged
    (now in a finally) — otherwise they orphan into ghost search hits, and no
    future sync re-detects them because the map row is already deleted."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod
    from local_deep_research.research_library.zotero.client import (
        ZoteroTransientError,
    )

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    # Don't run the real auto-indexer on the (successful) first sync.
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    # A truthy password so the cleanup guard (`and self.password`) is reached.
    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cleanup = (
        MagicMock()
    )  # spy: collection isn't RAG-indexed, so only assert calls
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", cleanup)

    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    client = MagicMock()
    client.get_children.return_value = []
    client.get_top_item_versions.side_effect = [
        ({"A": 1, "B": 1}, 10),
        ({"A": 2}, 20),  # B removed, A bumped
    ]
    client.get_items.side_effect = [
        [item("A", 1, "Paper A"), item("B", 1, "Paper B")],
        ZoteroTransientError(
            "boom"
        ),  # additions fail AFTER B's removal commits
    ]

    assert svc._sync_one(client, cfg, "")["status"] == "completed"
    b_doc_id = (
        db_session.query(ZoteroItemMap)
        .filter_by(zotero_item_key="B")
        .one()
        .document_id
    )
    assert b_doc_id is not None
    cleanup.reset_mock()

    stats2 = svc._sync_one(client, cfg, "")
    assert stats2["status"] == "failed"
    assert stats2["removed"] == 1
    # Removal is durably committed even though the sync then failed.
    assert (
        db_session.query(ZoteroItemMap).filter_by(zotero_item_key="B").count()
        == 0
    )
    # THE FIX: the removed doc's vectors are cleaned on the failure path too.
    cleaned = [c.args[0] for c in cleanup.call_args_list]
    assert any(b_doc_id in ids for ids in cleaned), (
        "removed doc's FAISS vectors were not cleaned on the additions-failure "
        "path — they would orphan into ghost search hits"
    )


def test_sync_one_keeps_vectors_when_removal_commit_fails(
    db_session, monkeypatch, tmp_path
):
    """Guard against the INVERSE leak the finally could cause: if a removal's
    per-item commit fails (rolled back), the document still exists, so its
    FAISS vectors must NOT be purged — and the failure stays contained to
    that item (the sync itself completes and the removal retries next run)."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cleanup = MagicMock()
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", cleanup)

    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    client = MagicMock()
    client.get_children.return_value = []
    client.get_top_item_versions.side_effect = [
        ({"A": 1, "B": 1}, 10),
        ({"A": 1}, 20),  # B removed
    ]
    client.get_items.side_effect = [
        [item("A", 1, "Paper A"), item("B", 1, "Paper B")],
        [],  # second sync has nothing to (re)process
    ]

    assert svc._sync_one(client, cfg, "")["status"] == "completed"
    cleanup.reset_mock()

    # Arm the batch commit that runs right after a removal is staged to fail.
    orig_remove = svc._remove_item
    orig_commit = db_session.commit
    armed = {"v": False}

    def spy_remove(session, row, stid):
        rid = orig_remove(session, row, stid)
        armed["v"] = True  # the next commit is the removals batch commit
        return rid

    def guarded_commit():
        if armed["v"]:
            armed["v"] = False
            raise RuntimeError("removals commit boom")
        return orig_commit()

    monkeypatch.setattr(svc, "_remove_item", spy_remove)
    monkeypatch.setattr(db_session, "commit", guarded_commit)

    stats2 = svc._sync_one(client, cfg, "")
    # The failure is contained to B's removal: the sync completes, nothing
    # is reported removed, and B stays mapped so next sync retries it.
    assert stats2["status"] == "completed"
    assert stats2["removed"] == 0
    assert stats2["errors"] == 1
    assert stats2["skipped"] == 0
    assert (
        db_session.query(ZoteroItemMap).filter_by(zotero_item_key="B").first()
        is not None
    )
    # The removal rolled back, so the document still exists and the finally
    # must NOT purge its vectors — otherwise a rolled-back removal would
    # delete a still-valid document's vectors (the inverse leak).
    cleanup.assert_not_called()


def test_sync_one_aborts_batch_on_transient_ingest_error(
    db_session, monkeypatch, tmp_path
):
    """A rate-limit / 5xx during item ingest must ABORT the sync (so it retries
    next run) rather than skip the item and keep hammering the rest of the
    batch against the rate limit."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    from local_deep_research.research_library.zotero.client import (
        ZoteroTransientError,
    )

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    calls = {"n": 0}

    def boom(*_a, **_k):
        calls["n"] += 1
        raise ZoteroTransientError("429")

    monkeypatch.setattr(svc, "_ingest_item", boom)

    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key):
        return ZoteroItem(
            key=key, version=1, item_type="journalArticle", data={"title": key}
        )

    client = MagicMock()
    client.get_children.return_value = []
    client.get_top_item_versions.side_effect = [({"A": 1, "B": 1}, 10)]
    client.get_items.side_effect = [[item("A"), item("B")]]

    stats = svc._sync_one(client, cfg, "")
    assert stats["status"] == "failed"
    assert stats["imported"] == 0
    # Aborted after the FIRST item's transient error — B was never attempted.
    assert calls["n"] == 1


def test_sync_one_releases_orphaned_doc_on_dedup_remap(
    db_session, monkeypatch, tmp_path
):
    """When an edited item's new content dedups onto a DIFFERENT existing doc,
    the item's prior document is released (not left as a stale orphan) and its
    FAISS vectors are purged."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cleanup = MagicMock()
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", cleanup)

    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    client = MagicMock()
    client.get_children.return_value = []
    client.get_top_item_versions.side_effect = [
        ({"K": 1, "M": 1}, 10),
        ({"K": 2, "M": 1}, 20),  # K edited, M unchanged
    ]
    client.get_items.side_effect = [
        [item("K", 1, "Original K"), item("M", 1, "Shared Paper")],
        [item("K", 2, "Shared Paper")],  # K's new content == M's -> dedup
    ]

    assert svc._sync_one(client, cfg, "")["status"] == "completed"
    assert db_session.query(Document).count() == 2

    def doc_of(key):
        return (
            db_session.query(ZoteroItemMap)
            .filter_by(zotero_item_key=key)
            .one()
            .document_id
        )

    d1, d2 = doc_of("K"), doc_of("M")
    assert d1 != d2
    cleanup.reset_mock()

    assert svc._sync_one(client, cfg, "")["status"] == "completed"
    # K now maps to M's doc; the orphaned prior doc D1 is gone...
    assert doc_of("K") == d2
    assert db_session.query(Document).filter_by(id=d1).first() is None
    assert db_session.query(Document).count() == 1
    # ...and its stale FAISS vectors were purged.
    cleaned = [c.args[0] for c in cleanup.call_args_list]
    assert any(d1 in ids for ids in cleaned), (
        "orphaned prior document's vectors were not cleaned on dedup remap"
    )


def test_remove_item_keeps_doc_used_by_twin_in_same_collection(
    db_session, seeded
):
    """Two byte-identical items in the SAME collection share one document via
    dedup. Removing one must NOT drop the shared link/doc — the twin needs it."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client()  # metadata-only

    def meta_item(key):
        return ZoteroItem(
            key=key,
            version=1,
            item_type="journalArticle",
            data={"title": "Dup"},
        )

    for key in ("K", "J"):
        did, _, _ = svc._ingest_item(
            db_session,
            client,
            cfg,
            meta_item(key),
            collection_id,
            source_type_id,
            MagicMock(),
        )
        svc._upsert_map(db_session, collection_id, meta_item(key), did)
        db_session.commit()

    assert db_session.query(Document).count() == 1  # K & J deduped

    k_row = db_session.query(ZoteroItemMap).filter_by(zotero_item_key="K").one()
    j_doc = (
        db_session.query(ZoteroItemMap)
        .filter_by(zotero_item_key="J")
        .one()
        .document_id
    )
    # Returns None (nothing released), so the sync loop does NOT purge the
    # surviving twin's FAISS vectors.
    assert svc._remove_item(db_session, k_row, source_type_id) is None
    db_session.commit()

    # J's document + its collection link survive.
    assert db_session.query(Document).filter_by(id=j_doc).first() is not None
    assert (
        db_session.query(DocumentCollection)
        .filter_by(document_id=j_doc, collection_id=collection_id)
        .count()
        == 1
    )


def test_item_has_tags_matches_all_case_insensitive():
    item = ZoteroItem(
        key="K",
        version=1,
        item_type="journalArticle",
        data={"tags": [{"tag": "ML"}, {"tag": "Robotics"}]},
    )
    has = ZoteroSyncService._item_has_tags
    assert has(item, ["ml"]) is True
    assert has(item, ["ML", "robotics"]) is True  # all required, any case
    assert has(item, ["ml", "biology"]) is False  # missing one
    blank = ZoteroItem("K", 1, "x", {})
    assert has(blank, ["ml"]) is False


def test_sync_one_tag_filter_skips_nonmatching(
    db_session, monkeypatch, tmp_path
):
    """With import_tags set, items lacking the tag are skipped (not imported)
    and recorded so they aren't re-fetched."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
        import_tags=["keep"],
    )

    def item(key, tags):
        return ZoteroItem(
            key=key,
            version=1,
            item_type="journalArticle",
            data={"title": key, "tags": [{"tag": t} for t in tags]},
        )

    client = MagicMock()
    client.get_children.return_value = []
    client.get_attachment_fulltext.return_value = None
    client.get_top_item_versions.side_effect = [({"A": 1, "B": 1}, 10)]
    client.get_items.side_effect = [[item("A", ["keep"]), item("B", ["other"])]]

    stats = svc._sync_one(client, cfg, "")
    assert stats["imported"] == 1  # only A (tagged "keep")
    assert stats["skipped"] == 1  # B filtered out
    assert db_session.query(Document).count() == 1
    # B is recorded (version) so it won't be re-fetched next time.
    b_row = db_session.query(ZoteroItemMap).filter_by(zotero_item_key="B").one()
    assert b_row.document_id is None


def test_ingest_appends_annotations_when_enabled(db_session, seeded):
    """With import_annotations on, annotations + notes are appended to the
    document's searchable text."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(
        pdf_storage_mode="none",
        import_items_without_pdf=True,
        import_annotations=True,
    )
    client = _fake_client(pdf_bytes=b"%PDF-1.7 body")
    client.get_annotations.return_value = ["highlight one", "highlight two"]
    client.get_notes.return_value = ["a standalone note"]

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()
    assert action == "imported"
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert "highlight one" in doc.text_content
    assert "highlight two" in doc.text_content
    assert "a standalone note" in doc.text_content
    client.get_annotations.assert_called()  # fetched per attachment


def test_ingest_skips_annotations_when_disabled(db_session, seeded):
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=b"%PDF-1.7 body")

    svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()
    client.get_annotations.assert_not_called()
    client.get_notes.assert_not_called()


def test_ingest_uses_zotero_fulltext_in_text_mode(db_session, seeded):
    """In text-only mode, Zotero's extracted full text is used and the PDF is
    NOT downloaded."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(
        pdf_bytes=b"%PDF-should-not-be-used",
        fulltext="EXTRACTED BY ZOTERO",
    )

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert action == "imported"
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert doc.file_type == "pdf"
    assert doc.text_content == "EXTRACTED BY ZOTERO"
    client.download_attachment.assert_not_called()  # skipped the download


def test_ingest_downloads_when_no_zotero_fulltext(db_session, seeded):
    """Falls back to downloading when Zotero has no indexed text."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=b"%PDF-1.7 body", fulltext=None)

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()
    assert action == "imported"
    client.download_attachment.assert_called_once()


def test_ingest_rejects_non_pdf_body(db_session, seeded):
    """A /file response that isn't a PDF must not be stored as one."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=b"<html>403 Forbidden</html>")

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(title="HTML Trap"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()
    assert action == "imported"
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert doc.file_type == "text"  # fell back to metadata, NOT stored as pdf


def test_ingest_non_pdf_body_skipped_when_import_disabled(db_session, seeded):
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=False)
    client = _fake_client(pdf_bytes=b"<html>not a pdf</html>")

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    assert doc_id is None
    assert action == "skipped"
    assert db_session.query(Document).count() == 0


def test_remove_item_deletes_rag_chunks(db_session, seeded):
    """The unshared hard-delete path must also remove the doc's RAG chunks."""
    from local_deep_research.database.models.library import DocumentChunk
    from local_deep_research.research_library.utils import (
        ensure_in_collection,
    )

    source_type_id, collection_id = seeded
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type_id,
        document_hash="chunk-hash",
        file_size=1,
        file_type="pdf",
        status=DocumentStatus.COMPLETED,
        processed_at=datetime.now(UTC),
    )
    db_session.add(doc)
    db_session.flush()
    doc_id = doc.id
    ensure_in_collection(db_session, doc_id, collection_id)
    db_session.add(
        DocumentChunk(
            source_id=doc_id,
            source_type="document",
            collection_name=f"collection_{collection_id}",
            chunk_index=0,
            chunk_text="hello",
            chunk_hash="ch",
            start_char=0,
            end_char=5,
            word_count=1,
            embedding_id="emb-0",
            embedding_model="all-MiniLM-L6-v2",
            embedding_model_type="sentence_transformers",
        )
    )
    row = ZoteroItemMap(
        ldr_collection_id=collection_id,
        zotero_item_key="K",
        zotero_version=1,
        document_id=doc_id,
    )
    db_session.add(row)
    db_session.commit()
    assert db_session.query(DocumentChunk).count() == 1

    svc = ZoteroSyncService("user", None)
    svc._remove_item(db_session, row, source_type_id)
    db_session.commit()
    assert db_session.query(Document).filter_by(id=doc_id).first() is None
    assert db_session.query(DocumentChunk).count() == 0  # chunks cleaned up


def test_remove_item_keeps_foreign_owned_document(db_session, seeded):
    """A document Zotero only *adopted* via content-hash dedup (a user upload
    or research download owned by a different source type) must NOT be
    hard-deleted when its Zotero item disappears — even when nothing else
    references it. Zotero only owns the collection link + mapping row."""
    from local_deep_research.research_library.utils import (
        ensure_in_collection,
    )

    zotero_source_type_id, collection_id = seeded
    # A different (non-Zotero) source type owns the document.
    foreign_source_id = str(uuid.uuid4())
    db_session.add(
        SourceType(
            id=foreign_source_id,
            name="user_uploads",
            display_name="User Uploads",
        )
    )
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=foreign_source_id,  # NOT Zotero's
        document_hash="user-upload-hash",
        file_size=1,
        file_type="pdf",
        status=DocumentStatus.COMPLETED,
        processed_at=datetime.now(UTC),
    )
    db_session.add(doc)
    db_session.flush()
    doc_id = doc.id
    ensure_in_collection(db_session, doc_id, collection_id)
    row = ZoteroItemMap(
        ldr_collection_id=collection_id,
        zotero_item_key="K",
        zotero_version=1,
        document_id=doc_id,
    )
    db_session.add(row)
    db_session.commit()

    svc = ZoteroSyncService("user", None)
    returned = svc._remove_item(db_session, row, zotero_source_type_id)
    db_session.commit()

    # The user's document survives; only this collection's link + the map row go.
    assert returned == doc_id
    assert db_session.query(Document).filter_by(id=doc_id).first() is not None
    assert (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id, collection_id=collection_id)
        .first()
        is None
    )
    assert db_session.query(ZoteroItemMap).filter_by(id=row.id).first() is None


def test_ingest_update_purges_stale_chunks(db_session, seeded):
    """When a mapped item's content changes, _ingest_item must purge the doc's
    stale RAG chunks (and flag a re-index) so outdated text stops surfacing in
    search instead of accumulating alongside the new chunks."""
    from local_deep_research.database.models.library import DocumentChunk

    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = _fake_client(pdf_bytes=None)  # metadata-only

    # First ingest (fresh import).
    doc_id, action, reindexed = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="UPD1", title="Original Title"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()
    assert action == "imported"
    assert reindexed is False

    # Simulate the sync loop's mapping + an indexed chunk for this doc.
    db_session.add(
        ZoteroItemMap(
            ldr_collection_id=collection_id,
            zotero_item_key="UPD1",
            zotero_version=3,
            document_id=doc_id,
        )
    )
    db_session.add(
        DocumentChunk(
            source_id=doc_id,
            source_type="document",
            collection_name=f"collection_{collection_id}",
            chunk_index=0,
            chunk_text="original title stale text",
            chunk_hash="stale-chunk",
            start_char=0,
            end_char=25,
            word_count=4,
            embedding_id="emb-stale",
            embedding_model="all-MiniLM-L6-v2",
            embedding_model_type="sentence_transformers",
        )
    )
    db_session.commit()
    assert db_session.query(DocumentChunk).count() == 1

    # Re-ingest the SAME item key with changed content (new title -> new hash).
    doc_id2, action2, reindexed2 = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="UPD1", title="Completely Different Title"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert doc_id2 == doc_id  # same document, updated in place
    assert action2 == "updated"
    assert reindexed2 is True  # caller must clear the FAISS vectors
    # The stale chunk is gone (re-index will rebuild from current content).
    assert db_session.query(DocumentChunk).count() == 0
    # The collection link is flagged for re-index.
    link = (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id, collection_id=collection_id)
        .one()
    )
    assert link.indexed is False


def test_ingest_save_pdf_failure_downgrades_to_text_only(db_session, seeded):
    """If storing the PDF blob fails, the doc must be downgraded to text-only so
    ZoteroItemMap.has_pdf (keyed off file_type) doesn't advertise a PDF that
    load_pdf() can never return."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(
        pdf_storage_mode="database", import_items_without_pdf=True
    )
    client = _fake_client(pdf_bytes=b"%PDF-1.7 realish body")

    failing_storage = MagicMock()
    failing_storage.save_pdf.side_effect = Exception("disk full")

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="PDFFAIL1"),
        collection_id,
        source_type_id,
        failing_storage,
    )
    db_session.commit()

    assert action == "imported"
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    # Downgraded to a consistent text-only document (no phantom PDF).
    assert doc.file_type == "text"
    assert doc.storage_mode == "none"
    # Extracted text is preserved so the item is still searchable.
    assert doc.text_content


def test_sync_one_empty_remote_map_skips_removals(
    db_session, monkeypatch, tmp_path
):
    """Mass-deletion guard: if the remote version map comes back EMPTY while
    items are still mapped locally (a truncated/transient response), _sync_one
    must NOT delete anything."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key, ver):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": key},
        )

    client = MagicMock()
    client.get_children.return_value = []
    # First sync imports A + B; second sync returns an EMPTY map.
    client.get_top_item_versions.side_effect = [
        ({"A": 1, "B": 1}, 10),
        ({}, 20),
    ]
    client.get_items.side_effect = [
        [item("A", 1), item("B", 1)],
        [],
    ]

    svc._sync_one(client, cfg, "")
    assert db_session.query(Document).count() == 2

    stats2 = svc._sync_one(client, cfg, "")
    # Guard fired: nothing removed, both documents survive.
    assert stats2["removed"] == 0
    assert db_session.query(Document).count() == 2
    coll = db_session.query(Collection).filter_by(name="Zotero Library").one()
    assert (
        db_session.query(ZoteroItemMap)
        .filter_by(ldr_collection_id=coll.id)
        .count()
        == 2
    )


def test_sync_one_marks_state_failed_on_version_fetch_error(
    db_session, monkeypatch, tmp_path
):
    """A ZoteroError while fetching the version map must fail the collection
    sync AND persist last_status='failed' + last_error on the state row."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    from local_deep_research.research_library.zotero.client import ZoteroError

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cfg = ZoteroConfig(
        enabled=True, api_key="k", library_id="1", pdf_storage_mode="none"
    )

    client = MagicMock()
    client.get_top_item_versions.side_effect = ZoteroError("rate limited")

    stats = svc._sync_one(client, cfg, "")
    assert stats["status"] == "failed"
    assert stats["error"] == (
        "Zotero request failed. Check the integration settings."
    )
    assert "rate limited" not in stats["error"]
    state = db_session.query(ZoteroSyncState).one()
    assert state.last_status == "failed"
    assert state.last_error == stats["error"]


def _sync_one_with_version_fetch_error(
    db_session, monkeypatch, tmp_path, error
):
    """Drive ``_sync_one`` to its outer handler and return ``stats``.

    Shares the wiring of
    ``test_sync_one_marks_state_failed_on_version_fetch_error``; the only
    variable is which exception the version fetch raises, which is what
    selects between the two outer handlers.
    """
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cfg = ZoteroConfig(
        enabled=True,
        api_key="zotero-api-key-abcdefghijklmnop",
        library_id="1",
        pdf_storage_mode="none",
    )

    client = MagicMock()
    client.get_top_item_versions.side_effect = error
    return svc._sync_one(client, cfg, "")


def test_sync_one_bare_exception_does_not_return_the_exception_text(
    db_session, monkeypatch, tmp_path
):
    """The non-ZoteroError outer handler must return its fixed string.

    Catches the revert of ``sync_service.py``'s
    ``except Exception:`` arm back to
    ``stats["error"] = sanitize_error_for_client(str(exc))``. That scrubber
    removes credential SHAPES only, so the server path and the OS account
    name below would ship to the browser through ``get_sync_status`` -- and
    ``state.last_error`` persists them for every later page load.
    """
    leaky = OSError(
        "[Errno 13] Permission denied: "
        "/srv/ldr-data/encrypted_databases/victim_user.db"
    )

    stats = _sync_one_with_version_fetch_error(
        db_session, monkeypatch, tmp_path, leaky
    )

    assert stats["status"] == "failed"
    assert stats["error"] == "Zotero sync failed. See server logs for details."
    assert "/srv/ldr-data" not in stats["error"]
    assert "victim_user" not in stats["error"]
    assert "Permission denied" not in stats["error"]
    # Persisted state is a second client-visible copy of the same string.
    state = db_session.query(ZoteroSyncState).one()
    assert state.last_error == stats["error"]


def test_sync_one_forwards_curated_zotero_guidance(
    db_session, monkeypatch, tmp_path
):
    """The author-written HTTP 400 diagnostic must survive to the caller.

    Catches the revert that replaces every ``ZoteroError`` message with one
    generic string: this text is the only place the product tells a user that
    their library-ID setting holds a username instead of the numeric ID, which
    is the most common Zotero misconfiguration and unguessable from "Zotero
    request failed".

    It does NOT catch a revert to ``sanitize_error_for_client(str(exc))``:
    that scrubber returns a curated constant unchanged, so this assertion
    would still pass. ``test_sync_one_uncurated_zotero_message_falls_back``
    below is the leg that goes red on that revert.
    """
    from local_deep_research.research_library.zotero.client import (
        ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE,
        ZoteroError,
    )

    stats = _sync_one_with_version_fetch_error(
        db_session,
        monkeypatch,
        tmp_path,
        ZoteroError(ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE),
    )

    assert stats["status"] == "failed"
    assert stats["error"] == ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE
    assert "NUMERIC userID" in stats["error"]


def test_sync_one_uncurated_zotero_message_falls_back(
    db_session, monkeypatch, tmp_path
):
    """Non-vacuity control for the test above: the allowlist is a whitelist,
    not a passthrough. A ``ZoteroError`` whose text is not one of the module
    constants gets the generic message, so the previous test cannot pass by
    the router simply echoing ``str(exc)``.
    """
    from local_deep_research.research_library.zotero.client import ZoteroError

    stats = _sync_one_with_version_fetch_error(
        db_session,
        monkeypatch,
        tmp_path,
        ZoteroError("Zotero said: token sk-live-ABCDEFGHIJKLMNOPQRSTUV"),
    )

    assert stats["error"] == (
        "Zotero request failed. Check the integration settings."
    )
    assert "sk-live" not in stats["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/web/routers/test_zotero_router_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_sync_one_bare_exception_log_has_no_traceback_and_no_secrets(
    db_session, monkeypatch, tmp_path
):
    """Revert guard for the ``_sync_one`` bare-``Exception`` arm's LOG call.

    ``test_sync_one_bare_exception_does_not_return_the_exception_text``
    above only pins the client-visible ``stats["error"]`` string. This test
    pins the server-side log record instead: the handler must stay
    ``logger.warning(safe_msg)``, not ``logger.exception(...)``. Both ``cfg``
    (whose dataclass repr renders ``api_key``) and ``self.password`` (the
    SQLCipher passphrase) are live in this frame, so an attached traceback
    would render them under loguru's default ``diagnose=True`` even though
    the logged MESSAGE is already scrubbed. Revert this arm's
    ``logger.warning`` back to ``logger.exception`` (the site the security
    hardening changed) and this test goes red on both the traceback header
    and the leaked secrets.
    """
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    api_key = "zotero-sentinel-api-key-QWERTY123456"
    db_password = "sentinel-sqlcipher-passphrase-998877"  # gitleaks:allow

    svc = ZoteroSyncService("user", db_password)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cfg = ZoteroConfig(
        enabled=True, api_key=api_key, library_id="1", pdf_storage_mode="none"
    )

    client = MagicMock()
    client.get_top_item_versions.side_effect = RuntimeError(
        f"connection reset (key={api_key} pw={db_password})"
    )

    with _capture_loguru() as records:
        stats = svc._sync_one(client, cfg, "")

    text = "".join(records)
    assert stats["status"] == "failed"
    assert "Zotero: sync failed for collection ALL (RuntimeError):" in text
    assert "Traceback (most recent call last)" not in text
    assert api_key not in text
    assert db_password not in text


def test_sync_one_skipped_no_pdf_item_not_reprocessed(
    db_session, monkeypatch, tmp_path
):
    """Regression: a no-PDF item with import disabled is mapped (doc_id None)
    and must NOT be re-fetched on the next sync when its version is unchanged."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=False,  # no-PDF items are skipped
    )

    def item(key, ver):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": key},
        )

    client = MagicMock()
    client.get_children.return_value = []  # no PDF -> skipped
    client.get_top_item_versions.side_effect = [({"A": 1}, 10), ({"A": 1}, 10)]
    client.get_items.side_effect = [[item("A", 1)], []]

    stats1 = svc._sync_one(client, cfg, "")
    assert stats1["skipped"] == 1
    assert stats1["imported"] == 0
    row = db_session.query(ZoteroItemMap).filter_by(zotero_item_key="A").one()
    assert row.document_id is None

    # Second sync: version unchanged -> A must NOT be reprocessed.
    svc._sync_one(client, cfg, "")
    assert client.get_items.call_args_list[1].args[0] == []


def test_sync_all_skips_when_already_running(monkeypatch):
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    cfg = ZoteroConfig(enabled=True, api_key="k", library_id="1")
    monkeypatch.setattr(ZoteroSyncService, "get_config", lambda self: cfg)

    svc = ZoteroSyncService("user", None)
    lock = svc_mod._get_user_sync_lock("user")
    assert lock.acquire(blocking=False)
    try:
        assert ZoteroSyncService.is_user_syncing("user") is True
        result = svc.sync_all()
        assert result.get("already_running") is True
        assert result["imported"] == 0
    finally:
        lock.release()
    assert ZoteroSyncService.is_user_syncing("user") is False


def test_sync_one_omitted_key_not_refetched(db_session, monkeypatch, tmp_path):
    """A key present in the version map but NOT returned by get_items (e.g.
    trashed/permission-changed) must have its version recorded so it isn't
    re-requested on every subsequent sync."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cfg = ZoteroConfig(
        enabled=True, api_key="k", library_id="1", pdf_storage_mode="none"
    )

    client = MagicMock()
    client.get_children.return_value = []
    # "A" is in the version map both syncs, but get_items never returns it.
    client.get_top_item_versions.side_effect = [({"A": 1}, 10), ({"A": 1}, 10)]
    client.get_items.side_effect = [[], []]

    svc._sync_one(client, cfg, "")
    row = db_session.query(ZoteroItemMap).filter_by(zotero_item_key="A").one()
    assert row.zotero_version == 1
    assert row.document_id is None

    # Second sync: version unchanged + already recorded -> not re-requested.
    svc._sync_one(client, cfg, "")
    assert client.get_items.call_args_list[1].args[0] == []


def test_remove_item_returns_affected_doc_id(db_session, seeded):
    """_remove_item returns the document id so the caller can clear its
    FAISS vectors after the transaction."""
    source_type_id, collection_id = seeded
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type_id,
        document_hash="ret-hash",
        file_size=1,
        file_type="pdf",
        status=DocumentStatus.COMPLETED,
        processed_at=datetime.now(UTC),
    )
    db_session.add(doc)
    db_session.flush()
    doc_id = doc.id
    row = ZoteroItemMap(
        ldr_collection_id=collection_id,
        zotero_item_key="K",
        zotero_version=1,
        document_id=doc_id,
    )
    db_session.add(row)
    db_session.commit()

    svc = ZoteroSyncService("user", None)
    assert svc._remove_item(db_session, row, source_type_id) == doc_id


# ---------------------------------------------------------------------------
# In-place rewrite safety: adopted, shared, and cross-collection documents
# ---------------------------------------------------------------------------


def _seed_foreign_upload(db_session, content: bytes):
    """A user-uploaded document (foreign source type) with a stored PDF blob."""
    from local_deep_research.database.models.library import DocumentBlob

    foreign_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(
            id=foreign_type_id,
            name="user_upload",
            display_name="User Upload",
        )
    )
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=foreign_type_id,
        document_hash=hashlib.sha256(content).hexdigest(),
        file_size=len(content),
        file_type="pdf",
        title="User Title",
        tags=["mine"],
        text_content="the user's own extracted text",
        status=DocumentStatus.COMPLETED,
        processed_at=datetime.now(UTC),
    )
    db_session.add(doc)
    db_session.flush()
    db_session.add(DocumentBlob(document_id=doc.id, pdf_binary=content))
    db_session.commit()
    return doc


def test_ingest_change_never_rewrites_adopted_foreign_document(
    db_session, seeded
):
    """An item that dedup-adopted a user upload must FORK on a later content
    change — never rewrite the user's document or delete its PDF blob."""
    from local_deep_research.database.models.library import DocumentBlob

    source_type_id, collection_id = seeded
    original = b"%PDF-1.7 THE-USERS-BYTES"
    foreign = _seed_foreign_upload(db_session, original)
    foreign_id = foreign.id

    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)

    # Step 1: first contact — byte-identical, so the item adopts the upload.
    client = _fake_client(pdf_bytes=original)
    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="ADOPT1"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    assert doc_id == foreign_id
    svc._upsert_map(db_session, collection_id, _pdf_item(key="ADOPT1"), doc_id)
    db_session.commit()

    # Step 2: the PDF is replaced in Zotero — content now differs.
    client2 = _fake_client(pdf_bytes=b"%PDF-1.7 REPLACED-IN-ZOTERO")
    doc_id2, action2, reindexed2 = svc._ingest_item(
        db_session,
        client2,
        cfg,
        _pdf_item(key="ADOPT1", title="Zotero Title"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    # A NEW zotero-owned document was created; the upload is untouched.
    assert doc_id2 != foreign_id
    assert action2 == "imported"
    assert reindexed2 is False  # nothing was rewritten in place
    db_session.refresh(foreign)
    assert foreign.title == "User Title"
    assert foreign.tags == ["mine"]
    assert foreign.text_content == "the user's own extracted text"
    assert (
        db_session.query(DocumentBlob).filter_by(document_id=foreign_id).count()
        == 1
    )
    new_doc = db_session.query(Document).filter_by(id=doc_id2).one()
    assert new_doc.source_type_id == source_type_id


def test_ingest_change_forks_instead_of_rewriting_shared_twin_doc(
    db_session, seeded
):
    """Two byte-identical items share one document. Editing ONE of them must
    fork a new document — the twin keeps serving the original content."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    shared = b"%PDF-1.7 TWIN-BYTES"

    client = _fake_client(pdf_bytes=shared)
    doc_id, _, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="TWINA"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    svc._upsert_map(db_session, collection_id, _pdf_item(key="TWINA"), doc_id)
    doc_id_b, _, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="TWINB"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    assert doc_id_b == doc_id  # deduped onto the same doc
    svc._upsert_map(db_session, collection_id, _pdf_item(key="TWINB"), doc_id)
    db_session.commit()
    original_text = (
        db_session.query(Document).filter_by(id=doc_id).one().text_content
    )

    # Edit TWINA's content in Zotero.
    client2 = _fake_client(pdf_bytes=b"%PDF-1.7 TWIN-A-EDITED")
    doc_id2, action2, reindexed2 = svc._ingest_item(
        db_session,
        client2,
        cfg,
        _pdf_item(key="TWINA", title="Edited"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert doc_id2 != doc_id  # forked, not rewritten
    assert action2 == "imported"
    assert reindexed2 is False
    # The shared doc still carries the ORIGINAL content for TWINB.
    shared_doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert shared_doc.text_content == original_text
    twin_b_row = (
        db_session.query(ZoteroItemMap)
        .filter_by(ldr_collection_id=collection_id, zotero_item_key="TWINB")
        .one()
    )
    assert twin_b_row.document_id == doc_id
    # Releasing TWINA's claim on the old doc keeps it (twin still maps to it).
    twin_a_row = (
        db_session.query(ZoteroItemMap)
        .filter_by(ldr_collection_id=collection_id, zotero_item_key="TWINA")
        .one()
    )
    assert (
        svc._release_document(
            db_session, doc_id, collection_id, twin_a_row.id, source_type_id
        )
        is None
    )
    db_session.commit()
    assert db_session.query(Document).filter_by(id=doc_id).first() is not None


def test_ingest_change_forks_when_doc_linked_to_another_collection(
    db_session, seeded
):
    """A zotero-owned doc the user also linked into ANOTHER collection must not
    be rewritten in place — the other collection's chunks would go stale."""
    from local_deep_research.research_library.utils import (
        ensure_in_collection,
    )

    source_type_id, collection_id = seeded
    other_coll = str(uuid.uuid4())
    db_session.add(
        Collection(
            id=other_coll,
            name="My Manual Collection",
            collection_type="manual",
            is_default=False,
        )
    )
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)

    client = _fake_client(pdf_bytes=b"%PDF-1.7 LINKED-BYTES")
    doc_id, _, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="LINK1"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    svc._upsert_map(db_session, collection_id, _pdf_item(key="LINK1"), doc_id)
    ensure_in_collection(db_session, doc_id, other_coll)
    db_session.commit()
    original_text = (
        db_session.query(Document).filter_by(id=doc_id).one().text_content
    )

    client2 = _fake_client(pdf_bytes=b"%PDF-1.7 LINKED-BYTES-EDITED")
    doc_id2, action2, _ = svc._ingest_item(
        db_session,
        client2,
        cfg,
        _pdf_item(key="LINK1"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert doc_id2 != doc_id
    assert action2 == "imported"
    old_doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert old_doc.text_content == original_text  # other collection intact
    assert (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id, collection_id=other_coll)
        .first()
        is not None
    )


# ---------------------------------------------------------------------------
# Annotation/text refresh on unchanged content bytes
# ---------------------------------------------------------------------------


def test_ingest_refreshes_annotations_on_unchanged_pdf(db_session, seeded):
    """New annotations must reach the document even when the PDF bytes (and
    thus the content hash) are unchanged, flagging a re-index."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(
        pdf_storage_mode="none",
        import_items_without_pdf=True,
        import_annotations=True,
    )
    pdf = b"%PDF-1.7 ANNOTATED"

    client = _fake_client(pdf_bytes=pdf)
    client.get_annotations.return_value = ["first highlight"]
    client.get_notes.return_value = []
    doc_id, _, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="ANN1"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    svc._upsert_map(db_session, collection_id, _pdf_item(key="ANN1"), doc_id)
    db_session.commit()

    # Same PDF bytes, new annotation.
    client2 = _fake_client(pdf_bytes=pdf)
    client2.get_annotations.return_value = [
        "first highlight",
        "second highlight",
    ]
    client2.get_notes.return_value = []
    doc_id2, action2, reindexed2 = svc._ingest_item(
        db_session,
        client2,
        cfg,
        _pdf_item(key="ANN1"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert doc_id2 == doc_id
    assert action2 == "updated"
    assert reindexed2 is True  # caller must clear stale FAISS vectors
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert "second highlight" in doc.text_content
    link = (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id, collection_id=collection_id)
        .one()
    )
    assert link.indexed is False  # re-index forced


def test_ingest_skips_text_refresh_on_shared_doc(db_session, seeded):
    """Annotation refresh must NOT rewrite a document shared with a twin
    mapping — one item's annotations would overwrite what the other item
    surfaces."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(
        pdf_storage_mode="none",
        import_items_without_pdf=True,
        import_annotations=True,
    )
    pdf = b"%PDF-1.7 SHARED-ANNOTATED"

    client = _fake_client(pdf_bytes=pdf)
    client.get_annotations.return_value = []
    client.get_notes.return_value = []
    doc_id, _, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _pdf_item(key="SHRA"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    svc._upsert_map(db_session, collection_id, _pdf_item(key="SHRA"), doc_id)
    svc._upsert_map(db_session, collection_id, _pdf_item(key="SHRB"), doc_id)
    db_session.commit()
    original_text = (
        db_session.query(Document).filter_by(id=doc_id).one().text_content
    )

    client2 = _fake_client(pdf_bytes=pdf)
    client2.get_annotations.return_value = ["only A's highlight"]
    client2.get_notes.return_value = []
    doc_id2, action2, reindexed2 = svc._ingest_item(
        db_session,
        client2,
        cfg,
        _pdf_item(key="SHRA"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert doc_id2 == doc_id
    assert action2 == "updated"
    assert reindexed2 is False
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert doc.text_content == original_text  # untouched


# ---------------------------------------------------------------------------
# Release failures must propagate (no half-committed releases)
# ---------------------------------------------------------------------------


def test_release_document_propagates_cascade_failure(
    db_session, seeded, monkeypatch
):
    """A chunk-cascade failure during release must raise so the surrounding
    transaction rolls back — swallowing it would let the caller purge FAISS
    vectors for a document whose chunks still exist."""
    from local_deep_research.research_library.zotero import (
        sync_service as svc_mod,
    )

    source_type_id, collection_id = seeded
    doc = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type_id,
        document_hash="cascade-fail-hash",
        file_size=1,
        file_type="pdf",
        status=DocumentStatus.COMPLETED,
        processed_at=datetime.now(UTC),
    )
    db_session.add(doc)
    db_session.flush()
    row = ZoteroItemMap(
        ldr_collection_id=collection_id,
        zotero_item_key="CASC",
        zotero_version=1,
        document_id=doc.id,
    )
    db_session.add(row)
    db_session.commit()

    def boom(*_a, **_k):
        raise RuntimeError("cascade failed")

    monkeypatch.setattr(
        svc_mod.CascadeHelper, "delete_document_chunks", staticmethod(boom)
    )
    svc = ZoteroSyncService("user", None)
    with pytest.raises(RuntimeError, match="cascade failed"):
        svc._release_document(
            db_session, doc.id, collection_id, row.id, source_type_id
        )


def test_extraction_method_reflects_actual_text_source(db_session, seeded):
    """extraction_method must say where the text really came from: local PDF
    extraction, Zotero's fulltext index, or the metadata fallback."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)

    # No PDF at all -> metadata.
    doc_id, _, _ = svc._ingest_item(
        db_session,
        _fake_client(),
        cfg,
        _pdf_item(key="EXM1", title="Meta Only"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    assert (
        db_session.query(Document).filter_by(id=doc_id).one().extraction_method
        == "metadata"
    )

    # Zotero-side fulltext -> zotero_fulltext.
    doc_id2, _, _ = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-x", fulltext="ZOTERO TEXT"),
        cfg,
        _pdf_item(key="EXM2", title="Fulltext"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    assert (
        db_session.query(Document).filter_by(id=doc_id2).one().extraction_method
        == "zotero_fulltext"
    )

    # PDF bytes that fail local extraction -> metadata fallback, NOT
    # "pdf_extraction".
    doc_id3, _, _ = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-1.7 not-a-real-pdf EXM3"),
        cfg,
        _pdf_item(key="EXM3", title="Broken PDF"),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()
    doc3 = db_session.query(Document).filter_by(id=doc_id3).one()
    assert doc3.file_type == "pdf"
    assert doc3.extraction_method == "metadata"


def test_sync_one_isolates_removal_cascade_failure(
    db_session, monkeypatch, tmp_path
):
    """One document whose delete-cascade fails must not abort the removals
    batch: the other removals complete (and get their vectors purged), the
    failing item stays mapped for a retry next sync, and the sync itself
    completes."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cleanup = MagicMock()
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", cleanup)

    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    client = MagicMock()
    client.get_children.return_value = []
    client.get_top_item_versions.side_effect = [
        ({"S": 1, "A": 1, "B": 1}, 10),
        ({"S": 1}, 20),  # A and B removed
    ]
    client.get_items.side_effect = [
        [item("S", 1, "Survivor"), item("A", 1, "Paper A"), item("B", 1, "B")],
        [],
    ]

    assert svc._sync_one(client, cfg, "")["status"] == "completed"
    cleanup.reset_mock()

    def doc_of(key):
        return (
            db_session.query(ZoteroItemMap)
            .filter_by(zotero_item_key=key)
            .one()
            .document_id
        )

    a_doc, b_doc = doc_of("A"), doc_of("B")

    orig_cascade = svc_mod.CascadeHelper.delete_document_chunks

    def guarded(session, document_id, collection_name=None):
        if document_id == b_doc:
            raise RuntimeError("cascade boom")
        return orig_cascade(
            session, document_id, collection_name=collection_name
        )

    monkeypatch.setattr(
        svc_mod.CascadeHelper,
        "delete_document_chunks",
        staticmethod(guarded),
    )

    stats2 = svc._sync_one(client, cfg, "")
    assert stats2["status"] == "completed"
    assert stats2["removed"] == 1  # A
    assert stats2["errors"] == 1  # B, contained
    # The contained failure is surfaced on the persisted sync state.
    state = db_session.query(ZoteroSyncState).one()
    assert state.last_status == "completed"
    assert "1 item(s) failed" in (state.last_error or "")
    # A is fully removed and its vectors purged.
    assert db_session.query(Document).filter_by(id=a_doc).first() is None
    cleaned = [c.args[0] for c in cleanup.call_args_list]
    assert any(a_doc in ids for ids in cleaned)
    # B rolled back cleanly: doc + map row intact, vectors untouched.
    assert db_session.query(Document).filter_by(id=b_doc).first() is not None
    assert (
        db_session.query(ZoteroItemMap).filter_by(zotero_item_key="B").first()
        is not None
    )
    assert not any(b_doc in ids for ids in cleaned)


def test_sync_one_isolates_remap_release_failure(
    db_session, monkeypatch, tmp_path
):
    """A failing release during a dedup remap must roll back the whole item
    (map still points at the prior doc, version not advanced -> retried next
    sync) without failing the sync or purging any vectors."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cleanup = MagicMock()
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", cleanup)

    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    client = MagicMock()
    client.get_children.return_value = []
    client.get_top_item_versions.side_effect = [
        ({"K": 1, "M": 1}, 10),
        ({"K": 2, "M": 1}, 20),  # K edited
    ]
    client.get_items.side_effect = [
        [item("K", 1, "Original K"), item("M", 1, "Shared Paper")],
        [item("K", 2, "Shared Paper")],  # K's new content == M's -> remap
    ]

    assert svc._sync_one(client, cfg, "")["status"] == "completed"

    def row_of(key):
        return (
            db_session.query(ZoteroItemMap).filter_by(zotero_item_key=key).one()
        )

    d1 = row_of("K").document_id
    cleanup.reset_mock()

    orig_cascade = svc_mod.CascadeHelper.delete_document_chunks

    def guarded(session, document_id, collection_name=None):
        if document_id == d1:
            raise RuntimeError("cascade boom")
        return orig_cascade(
            session, document_id, collection_name=collection_name
        )

    monkeypatch.setattr(
        svc_mod.CascadeHelper,
        "delete_document_chunks",
        staticmethod(guarded),
    )

    stats2 = svc._sync_one(client, cfg, "")
    assert stats2["status"] == "completed"
    assert stats2["errors"] == 1
    assert stats2["updated"] == 0
    # Whole item rolled back: still mapped to the prior doc at the OLD
    # version, so the next sync retries the remap.
    row = row_of("K")
    assert row.document_id == d1
    assert row.zotero_version == 1
    assert db_session.query(Document).filter_by(id=d1).first() is not None
    cleanup.assert_not_called()


def test_sync_one_indexes_committed_docs_even_when_sync_fails(
    db_session, monkeypatch, tmp_path
):
    """Items committed before a mid-batch failure must still get their RAG
    auto-index triggered — their versions already match, so no future sync
    reprocesses them, and without the trigger they stay invisible to
    semantic search."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod
    from local_deep_research.research_library.zotero.client import (
        ZoteroTransientError,
    )

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    auto_index = MagicMock()
    monkeypatch.setattr(rag_mod, "trigger_auto_index", auto_index)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", MagicMock())

    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    client = MagicMock()

    def children(key):
        if key == "B":
            raise ZoteroTransientError("rate limited")
        return []

    client.get_children.side_effect = children
    client.get_top_item_versions.return_value = ({"A": 1, "B": 1}, 10)
    client.get_items.return_value = [
        item("A", 1, "Paper A"),
        item("B", 1, "Paper B"),
    ]

    stats = svc._sync_one(client, cfg, "")
    assert stats["status"] == "failed"  # the transient error aborts the batch
    assert stats["imported"] == 1  # but A was committed first

    a_doc = (
        db_session.query(ZoteroItemMap)
        .filter_by(zotero_item_key="A")
        .one()
        .document_id
    )
    # A's committed document still gets its auto-index triggered.
    auto_index.assert_called_once()
    assert auto_index.call_args.args[0] == [a_doc]


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


def _standalone_pdf_item(key="STAND001", filename="dropped.pdf", **overrides):
    data = {
        "title": filename,
        "filename": filename,
        "contentType": "application/pdf",
        "linkMode": "imported_file",
    }
    data.update(overrides)
    return ZoteroItem(key=key, version=4, item_type="attachment", data=data)


def test_ingest_standalone_pdf_attachment(db_session, seeded):
    """A top-level PDF attachment with no parent is imported as its own
    document: the item itself is the attachment (no children fetch)."""
    source_type_id, collection_id = seeded
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none", import_items_without_pdf=True)
    client = MagicMock()
    client.get_attachment_fulltext.return_value = None
    client.download_attachment.return_value = b"%PDF-1.7 dropped-pdf-bytes"

    doc_id, action, _ = svc._ingest_item(
        db_session,
        client,
        cfg,
        _standalone_pdf_item(),
        collection_id,
        source_type_id,
        MagicMock(),
    )
    db_session.commit()

    assert action == "imported"
    client.get_children.assert_not_called()  # the item IS the attachment
    client.download_attachment.assert_called_once_with("STAND001")
    doc = db_session.query(Document).filter_by(id=doc_id).one()
    assert doc.file_type == "pdf"
    assert doc.title == "dropped.pdf"


def test_sync_one_imports_standalone_pdf_skips_other_strays(
    db_session, monkeypatch, tmp_path
):
    """Sync level: a standalone PDF imports; a standalone note and a
    linked-file attachment are version-touched and counted as skipped; a
    child that slipped into /top is touched silently."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", MagicMock())
    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    standalone = _standalone_pdf_item(key="SPDF0001")
    note = ZoteroItem("NOTE0001", 2, "note", {})
    linked = _standalone_pdf_item(key="LINK0001", linkMode="linked_file")
    child = ZoteroItem(
        "CHLD0001",
        2,
        "attachment",
        {"parentItem": "PARENT01", "contentType": "application/pdf"},
    )

    client = MagicMock()
    client.get_attachment_fulltext.return_value = None
    client.download_attachment.return_value = b"%PDF-1.7 standalone"
    client.get_children.return_value = []
    client.get_top_item_versions.return_value = (
        {"SPDF0001": 4, "NOTE0001": 2, "LINK0001": 4, "CHLD0001": 2},
        10,
    )
    client.get_items.return_value = [standalone, note, linked, child]

    stats = svc._sync_one(client, cfg, "")
    assert stats["status"] == "completed"
    assert stats["imported"] == 1  # the standalone PDF
    assert stats["skipped"] == 2  # note + linked-file (visible)
    assert stats["errors"] == 0

    def row(key):
        return (
            db_session.query(ZoteroItemMap).filter_by(zotero_item_key=key).one()
        )

    assert row("SPDF0001").document_id is not None
    # Strays are version-touched so they aren't re-fetched every sync.
    for key in ("NOTE0001", "LINK0001", "CHLD0001"):
        assert row(key).document_id is None
    doc = (
        db_session.query(Document)
        .filter_by(id=row("SPDF0001").document_id)
        .one()
    )
    assert doc.file_type == "pdf"


def _probe_client(monkeypatch, key_info):
    """Patch the /keys/current probe client used by _resolve_library_id."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    probe = MagicMock()
    probe.__enter__ = lambda s: s
    probe.__exit__ = lambda s, *a: False
    probe.get_current_key_info.return_value = key_info
    monkeypatch.setattr(svc_mod, "ZoteroClient", MagicMock(return_value=probe))
    return probe


def test_resolve_library_id_accepts_username_and_blank(monkeypatch):
    """Personal libraries need no manually entered ID: a blank field or a
    pasted username is resolved to the key's numeric userID."""
    svc = ZoteroSyncService("user", None)
    _probe_client(monkeypatch, {"userID": 20971466})

    for entered in ("cat-fox", ""):
        cfg = ZoteroConfig(
            enabled=True,
            api_key="k",
            library_type="user",
            library_id=entered,
        )
        assert svc._resolve_library_id(cfg) is None
        assert cfg.library_id == "20971466"

    # A numeric ID passes through untouched (no probe needed).
    cfg = ZoteroConfig(enabled=True, api_key="k", library_id="42")
    assert svc._resolve_library_id(cfg) is None
    assert cfg.library_id == "42"


def test_resolve_library_id_group_requires_numeric():
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(
        enabled=True, api_key="k", library_type="group", library_id="my-group"
    )
    err = svc._resolve_library_id(cfg)
    assert err is not None and "group" in err.lower()
    ok = ZoteroConfig(
        enabled=True, api_key="k", library_type="group", library_id="99"
    )
    assert svc._resolve_library_id(ok) is None


def test_resolve_library_id_local_defaults_to_zero():
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(enabled=True, use_local_api=True, library_id="")
    assert svc._resolve_library_id(cfg) is None
    assert cfg.library_id == "0"


def test_resolve_library_id_failure_gives_actionable_error(monkeypatch):
    """If /keys/current can't resolve a userID the error says what to do."""
    svc = ZoteroSyncService("user", None)
    _probe_client(monkeypatch, {})
    cfg = ZoteroConfig(
        enabled=True, api_key="k", library_type="user", library_id=""
    )
    err = svc._resolve_library_id(cfg)
    assert err is not None
    assert "userID" in err


def test_connection_resolves_username_in_library_id(monkeypatch):
    """test_connection works even when the user pasted their username: the
    key's userID is resolved and used transparently."""
    cfg = ZoteroConfig(
        enabled=True, api_key="k", library_type="user", library_id="cat-fox"
    )
    monkeypatch.setattr(ZoteroSyncService, "get_config", lambda self: cfg)
    _probe_client(monkeypatch, {"userID": 20971466})

    svc = ZoteroSyncService("user", None)
    fake_client = MagicMock()
    fake_client.__enter__ = lambda s: s
    fake_client.__exit__ = lambda s, *a: False
    fake_client.test_connection.return_value = 7
    fake_client.get_collections.return_value = []
    monkeypatch.setattr(svc, "_make_client", lambda c: fake_client)

    result = svc.test_connection()
    assert result["success"] is True
    assert cfg.library_id == "20971466"


def test_connection_withholds_zotero_exception_detail(monkeypatch):
    from local_deep_research.research_library.zotero.client import ZoteroError

    cfg = ZoteroConfig(enabled=True, api_key="k", library_id="1")
    monkeypatch.setattr(ZoteroSyncService, "get_config", lambda self: cfg)

    fake_client = MagicMock()
    fake_client.__enter__.return_value = fake_client
    fake_client.test_connection.side_effect = ZoteroError(
        "private upstream response"
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_make_client", lambda _cfg: fake_client)

    result = svc.test_connection()

    assert result == {
        "success": False,
        "error": (
            "Zotero connection failed. Check the configured API key and "
            "library settings."
        ),
    }
    assert "private upstream response" not in result["error"]


def test_connection_forwards_curated_zotero_guidance(monkeypatch):
    """``test_connection``'s ``except ZoteroError:`` arm must keep the
    curated text. Catches the revert that hardcodes a single generic
    "connection failed" string for every Zotero failure, which loses the
    numeric-library-ID hint on the endpoint the settings UI calls first.

    It does NOT catch a revert to ``sanitize_error_for_client(str(exc))`` --
    that scrubber leaves a curated constant unchanged.
    ``test_connection_withholds_zotero_exception_detail`` above is the leg
    that fails on that revert.
    """
    from local_deep_research.research_library.zotero.client import (
        ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE,
        ZoteroError,
    )

    cfg = ZoteroConfig(enabled=True, api_key="k", library_id="1")
    monkeypatch.setattr(ZoteroSyncService, "get_config", lambda self: cfg)

    fake_client = MagicMock()
    fake_client.__enter__.return_value = fake_client
    fake_client.test_connection.side_effect = ZoteroError(
        ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE
    )

    svc = ZoteroSyncService("user", None)
    monkeypatch.setattr(svc, "_make_client", lambda _cfg: fake_client)

    result = svc.test_connection()

    assert result == {
        "success": False,
        "error": ZOTERO_LIBRARY_ID_NOT_NUMERIC_MESSAGE,
    }


def test_config_configured_without_library_id_for_user_library():
    """A personal cloud library is configured with just key+enabled; a group
    library still requires an explicit ID."""
    user_cfg = ZoteroConfig(enabled=True, api_key="k", library_id="")
    assert user_cfg.is_configured is True
    group_cfg = ZoteroConfig(
        enabled=True, api_key="k", library_type="group", library_id=""
    )
    assert group_cfg.is_configured is False
    local_cfg = ZoteroConfig(enabled=True, use_local_api=True, library_id="")
    assert local_cfg.is_configured is True


def test_manual_sync_reprocesses_previously_skipped_items(
    db_session, monkeypatch, tmp_path
):
    """An item version-touched as a stray by an earlier sync (document_id
    None, version unchanged) is re-examined by a manual sync — so upgrades
    and settings changes take effect — while a scheduled sync still skips
    it."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", MagicMock())
    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
        import_items_without_pdf=True,
    )

    standalone = _standalone_pdf_item(key="OLDSKIP1")
    client = MagicMock()
    client.get_attachment_fulltext.return_value = None
    client.download_attachment.return_value = b"%PDF-1.7 finally-imported"
    client.get_children.return_value = []
    client.get_top_item_versions.return_value = ({"OLDSKIP1": 4}, 10)
    # Faithful to the real client: only requested keys are returned.
    client.get_items.side_effect = lambda keys: (
        [standalone] if "OLDSKIP1" in keys else []
    )

    # Simulate the pre-upgrade state: the item was version-touched with no
    # document by an earlier sync of THIS collection. The state row must
    # exist too, or _sync_one would mint a fresh collection id and never
    # see the map row.
    ldr_collection_id, _created = svc._ensure_ldr_collection(
        db_session, client, cfg, ""
    )
    svc._get_or_create_state(db_session, cfg, "", ldr_collection_id)
    db_session.add(
        ZoteroItemMap(
            ldr_collection_id=ldr_collection_id,
            zotero_item_key="OLDSKIP1",
            zotero_version=4,  # matches remote -> normally not reprocessed
            document_id=None,
        )
    )
    db_session.commit()

    # Scheduled (default): the stamped item stays skipped.
    stats = svc._sync_one(client, cfg, "")
    assert stats["imported"] == 0
    client.get_items.assert_called_with([])

    # Manual: it is re-fetched and now imports.
    stats2 = svc._sync_one(client, cfg, "", reprocess_skipped=True)
    assert stats2["imported"] == 1
    row = (
        db_session.query(ZoteroItemMap)
        .filter_by(zotero_item_key="OLDSKIP1")
        .one()
    )
    assert row.document_id is not None


def test_sync_progress_lifecycle():
    """Progress is only visible while the user's sync lock is held, and
    clears cleanly."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod

    svc = ZoteroSyncService("progress-user", None)
    assert ZoteroSyncService.get_sync_progress("progress-user") is None

    lock = svc_mod._get_user_sync_lock("progress-user")
    assert lock.acquire(blocking=False)
    try:
        svc._set_progress(phase="importing", done=3, total=10, current="Paper")
        prog = ZoteroSyncService.get_sync_progress("progress-user")
        assert prog == {
            "phase": "importing",
            "done": 3,
            "total": 10,
            "current": "Paper",
        }
        svc._set_progress(done=4)
        assert ZoteroSyncService.get_sync_progress("progress-user")["done"] == 4
        svc._clear_progress()
        assert ZoteroSyncService.get_sync_progress("progress-user") is None
    finally:
        lock.release()


# ---------------------------------------------------------------------------
# Default "Library" collection: ambient membership
# ---------------------------------------------------------------------------


def _seed_default_collection(db_session):
    default_id = str(uuid.uuid4())
    db_session.add(
        Collection(
            id=default_id,
            name="Library",
            collection_type="library",
            is_default=True,
        )
    )
    db_session.commit()
    return default_id


def test_ingest_links_default_library_collection(db_session, seeded):
    """Imports are linked into the default Library collection too, so the
    main library view lists them (like uploads and research downloads)."""
    source_type_id, collection_id = seeded
    default_id = _seed_default_collection(db_session)
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none")

    doc_id, action, _ = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-1.7 default-linked"),
        cfg,
        _pdf_item(key="DEFL0001"),
        collection_id,
        source_type_id,
        MagicMock(),
        default_collection_id=default_id,
    )
    db_session.commit()

    assert action == "imported"
    for cid in (collection_id, default_id):
        assert (
            db_session.query(DocumentCollection)
            .filter_by(document_id=doc_id, collection_id=cid)
            .first()
            is not None
        )


def test_default_membership_does_not_block_in_place_update(db_session, seeded):
    """Ambient default-collection membership must not make a document look
    'shared': a content edit still updates in place instead of forking."""
    source_type_id, collection_id = seeded
    default_id = _seed_default_collection(db_session)
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none")

    doc_id, _, _ = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-1.7 v1-bytes"),
        cfg,
        _pdf_item(key="DEFU0001"),
        collection_id,
        source_type_id,
        MagicMock(),
        default_collection_id=default_id,
    )
    svc._upsert_map(
        db_session, collection_id, _pdf_item(key="DEFU0001"), doc_id
    )
    db_session.commit()

    doc_id2, action2, reindexed2 = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-1.7 v2-bytes"),
        cfg,
        _pdf_item(key="DEFU0001"),
        collection_id,
        source_type_id,
        MagicMock(),
        default_collection_id=default_id,
    )
    db_session.commit()

    assert doc_id2 == doc_id  # updated in place, not forked
    assert action2 == "updated"
    assert reindexed2 is True


def test_remove_item_deletes_doc_despite_default_membership(db_session, seeded):
    """Ambient default-collection membership must not keep a removed item's
    document alive."""
    source_type_id, collection_id = seeded
    default_id = _seed_default_collection(db_session)
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none")

    doc_id, _, _ = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-1.7 removable"),
        cfg,
        _pdf_item(key="DEFR0001"),
        collection_id,
        source_type_id,
        MagicMock(),
        default_collection_id=default_id,
    )
    svc._upsert_map(
        db_session, collection_id, _pdf_item(key="DEFR0001"), doc_id
    )
    db_session.commit()
    row = (
        db_session.query(ZoteroItemMap)
        .filter_by(zotero_item_key="DEFR0001")
        .one()
    )

    released = svc._remove_item(
        db_session, row, source_type_id, default_collection_id=default_id
    )
    db_session.commit()

    assert released == doc_id
    assert db_session.query(Document).filter_by(id=doc_id).first() is None
    assert (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id)
        .count()
        == 0
    )


def test_in_place_update_purges_default_collection_chunks(db_session, seeded):
    """An in-place content change must also purge the default Library
    collection's chunks and clear its indexed flag — its ambient copy is
    derived from the same now-changed text."""
    from local_deep_research.database.models.library import DocumentChunk

    source_type_id, collection_id = seeded
    default_id = _seed_default_collection(db_session)
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(pdf_storage_mode="none")

    doc_id, _, _ = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-1.7 amb-v1"),
        cfg,
        _pdf_item(key="AMBP0001"),
        collection_id,
        source_type_id,
        MagicMock(),
        default_collection_id=default_id,
    )
    svc._upsert_map(
        db_session, collection_id, _pdf_item(key="AMBP0001"), doc_id
    )
    # Simulate the default collection having indexed the doc.
    db_session.add(
        DocumentChunk(
            source_id=doc_id,
            source_type="document",
            collection_name=f"collection_{default_id}",
            chunk_index=0,
            chunk_text="stale ambient text",
            chunk_hash="amb-stale",
            start_char=0,
            end_char=18,
            word_count=3,
            embedding_id="emb-amb",
            embedding_model="all-MiniLM-L6-v2",
            embedding_model_type="sentence_transformers",
        )
    )
    link = (
        db_session.query(DocumentCollection)
        .filter_by(document_id=doc_id, collection_id=default_id)
        .one()
    )
    link.indexed = True
    db_session.commit()

    doc_id2, action2, reindexed2 = svc._ingest_item(
        db_session,
        _fake_client(pdf_bytes=b"%PDF-1.7 amb-v2-changed"),
        cfg,
        _pdf_item(key="AMBP0001"),
        collection_id,
        source_type_id,
        MagicMock(),
        default_collection_id=default_id,
    )
    db_session.commit()

    assert doc_id2 == doc_id and action2 == "updated" and reindexed2 is True
    assert (
        db_session.query(DocumentChunk)
        .filter_by(source_id=doc_id, collection_name=f"collection_{default_id}")
        .count()
        == 0
    )
    db_session.refresh(link)
    assert link.indexed is False


def test_sync_one_purges_default_vectors_for_hard_deleted_docs(
    db_session, monkeypatch, tmp_path
):
    """A hard-deleted document's FAISS vectors must leave the default
    Library collection's index too — not just the Zotero collection's."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    default_id = _seed_default_collection(db_session)

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cleanup = MagicMock()
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", cleanup)
    cfg = ZoteroConfig(
        enabled=True,
        api_key="k",
        library_id="1",
        pdf_storage_mode="none",
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    # Each item needs its own downloadable PDF with DISTINCT bytes —
    # identical bytes would dedup both items onto one shared document and
    # the twin would (correctly) keep it alive.
    client = MagicMock()
    client.get_attachment_fulltext.return_value = None
    client.get_children.side_effect = lambda key: [
        ZoteroAttachment(
            key=f"A{key}",
            version=1,
            content_type="application/pdf",
            link_mode="imported_file",
            filename=f"{key}.pdf",
            title=key,
        )
    ]
    client.download_attachment.side_effect = lambda att_key: (
        f"%PDF-1.7 {att_key}".encode()
    )
    client.get_top_item_versions.side_effect = [
        ({"HD1": 1, "KEEP1": 1}, 10),
        # KEEP1 survives so the remote set is non-empty and the
        # mass-deletion guard lets the removal of HD1 proceed.
        ({"KEEP1": 1}, 20),
    ]
    client.get_items.side_effect = [
        [item("HD1", 1, "Doomed"), item("KEEP1", 1, "Keeper")],
        [],
    ]

    assert svc._sync_one(client, cfg, "")["status"] == "completed"
    doc_id = (
        db_session.query(ZoteroItemMap)
        .filter_by(zotero_item_key="HD1")
        .one()
        .document_id
    )
    cleanup.reset_mock()

    stats2 = svc._sync_one(client, cfg, "")
    assert stats2["removed"] == 1
    assert db_session.query(Document).filter_by(id=doc_id).first() is None
    # Vectors purged from BOTH the zotero collection and the default one.
    calls = [(c.args[0], c.args[1]) for c in cleanup.call_args_list]
    purged_collections = {cid for ids, cid in calls if doc_id in ids}
    assert default_id in purged_collections
    assert len(purged_collections) == 2


def test_filename_for_avoids_double_extension():
    item = ZoteroItem(
        "K",
        1,
        "attachment",
        {"title": "dropped.pdf", "filename": "dropped.pdf"},
    )
    assert ZoteroSyncService._filename_for(item, "pdf") == "dropped.pdf"
    upper = ZoteroItem("K", 1, "attachment", {"title": "PAPER.PDF"})
    assert ZoteroSyncService._filename_for(upper, "pdf") == "PAPER.pdf"


def test_resolve_library_id_group_error_wins_over_local_default():
    """Local mode with a group library and no ID must surface the clear
    group-ID error, not silently address the desktop user 0."""
    svc = ZoteroSyncService("user", None)
    cfg = ZoteroConfig(
        enabled=True, use_local_api=True, library_type="group", library_id=""
    )
    err = svc._resolve_library_id(cfg)
    assert err is not None and "group" in err.lower()


def test_default_purge_spares_kept_docs_in_mixed_removal_sync(
    db_session, monkeypatch, tmp_path
):
    """One sync removes two items: an owned doc (hard-deleted) and a
    dedup-adopted foreign doc (released but kept). Only the hard-deleted
    doc's vectors may leave the default Library collection's index."""
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    import local_deep_research.web.routers.rag as rag_mod

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    default_id = _seed_default_collection(db_session)
    foreign_bytes = b"%PDF-1.7 AFOREIGN-shared"
    foreign = _seed_foreign_upload(db_session, foreign_bytes)
    foreign_id = foreign.id

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(rag_mod, "trigger_auto_index", lambda *a, **k: None)

    svc = ZoteroSyncService("user", "pw")
    monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
    cleanup = MagicMock()
    monkeypatch.setattr(svc, "_cleanup_removed_vectors", cleanup)
    cfg = ZoteroConfig(
        enabled=True, api_key="k", library_id="1", pdf_storage_mode="none"
    )

    def item(key, ver, title):
        return ZoteroItem(
            key=key,
            version=ver,
            item_type="journalArticle",
            data={"title": title},
        )

    client = MagicMock()
    client.get_attachment_fulltext.return_value = None
    client.get_children.side_effect = lambda key: [
        ZoteroAttachment(
            key=f"A{key}",
            version=1,
            content_type="application/pdf",
            link_mode="imported_file",
            filename=f"{key}.pdf",
            title=key,
        )
    ]
    # AFOREIGN's bytes match the user upload -> dedup adoption; AOWNED gets
    # its own distinct bytes.
    client.download_attachment.side_effect = lambda att_key: (
        foreign_bytes
        if att_key == "AFOREIGN"
        else f"%PDF-1.7 {att_key}-own".encode()
    )
    client.get_top_item_versions.side_effect = [
        ({"OWNED": 1, "FOREIGN": 1, "KEEP": 1}, 10),
        ({"KEEP": 1}, 20),  # both removed; KEEP survives past the guard
    ]
    client.get_items.side_effect = [
        [
            item("OWNED", 1, "Owned"),
            item("FOREIGN", 1, "F"),
            item("KEEP", 1, "K"),
        ],
        [],
    ]

    assert svc._sync_one(client, cfg, "")["status"] == "completed"
    owned_doc = (
        db_session.query(ZoteroItemMap)
        .filter_by(zotero_item_key="OWNED")
        .one()
        .document_id
    )
    assert (
        db_session.query(ZoteroItemMap)
        .filter_by(zotero_item_key="FOREIGN")
        .one()
        .document_id
        == foreign_id
    )
    cleanup.reset_mock()

    stats2 = svc._sync_one(client, cfg, "")
    assert stats2["removed"] == 2
    # Hard-deleted vs kept:
    assert db_session.query(Document).filter_by(id=owned_doc).first() is None
    assert (
        db_session.query(Document).filter_by(id=foreign_id).first() is not None
    )
    default_purged = set()
    for c in cleanup.call_args_list:
        ids, cid = c.args[0], c.args[1]
        if cid == default_id:
            default_purged.update(ids)
    assert owned_doc in default_purged
    assert foreign_id not in default_purged  # kept doc stays searchable


def test_sync_creating_ldr_collection_invalidates_engine_cache(
    db_session, monkeypatch, tmp_path
):
    """A sync that CREATES the backing LDR collection must invalidate the
    user's cached collection-engine list, and only once the row is durable,
    so a research started within the 30 s TTL offers the fresh collection
    as a tool (#6609). An existing collection must NOT churn the cache.

    The ordering is pinned through an event log: the invalidation must
    come after the commit that follows the creation. An invalidation
    placed before that commit, or at flush time inside
    ``_ensure_ldr_collection``, would let a refill in the flush→commit
    window re-cache the pre-creation list, and fails here. The version
    fetch raises, so the sync fails right after the creation commit.
    """
    import local_deep_research.research_library.zotero.sync_service as svc_mod
    from local_deep_research.research_library.zotero.client import ZoteroError

    source_type_id = str(uuid.uuid4())
    db_session.add(
        SourceType(id=source_type_id, name="zotero", display_name="Zotero")
    )
    db_session.commit()

    @contextmanager
    def fake_session(*_a, **_k):
        yield db_session

    monkeypatch.setattr(svc_mod, "get_user_db_session", fake_session)
    monkeypatch.setattr(
        svc_mod, "get_source_type_id", lambda *a, **k: source_type_id
    )
    events = []
    real_commit = db_session.commit

    def recording_commit():
        real_commit()
        events.append("commit")

    monkeypatch.setattr(db_session, "commit", recording_commit)
    monkeypatch.setattr(
        "local_deep_research.web_search_engines.search_engines_config."
        "invalidate_collection_engines_cache",
        lambda username=None: events.append(("invalidate", username)),
    )

    def recording_service():
        svc = ZoteroSyncService("cache_user", None)
        monkeypatch.setattr(svc, "_library_root", lambda: str(tmp_path))
        real_ensure = svc._ensure_ldr_collection

        def recording_ensure(*args, **kwargs):
            result = real_ensure(*args, **kwargs)
            events.append("created" if result[1] else "found")
            return result

        monkeypatch.setattr(svc, "_ensure_ldr_collection", recording_ensure)
        return svc

    cfg = ZoteroConfig(
        enabled=True,
        api_key="zotero-api-key-abcdefghijklmnop",
        library_id="1",
        pdf_storage_mode="none",
    )
    client = MagicMock()
    client.get_top_item_versions.side_effect = ZoteroError("rate limited")

    stats = recording_service()._sync_one(client, cfg, "")
    assert stats["status"] == "failed"
    assert (
        db_session.query(Collection).filter_by(collection_type="zotero").count()
        == 1
    )
    invalidation = ("invalidate", "cache_user")
    assert events.count(invalidation) == 1, events
    created_at = events.index("created")
    invalidated_at = events.index(invalidation)
    # Durable-after-commit: a commit sits between the creation and the
    # invalidation.
    assert "commit" in events[created_at:invalidated_at], events

    # A re-sync that finds the collection existing must not invalidate again.
    events.clear()
    client2 = MagicMock()
    client2.get_top_item_versions.side_effect = ZoteroError("rate limited")
    stats2 = recording_service()._sync_one(client2, cfg, "")
    assert stats2["status"] == "failed"
    assert "found" in events
    assert invalidation not in events