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.

168 KB
Raw Blame
"""Contracts for what "Sync library" does with downloads whose PDF is missing.

``LibraryService.sync_library_with_filesystem`` is there to deal with a
downloaded document whose PDF file was removed from disk, so that its URL
can be downloaded again. Before PDF and text documents were merged into
one ``Document`` row, the sync checked PDF records only and left text
documents alone. Deleting the merged row also deletes its text, its PDF
blob, its collection links and, after the commit, its chunks and vectors.

The contract pinned here: the sync deletes a download whose file is
missing only when nothing else of it is stored (no extracted text, no PDF
blob in the database, no file at the document's own path). The
``storage_mode`` label and the tracker's ``file_path`` cannot decide this
on their own, because production writers leave both stale:

  * R1: re-downloading an existing document in "none" mode, or in an
    unrecognised mode that ``PDFStorageManager`` runs as "none", clears
    the tracker path. The document keeps its stored PDF and its label.
  * R2: ``_record_failed_text_extraction`` creates its placeholder with
    the ``"database"`` column default. ``_save_text_with_db`` later
    completes it with text and never stores a PDF.
  * R3: rows already written with ``storage_mode="database"`` and no blob
    keep that shape; nothing repairs them. A content-hash duplicate
    download copies ``"database"`` into the shared tracker.
  * A Zotero import stores its PDF blob and the item's source URL but
    creates no tracker at all.
  * A URL has one tracker, which every document with that URL shares: the
    documents of two researches that cite the same paper, or a
    failed-download placeholder that "Extract Text to DB" completed next
    to the document holding the PDF. Its path names the file written
    last, so it describes a document only when it is the document's own
    path, or when it is ``"database"`` and the document is labelled as
    stored in the database, which can be several documents. The sync
    judges every other document by what it stores itself and leaves the
    tracker alone. A ``"database"`` tracker is reset only when no
    document under it still holds a PDF blob.

A download whose file was removed while its extracted text was not keeps
the text. If it still has its research resource, the sync makes it
text-only, resets its tracker and reports the file missing, and the
download manager's PDF downloads put the PDF back into the same document,
also when the PDF downloaded again has other bytes. A run downloads it
once, also when a second research's resource is linked to the document:
through the document's own resource, unless that one cannot succeed or a
restart left its queue entry PROCESSING, and the linked one can.
Choosing that resource records no denial of the egress policy and loads
no document's text. The Download Manager counts such a document as
available to download, in its totals and in its research's sources, and
so do the library's source routes, through which ``download_source``
puts its PDF back. Every document the sync reports this way is one
``document_awaits_pdf`` asks for, including a failed-download placeholder
that a PDF download completed. Text extraction leaves such a document
alone: it already has the text. Without its resource no download path
asks for the PDF, so the sync makes the document text-only without
reporting it, and a later download of the same PDF bytes for another
resource stores the PDF in it. A PDF removed with "Remove PDF to save
space" is not a missing file: the document is kept as it is, and its PDF
is not downloaded again. The sync's response names each missing file by
its stored path, relative to the library root, never by its absolute
path on the server, and counts each tracker it changes once in
``trackers_updated``, whichever document of the tracker's URL it reads
first.

A loaded ``Document.status`` is a ``DocumentStatus`` member, which never
equals its string value, so it is compared with the member: text
extraction reads the PDF file that a completed download stores before it
asks the source, and the document details page shows the document's
status.

The deletion the sync is for still happens. A filesystem-mode download
with no text whose file was removed is deleted and can be downloaded
again, and so can a database-mode download with no blob and no text,
which the sync reports as ``"database"``.

The harness follows ``test_download_service_contracts.py``: a real
on-disk SQLite file with foreign keys on, a real ``tmp_path`` library
store, the production ``DownloadService`` writers (built with ``__new__``
around a stub downloader, so no request is ever made), the production
sync, ``delete_blob_only``, download queueing, the bulk download route,
the Download Manager page, the library's source routes and the document
details page. Only the session, settings and password sources are
replaced, plus the retry-policy filter of the "Download All New" route
and the routes' ``DownloadService`` factory. The service's retry manager
has no attempts on record, except where a case uses the production one
over the test database, and its egress policy allows every URL, except
where a case refuses one, through a stand-in or, in one case, the
production policy. Rows are read back through a fresh session.
"""

import asyncio
import hashlib
import json
import re
import uuid
from collections.abc import Set as AbstractSet
from contextlib import contextmanager, nullcontext
from datetime import UTC, datetime, timedelta
from types import SimpleNamespace

import pytest
from loguru import logger
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from starlette.requests import Request

from local_deep_research.constants import (
    FILE_PATH_BLOB_DELETED,
    FILE_PATH_TEXT_ONLY,
)
from local_deep_research.database.models import Base
from local_deep_research.database.models.download_tracker import (
    DownloadAttempt,
    DownloadTracker,
)
from local_deep_research.database.models.library import (
    Collection,
    Document,
    DocumentBlob,
    DocumentChunk,
    DocumentCollection,
    DocumentStatus,
    DownloadQueue,
    EmbeddingProvider,
    SourceType,
)
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)
from local_deep_research.library.download_management import (
    FailureClassifier,
    ResourceDownloadStatus,
    ResourceFilter,
    ResourceStatusTracker,
    RetryDecision,
    RetryManager,
)
from local_deep_research.library.download_management import (
    models as retry_models,
)
from local_deep_research.research_library import utils as library_utils
from local_deep_research.research_library.deletion.services import (
    document_deletion,
)
from local_deep_research.research_library.downloaders.base import (
    ContentType,
    DownloadResult,
)
from local_deep_research.research_library.services import (
    download_service,
    library_service,
)
from local_deep_research.research_library.services.download_service import (
    DownloadService,
)
from local_deep_research.research_library.services.pdf_storage_manager import (
    PDFStorageManager,
    resolve_pdf_storage_mode,
)
from local_deep_research.research_library.utils import (
    document_awaits_pdf,
    find_tracker_by_url,
    get_url_hash,
)
from local_deep_research.security.egress.audit_hook import (
    clear_active_context,
)
from local_deep_research.security.egress.policy import (
    EgressContext,
    EgressScope,
)
from local_deep_research.utilities import db_utils
from local_deep_research.web.routers import library as library_router

USERNAME = "library_sync_user"
LIBRARY_COLLECTION_ID = "library-sync-contracts"
PDF_BYTES = (
    b"%PDF-1.4\n1 0 obj<</Type/Catalog>>endobj\ntrailer<</Root 1 0 R>>\n%%EOF\n"
)
PDF_HASH = hashlib.sha256(PDF_BYTES).hexdigest()
SAVED_TEXT = "Text that only this document holds."
EXTRACTED_TEXT = "Text extracted from the downloaded PDF."


def _text_pdf(text):
    """A one-page PDF with a real text layer.

    ``PDF_BYTES`` has no pages, so the download's text extraction finds no
    text in it. This one goes through the real extraction and yields
    ``text``, the usual case for a downloaded paper.
    """
    objects = [
        b"<< /Type /Catalog /Pages 2 0 R >>",
        b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
        b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 200] "
        b"/Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>",
        b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
    ]
    stream = f"BT /F1 12 Tf 10 100 Td ({text}) Tj ET".encode("latin-1")
    objects.append(
        b"<< /Length "
        + str(len(stream)).encode()
        + b" >>\nstream\n"
        + stream
        + b"\nendstream"
    )
    out = bytearray(b"%PDF-1.4\n")
    offsets = []
    for number, body in enumerate(objects, start=1):
        offsets.append(len(out))
        out += f"{number} 0 obj\n".encode() + body + b"\nendobj\n"
    xref_offset = len(out)
    out += f"xref\n0 {len(objects) + 1}\n0000000000 65535 f \n".encode()
    for offset in offsets:
        out += f"{offset:010d} 00000 n \n".encode()
    out += (
        f"trailer\n<< /Size {len(objects) + 1} /Root 1 0 R >>\n"
        f"startxref\n{xref_offset}\n%%EOF\n"
    ).encode()
    return bytes(out)


TEXT_PDF_BYTES = _text_pdf(EXTRACTED_TEXT)
# The same paper served again as a regenerated PDF: other bytes, other text.
REGENERATED_TEXT = "Text extracted from the regenerated PDF."
REGENERATED_PDF_BYTES = _text_pdf(REGENERATED_TEXT)
# Hard-coded for the same reason as in test_download_service_contracts.py:
# a renamed gate must fail the filesystem cases, not turn them into
# database-mode cases.
FILESYSTEM_GATE_ENV = "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE"


class StubSettings:
    """Minimal stand-in for SettingsManager.get_setting."""

    def __init__(self, values):
        self._values = values

    def get_setting(self, key, default=None):
        return self._values.get(key, default)


class StubDownloader:
    """Returns ``content`` for every URL and records the calls.

    A URL in ``unreachable`` fails the way a server that is down does
    (HTTP 503).
    """

    def __init__(self, content=PDF_BYTES, unreachable=()):
        self.content = content
        self.unreachable = set(unreachable)
        self.urls = []

    def can_handle(self, url):
        return True

    def download_with_result(self, url, content_type=ContentType.PDF):
        self.urls.append(url)
        if url in self.unreachable:
            return DownloadResult(
                skip_reason="Service unavailable", status_code=503
            )
        return DownloadResult(
            content=self.content, is_success=True, status_code=200
        )


class NoRetryHistory:
    """A retry manager with no attempts on record: it holds nothing back."""

    status_tracker = SimpleNamespace(get_resource_status=lambda _id: None)

    def record_attempt(self, **kwargs):
        pass

    def should_retry_resource(self, resource_id):
        return RetryDecision(can_retry=True)


@pytest.fixture
def store(tmp_path):
    """The per-user library store the download and the sync both use."""
    base = tmp_path / "library"
    root = base / USERNAME
    (root / "pdfs").mkdir(parents=True)
    return SimpleNamespace(base=base, root=root)


def _tree(root):
    """Every file under ``root``, as posix-relative strings."""
    return {
        p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file()
    }


@pytest.fixture
def db(tmp_path):
    """Real on-disk SQLite, FK enforcement on, plus a fresh-session reader."""
    db_dir = tmp_path / "db"
    db_dir.mkdir()
    engine = create_engine(f"sqlite:///{db_dir / 'library.db'}")

    @event.listens_for(engine, "connect")
    def _fk_on(dbapi_connection, _record):
        cur = dbapi_connection.cursor()
        cur.execute("PRAGMA foreign_keys = ON")
        cur.close()

    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    @contextmanager
    def fresh():
        """A brand-new session on the same file, never the mutated one."""
        with Session() as other:
            yield other

    with Session() as session:
        try:
            yield SimpleNamespace(engine=engine, session=session, fresh=fresh)
        finally:
            engine.dispose()


@pytest.fixture
def world(db, monkeypatch):
    """A research session, the Library collection and resource factory.

    The download service's own session, source-type and default-collection
    lookups are pointed at this database.
    """
    session = db.session
    source_type = SourceType(
        id=str(uuid.uuid4()),
        name="research_download",
        display_name="Research Download",
    )
    research = ResearchHistory(
        id=str(uuid.uuid4()),
        query="library sync contract",
        mode="quick",
        status="completed",
        created_at="2026-08-01T00:00:00",
    )
    session.add_all(
        [
            source_type,
            research,
            Collection(id=LIBRARY_COLLECTION_ID, name="Library"),
        ]
    )
    session.commit()
    source_type_id = source_type.id
    research_id = research.id

    monkeypatch.setattr(
        download_service, "get_source_type_id", lambda *a, **k: source_type_id
    )
    monkeypatch.setattr(
        download_service,
        "get_default_library_id",
        lambda *a, **k: LIBRARY_COLLECTION_ID,
    )
    monkeypatch.setattr(
        download_service, "get_user_db_session", lambda *a, **k: db.fresh()
    )

    def add_resource(url, *, tracker=True, downloaded=False):
        """A cited resource, with the URL's tracker unless one exists."""
        resource = ResearchResource(
            research_id=research_id,
            title="Sync Contract Paper",
            url=url,
            source_type="academic",
            created_at="2026-08-01T00:00:00",
        )
        session.add(resource)
        session.commit()
        if tracker:
            session.add(
                DownloadTracker(
                    url=url,
                    url_hash=get_url_hash(url),
                    first_resource_id=resource.id,
                    is_downloaded=downloaded,
                    file_hash=PDF_HASH if downloaded else None,
                )
            )
            session.commit()
        return resource.id

    return SimpleNamespace(
        source_type_id=source_type_id,
        research_id=research_id,
        add_resource=add_resource,
    )


@pytest.fixture
def one_download_session(db, world, monkeypatch):
    """Give the download service one reused session, as production does.

    ``get_user_db_session`` hands every scope on a thread the same
    thread-local session and does not close it at scope exit. The
    download saves the extracted text in a later scope than the one that
    stored the PDF, through the resource instance that scope loaded. With
    the fresh session per scope that ``world`` sets up, that instance is
    detached by then and the text is never saved. The cases whose
    documents must hold the text of a real extraction use this.
    """
    session = sessionmaker(bind=db.engine)()
    monkeypatch.setattr(
        download_service,
        "get_user_db_session",
        lambda *a, **k: nullcontext(session),
    )
    try:
        yield session
    finally:
        session.close()


@pytest.fixture
def filesystem_mode(monkeypatch):
    """Open the operator gate for unencrypted filesystem PDF storage."""
    monkeypatch.setenv(FILESYSTEM_GATE_ENV, "true")
    assert resolve_pdf_storage_mode("filesystem") == "filesystem", (
        "the filesystem-storage operator gate did not open; the filesystem "
        "cases would silently run in database mode"
    )


@pytest.fixture
def retry_history(db):
    """The production retry manager, keeping its record in this database.

    ``download_resource`` records each attempt with it, and the refill
    ranking and the "Download All New" retry-policy filter read it back.
    Only its database source is replaced.
    """
    retry_models.Base.metadata.create_all(db.engine)
    tracker = ResourceStatusTracker.__new__(ResourceStatusTracker)
    tracker.username = USERNAME
    tracker.password = None
    tracker.engine = db.engine
    tracker.Session = sessionmaker(bind=db.engine)
    manager = RetryManager.__new__(RetryManager)
    manager.username = USERNAME
    manager.failure_classifier = FailureClassifier()
    manager.status_tracker = tracker
    return manager


def make_service(
    store, downloader, *, storage_mode, retry_manager=None, refused_urls=()
):
    """A ``DownloadService`` reduced to what ``download_resource`` uses.

    ``retry_manager`` defaults to one without attempts on record. The
    egress policy refuses ``refused_urls`` and allows every other URL, at
    queueing and at the download alike.
    """
    service = DownloadService.__new__(DownloadService)
    service.username = USERNAME
    service.password = None
    service._closed = False
    service.settings = StubSettings(
        {
            "research_library.pdf_storage_mode": storage_mode,
            "research_library.max_pdf_size_mb": 3072,
        }
    )
    service.library_root = str(store.root)
    service.downloaders = [downloader]
    service.retry_manager = retry_manager or NoRetryHistory()
    service._egress_context = None
    if refused_urls:
        refused = set(refused_urls)
        service._check_url_against_policy = lambda url, record_denial=True: (
            (False, "refused_by_test_policy")
            if url in refused
            else (True, "no_context")
        )
    return service


def make_library_service(db, store, monkeypatch):
    """A ``LibraryService`` whose real sync runs against this database.

    Only the session and settings sources are replaced. The keep/delete
    rule, tracker lookup, blob check, path resolution against ``store``
    and the post-commit purge are the production code.
    """
    monkeypatch.setattr(
        library_service, "get_user_db_session", lambda *a, **k: db.fresh()
    )
    monkeypatch.setattr(
        document_deletion, "get_user_db_session", lambda *a, **k: db.fresh()
    )
    settings = StubSettings({"research_library.storage_path": str(store.base)})
    monkeypatch.setattr(
        library_service,
        "get_settings_manager",
        lambda **_kwargs: SimpleNamespace(
            get_setting=settings.get_setting, close=lambda: None
        ),
    )
    service = library_service.LibraryService.__new__(
        library_service.LibraryService
    )
    service.username = USERNAME
    return service


def _library_links(session, document_id):
    return (
        session.query(DocumentCollection)
        .filter_by(document_id=document_id)
        .count()
    )


def _add_chunk(db, document_id):
    """A search-index chunk row of the document, as indexing writes one."""
    text = f"chunk of {document_id}"
    chunk = DocumentChunk(
        chunk_hash=hashlib.sha256(text.encode()).hexdigest(),
        source_type="document",
        source_id=document_id,
        collection_name=f"collection_{LIBRARY_COLLECTION_ID}",
        chunk_text=text,
        chunk_index=0,
        start_char=0,
        end_char=len(text),
        word_count=len(text.split()),
        embedding_id=str(uuid.uuid4()),
        embedding_model="fake-model",
        embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
        embedding_dimension=8,
    )
    db.session.add(chunk)
    db.session.commit()
    return chunk.id


def _cite_in_another_research(
    db, url, query="second research citing the paper"
):
    """A further research session citing ``url``, and its resource."""
    research = ResearchHistory(
        id=str(uuid.uuid4()),
        query=query,
        mode="quick",
        status="completed",
        created_at="2026-08-02T00:00:00",
    )
    db.session.add(research)
    db.session.commit()
    resource = ResearchResource(
        research_id=research.id,
        title="Sync Contract Paper",
        url=url,
        source_type="academic",
        created_at="2026-08-02T00:00:00",
    )
    db.session.add(resource)
    db.session.commit()
    return research.id, resource.id


def _add_database_row_without_pdf(
    db, world, resource_id, research_id, url, content
):
    """An existing database-mode download that holds no PDF and no text.

    The R3 shape without text: a database-mode download whose PDF the size
    limit refused, and that had no text to extract, left a completed row
    labelled ``"database"``, with no ``file_path``, no blob and no text.
    Nothing repairs such rows. ``content`` is the refused PDF, whose hash
    the row keeps, so a later download of the same bytes is linked to it.
    """
    document_id = str(uuid.uuid4())
    db.session.add(
        Document(
            id=document_id,
            source_type_id=world.source_type_id,
            resource_id=resource_id,
            research_id=research_id,
            document_hash=hashlib.sha256(content).hexdigest(),
            original_url=url,
            file_size=len(content),
            file_type="pdf",
            mime_type="application/pdf",
            title="Sync Contract Paper",
            status=DocumentStatus.COMPLETED,
            storage_mode="database",
            file_path=None,
        )
    )
    db.session.add(
        DocumentCollection(
            document_id=document_id, collection_id=LIBRARY_COLLECTION_ID
        )
    )
    db.session.commit()
    return document_id


class PassingResourceFilter:
    """The retry-policy filter, letting every resource through."""

    def __init__(self, *args, **kwargs):
        pass

    def filter_downloadable_resources(self, resources):
        return [
            SimpleNamespace(resource_id=row.id, can_retry=True, reason="ok")
            for row in resources
        ]

    def get_filter_summary(self, resources):
        return SimpleNamespace(
            to_dict=lambda: {},
            permanently_failed_count=0,
            temporarily_failed_count=0,
        )

    def get_skipped_resources_info(self, resources):
        return []


def claim_queue_entry(db, resource_id, research_id):
    """Queue ``resource_id`` and claim it as a bulk download stream does.

    The claim is the production one (``_claim_download_queue_item``), so
    the entry is PROCESSING and carries the time the claim records.
    """
    entry = DownloadQueue(
        resource_id=resource_id,
        research_id=research_id,
        collection_id=LIBRARY_COLLECTION_ID,
        status=DocumentStatus.PENDING,
    )
    db.session.add(entry)
    db.session.commit()
    assert library_router._claim_download_queue_item(db.session, entry.id)
    with db.fresh() as other:
        claimed = other.get(DownloadQueue, entry.id)
        assert claimed.status is DocumentStatus.PROCESSING
        assert claimed.last_attempt_at is not None
    return entry.id


class RetryPolicyFilter(ResourceFilter):
    """The production retry-policy filter, over the given retry manager."""

    def __init__(self, retry_manager):
        self.username = USERNAME
        self.retry_manager = retry_manager


# The ranking-only DownloadService of "Download All New" stores nothing.
_NO_STORE = SimpleNamespace(root="unused-library-root", base="unused-base")


def download_all_new(db, monkeypatch, *, retry_policy=None, **service_options):
    """Queue what the download manager's "Download All New" would.

    Runs the ``queue_all_undownloaded`` route against this database. Its
    session, the user's password and the retry-policy filter are replaced:
    the filter lets every resource through, or is the production filter
    over ``retry_policy``. So is the ``DownloadService`` the route ranks a
    document's resources with (``service_options`` as for
    ``make_service``). Which resources count as not downloaded, and which
    of a document's resources is queued, are the production code.
    """
    monkeypatch.setattr(
        library_router, "get_user_db_session", lambda *a, **k: db.fresh()
    )
    monkeypatch.setattr(
        library_router, "get_authenticated_user_password", lambda *a, **k: "pw"
    )
    monkeypatch.setattr(
        library_router,
        "ResourceFilter",
        PassingResourceFilter
        if retry_policy is None
        else (lambda *a, **k: RetryPolicyFilter(retry_policy)),
    )
    monkeypatch.setattr(
        library_router,
        "DownloadService",
        lambda *a, **k: make_service(
            _NO_STORE, StubDownloader(), storage_mode="none", **service_options
        ),
    )
    return library_router.queue_all_undownloaded(None, username=USERNAME)


def bulk_download(
    db, store, downloader, monkeypatch, research_ids, *, mode, **service_options
):
    """Run the download manager's bulk route and return its SSE events.

    "Get All Research PDFs" posts ``mode="pdf"``, "Extract Text to DB"
    posts ``mode="text_only"``. The route's session, the user's password
    and its ``DownloadService`` (a filesystem-mode service around
    ``downloader``, with ``service_options`` as for ``make_service``) are
    replaced. Which resources it queues, and the claim and download of
    each, are the production code.
    """
    monkeypatch.setattr(
        library_router, "get_user_db_session", lambda *a, **k: db.fresh()
    )
    monkeypatch.setattr(
        library_router, "get_authenticated_user_password", lambda *a, **k: "pw"
    )
    monkeypatch.setattr(
        library_router,
        "DownloadService",
        lambda *a, **k: make_service(
            store, downloader, storage_mode="filesystem", **service_options
        ),
    )
    body = json.dumps(
        {
            "research_ids": research_ids,
            "mode": mode,
            "collection_id": LIBRARY_COLLECTION_ID,
        }
    ).encode()

    async def receive():
        return {"type": "http.request", "body": body, "more_body": False}

    request = Request(
        {
            "type": "http",
            "method": "POST",
            "path": "/library/api/download-bulk",
            "headers": [(b"content-type", b"application/json")],
            "query_string": b"",
            "session": {"session_id": "sid"},
        },
        receive,
    )
    response = asyncio.run(
        library_router.download_bulk(request, username=USERNAME)
    )

    async def drain():
        chunks = []
        async for chunk in response.body_iterator:
            chunks.append(chunk if isinstance(chunk, bytes) else chunk.encode())
        return b"".join(chunks).decode()

    return [
        json.loads(line[len("data: ") :])
        for line in asyncio.run(drain()).split("\n")
        if line.startswith("data: ")
    ]


# ===========================================================================
# Documents that still hold content are kept
# ===========================================================================


@pytest.mark.parametrize("redownload_mode", ["none", "Database"])
@pytest.mark.parametrize("stored_mode", ["database", "filesystem"])
def test_redownload_that_stores_no_pdf_keeps_the_stored_one(
    db, store, world, filesystem_mode, monkeypatch, stored_mode, redownload_mode
):
    """R1: the PDF stored earlier keeps the document through a sync.

    No text is extracted, so the stored PDF is the document's only
    content. After the re-download the tracker has no path while the
    document still names its store. The sync kept a path-less document
    only when its label was "none", so it deleted this one, and with it
    the blob (database) or every reference to the file (filesystem).
    """
    url = "https://example.com/stored-then-redownloaded.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader()
    service = make_service(store, downloader, storage_mode=stored_mode)
    monkeypatch.setattr(service, "_extract_text_from_pdf", lambda _data: None)
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document_id = (
            other.query(Document).filter_by(resource_id=resource_id).one().id
        )
    stored_files = _tree(store.root)
    assert len(stored_files) == (1 if stored_mode == "filesystem" else 0)

    library = make_library_service(db, store, monkeypatch)
    assert library.mark_for_redownload([document_id]) == 1
    service.settings._values["research_library.pdf_storage_mode"] = (
        redownload_mode
    )
    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == 2
    with db.fresh() as other:
        # The R1 state: the tracker lost its path, the document kept its PDF.
        assert find_tracker_by_url(other, url).file_path is None
        document = other.get(Document, document_id)
        assert document.status is DocumentStatus.COMPLETED
        assert document.storage_mode == stored_mode
        assert document.text_content is None

    stats = library.sync_library_with_filesystem()

    assert stats["files_missing"] == 0
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document is not None, "the library sync deleted the document"
        assert (
            PDFStorageManager(store.root, "none").load_pdf(document, other)
            == PDF_BYTES
        )
        assert other.query(DocumentBlob).count() == (
            1 if stored_mode == "database" else 0
        )
        assert _library_links(other, document_id) == 1
    assert _tree(store.root) == stored_files
    assert library.get_document_by_id(document_id)["has_pdf"] is True
    # The kept document still counts as downloaded: no third request.
    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == 2


@pytest.mark.parametrize(
    "tracker", [True, False], ids=["tracker-without-path", "no-tracker"]
)
def test_failed_placeholder_completed_with_text_is_kept(
    db, store, world, monkeypatch, tracker
):
    """R2: text saved into a failed-download placeholder survives a sync.

    The placeholder keeps the ``"database"`` column default and never gets
    a blob, so before the fix the sync treated it as a stored PDF that had
    gone missing and deleted the text with it. ``download_as_text`` creates
    no tracker; a resource whose PDF download was attempted first has one
    without a path.
    """
    url = "https://example.com/placeholder-then-text.pdf"
    resource_id = world.add_resource(url, tracker=tracker)
    service = make_service(store, StubDownloader(), storage_mode="database")
    resource = db.session.get(ResearchResource, resource_id)
    service._record_failed_text_extraction(
        db.session, resource, error="PDF download failed: HTTP 503"
    )
    db.session.commit()
    service._save_text_with_db(
        resource,
        SAVED_TEXT,
        db.session,
        extraction_method="native_api",
        extraction_source="arxiv_api",
    )
    db.session.commit()
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        assert document.status is DocumentStatus.COMPLETED
        assert document.storage_mode == "database"
        assert document.file_path is None
        assert other.query(DocumentBlob).count() == 0

    library = make_library_service(db, store, monkeypatch)
    stats = library.sync_library_with_filesystem()

    assert stats["files_missing"] == 0
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document is not None, "the library sync deleted the document"
        assert document.status is DocumentStatus.COMPLETED
        assert document.text_content == SAVED_TEXT
        # Kept as it was: the sync does not rewrite the label.
        assert document.storage_mode == "database"
    view = library.get_document_by_id(document_id)
    assert view["has_text_db"] is True
    assert view["has_pdf"] is False


@pytest.mark.parametrize("shape", ["tracker-without-path", "duplicate-link"])
def test_database_row_without_blob_keeps_its_text(
    db, store, world, monkeypatch, shape
):
    """R3: an existing ``"database"`` row with no blob keeps its text.

    A PDF that the size limit refused in database mode left this row: a
    ``"database"`` label, no blob, the extracted text, and a downloaded
    tracker without a path. Nothing repairs such rows, so the sync must
    tolerate them. In ``duplicate-link`` a second research cites the same
    URL and downloads identical bytes. The content-hash dedup links it to
    this row and copies the row's ``"database"`` label into the shared
    tracker. A ``"database"`` tracker describes a row labelled as stored
    in the database, without a path, so the sync takes its tracker-path
    branch for this row instead, which keeps the text and the tracker
    too.
    """
    url = "https://example.com/refused-earlier.pdf"
    resource_id = world.add_resource(url, downloaded=True)
    document_id = str(uuid.uuid4())
    db.session.add(
        Document(
            id=document_id,
            source_type_id=world.source_type_id,
            resource_id=resource_id,
            research_id=world.research_id,
            document_hash=PDF_HASH,
            original_url=url,
            file_size=len(PDF_BYTES),
            file_type="pdf",
            mime_type="application/pdf",
            title="Sync Contract Paper",
            status=DocumentStatus.COMPLETED,
            storage_mode="database",
            file_path=None,
            text_content=SAVED_TEXT,
        )
    )
    db.session.add(
        DocumentCollection(
            document_id=document_id, collection_id=LIBRARY_COLLECTION_ID
        )
    )
    db.session.commit()
    expected_tracker_path = None
    if shape == "duplicate-link":
        duplicate_id = world.add_resource(url, tracker=False)
        service = make_service(store, StubDownloader(), storage_mode="database")
        monkeypatch.setattr(
            service, "_extract_text_from_pdf", lambda _data: None
        )
        assert service.download_resource(duplicate_id) == (True, None)
        expected_tracker_path = "database"
        with db.fresh() as other:
            linked = other.get(ResearchResource, duplicate_id)
            assert linked.document_id == document_id
            assert other.query(Document).count() == 1
            assert other.query(DocumentBlob).count() == 0
    with db.fresh() as other:
        assert find_tracker_by_url(other, url).file_path == (
            expected_tracker_path
        )

    library = make_library_service(db, store, monkeypatch)
    stats = library.sync_library_with_filesystem()

    assert stats["files_missing"] == 0
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document is not None, "the library sync deleted the document"
        assert document.text_content == SAVED_TEXT
        assert document.storage_mode == "database"
        assert _library_links(other, document_id) == 1
        # The tracker is left as it was, still marked downloaded.
        tracker = find_tracker_by_url(other, url)
        assert tracker.is_downloaded is True
        assert tracker.file_path == expected_tracker_path
    view = library.get_document_by_id(document_id)
    assert view["has_text_db"] is True
    assert view["has_pdf"] is False


def test_zotero_import_with_a_source_url_is_kept(db, store, monkeypatch):
    """A Zotero-imported PDF has a source URL but no download tracker.

    ``ZoteroSyncService`` stores the PDF blob through ``save_pdf`` in
    "database" mode and copies the item's URL (or DOI link) into
    ``original_url``, which puts the document in the sync's scope. With no
    tracker it took the no-path branch, which kept only "none"-mode and
    sentinel documents.
    """
    source_type = SourceType(
        id=str(uuid.uuid4()), name="zotero", display_name="Zotero"
    )
    db.session.add(source_type)
    db.session.flush()
    document = Document(
        id=str(uuid.uuid4()),
        source_type_id=source_type.id,
        document_hash=PDF_HASH,
        original_url="https://doi.org/10.1234/zotero-item",
        filename="Zotero Item.pdf",
        file_size=len(PDF_BYTES),
        file_type="pdf",
        mime_type="application/pdf",
        title="Zotero Item",
        status=DocumentStatus.COMPLETED,
        text_content=SAVED_TEXT,
        extraction_source="zotero",
    )
    db.session.add(document)
    db.session.flush()
    PDFStorageManager(store.root, "database").save_pdf(
        pdf_content=PDF_BYTES,
        document=document,
        session=db.session,
        filename="Zotero Item.pdf",
    )
    db.session.commit()
    document_id = document.id

    stats = make_library_service(
        db, store, monkeypatch
    ).sync_library_with_filesystem()

    assert stats["files_missing"] == 0
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document is not None, "the library sync deleted the document"
        assert document.storage_mode == "database"
        assert document.text_content == SAVED_TEXT
        assert (
            PDFStorageManager(store.root, "none").load_pdf(document, other)
            == PDF_BYTES
        )


# ===========================================================================
# A removed PDF file whose text is left: keep the text, re-download the PDF
# ===========================================================================


@pytest.mark.parametrize("via", ["research", "download-all-new"])
@pytest.mark.parametrize("tracker_path", ["kept", "lost"])
def test_removed_file_of_a_download_with_text_is_reported_and_downloaded_again(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    tracker_path,
    via,
):
    """The text stays, the missing PDF is reported and downloaded again.

    The usual filesystem-mode case: the download's real text extraction
    saved the PDF's text, then the user removed the PDF file from disk.
    The sync keeps the document, its text, its collection link and its
    chunks. It stores the document as text-only and resets the tracker, so
    the library no longer offers a PDF that is not there, and it reports
    the file missing. The download manager's research download and its
    "Download All New" both queue the document, and the download puts the
    PDF back into the same document. ``lost`` is the R1 sequence (a
    "none"-mode re-download cleared the tracker path) with the file removed
    afterwards, which reaches the sync's no-path branch.
    """
    url = "https://example.com/removed-but-extracted.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        own_path = document.file_path
        assert document.text_content == EXTRACTED_TEXT
    stored = store.root / own_path
    assert stored.is_file()
    library = make_library_service(db, store, monkeypatch)
    if tracker_path == "lost":
        assert library.mark_for_redownload([document_id]) == 1
        service.settings._values["research_library.pdf_storage_mode"] = "none"
        assert service.download_resource(resource_id) == (True, None)
        with db.fresh() as other:
            assert find_tracker_by_url(other, url).file_path is None
            assert other.get(Document, document_id).storage_mode == (
                "filesystem"
            )
    chunk_id = _add_chunk(db, document_id)

    # While the file is on disk, text or no text, nothing is missing.
    stats = library.sync_library_with_filesystem()
    assert (stats["files_found"], stats["files_missing"]) == (1, 0)
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.file_path == own_path
    assert library.get_document_by_id(document_id)["has_pdf"] is True

    stored.unlink()

    stats = library.sync_library_with_filesystem()

    assert stats["files_found"] == 0
    assert stats["files_missing"] == 1
    assert [entry["id"] for entry in stats["missing_files"]] == [document_id]
    assert stats["trackers_updated"] == 1
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document is not None, "the library sync deleted the document"
        assert document.status is DocumentStatus.COMPLETED
        assert document.text_content == EXTRACTED_TEXT
        # Stored the way text-only documents are.
        assert document.storage_mode == "none"
        assert document.file_path == FILE_PATH_TEXT_ONLY
        assert _library_links(other, document_id) == 1
        assert other.get(DocumentChunk, chunk_id) is not None
        tracker = find_tracker_by_url(other, url)
        assert tracker.is_downloaded is False
        assert tracker.file_path is None
    view = library.get_document_by_id(document_id)
    assert view["has_pdf"] is False
    assert view["has_text_db"] is True

    # The download manager offers the document again...
    service.settings._values["research_library.pdf_storage_mode"] = "filesystem"
    if via == "research":
        assert (
            service.queue_research_downloads(
                world.research_id, LIBRARY_COLLECTION_ID
            )
            == 1
        )
    else:
        queued = download_all_new(db, monkeypatch)
        assert queued["queued"] == 1
        assert queued["research_ids"] == [world.research_id]
    # ...and its download stores the PDF in the same document.
    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == (2 if tracker_path == "kept" else 3)
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert document.status is DocumentStatus.COMPLETED
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        assert other.get(DocumentChunk, chunk_id) is not None
        restored = store.root / document.file_path
        tracker = find_tracker_by_url(other, url)
        assert tracker.is_downloaded is True
        assert tracker.file_path == document.file_path
    assert restored.read_bytes() == TEXT_PDF_BYTES
    assert library.get_document_by_id(document_id)["has_pdf"] is True
    stats = library.sync_library_with_filesystem()
    assert (stats["files_found"], stats["files_missing"]) == (1, 0)


def test_pdf_still_stored_in_the_database_keeps_the_document_unchanged(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """A document whose PDF is still a blob is not missing its PDF.

    Downloaded in "database" mode and then again in "filesystem" mode,
    the document keeps its blob next to the new file, because the
    filesystem save does not remove it. When that file is removed the PDF
    is still stored, so the sync keeps the document, its text and its
    tracker unchanged. Making it text-only would hide the blob.
    """
    url = "https://example.com/blob-and-file.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="database")
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document_id = (
            other.query(Document).filter_by(resource_id=resource_id).one().id
        )
    library = make_library_service(db, store, monkeypatch)
    assert library.mark_for_redownload([document_id]) == 1
    service.settings._values["research_library.pdf_storage_mode"] = "filesystem"
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        assert other.query(DocumentBlob).count() == 1
        own_path = document.file_path
        assert find_tracker_by_url(other, url).file_path == own_path
    (store.root / own_path).unlink()

    stats = library.sync_library_with_filesystem()

    assert (stats["files_found"], stats["files_missing"]) == (1, 0)
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.file_path == own_path
        assert document.text_content == EXTRACTED_TEXT
        tracker = find_tracker_by_url(other, url)
        assert tracker.is_downloaded is True
        assert tracker.file_path == own_path
        assert (
            PDFStorageManager(store.root, "none").load_pdf(document, other)
            == TEXT_PDF_BYTES
        )


def test_resource_linked_by_content_in_another_research_is_queued_again(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """A second research's resource linked to the document queues it.

    A second research cites the same paper. Its download stores no
    duplicate: the content-hash dedup links its resource to the existing
    document through ``document_id``. Once the sync has made that document
    text-only, the second research's own "Download PDFs" queues the linked
    resource, and its download stores the PDF in the shared document.
    """
    url = "https://example.com/cited-twice.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(resource_id) == (True, None)
    second_research = ResearchHistory(
        id=str(uuid.uuid4()),
        query="second research citing the paper",
        mode="quick",
        status="completed",
        created_at="2026-08-02T00:00:00",
    )
    db.session.add(second_research)
    db.session.commit()
    second_research_id = second_research.id
    linked = ResearchResource(
        research_id=second_research_id,
        title="Sync Contract Paper",
        url=url,
        source_type="academic",
        created_at="2026-08-02T00:00:00",
    )
    db.session.add(linked)
    db.session.commit()
    linked_id = linked.id
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        document_id = document.id
        assert other.get(ResearchResource, linked_id).document_id == (
            document_id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1

    assert (
        service.queue_research_downloads(
            second_research_id, LIBRARY_COLLECTION_ID
        )
        == 1
    )
    assert service.download_resource(linked_id) == (True, None)
    assert len(downloader.urls) == 3
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        restored = store.root / document.file_path
    assert restored.read_bytes() == TEXT_PDF_BYTES


def test_redownload_that_stores_no_pdf_stops_asking_for_it(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """A download in "none" mode settles a document that needed its PDF.

    The storage mode keeps no PDF, so the download can only refresh the
    text. The document must then count as downloaded, like any "none"-mode
    download. If it still asked for its PDF, every "Download All New"
    would fetch it again.
    """
    url = "https://example.com/removed-then-none-mode.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        (store.root / document.file_path).unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1

    service.settings._values["research_library.pdf_storage_mode"] = "none"
    assert download_all_new(db, monkeypatch)["queued"] == 1
    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == 2

    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "none"
        assert document.file_path is None
        assert document.text_content == EXTRACTED_TEXT
        assert find_tracker_by_url(other, url).is_downloaded is True
    assert _tree(store.root) == set()
    assert download_all_new(db, monkeypatch)["queued"] == 0
    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 0
    )
    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == 2


@pytest.mark.parametrize("label", ["written-now", "written-before-the-fix"])
def test_failed_placeholder_completed_by_a_pdf_download_is_downloaded_again(
    db, store, world, one_download_session, filesystem_mode, monkeypatch, label
):
    """A placeholder that a PDF download completed gets its PDF back.

    A failed download leaves a placeholder labelled ``file_type``
    "unknown", and a later PDF download completes that placeholder with the
    PDF and its text. The download kept the "unknown" label. Once the PDF
    file was removed, the sync made the document text-only and reported
    the file missing, but no download path asked for its PDF again: they
    ask only for documents labelled "pdf". Such a download now labels the
    document "pdf" (``written-now``). A row a download completed before
    that (``written-before-the-fix``) is labelled by the sync when it makes
    the document text-only.
    """
    url = "https://example.com/placeholder-then-pdf.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    resource = db.session.get(ResearchResource, resource_id)
    service._record_failed_text_extraction(
        db.session, resource, error="PDF download failed: HTTP 503"
    )
    db.session.commit()
    with db.fresh() as other:
        placeholder = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = placeholder.id
        assert placeholder.status is DocumentStatus.FAILED
        assert placeholder.file_type == "unknown"

    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert document.status is DocumentStatus.COMPLETED
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        stored = store.root / document.file_path
    assert stored.is_file()
    library = make_library_service(db, store, monkeypatch)
    if label == "written-now":
        with db.fresh() as other:
            document = other.get(Document, document_id)
            assert (document.file_type, document.mime_type) == (
                "pdf",
                "application/pdf",
            )
        assert library.get_document_by_id(document_id)["is_pdf"] is True
    else:
        # The labels such a download left before it labelled the document.
        db.session.query(Document).filter_by(id=document_id).update(
            {Document.file_type: "unknown", Document.mime_type: None},
            synchronize_session=False,
        )
        db.session.commit()
    chunk_id = _add_chunk(db, document_id)
    stored.unlink()

    stats = library.sync_library_with_filesystem()

    assert [entry["id"] for entry in stats["missing_files"]] == [document_id]
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.text_content == EXTRACTED_TEXT
        assert document.storage_mode == "none"
        assert document.file_path == FILE_PATH_TEXT_ONLY
        assert document.file_type == "pdf"
        # The sync reports only documents the download paths ask for.
        assert document_awaits_pdf(document)

    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 1
    )
    assert download_all_new(db, monkeypatch)["queued"] == 1
    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == 2
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        assert other.get(DocumentChunk, chunk_id) is not None
        restored = store.root / document.file_path
    assert restored.read_bytes() == TEXT_PDF_BYTES
    assert download_all_new(db, monkeypatch)["queued"] == 0
    stats = library.sync_library_with_filesystem()
    assert (stats["files_found"], stats["files_missing"]) == (1, 0)


def test_regenerated_pdf_goes_back_into_the_document_that_awaits_it(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """A PDF downloaded again with other bytes refills the same document.

    Publishers and arXiv regenerate PDFs, so the PDF downloaded for a
    document that the sync made text-only can differ from the removed one.
    It still belongs in that document: the document stores no other PDF,
    and its text, chunks and collection links are the paper's. A second
    document for the resource would leave this one waiting for its PDF
    for good, with two completed documents for one resource. The document
    takes the new PDF's hash, as a new download would, when no other
    document owns those bytes. A collision must preserve both documents;
    the following test covers that case and a later content dedup hit.
    """
    url = "https://example.com/regenerated.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(resource_id) == (True, None)
    original_hash = hashlib.sha256(TEXT_PDF_BYTES).hexdigest()
    regenerated_hash = hashlib.sha256(REGENERATED_PDF_BYTES).hexdigest()
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        assert document.document_hash == original_hash
        stored = store.root / document.file_path
    chunk_id = _add_chunk(db, document_id)
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1

    downloader.content = REGENERATED_PDF_BYTES
    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 1
    )
    assert service.download_resource(resource_id) == (True, None)

    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert document.status is DocumentStatus.COMPLETED
        assert document.storage_mode == "filesystem"
        assert document.file_size == len(REGENERATED_PDF_BYTES)
        assert document.text_content == REGENERATED_TEXT
        assert document.document_hash == regenerated_hash
        assert other.get(DocumentChunk, chunk_id) is not None
        assert _library_links(other, document_id) == 1
        tracker = find_tracker_by_url(other, url)
        assert tracker.is_downloaded is True
        assert tracker.file_path == document.file_path
        restored = store.root / document.file_path
    assert restored.read_bytes() == REGENERATED_PDF_BYTES
    assert download_all_new(db, monkeypatch)["queued"] == 0
    stats = library.sync_library_with_filesystem()
    assert stats["files_missing"] == 0


@pytest.mark.parametrize("holder_storage", ["filesystem", "database"])
def test_colliding_refill_preserves_content_identity_for_later_downloads(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    retry_history,
    holder_storage,
):
    """Never put another document's bytes behind the missing PDF's hash.

    A failed refill must retain both documents and report failure. A later
    URL returning the original PDF must get those bytes, not the colliding
    PDF incorrectly stored under their hash. Use real downloads, sync,
    database rows, text extraction and PDF storage for the whole sequence.
    """
    url = "https://example.com/original.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(
        store,
        downloader,
        storage_mode="filesystem",
        retry_manager=retry_history,
    )
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        original_path = store.root / document.file_path
    chunk_id = _add_chunk(db, document_id)
    original_path.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1

    holder_url = "https://example.com/regenerated-elsewhere.pdf"
    holder_resource_id = world.add_resource(holder_url)
    downloader.content = REGENERATED_PDF_BYTES
    holder_service = make_service(
        store, downloader, storage_mode=holder_storage
    )
    assert holder_service.download_resource(holder_resource_id) == (True, None)
    with db.fresh() as other:
        holder = (
            other.query(Document)
            .filter_by(resource_id=holder_resource_id)
            .one()
        )
        holder_id = holder.id
        holder_path = holder.file_path
        tracker = find_tracker_by_url(other, url)
        tracker_before = (
            tracker.file_hash,
            tracker.file_path,
            tracker.file_size,
            tracker.downloaded_at,
        )
    holder_chunk_id = _add_chunk(db, holder_id)
    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 1
    )
    expected_error = "PDF refill conflicts with another library document"
    assert service.download_resource(resource_id) == (False, expected_error)

    with db.fresh() as other:
        assert other.query(Document).count() == 2
        document = other.get(Document, document_id)
        assert document_awaits_pdf(document)
        assert (
            document.document_hash == hashlib.sha256(TEXT_PDF_BYTES).hexdigest()
        )
        assert document.text_content == EXTRACTED_TEXT
        assert document.file_size == len(TEXT_PDF_BYTES)
        assert not original_path.exists()
        # PDF text persistence now pins the resource to the exact stored
        # document. Refusing a collision must preserve that explicit link.
        assert (
            other.get(ResearchResource, resource_id).document_id == document_id
        )
        assert other.get(DocumentChunk, chunk_id) is not None
        assert _library_links(other, document_id) == 1
        tracker = find_tracker_by_url(other, url)
        assert tracker.is_downloaded is False
        assert (
            tracker.file_hash,
            tracker.file_path,
            tracker.file_size,
            tracker.downloaded_at,
        ) == tracker_before
        attempts = (
            other.query(DownloadAttempt)
            .filter_by(url_hash=tracker.url_hash)
            .order_by(DownloadAttempt.id)
            .all()
        )
        assert [attempt.succeeded for attempt in attempts] == [True, False]
        assert attempts[-1].error_message == expected_error
        assert (
            other.query(DownloadQueue)
            .filter_by(resource_id=resource_id)
            .one()
            .status
            is DocumentStatus.FAILED
        )
        assert (
            other.query(ResourceDownloadStatus)
            .filter_by(resource_id=resource_id)
            .one()
            .status
            == "temporarily_failed"
        )

        holder = other.get(Document, holder_id)
        assert (
            holder.document_hash
            == hashlib.sha256(REGENERATED_PDF_BYTES).hexdigest()
        )
        assert holder.text_content == REGENERATED_TEXT
        assert holder.file_path == holder_path
        assert holder.storage_mode == holder_storage
        assert (
            PDFStorageManager(
                store.root, holder_storage, username=USERNAME
            ).load_pdf(holder, other)
            == REGENERATED_PDF_BYTES
        )
        assert other.get(DocumentChunk, holder_chunk_id) is not None
        assert _library_links(other, holder_id) == 1
        assert find_tracker_by_url(other, holder_url).is_downloaded is True

    mirror_resource_id = world.add_resource(
        "https://example.com/original-mirror.pdf"
    )
    downloader.content = TEXT_PDF_BYTES
    assert service.download_resource(mirror_resource_id) == (True, None)
    with db.fresh() as other:
        mirror = other.get(ResearchResource, mirror_resource_id)
        assert mirror.document_id == document_id
        document = other.get(Document, mirror.document_id)
        stored = PDFStorageManager(
            store.root, "filesystem", username=USERNAME
        ).load_pdf(document, other)
        assert stored == TEXT_PDF_BYTES
        assert hashlib.sha256(stored).hexdigest() == document.document_hash
        assert document.text_content == EXTRACTED_TEXT
        assert other.get(DocumentChunk, chunk_id) is not None
        assert other.query(Document).count() == 2


def test_text_extraction_leaves_a_document_that_awaits_its_pdf_alone(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """Text extraction does not queue a document that has its text.

    The sync made the document text-only after its PDF file was removed,
    and the PDF downloads ask for it again. A text extraction has nothing
    to add: the document holds its text, and the extraction never fetches
    the PDF that would settle it. Queued anyway, the document was reported
    as processed on every run, and a second research's resource linked to
    it went to the network-bound text extraction each time. The text mode
    skips both, as it did before the sync kept such documents, and the PDF
    mode still puts the PDF back.
    """
    url = "https://example.com/extract-text-after-removal.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(resource_id) == (True, None)
    second_research = ResearchHistory(
        id=str(uuid.uuid4()),
        query="second research citing the paper",
        mode="quick",
        status="completed",
        created_at="2026-08-02T00:00:00",
    )
    db.session.add(second_research)
    db.session.commit()
    second_research_id = second_research.id
    linked = ResearchResource(
        research_id=second_research_id,
        title="Sync Contract Paper",
        url=url,
        source_type="academic",
        created_at="2026-08-02T00:00:00",
    )
    db.session.add(linked)
    db.session.commit()
    linked_id = linked.id
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        document_id = document.id
        assert other.get(ResearchResource, linked_id).document_id == (
            document_id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    requests = len(downloader.urls)
    both = [world.research_id, second_research_id]

    for _ in range(2):
        events = bulk_download(
            db, store, downloader, monkeypatch, both, mode="text_only"
        )
        assert events[0]["total"] == 0, events
        assert events[-1]["complete"] is True
        assert events[-1]["error"] == (
            "No new papers to download for the selected research session(s)."
        )
    assert len(downloader.urls) == requests
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document_awaits_pdf(document)
        assert document.text_content == EXTRACTED_TEXT
        assert other.query(DownloadQueue).count() == 0

    events = bulk_download(
        db, store, downloader, monkeypatch, [world.research_id], mode="pdf"
    )
    assert events[0]["total"] == 1, events
    assert [event.get("status") for event in events[1:-1]] == ["success"]
    assert events[-1]["complete"] is True
    assert "error" not in events[-1]
    assert len(downloader.urls) == requests + 1
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        restored = store.root / document.file_path
    assert restored.read_bytes() == TEXT_PDF_BYTES


@pytest.mark.parametrize(
    "via",
    [
        "owner-research-first",
        "linked-research-first",
        "download-all-new",
        "linked-queued-earlier",
        "linked-queued-earlier-run-over-both",
        "download-all-new-linked-queued-earlier",
        "owner-not-downloadable",
    ],
)
def test_paper_cited_by_two_researches_is_downloaded_once(
    db, store, world, one_download_session, filesystem_mode, monkeypatch, via
):
    """One download puts back the PDF of a document two researches share.

    A second research cites the same paper. Its download stored no
    duplicate: the content-hash dedup linked its resource to the first
    research's document (``document_id``). Once the sync has made that
    document text-only, either resource refills it. A bulk run over both
    researches, or "Download All New" and the bulk run it starts, queued
    both. The first download put the PDF back. The second fetched it
    again, saved it under its own resource's file name and left the first
    file on disk with nothing pointing to it.

    One resource per such document is queued now, the document's own one
    whichever research the bulk run queues first, and the other resource
    counts as downloaded once the PDF is back. A linked resource that an
    earlier run left queued does not keep a run over the first research
    from queueing the document's own resource
    (``linked-queued-earlier``), and a run over both researches downloads
    that queued resource rather than queue a second one
    (``linked-queued-earlier-run-over-both``), as "Download All New" does
    (``download-all-new-linked-queued-earlier``). When the document's own
    resource cannot be queued (``owner-not-downloadable``), the linked one
    refills it.
    """
    url = "https://example.com/cited-by-two-researches.pdf"
    owner_url = (
        "https://example.com/landing/cited-by-two-researches"
        if via == "owner-not-downloadable"
        else url
    )
    owner_id = world.add_resource(owner_url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(owner_id) == (True, None)
    second_research_id, linked_id = _cite_in_another_research(db, url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        document_id = document.id
        assert document.resource_id == owner_id
        assert other.get(ResearchResource, linked_id).document_id == (
            document_id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    requests = len(downloader.urls)
    both = [world.research_id, second_research_id]

    refilled_by = owner_id
    if via == "download-all-new":
        queued = download_all_new(db, monkeypatch)
        assert (queued["queued"], queued["skipped"]) == (1, 1), queued
        assert queued["research_ids"] == [world.research_id]
        research_ids = queued["research_ids"]
    elif via == "owner-research-first":
        research_ids = both
    elif via == "linked-research-first":
        research_ids = both[::-1]
    elif "linked-queued-earlier" in via:
        # A run over the second research queued its resource and stopped
        # before downloading it.
        assert (
            service.queue_research_downloads(
                second_research_id, LIBRARY_COLLECTION_ID
            )
            == 1
        )
        if via == "linked-queued-earlier":
            research_ids = [world.research_id]
        elif via == "linked-queued-earlier-run-over-both":
            research_ids = both
            refilled_by = linked_id
        else:
            queued = download_all_new(db, monkeypatch)
            assert (queued["queued"], queued["skipped"]) == (1, 1), queued
            assert queued["research_ids"] == [second_research_id]
            research_ids = queued["research_ids"]
            refilled_by = linked_id
    else:
        research_ids = both[::-1]
        refilled_by = linked_id
    events = bulk_download(
        db, store, downloader, monkeypatch, research_ids, mode="pdf"
    )

    assert events[0]["total"] == 1, events
    assert [event.get("status") for event in events[1:-1]] == ["success"]
    assert len(downloader.urls) == requests + 1
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        restored = document.file_path
        downloaded = {
            entry.resource_id
            for entry in other.query(DownloadQueue).filter_by(
                status=DocumentStatus.COMPLETED
            )
        }
    assert downloaded == {refilled_by}
    # The one file in the library is the one the document names.
    assert _tree(store.root) == {restored}
    assert (store.root / restored).read_bytes() == TEXT_PDF_BYTES

    # The other resource counts as downloaded: no request, no second file,
    # also when a run downloads the entry an earlier run left queued.
    events = bulk_download(db, store, downloader, monkeypatch, both, mode="pdf")
    left_queued = via == "linked-queued-earlier"
    assert events[0]["total"] == (1 if left_queued else 0), events
    other_id = linked_id if refilled_by == owner_id else owner_id
    assert service.download_resource(other_id) == (True, None)
    assert len(downloader.urls) == requests + 1
    assert _tree(store.root) == {restored}
    assert download_all_new(db, monkeypatch)["queued"] == 0
    stats = library.sync_library_with_filesystem()
    assert (stats["files_found"], stats["files_missing"]) == (1, 0)


@pytest.mark.parametrize(
    "own_link",
    [False, True],
    ids=["own-resource-unlinked", "own-resource-linked-to-its-document"],
)
def test_research_citing_a_paper_twice_queues_the_documents_own_resource(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    own_link,
):
    """Within one research, the document's own resource is queued.

    A research cites the paper under two URLs, and the second download was
    linked to the first one's document by content. The linked resource
    comes first in the research, so the queueing met it before the
    document's own resource, queued it, and skipped the own one. The
    document's own resource is considered first now, also by a "Download
    PDFs" that only queues (no run downloads the queue right away), and
    also when it points to its own document through ``document_id``
    (``own-resource-linked-to-its-document``): the order keys on the
    document's owner, not on ``document_id``.
    """
    # Created first, downloaded second: linked to the other's document.
    linked_id = world.add_resource("https://example.com/twice/abstract.pdf")
    owner_id = world.add_resource("https://example.com/twice/paper.pdf")
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(owner_id) == (True, None)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        owner_document_id = document.id
        assert document.resource_id == owner_id
        assert other.get(ResearchResource, linked_id).document_id == (
            document.id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    if own_link:
        owner = db.session.get(ResearchResource, owner_id)
        owner.document_id = owner_document_id
        db.session.commit()

    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 1
    )
    with db.fresh() as other:
        queued = [entry.resource_id for entry in other.query(DownloadQueue)]
    assert queued == [owner_id]


def test_research_citing_a_paper_twice_queues_the_linked_one_if_the_own_failed(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    retry_history,
):
    """Within one research, a failing own resource leaves the PDF to the other.

    As in the case above, a research cites the paper under two URLs, and
    the second download was linked to the first one's document by content.
    Then the first URL stopped working, and a refill through it failed.
    "Download PDFs" for the research, which only queues, queued the failing
    resource again and skipped the linked one, which would have worked. It
    queues the linked resource now, and only that one.
    """
    linked_id = world.add_resource("https://example.com/twice/abstract.pdf")
    owner_url = "https://example.com/twice/paper.pdf"
    owner_id = world.add_resource(owner_url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(
        store,
        downloader,
        storage_mode="filesystem",
        retry_manager=retry_history,
    )
    assert service.download_resource(owner_id) == (True, None)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        assert document.resource_id == owner_id
        assert other.get(ResearchResource, linked_id).document_id == (
            document.id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    downloader.unreachable.add(owner_url)
    assert service.download_resource(owner_id) == (False, "Service unavailable")

    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 1
    )
    with db.fresh() as other:
        queued = [entry.resource_id for entry in other.query(DownloadQueue)]
    assert queued == [linked_id]


@pytest.mark.parametrize(
    "via",
    ["owner-research-first", "linked-research-first", "download-all-new"],
)
@pytest.mark.parametrize("owner", ["unreachable", "refused-by-egress-policy"])
def test_owner_that_cannot_succeed_leaves_the_document_to_a_linked_resource(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    retry_history,
    owner,
    via,
):
    """A linked resource refills the document when its own one cannot.

    A second research cites the paper under another URL, and its download
    was linked to the document by content. After the sync made the
    document text-only, the URL of the document's own resource stopped
    working (``unreachable``: a refill through it failed, and the retry
    manager's cooldown has passed since), or the egress policy now refuses
    it (``refused-by-egress-policy``). The document's one download still
    went to its own resource. A bulk run over both researches queued that
    resource again on every run and skipped the linked one, which would
    have worked. "Download All New" kept the own resource without asking
    the egress policy, so the bulk run it started fetched nothing. A
    resource that cannot succeed no longer takes the download from a
    viable one: the linked resource refills the document, in either order
    of the researches and through "Download All New".
    """
    owner_url = "https://example.com/owner-copy/paper.pdf"
    linked_url = "https://mirror.example.org/paper.pdf"
    owner_id = world.add_resource(owner_url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(
        store,
        downloader,
        storage_mode="filesystem",
        retry_manager=retry_history,
    )
    assert service.download_resource(owner_id) == (True, None)
    second_research_id, linked_id = _cite_in_another_research(db, linked_url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        document_id = document.id
        assert document.resource_id == owner_id
        assert other.get(ResearchResource, linked_id).document_id == (
            document_id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    both = [world.research_id, second_research_id]
    options = {"retry_manager": retry_history}
    if owner == "refused-by-egress-policy":
        options["refused_urls"] = {owner_url}
    else:
        downloader.unreachable.add(owner_url)
        # A refill through the document's own resource fails...
        events = bulk_download(
            db,
            store,
            downloader,
            monkeypatch,
            [world.research_id],
            mode="pdf",
            **options,
        )
        assert events[0]["total"] == 1, events
        assert [event.get("status") for event in events[1:-1]] == ["skipped"]
        # ...and the retry manager's cooldown passes.
        with db.fresh() as other:
            status = (
                other.query(ResourceDownloadStatus)
                .filter_by(resource_id=owner_id)
                .one()
            )
            assert status.status == "temporarily_failed"
            status.retry_after_timestamp = datetime.now(UTC) - timedelta(
                minutes=1
            )
            other.commit()
        assert retry_history.should_retry_resource(owner_id).can_retry
    requests = len(downloader.urls)

    if via == "download-all-new":
        queued = download_all_new(
            db, monkeypatch, retry_policy=retry_history, **options
        )
        assert (queued["queued"], queued["skipped"]) == (1, 1), queued
        assert queued["research_ids"] == [second_research_id]
        research_ids = queued["research_ids"]
    elif via == "owner-research-first":
        research_ids = both
    else:
        research_ids = both[::-1]
    events = bulk_download(
        db, store, downloader, monkeypatch, research_ids, mode="pdf", **options
    )

    assert events[0]["total"] == 1, events
    assert [event.get("status") for event in events[1:-1]] == ["success"]
    assert downloader.urls[requests:] == [linked_url]
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        restored = document.file_path
        downloaded = {
            entry.resource_id
            for entry in other.query(DownloadQueue).filter_by(
                status=DocumentStatus.COMPLETED
            )
        }
    assert downloaded == {linked_id}
    assert _tree(store.root) == {restored}
    assert (store.root / restored).read_bytes() == TEXT_PDF_BYTES

    # The document holds its PDF again, so nothing is queued or fetched.
    events = bulk_download(
        db, store, downloader, monkeypatch, both, mode="pdf", **options
    )
    assert events[0]["total"] == 0, events
    queued = download_all_new(
        db, monkeypatch, retry_policy=retry_history, **options
    )
    assert queued["queued"] == 0
    assert downloader.urls[requests:] == [linked_url]


@pytest.mark.parametrize(
    "via", ["owner-research-first", "linked-research-first"]
)
def test_owner_the_retry_manager_holds_back_leaves_the_document_to_a_linked_one(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    retry_history,
    via,
):
    """A linked resource refills the document while its own one is held back.

    The URL of the document's own resource failed three times today before
    a download through it succeeded. The retry manager records the
    success, so the latest attempt did not fail, but it still counts
    today's three attempts, its daily limit, and holds the resource back
    until tomorrow. A second research cites the paper under another URL,
    and its download was linked to the document by content. After the
    sync made the document text-only, a bulk run over both researches
    downloads the PDF through the linked resource, in either order of the
    researches, as it does when the own resource's latest attempt failed.
    """
    owner_url = "https://example.com/owner-copy/held-back.pdf"
    linked_url = "https://mirror.example.org/held-back.pdf"
    owner_id = world.add_resource(owner_url)
    downloader = StubDownloader(TEXT_PDF_BYTES, unreachable={owner_url})
    service = make_service(
        store,
        downloader,
        storage_mode="filesystem",
        retry_manager=retry_history,
    )
    for _ in range(3):
        assert service.download_resource(owner_id) == (
            False,
            "Service unavailable",
        )
    downloader.unreachable.clear()
    assert service.download_resource(owner_id) == (True, None)
    assert (
        retry_history.status_tracker.get_resource_status(owner_id)["status"]
        == "completed"
    )
    assert not retry_history.should_retry_resource(owner_id).can_retry
    second_research_id, linked_id = _cite_in_another_research(db, linked_url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        document_id = document.id
        assert document.resource_id == owner_id
        assert other.get(ResearchResource, linked_id).document_id == (
            document_id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    requests = len(downloader.urls)
    both = [world.research_id, second_research_id]

    events = bulk_download(
        db,
        store,
        downloader,
        monkeypatch,
        both if via == "owner-research-first" else both[::-1],
        mode="pdf",
        retry_manager=retry_history,
    )

    assert events[0]["total"] == 1, events
    assert [event.get("status") for event in events[1:-1]] == ["success"]
    assert downloader.urls[requests:] == [linked_url]
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        restored = document.file_path
        downloaded = {
            entry.resource_id
            for entry in other.query(DownloadQueue).filter_by(
                status=DocumentStatus.COMPLETED
            )
        }
    assert downloaded == {linked_id}
    assert _tree(store.root) == {restored}
    assert (store.root / restored).read_bytes() == TEXT_PDF_BYTES


@contextmanager
def logged_records():
    """The loguru records emitted inside the block, at every level.

    The package disables its loguru logging (``local_deep_research``'s
    ``__init__``), so it is enabled for the block and disabled again after.
    """
    records = []
    logger.enable("local_deep_research")
    sink = logger.add(lambda message: records.append(message.record))
    try:
        yield records
    finally:
        logger.remove(sink)
        logger.disable("local_deep_research")


def test_ranking_records_no_denial_of_the_egress_policy():
    """Ranking a URL the egress policy refuses records no denied fetch.

    ``refill_rank`` asks the egress policy whether a resource's URL could
    be queued, only to rank the resources that could refill a document.
    Each URL the policy refused was recorded as a denied fetch although
    nothing was fetched: a ``policy_audit`` warning from the policy, one
    from ``DownloadService``, and a count toward the run's denied-fetch
    quota, which refuses every URL once it is used up. The rank still
    treats the URL as one that cannot be queued, and records nothing. The
    check before a fetch records the denial as before.
    """
    service = make_service(_NO_STORE, StubDownloader(), storage_mode="none")
    context = EgressContext(
        scope=EgressScope.PRIVATE_ONLY,
        primary_engine="library",
        require_local_llm=False,
        require_local_embeddings=False,
    )
    service._egress_context = context
    # A public IP literal: classified without a DNS lookup, and refused
    # under PRIVATE_ONLY.
    refused_url = "https://8.8.8.8/papers/refused.pdf"
    warning = logger.level("WARNING").no

    def recorded(records):
        return [
            record["message"]
            for record in records
            if record["level"].no >= warning
            or record["extra"].get("policy_audit")
        ]

    # No armed run context, whose counter would take the denial instead.
    clear_active_context()
    with logged_records() as records:
        rank = service.refill_rank(1, refused_url, 1)
    assert rank.cannot_queue
    assert recorded(records) == []
    assert context._fetch_denial_count["count"] == 0

    with logged_records() as records:
        assert service._check_url_against_policy(refused_url) == (
            False,
            "scope_mismatch_private_only",
        )
    assert len(recorded(records)) == 2, recorded(records)
    assert context._fetch_denial_count["count"] == 1


@pytest.mark.parametrize("owner", ["unreachable", "refused-by-egress-policy"])
def test_queued_owner_that_cannot_succeed_does_not_hold_the_document(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    retry_history,
    owner,
):
    """A queued resource that cannot succeed does not hold the document.

    The document's own resource is already queued: "Download PDFs" for its
    research queued it again after a failed refill (``unreachable``), or it
    was queued before the egress policy came to refuse its URL
    (``refused-by-egress-policy``). A bulk run over both researches
    downloads that entry, and it skipped the second research's linked
    resource, so the document stayed without its PDF. The run queues the
    linked resource too now, and that one refills the document.
    """
    owner_url = "https://example.com/owner-copy/queued-paper.pdf"
    linked_url = "https://mirror.example.org/queued-paper.pdf"
    owner_id = world.add_resource(owner_url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(
        store,
        downloader,
        storage_mode="filesystem",
        retry_manager=retry_history,
    )
    assert service.download_resource(owner_id) == (True, None)
    second_research_id, linked_id = _cite_in_another_research(db, linked_url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        document_id = document.id
        assert other.get(ResearchResource, linked_id).document_id == (
            document_id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    options = {"retry_manager": retry_history}
    if owner == "unreachable":
        downloader.unreachable.add(owner_url)
        assert service.download_resource(owner_id) == (
            False,
            "Service unavailable",
        )
        assert (
            service.queue_research_downloads(
                world.research_id, LIBRARY_COLLECTION_ID
            )
            == 1
        )
        failed_requests = [owner_url]
    else:
        db.session.add(
            DownloadQueue(
                resource_id=owner_id,
                research_id=world.research_id,
                collection_id=LIBRARY_COLLECTION_ID,
                status=DocumentStatus.PENDING,
            )
        )
        db.session.commit()
        options["refused_urls"] = {owner_url}
        failed_requests = []
    requests = len(downloader.urls)

    events = bulk_download(
        db,
        store,
        downloader,
        monkeypatch,
        [world.research_id, second_research_id],
        mode="pdf",
        **options,
    )

    assert events[0]["total"] == 2, events
    assert [event.get("status") for event in events[1:-1]] == [
        "skipped",
        "success",
    ]
    assert downloader.urls[requests:] == [*failed_requests, linked_url]
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        restored = document.file_path
        entries = {
            entry.resource_id: entry.status
            for entry in other.query(DownloadQueue).all()
        }
    assert entries == {
        owner_id: DocumentStatus.FAILED,
        linked_id: DocumentStatus.COMPLETED,
    }
    assert _tree(store.root) == {restored}
    assert (store.root / restored).read_bytes() == TEXT_PDF_BYTES


def test_download_all_new_does_not_keep_a_queued_owner_whose_last_attempt_failed(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    retry_history,
):
    """A queued owner that failed last loses "Download All New" to a viable one.

    The document's own resource is queued: a refill through it failed, and
    "Download PDFs" for its research queued it again. The retry manager's
    cooldown has passed since, so the retry-policy filter of "Download All
    New" lets it through, but its latest attempt failed. "Download All New"
    kept a resource whose queue entry is PENDING over the second research's
    linked resource, which would have worked, so the run it started fetched
    the failing URL again, and the document stayed without its PDF. It
    keeps the linked resource now, as a bulk run over both researches does
    (see ``test_queued_owner_that_cannot_succeed_does_not_hold_the_document``),
    and that one refills the document.
    """
    owner_url = "https://example.com/owner-copy/queued-again.pdf"
    linked_url = "https://mirror.example.org/queued-again.pdf"
    owner_id = world.add_resource(owner_url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(
        store,
        downloader,
        storage_mode="filesystem",
        retry_manager=retry_history,
    )
    assert service.download_resource(owner_id) == (True, None)
    second_research_id, linked_id = _cite_in_another_research(db, linked_url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        document_id = document.id
        assert other.get(ResearchResource, linked_id).document_id == (
            document_id
        )
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    # A refill through the document's own resource fails, and "Download
    # PDFs" for its research queues it again...
    downloader.unreachable.add(owner_url)
    assert service.download_resource(owner_id) == (False, "Service unavailable")
    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 1
    )
    # ...and the retry manager's cooldown passes.
    with db.fresh() as other:
        status = (
            other.query(ResourceDownloadStatus)
            .filter_by(resource_id=owner_id)
            .one()
        )
        assert status.status == "temporarily_failed"
        status.retry_after_timestamp = datetime.now(UTC) - timedelta(minutes=1)
        other.commit()
    assert retry_history.should_retry_resource(owner_id).can_retry
    requests = len(downloader.urls)

    queued = download_all_new(
        db, monkeypatch, retry_policy=retry_history, retry_manager=retry_history
    )

    assert (queued["queued"], queued["skipped"]) == (1, 1), queued
    assert queued["research_ids"] == [second_research_id]
    events = bulk_download(
        db,
        store,
        downloader,
        monkeypatch,
        queued["research_ids"],
        mode="pdf",
        retry_manager=retry_history,
    )
    assert events[0]["total"] == 1, events
    assert [event.get("status") for event in events[1:-1]] == ["success"]
    assert downloader.urls[requests:] == [linked_url]
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        restored = document.file_path
        entries = {
            entry.resource_id: entry.status
            for entry in other.query(DownloadQueue).all()
        }
    # The owner's entry stays for a run over its research, which finds the
    # document holding its PDF again.
    assert entries == {
        owner_id: DocumentStatus.PENDING,
        linked_id: DocumentStatus.COMPLETED,
    }
    assert _tree(store.root) == {restored}
    assert (store.root / restored).read_bytes() == TEXT_PDF_BYTES


@pytest.mark.parametrize("via", ["bulk-run", "download-all-new"])
def test_document_refilled_by_another_stream_is_not_queued_again(
    db, store, world, one_download_session, filesystem_mode, monkeypatch, via
):
    """A document another download stream is refilling is not queued.

    The second research's linked resource is PROCESSING: a bulk run over
    that research has claimed it, at the time the claim records, and is
    downloading the PDF into the shared document. A run over the first
    research would queue the document's own resource and fetch the same
    PDF at the same time, which the check in ``download_resource`` cannot
    stop, since neither download has stored the PDF yet. The run leaves
    the document to that stream, and so does "Download All New": it keeps
    the claimed resource, which the bulk run it starts does not download
    again.
    """
    url = "https://example.com/refilled-elsewhere.pdf"
    owner_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(owner_id) == (True, None)
    second_research_id, linked_id = _cite_in_another_research(db, url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        stored = store.root / other.query(Document).one().file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    # The other stream's claim on the linked resource's queue entry.
    claim_queue_entry(db, linked_id, second_research_id)
    requests = len(downloader.urls)

    if via == "bulk-run":
        research_ids = [world.research_id]
    else:
        queued = download_all_new(db, monkeypatch)
        assert (queued["queued"], queued["skipped"]) == (1, 1), queued
        assert queued["research_ids"] == [second_research_id]
        research_ids = queued["research_ids"]
    events = bulk_download(
        db, store, downloader, monkeypatch, research_ids, mode="pdf"
    )

    assert events[0]["total"] == 0, events
    assert len(downloader.urls) == requests
    with db.fresh() as other:
        assert (
            other.query(DownloadQueue).filter_by(resource_id=owner_id).count()
            == 0
        )
        assert (
            other.query(DownloadQueue).filter_by(resource_id=linked_id).one()
        ).status is DocumentStatus.PROCESSING


@pytest.mark.parametrize("via", ["bulk-run", "download-all-new"])
@pytest.mark.parametrize("claimed_at", ["before-the-restart", "not-recorded"])
def test_download_cut_off_by_a_restart_does_not_hold_the_document(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    claimed_at,
    via,
):
    """A queue entry a restart left PROCESSING does not hold the document.

    A bulk run claimed the document's own resource and the server
    restarted during the download. Nothing reclaims the entry: it stays
    PROCESSING, and no reset touches it. It held the document as one
    being downloaded, so no run and no "Download All New" ever queued the
    second research's linked resource, and the document never got its PDF
    back. The claim records its time now, and an entry claimed before this
    server process started (``before-the-restart``), or before claims
    recorded their time (``not-recorded``), stands for no download. The
    linked resource refills the document, and the stranded entry is left
    as it is. A claim this process made still holds the document, see
    ``test_document_refilled_by_another_stream_is_not_queued_again``.
    """
    url = "https://example.com/cut-off-by-a-restart.pdf"
    owner_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(owner_id) == (True, None)
    second_research_id, linked_id = _cite_in_another_research(db, url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).one()
        document_id = document.id
        stored = store.root / document.file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    if claimed_at == "before-the-restart":
        claim_queue_entry(db, owner_id, world.research_id)
        # The server restarts: this process started after the claim.
        monkeypatch.setattr(
            library_utils,
            "_PROCESS_STARTED_AT",
            datetime.now(UTC) + timedelta(seconds=1),
        )
    else:
        # Claimed before claims recorded their time.
        db.session.add(
            DownloadQueue(
                resource_id=owner_id,
                research_id=world.research_id,
                collection_id=LIBRARY_COLLECTION_ID,
                status=DocumentStatus.PROCESSING,
            )
        )
        db.session.commit()
    requests = len(downloader.urls)
    both = [world.research_id, second_research_id]

    if via == "bulk-run":
        research_ids = both
    else:
        queued = download_all_new(db, monkeypatch)
        assert (queued["queued"], queued["skipped"]) == (1, 1), queued
        assert queued["research_ids"] == [second_research_id]
        research_ids = queued["research_ids"]
    events = bulk_download(
        db, store, downloader, monkeypatch, research_ids, mode="pdf"
    )

    assert events[0]["total"] == 1, events
    assert [event.get("status") for event in events[1:-1]] == ["success"]
    assert len(downloader.urls) == requests + 1
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == EXTRACTED_TEXT
        restored = document.file_path
        entries = {
            entry.resource_id: entry.status
            for entry in other.query(DownloadQueue).all()
        }
    assert entries == {
        owner_id: DocumentStatus.PROCESSING,
        linked_id: DocumentStatus.COMPLETED,
    }
    assert _tree(store.root) == {restored}
    assert (store.root / restored).read_bytes() == TEXT_PDF_BYTES


class _LookupOnlyRun(AbstractSet):
    """The research sessions of a bulk run, which may only be looked up."""

    def __init__(self, research_ids):
        self._ids = frozenset(research_ids)

    def __contains__(self, research_id):
        return research_id in self._ids

    def __len__(self):
        return len(self._ids)

    def __iter__(self):
        raise AssertionError(
            "queueing one research copied the research sessions of the run"
        )


def test_bulk_queueing_looks_the_runs_research_sessions_up(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """Queueing a research of a bulk run does not copy the run's sessions.

    ``download_bulk`` queues each research of the run with the run's
    research sessions, which decide which resource refills a document that
    awaits its PDF. It passed its list, and each call copied it into a new
    set, so queueing the research sessions of a run took time quadratic in
    their number, which a bulk request may name up to 100,000 of. The
    route builds one set for the run now, and each call only looks it up.
    """
    url = "https://example.com/run-of-two-researches.pdf"
    owner_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(owner_id) == (True, None)
    second_research_id, linked_id = _cite_in_another_research(db, url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        stored = store.root / other.query(Document).one().file_path
    stored.unlink()
    library = make_library_service(db, store, monkeypatch)
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    both = [world.research_id, second_research_id]

    run = _LookupOnlyRun(both)
    queued = [
        service.queue_research_downloads(
            research_id, LIBRARY_COLLECTION_ID, run_research_ids=run
        )
        for research_id in both[::-1]
    ]
    assert queued == [0, 1]
    with db.fresh() as other:
        assert [entry.resource_id for entry in other.query(DownloadQueue)] == [
            owner_id
        ]

    passed = []
    queue_research_downloads = DownloadService.queue_research_downloads

    def record(self, research_id, collection_id=None, **kwargs):
        passed.append(kwargs["run_research_ids"])
        return queue_research_downloads(
            self, research_id, collection_id, **kwargs
        )

    monkeypatch.setattr(DownloadService, "queue_research_downloads", record)
    events = bulk_download(db, store, downloader, monkeypatch, both, mode="pdf")
    assert events[0]["total"] == 1, events
    assert [event.get("status") for event in events[1:-1]] == ["success"]
    assert len(passed) == 2
    assert passed[0] is passed[1]
    assert isinstance(passed[0], AbstractSet)
    assert passed[0] == set(both)


def test_queueing_a_research_loads_no_text_of_the_documents_it_links_to(
    db, store, world, monkeypatch
):
    """Queueing reads the documents resources are linked to without text.

    A second research cites a paper the first one downloaded, and the
    content-hash dedup linked its resource to the first research's
    document. "Download PDFs" for the second research queues nothing for
    it, since the document holds its PDF. To consider each document's own
    resource first, the queueing loaded every document such a resource
    points to as a whole row, with its extracted text, which #4560 kept
    such scans from loading. It reads only the columns it compares now.
    This covers those lookups only: the check whether a resource already
    has a completed document of its own reads that document whole, as it
    did before.
    """
    url = "https://example.com/cited-again.pdf"
    owner_id = world.add_resource(url)
    service = make_service(
        store, StubDownloader(TEXT_PDF_BYTES), storage_mode="database"
    )
    assert service.download_resource(owner_id) == (True, None)
    second_research_id, linked_id = _cite_in_another_research(db, url)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        assert (
            other.get(ResearchResource, linked_id).document_id
            == other.query(Document).one().id
        )
    statements = []

    def record(conn, cursor, statement, parameters, context, executemany):
        statements.append(statement)

    event.listen(db.engine, "before_cursor_execute", record)
    try:
        queued = service.queue_research_downloads(
            second_research_id, LIBRARY_COLLECTION_ID
        )
    finally:
        event.remove(db.engine, "before_cursor_execute", record)

    assert queued == 0
    assert any(
        "JOIN documents" in s or "FROM documents" in s for s in statements
    )
    assert [s for s in statements if "text_content" in s] == []


def render_download_manager(db, monkeypatch):
    """The Download Manager page, as the browser gets it.

    Runs the ``download_manager_page`` route and renders its template
    against this database. The route's session and settings sources are
    replaced; the ``LibraryService`` it builds reads through the session
    that ``make_library_service`` set up. Importing the app registers the
    template globals that the page's base template uses.
    """
    from local_deep_research.web import fastapi_app  # noqa: F401

    monkeypatch.setattr(
        library_router, "get_user_db_session", lambda *a, **k: db.fresh()
    )
    monkeypatch.setattr(
        library_router,
        "get_settings_manager",
        lambda *a, **k: StubSettings(
            {"research_library.pdf_storage_mode": "filesystem"}
        ),
    )
    request = Request(
        {
            "type": "http",
            "method": "GET",
            "path": "/library/download-manager",
            "headers": [],
            "query_string": b"",
            "session": {"session_id": "sid", "username": USERNAME},
        }
    )
    response = library_router.download_manager_page(request, username=USERNAME)
    return response.body.decode()


def test_download_manager_counts_a_document_awaiting_its_pdf_as_available(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """The Download Manager offers the PDF the sync reported missing.

    The sync's result sends the user to the Download Manager to download
    the missing files again. Its counts took every completed document as
    downloaded, so a document awaiting its PDF showed there as "Already
    Downloaded", and nothing was "Available to Download", while "Download
    All New" queued it. Until its PDF is back it counts as available now,
    in the totals and in its research's row, and its research's sources
    show it under "Sources by Domain" as not downloaded and without the
    "Downloaded" badge. Those two compared the document's status with the
    string "completed", which a loaded status never equals, so they showed
    no source as downloaded, also once its PDF was back.
    """
    url = "https://example.com/counted-as-available.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(resource_id) == (True, None)
    library = make_library_service(db, store, monkeypatch)

    def counts():
        summary = library.get_download_manager_summary_stats()
        (research,) = library.get_research_list_with_stats()
        page = render_download_manager(db, monkeypatch)
        return {
            "already_downloaded": summary["already_downloaded"],
            "available_to_download": summary["available_to_download"],
            "research_downloaded": research["downloaded_count"],
            "research_downloadable": research["downloadable_count"],
            "sources_by_domain": [
                " ".join(stats.split())
                for stats in re.findall(
                    r'<span class="ldr-domain-stats">(.*?)</span>', page, re.S
                )
            ],
            "downloaded_badges": page.count(
                '<span class="ldr-badge ldr-badge-success">Downloaded</span>'
            ),
        }

    downloaded = {
        "already_downloaded": 1,
        "available_to_download": 0,
        "research_downloaded": 1,
        "research_downloadable": 1,
        "sources_by_domain": ["1 total | 1 PDFs | 1 downloaded"],
        "downloaded_badges": 1,
    }
    assert counts() == downloaded
    with db.fresh() as other:
        stored = store.root / other.query(Document).one().file_path
    stored.unlink()
    assert library.sync_library_with_filesystem()["files_missing"] == 1

    assert counts() == {
        "already_downloaded": 0,
        "available_to_download": 1,
        "research_downloaded": 0,
        "research_downloadable": 1,
        "sources_by_domain": ["1 total | 1 PDFs | 0 downloaded"],
        "downloaded_badges": 0,
    }
    assert download_all_new(db, monkeypatch)["queued"] == 1
    assert service.download_resource(resource_id) == (True, None)
    assert counts() == downloaded


def _post(path, payload):
    """A POST request whose JSON body is ``payload``."""
    body = json.dumps(payload).encode()

    async def receive():
        return {"type": "http.request", "body": body, "more_body": False}

    return Request(
        {
            "type": "http",
            "method": "POST",
            "path": path,
            "headers": [(b"content-type", b"application/json")],
            "query_string": b"",
            "session": {"session_id": "sid"},
        },
        receive,
    )


def source_routes(db, store, downloader, monkeypatch, research_id, url):
    """What the library's source routes report for ``url`` in a research.

    Runs ``get_research_sources`` and ``check_downloads``, and then
    ``download_source``, which downloads the resource unless it answers
    "Already downloaded", against this database. Their session and the
    user's password are replaced, and so is the ``DownloadService`` that
    ``download_source`` downloads with (a filesystem-mode service around
    ``downloader``). Which documents count as downloaded, and the queue
    entry, claim and download of the resource, are the production code.
    """
    monkeypatch.setattr(
        library_router, "get_user_db_session", lambda *a, **k: db.fresh()
    )
    monkeypatch.setattr(
        library_router, "get_authenticated_user_password", lambda *a, **k: "pw"
    )
    monkeypatch.setattr(
        library_router,
        "DownloadService",
        lambda *a, **k: make_service(
            store, downloader, storage_mode="filesystem"
        ),
    )
    (source,) = library_router.get_research_sources(
        None, research_id, username=USERNAME
    )["sources"]
    checked = asyncio.run(
        library_router.check_downloads(
            _post(
                "/library/api/check-downloads",
                {"research_id": research_id, "urls": [url]},
            ),
            username=USERNAME,
        )
    )["download_status"][url]
    answer = asyncio.run(
        library_router.download_source(
            _post(
                "/library/api/download-source",
                {"research_id": research_id, "url": url},
            ),
            username=USERNAME,
        )
    )
    return {
        "research_sources": (source["downloaded"], source["document_id"]),
        "check_downloads": (
            checked["downloaded"],
            checked.get("document_id"),
            checked.get("resource_id"),
        ),
        "download_source": answer["message"],
    }


def test_source_routes_report_a_download_as_the_download_manager_counts_it(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """The library's source routes follow the Download Manager's counts.

    ``get_research_sources`` and ``check_downloads`` report whether a
    research's source is downloaded, and ``download_source`` answers
    "Already downloaded" for a source that is and downloads any other.
    They compared the document's loaded status with the string
    "completed", which a ``DocumentStatus`` member never equals, so they
    reported every source as not downloaded, and ``download_source``
    queued and claimed a downloaded one again. They apply the Download
    Manager's rule now (``document_counts_as_downloaded``): a document that
    holds its PDF is downloaded, and one that awaits its PDF after the sync
    reported its file missing is not, so ``download_source`` puts the PDF
    back into it.
    """
    url = "https://example.com/reported-by-the-source-routes.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        stored = store.root / document.file_path
    library = make_library_service(db, store, monkeypatch)

    def routes():
        return source_routes(
            db, store, downloader, monkeypatch, world.research_id, url
        )

    downloaded = {
        "research_sources": (True, document_id),
        "check_downloads": (True, document_id, None),
        "download_source": "Already downloaded",
    }
    assert routes() == downloaded
    assert downloader.urls == [url]
    with db.fresh() as other:
        assert other.query(DownloadQueue).count() == 0

    stored.unlink()
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    with db.fresh() as other:
        assert document_awaits_pdf(other.get(Document, document_id))

    assert routes() == {
        "research_sources": (False, None),
        "check_downloads": (False, None, resource_id),
        "download_source": "Download completed",
    }
    # download_source put the PDF back into the same document, with one
    # request, and finished its queue entry.
    assert downloader.urls == [url, url]
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert not document_awaits_pdf(document)
        assert document.text_content == EXTRACTED_TEXT
        assert (store.root / document.file_path).read_bytes() == TEXT_PDF_BYTES
        (entry,) = other.query(DownloadQueue).all()
        assert (entry.resource_id, entry.status) == (
            resource_id,
            DocumentStatus.COMPLETED,
        )
    assert routes() == downloaded
    assert downloader.urls == [url, url]


def render_document_details(db, monkeypatch, document_id):
    """The document details page, as the browser gets it.

    Runs the ``document_details_page`` route and renders its template. The
    ``LibraryService`` it builds reads through the session that
    ``make_library_service`` set up. Importing the app registers the
    template globals that the page's base template uses.
    """
    from local_deep_research.web import fastapi_app  # noqa: F401

    request = Request(
        {
            "type": "http",
            "method": "GET",
            "path": f"/library/document/{document_id}",
            "headers": [],
            "query_string": b"",
            "session": {"session_id": "sid", "username": USERNAME},
        }
    )
    response = library_router.document_details_page(
        request, document_id, username=USERNAME
    )
    return response.body.decode()


@pytest.mark.parametrize("status", ["completed", "failed"])
def test_document_details_page_shows_the_download_status(
    db, store, world, monkeypatch, status
):
    """The details page's "Download Status" badge shows the status.

    The page compares the status it gets from ``get_document_by_id`` with
    the string 'completed' and otherwise prints it in a warning badge. It
    got the loaded ``DocumentStatus`` member, which never equals that
    string, so every document showed a warning badge reading
    "Documentstatus.completed" or "Documentstatus.failed". It gets the
    status's value now: a completed download shows "Completed", and a
    failed download "Failed".
    """
    url = f"https://example.com/details-of-a-{status}-download.pdf"
    resource_id = world.add_resource(url)
    service = make_service(store, StubDownloader(), storage_mode="database")
    if status == "completed":
        assert service.download_resource(resource_id) == (True, None)
    else:
        resource = db.session.get(ResearchResource, resource_id)
        service._record_failed_text_extraction(
            db.session, resource, error="PDF download failed: HTTP 503"
        )
        db.session.commit()
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        assert document.status is DocumentStatus(status)
    make_library_service(db, store, monkeypatch)

    page = render_document_details(db, monkeypatch, document_id)

    badges = re.findall(
        r'Download Status</div>\s*<div class="ldr-metadata-value">\s*'
        r'<span class="ldr-badge (ldr-badge-[a-z]+)">([^<]*)</span>',
        page,
    )
    assert badges == [
        ("ldr-badge-success", "Completed")
        if status == "completed"
        else ("ldr-badge-warning", "Failed")
    ]


def test_text_extraction_reads_the_stored_pdf_file_without_a_request(
    db, store, world, filesystem_mode, monkeypatch
):
    """Text extraction reads a downloaded PDF file before fetching it.

    Text extraction (``download_as_text``) tries the PDF file that a
    completed download stores (``_try_existing_pdf_extraction``) before it
    asks the source; only an arXiv paper's text is requested from arXiv
    before that. The step compared the document's loaded status with the
    string "completed", which a ``DocumentStatus`` member never equals, so
    it never read the file, and the text came from a request for the URL.
    A completed filesystem download without text, here one whose
    extraction found none when it was downloaded, now gets the text of its
    stored file, and no request is made.
    """
    url = "https://example.com/text-from-the-stored-file.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    # The PDF is stored under the library root that the step resolves its
    # path against.
    service.settings._values["research_library.storage_path"] = str(store.base)
    service._extract_text_from_pdf = lambda _data: None
    assert service.download_resource(resource_id) == (True, None)
    del service._extract_text_from_pdf
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        assert document.status is DocumentStatus.COMPLETED
        assert document.text_content is None
        assert (store.root / document.file_path).read_bytes() == TEXT_PDF_BYTES
    assert downloader.urls == [url]

    assert service.download_as_text(resource_id) == (True, None)

    assert downloader.urls == [url], "the text was fetched again"
    with db.fresh() as other:
        assert other.query(Document).count() == 1
        document = other.get(Document, document_id)
        assert document.text_content == EXTRACTED_TEXT
        assert (document.extraction_method, document.extraction_source) == (
            "pdf_extraction",
            "pdfplumber",
        )


def test_cached_text_extraction_uses_explicit_document_over_retained_owner(
    db, store, world, one_download_session, filesystem_mode, monkeypatch
):
    """A retained older PDF must not supply text for a relinked resource."""
    linked_id = world.add_resource("https://example.com/current-version.pdf")
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    service.settings._values["research_library.storage_path"] = str(store.base)
    original_extract = service._extract_text_from_pdf
    service._extract_text_from_pdf = lambda _data: None
    assert service.download_resource(linked_id) == (True, None)
    _, mirror_id = _cite_in_another_research(
        db, "https://mirror.example.org/old-version.pdf"
    )
    assert service.download_resource(mirror_id) == (True, None)
    with db.fresh() as session:
        old_id = session.get(ResearchResource, linked_id).document_id
        assert session.get(ResearchResource, mirror_id).document_id == old_id

    _, owner_id = _cite_in_another_research(
        db, "https://example.org/new-version.pdf"
    )
    downloader.content = REGENERATED_PDF_BYTES
    assert service.download_resource(owner_id) == (True, None)
    with db.fresh() as session:
        canonical_id = session.get(ResearchResource, owner_id).document_id
        assert canonical_id != old_id
    library = make_library_service(db, store, monkeypatch)
    assert library.mark_for_redownload([old_id]) == 1
    assert service.download_resource(linked_id) == (True, None)
    downloader.content = TEXT_PDF_BYTES
    service._extract_text_from_pdf = original_extract
    assert service.download_resource(mirror_id) == (True, None)
    with db.fresh() as session:
        old = session.get(Document, old_id)
        canonical = session.get(Document, canonical_id)
        assert old.resource_id == linked_id
        assert old.status is DocumentStatus.COMPLETED
        assert canonical.status is DocumentStatus.COMPLETED
        assert old.text_content == EXTRACTED_TEXT
        assert canonical.text_content is None
        assert session.get(ResearchResource, linked_id).document_id == (
            canonical_id
        )
        assert (store.root / old.file_path).read_bytes() == TEXT_PDF_BYTES
        assert (store.root / canonical.file_path).read_bytes() == (
            REGENERATED_PDF_BYTES
        )
    requests_before = list(downloader.urls)

    assert service.download_as_text(linked_id) == (True, None)

    assert downloader.urls == requests_before, "the cached PDF was not used"
    with db.fresh() as session:
        assert session.get(ResearchResource, linked_id).document_id == (
            canonical_id
        )
        assert session.get(Document, canonical_id).text_content == (
            REGENERATED_TEXT
        )
        assert session.get(Document, old_id).text_content == EXTRACTED_TEXT


@pytest.mark.parametrize("tracker_path", ["kept", "lost"])
def test_download_whose_resource_was_deleted_takes_its_pdf_back_by_content(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    tracker_path,
):
    """Without its resource, a removed PDF is cleared but not reported.

    A second research cites the same URL, and its download stores a
    regenerated PDF as a document of its own. Deleting that resource from
    its research keeps the document but clears its ``resource_id``. If the
    PDF file is then removed, no download path asks for the PDF again,
    since they start from a research's resources, so the sync does not
    report a file to download again. It keeps the document's text and
    makes it text-only, and resets the URL's tracker, as it does for a
    document that has its resource. Left naming the missing file, the
    document kept a later download of the same PDF bytes, which the
    content-hash dedup links to it, from storing the PDF: the dedup copied
    the dangling path into that download's tracker instead. ``lost`` is the
    R1 sequence (a "none"-mode re-download cleared the tracker path) before
    the resource was deleted, which reaches the sync's no-path branch.
    """
    url = "https://example.com/cited-twice-then-removed.pdf"
    first_id = world.add_resource(url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(first_id) == (True, None)
    _, second_id = _cite_in_another_research(db, url)
    downloader.content = REGENERATED_PDF_BYTES
    assert service.download_resource(second_id) == (True, None)
    with db.fresh() as other:
        document = other.query(Document).filter_by(resource_id=second_id).one()
        document_id = document.id
        own_path = document.file_path
        assert document.text_content == REGENERATED_TEXT
        assert find_tracker_by_url(other, url).file_path == own_path
    library = make_library_service(db, store, monkeypatch)
    if tracker_path == "lost":
        assert library.mark_for_redownload([document_id]) == 1
        service.settings._values["research_library.pdf_storage_mode"] = "none"
        assert service.download_resource(second_id) == (True, None)
        service.settings._values["research_library.pdf_storage_mode"] = (
            "filesystem"
        )
        with db.fresh() as other:
            assert find_tracker_by_url(other, url).file_path is None
            assert other.get(Document, document_id).file_path == own_path
    # As resource_service.delete_resource does: the row goes, its
    # documents stay with resource_id cleared.
    db.session.delete(db.session.get(ResearchResource, second_id))
    db.session.commit()
    with db.fresh() as other:
        assert other.get(Document, document_id).resource_id is None
    (store.root / own_path).unlink()
    chunk_id = _add_chunk(db, document_id)
    requests = len(downloader.urls)

    stats = library.sync_library_with_filesystem()

    assert (stats["files_found"], stats["files_missing"]) == (2, 0)
    assert stats["missing_files"] == []
    assert stats["trackers_updated"] == 1
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document is not None, "the library sync deleted the document"
        assert document.text_content == REGENERATED_TEXT
        assert document.storage_mode == "none"
        assert document.file_path == FILE_PATH_TEXT_ONLY
        assert not document_awaits_pdf(document)
        assert other.get(DocumentChunk, chunk_id) is not None
        tracker = find_tracker_by_url(other, url)
        assert tracker.is_downloaded is False
        assert tracker.file_path is None
    view = library.get_document_by_id(document_id)
    assert view["has_pdf"] is False
    assert view["has_text_db"] is True
    # Nothing asks for its PDF...
    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 0
    )
    assert download_all_new(db, monkeypatch)["queued"] == 0
    assert len(downloader.urls) == requests

    # ...but a later download of the same PDF for another resource stores
    # it in this document.
    _, third_id = _cite_in_another_research(
        db, url, query="third research citing the paper"
    )
    assert service.download_resource(third_id) == (True, None)
    assert len(downloader.urls) == requests + 1
    with db.fresh() as other:
        assert other.query(Document).count() == 2
        assert other.get(ResearchResource, third_id).document_id == (
            document_id
        )
        document = other.get(Document, document_id)
        assert document.storage_mode == "filesystem"
        assert document.text_content == REGENERATED_TEXT
        assert other.get(DocumentChunk, chunk_id) is not None
        restored = store.root / document.file_path
        assert find_tracker_by_url(other, url).file_path == document.file_path
    assert restored.read_bytes() == REGENERATED_PDF_BYTES
    assert library.get_document_by_id(document_id)["has_pdf"] is True
    stats = library.sync_library_with_filesystem()
    assert (stats["files_found"], stats["files_missing"]) == (2, 0)


# ===========================================================================
# Documents that share their URL's tracker
# ===========================================================================


@pytest.mark.parametrize("first", ["download", "placeholder"])
def test_placeholder_sharing_the_url_of_a_removed_file_is_left_as_it_is(
    db, store, world, one_download_session, filesystem_mode, monkeypatch, first
):
    """Only the document whose file was removed is reported missing.

    A URL has one tracker, however many documents have that URL. The first
    research downloaded the PDF into its document, and the tracker names
    that document's file. A second research's download of the same URL
    failed, and "Extract Text to DB" completed its placeholder with text:
    no PDF, no file path, the ``"database"`` column default and no blob.
    Once the first document's file was removed, a sync that reached the
    placeholder first judged it by the tracker's path. It labelled the
    placeholder a PDF, made it text-only and reported the other document's
    file missing for it, and the download manager then downloaded the PDF
    a second time, into the placeholder. The removed file was counted
    twice. The sync judges each document by what it stores itself, so the
    placeholder is left as it is whichever document the sync reaches first
    (``first``).
    """
    url = "https://example.com/shared-with-a-text-placeholder.pdf"
    owner_id = world.add_resource(url)
    _, placeholder_resource_id = _cite_in_another_research(db, url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    placeholder_resource = db.session.get(
        ResearchResource, placeholder_resource_id
    )

    def record_failed_download():
        service._record_failed_text_extraction(
            db.session,
            placeholder_resource,
            error="PDF download failed: HTTP 503",
        )
        db.session.commit()

    if first == "placeholder":
        record_failed_download()
    assert service.download_resource(owner_id) == (True, None)
    if first == "download":
        record_failed_download()
    service._save_text_with_db(
        placeholder_resource,
        SAVED_TEXT,
        db.session,
        extraction_method="native_api",
        extraction_source="arxiv_api",
    )
    db.session.commit()
    with db.fresh() as other:
        downloaded = other.query(Document).filter_by(resource_id=owner_id).one()
        downloaded_id = downloaded.id
        own_path = downloaded.file_path
        assert downloaded.text_content == EXTRACTED_TEXT
        placeholder = (
            other.query(Document)
            .filter_by(resource_id=placeholder_resource_id)
            .one()
        )
        placeholder_id = placeholder.id
        assert placeholder.status is DocumentStatus.COMPLETED
        assert placeholder.text_content == SAVED_TEXT
        placeholder_shape = (
            placeholder.file_type,
            placeholder.mime_type,
            placeholder.storage_mode,
            placeholder.file_path,
        )
        assert placeholder_shape == ("unknown", None, "database", None)
        assert other.query(DocumentBlob).count() == 0
        assert find_tracker_by_url(other, url).file_path == own_path
        # The order in which the sync reads the documents.
        assert [
            document.id
            for document in other.query(Document)
            .filter_by(status=DocumentStatus.COMPLETED)
            .filter(Document.original_url.isnot(None))
        ] == (
            [downloaded_id, placeholder_id]
            if first == "download"
            else [placeholder_id, downloaded_id]
        )
    (store.root / own_path).unlink()
    library = make_library_service(db, store, monkeypatch)

    stats = library.sync_library_with_filesystem()

    assert (stats["files_found"], stats["files_missing"]) == (1, 1)
    assert [
        (entry["id"], entry["path"]) for entry in stats["missing_files"]
    ] == [(downloaded_id, own_path)]
    assert stats["trackers_updated"] == 1
    with db.fresh() as other:
        placeholder = other.get(Document, placeholder_id)
        assert placeholder.text_content == SAVED_TEXT
        assert (
            placeholder.file_type,
            placeholder.mime_type,
            placeholder.storage_mode,
            placeholder.file_path,
        ) == placeholder_shape
        assert not document_awaits_pdf(placeholder)
        downloaded = other.get(Document, downloaded_id)
        assert downloaded.text_content == EXTRACTED_TEXT
        assert document_awaits_pdf(downloaded)

    # The download manager downloads the PDF once, into the document whose
    # file was removed.
    queued = download_all_new(db, monkeypatch)
    assert queued["queued"] == 1, queued
    assert queued["research_ids"] == [world.research_id]
    assert service.download_resource(owner_id) == (True, None)
    assert service.download_resource(placeholder_resource_id) == (True, None)
    assert len(downloader.urls) == 2
    with db.fresh() as other:
        restored = other.get(Document, downloaded_id).file_path
        placeholder = other.get(Document, placeholder_id)
        assert placeholder.file_path is None
        assert placeholder.text_content == SAVED_TEXT
    assert _tree(store.root) == {restored}
    stats = library.sync_library_with_filesystem()
    assert (stats["files_found"], stats["files_missing"]) == (2, 0)


@pytest.mark.parametrize("text", [True, False], ids=["with-text", "no-text"])
@pytest.mark.parametrize("tracker_path", ["other-file", "sentinel"])
def test_removed_file_is_reported_although_the_url_tracker_names_another(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    tracker_path,
    text,
):
    """A document whose own file was removed is reported missing.

    Two researches cite the same URL, and each download stored its PDF in
    a document of its own: the second research got a regenerated PDF with
    other bytes, so the content-hash dedup did not link it to the first
    research's document. The URL's one tracker names the file stored
    last, the second research's (``other-file``). When the first
    research's file was removed, the sync judged the first document by
    the tracker's file, which is still there, and counted it found:
    nothing reported its PDF missing, and no download path asked for it
    again. A tracker that a content-hash duplicate download set to a
    sentinel such as ``blob_deleted``, before such downloads stopped
    copying sentinels, made the sync count every document of the URL found
    in the same way (``sentinel``). The sync judges the first document by
    its own file now. With its text left (``with-text``) it is made
    text-only and reported, and the download manager puts its PDF back.
    With nothing else left (``no-text``) it is deleted and reported, so
    that its URL can be downloaded again. The second research's document
    and the tracker are left as they are.
    """
    url = "https://example.com/two-copies-one-tracker.pdf"
    first_id = world.add_resource(url)
    content = TEXT_PDF_BYTES if text else PDF_BYTES
    downloader = StubDownloader(content)
    service = make_service(store, downloader, storage_mode="filesystem")
    assert service.download_resource(first_id) == (True, None)
    _, second_id = _cite_in_another_research(db, url)
    downloader.content = REGENERATED_PDF_BYTES
    assert service.download_resource(second_id) == (True, None)
    with db.fresh() as other:
        first = other.query(Document).filter_by(resource_id=first_id).one()
        first_document_id = first.id
        first_path = first.file_path
        assert first.text_content == (EXTRACTED_TEXT if text else None)
        second = other.query(Document).filter_by(resource_id=second_id).one()
        second_document_id = second.id
        second_path = second.file_path
        assert second_path != first_path
        assert find_tracker_by_url(other, url).file_path == second_path
    if tracker_path == "sentinel":
        db.session.query(DownloadTracker).filter_by(
            url_hash=get_url_hash(url)
        ).update(
            {DownloadTracker.file_path: FILE_PATH_BLOB_DELETED},
            synchronize_session=False,
        )
        db.session.commit()
    expected_tracker = (
        second_path if tracker_path == "other-file" else FILE_PATH_BLOB_DELETED,
        True,
    )
    (store.root / first_path).unlink()
    library = make_library_service(db, store, monkeypatch)

    stats = library.sync_library_with_filesystem()

    assert (stats["files_found"], stats["files_missing"]) == (1, 1)
    assert [
        (entry["id"], entry["path"]) for entry in stats["missing_files"]
    ] == [(first_document_id, first_path)]
    assert stats["trackers_updated"] == 0
    with db.fresh() as other:
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == expected_tracker
        second = other.get(Document, second_document_id)
        assert (second.storage_mode, second.file_path) == (
            "filesystem",
            second_path,
        )
        first = other.get(Document, first_document_id)
        if text:
            assert first.text_content == EXTRACTED_TEXT
            assert first.file_path == FILE_PATH_TEXT_ONLY
            assert document_awaits_pdf(first)
        else:
            assert first is None, "nothing of the document was left"
    assert (store.root / second_path).read_bytes() == REGENERATED_PDF_BYTES

    # The download manager downloads the first research's PDF again.
    downloader.content = content
    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 1
    )
    assert service.download_resource(first_id) == (True, None)
    with db.fresh() as other:
        first = other.query(Document).filter_by(resource_id=first_id).one()
        assert (first.id == first_document_id) is text
        restored = first.file_path
    assert (store.root / restored).read_bytes() == content
    stats = library.sync_library_with_filesystem()
    assert (stats["files_found"], stats["files_missing"]) == (2, 0)


@pytest.mark.parametrize("first", ["deleted", "kept"])
def test_tracker_reset_for_two_documents_counts_once_in_either_order(
    db, store, world, one_download_session, filesystem_mode, monkeypatch, first
):
    """``trackers_updated`` counts a tracker once, whichever document is first.

    Two researches cite the same URL, and each download stored its PDF in
    a document of its own (other bytes, so the content-hash dedup did not
    link them). The URL's one tracker names the file of the first
    research's document, which has no text, and both files were removed.
    The sync deletes that document and resets the tracker, and makes the
    second research's document, which has its text, text-only and reports
    it. When the sync read the deleted document first (``deleted``), it
    reached the other one with the tracker reset already, reset it again
    and counted it twice. In the other order (``kept``) the tracker still
    named the first research's file when the sync reached the second
    document, so it did not describe that document and was left alone
    there, and the deletion counted it once. Either order counts it once
    now, and the documents and the tracker end the same.
    """
    url = "https://example.com/one-tracker-reset-twice.pdf"
    deleted_resource_id = world.add_resource(url)
    _, kept_resource_id = _cite_in_another_research(db, url)
    downloader = StubDownloader()
    service = make_service(store, downloader, storage_mode="filesystem")

    def download(resource_id, content):
        downloader.content = content
        assert service.download_resource(resource_id) == (True, None)

    # The sync reads the documents in the order they were stored.
    if first == "deleted":
        download(deleted_resource_id, PDF_BYTES)
        download(kept_resource_id, TEXT_PDF_BYTES)
    else:
        download(kept_resource_id, TEXT_PDF_BYTES)
        download(deleted_resource_id, PDF_BYTES)
    with db.fresh() as other:
        deleted = (
            other.query(Document)
            .filter_by(resource_id=deleted_resource_id)
            .one()
        )
        deleted_id, deleted_path = deleted.id, deleted.file_path
        assert deleted.text_content is None
        kept = (
            other.query(Document).filter_by(resource_id=kept_resource_id).one()
        )
        kept_id, kept_path = kept.id, kept.file_path
        assert kept.text_content == EXTRACTED_TEXT
        read_order = [
            document.id
            for document in other.query(Document)
            .filter_by(status=DocumentStatus.COMPLETED)
            .filter(Document.original_url.isnot(None))
        ]
        assert read_order == (
            [deleted_id, kept_id]
            if first == "deleted"
            else [kept_id, deleted_id]
        )
    # The tracker names the file written last. In both orders it names the
    # first research's file here, as when that file was written last: with
    # that file's content hash, which identifies the document it names.
    db.session.query(DownloadTracker).filter_by(
        url_hash=get_url_hash(url)
    ).update(
        {
            DownloadTracker.file_path: deleted_path,
            DownloadTracker.file_hash: PDF_HASH,
            DownloadTracker.is_downloaded: True,
        },
        synchronize_session=False,
    )
    db.session.commit()
    (store.root / deleted_path).unlink()
    (store.root / kept_path).unlink()
    library = make_library_service(db, store, monkeypatch)

    stats = library.sync_library_with_filesystem()

    assert (stats["files_found"], stats["files_missing"]) == (0, 2)
    assert sorted(
        (entry["id"], entry["path"]) for entry in stats["missing_files"]
    ) == sorted([(deleted_id, deleted_path), (kept_id, kept_path)])
    assert stats["trackers_updated"] == 1
    with db.fresh() as other:
        assert other.get(Document, deleted_id) is None
        kept = other.get(Document, kept_id)
        assert kept.text_content == EXTRACTED_TEXT
        assert document_awaits_pdf(kept)
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == (None, False)


@pytest.mark.parametrize("first", ["empty", "second"])
@pytest.mark.parametrize("holds", ["blob", "text", "nothing"])
def test_database_documents_of_one_url_are_each_judged_by_their_own_blob(
    db, store, world, monkeypatch, holds, first
):
    """Each database document of a URL is judged by what it stores itself.

    Three researches cite the same paper. The first research's document is
    a database-mode download that holds nothing (see
    ``_add_database_row_without_pdf``). The third research downloaded the
    same bytes in database mode, and the content-hash dedup linked its
    resource to that empty document and recorded ``"database"`` in the
    URL's one tracker. The second research's document is a second one: a
    database-mode download of a regenerated PDF with other bytes, which
    holds that PDF as a blob (``blob``) or holds nothing either, the size
    limit having refused that PDF too (``nothing``), or a failed-download
    placeholder that "Extract Text to DB" completed, which holds only text
    (``text``). Both documents are labelled as stored in the database,
    without a path, so the tracker describes both, and the sync judges
    each by its own blob and text, whichever it reads first (``first``):
    it deletes and reports, as ``"database"``, each document that holds
    nothing, and keeps the other one as it is. The tracker records that
    the URL's PDF is stored in the database. While the second document
    holds a blob, that is still true and the tracker is left as it is.
    Otherwise no document holds the PDF any more, and the tracker is reset
    once. When the tracker counted only for a document storing the same
    path, it described neither document: the documents that held nothing
    were deleted without being reported, and the tracker was never reset.
    """
    url = "https://example.com/two-database-documents.pdf"
    empty_resource_id = world.add_resource(url, downloaded=True)
    second_research_id, second_resource_id = _cite_in_another_research(db, url)
    _, linked_resource_id = _cite_in_another_research(
        db, url, query="third research citing the paper"
    )
    downloader = StubDownloader(REGENERATED_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="database")
    monkeypatch.setattr(service, "_extract_text_from_pdf", lambda _data: None)

    def add_second_document():
        if holds == "blob":
            assert service.download_resource(second_resource_id) == (True, None)
        elif holds == "text":
            resource = db.session.get(ResearchResource, second_resource_id)
            service._record_failed_text_extraction(
                db.session, resource, error="PDF download failed: HTTP 503"
            )
            db.session.commit()
            service._save_text_with_db(
                resource,
                SAVED_TEXT,
                db.session,
                extraction_method="native_api",
                extraction_source="arxiv_api",
            )
            db.session.commit()
        else:
            _add_database_row_without_pdf(
                db,
                world,
                second_resource_id,
                second_research_id,
                url,
                REGENERATED_PDF_BYTES,
            )

    if first == "second":
        add_second_document()
    empty_id = _add_database_row_without_pdf(
        db, world, empty_resource_id, world.research_id, url, PDF_BYTES
    )
    if first == "empty":
        add_second_document()
    downloader.content = PDF_BYTES
    assert service.download_resource(linked_resource_id) == (True, None)
    with db.fresh() as other:
        linked = other.get(ResearchResource, linked_resource_id)
        assert linked.document_id == empty_id
        second = (
            other.query(Document)
            .filter_by(resource_id=second_resource_id)
            .one()
        )
        second_id = second.id
        second_shape = (
            second.file_type,
            second.storage_mode,
            second.file_path,
            second.text_content,
        )
        assert second_shape[1:] == (
            "database",
            None,
            SAVED_TEXT if holds == "text" else None,
        )
        assert [row.document_id for row in other.query(DocumentBlob)] == (
            [second_id] if holds == "blob" else []
        )
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == ("database", True)
        # The order in which the sync reads the documents.
        read_order = [
            document.id
            for document in other.query(Document)
            .filter_by(status=DocumentStatus.COMPLETED)
            .filter(Document.original_url.isnot(None))
        ]
        assert read_order == (
            [empty_id, second_id] if first == "empty" else [second_id, empty_id]
        )
    library = make_library_service(db, store, monkeypatch)

    stats = library.sync_library_with_filesystem()

    second_kept = holds != "nothing"
    assert (stats["files_found"], stats["files_missing"]) == (
        (1, 1) if second_kept else (0, 2)
    )
    assert [
        (entry["id"], entry["path"]) for entry in stats["missing_files"]
    ] == [
        (document_id, "database")
        for document_id in read_order
        if document_id == empty_id or not second_kept
    ]
    assert stats["trackers_updated"] == (0 if holds == "blob" else 1)
    with db.fresh() as other:
        assert other.get(Document, empty_id) is None
        second = other.get(Document, second_id)
        if second_kept:
            assert (
                second.file_type,
                second.storage_mode,
                second.file_path,
                second.text_content,
            ) == second_shape
        else:
            assert second is None, "nothing of the document was left"
        if holds == "blob":
            assert (
                PDFStorageManager(store.root, "none").load_pdf(second, other)
                == REGENERATED_PDF_BYTES
            )
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == (
            ("database", True) if holds == "blob" else (None, False)
        )


def test_document_stored_without_a_pdf_is_not_judged_by_a_database_tracker(
    db, store, world, monkeypatch
):
    """A ``"database"`` tracker does not describe a document without a PDF.

    The first research downloaded the paper with PDF storage set to
    "none", so its document stores no PDF, and the PDF had no text to
    extract. The second research got a regenerated PDF with other bytes in
    database mode, stored as a blob in a document of its own, and the
    URL's tracker records ``"database"``. That tracker describes only
    documents labelled as stored in the database. The first document is
    left alone, as every document stored without a PDF is, and not deleted
    for lacking a blob it never had.
    """
    url = "https://example.com/stored-without-a-pdf.pdf"
    first_id = world.add_resource(url)
    _, second_id = _cite_in_another_research(db, url)
    downloader = StubDownloader(PDF_BYTES)
    service = make_service(store, downloader, storage_mode="none")
    monkeypatch.setattr(service, "_extract_text_from_pdf", lambda _data: None)
    assert service.download_resource(first_id) == (True, None)
    downloader.content = REGENERATED_PDF_BYTES
    service.settings._values["research_library.pdf_storage_mode"] = "database"
    assert service.download_resource(second_id) == (True, None)
    with db.fresh() as other:
        first = other.query(Document).filter_by(resource_id=first_id).one()
        first_document_id = first.id
        first_shape = (first.storage_mode, first.file_path, first.text_content)
        assert first_shape == ("none", None, None)
        second = other.query(Document).filter_by(resource_id=second_id).one()
        second_document_id = second.id
        assert (second.storage_mode, second.file_path) == ("database", None)
        assert [row.document_id for row in other.query(DocumentBlob)] == [
            second_document_id
        ]
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == ("database", True)
    library = make_library_service(db, store, monkeypatch)

    stats = library.sync_library_with_filesystem()

    assert (stats["files_found"], stats["files_missing"]) == (2, 0)
    assert stats["missing_files"] == []
    assert stats["trackers_updated"] == 0
    with db.fresh() as other:
        first = other.get(Document, first_document_id)
        assert first is not None, "the library sync deleted the document"
        assert (
            first.storage_mode,
            first.file_path,
            first.text_content,
        ) == first_shape
        assert other.get(Document, second_document_id) is not None
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == ("database", True)


# ===========================================================================
# "Remove PDF to save space" is not a missing file
# ===========================================================================


@pytest.mark.parametrize("text", [True, False], ids=["with-text", "no-text"])
@pytest.mark.parametrize("stored_mode", ["database", "filesystem"])
def test_pdf_removed_to_save_space_is_kept_and_not_downloaded_again(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    stored_mode,
    text,
):
    """``delete_blob_only`` followed by a sync keeps the document as it is.

    "Remove PDF to save space" removes the stored PDF, blob or file, and
    marks the document ``blob_deleted``, but leaves the tracker naming the
    copy it removed. The sync resolved that name, found nothing, and
    deleted a document without text: its row, metadata and collection
    link were lost and the tracker was reset, so the download manager
    offered the PDF the user had removed for download again. A document
    with text must not be taken for one whose PDF went missing either.
    Neither the sync nor any download path brings the removed PDF back.
    """
    url = "https://example.com/removed-to-save-space.pdf"
    resource_id = world.add_resource(url)
    content = TEXT_PDF_BYTES if text else PDF_BYTES
    downloader = StubDownloader(content)
    service = make_service(store, downloader, storage_mode=stored_mode)
    if not text:
        monkeypatch.setattr(
            service, "_extract_text_from_pdf", lambda _data: None
        )
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document_id = (
            other.query(Document).filter_by(resource_id=resource_id).one().id
        )
        tracker_path = find_tracker_by_url(other, url).file_path
    assert tracker_path

    library = make_library_service(db, store, monkeypatch)
    # delete_blob_only resolves the file through the ambient settings.
    settings = StubSettings({"research_library.storage_path": str(store.base)})
    monkeypatch.setattr(
        db_utils,
        "get_settings_manager",
        lambda *a, **k: SimpleNamespace(
            get_setting=settings.get_setting, close=lambda: None
        ),
    )
    removed = document_deletion.DocumentDeletionService(
        USERNAME
    ).delete_blob_only(document_id)
    assert removed["deleted"] is True, removed
    assert removed["bytes_freed"] == len(content)
    assert _tree(store.root) == set()
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document.storage_mode == "none"
        assert document.file_path == FILE_PATH_BLOB_DELETED
        assert other.query(DocumentBlob).count() == 0
        # The shape the sync has to handle: the tracker still names the
        # removed copy.
        assert find_tracker_by_url(other, url).file_path == tracker_path

    stats = library.sync_library_with_filesystem()

    assert (stats["files_found"], stats["files_missing"]) == (1, 0)
    assert stats["missing_files"] == []
    with db.fresh() as other:
        document = other.get(Document, document_id)
        assert document is not None, "the library sync deleted the document"
        assert document.storage_mode == "none"
        assert document.file_path == FILE_PATH_BLOB_DELETED
        assert document.text_content == (EXTRACTED_TEXT if text else None)
        assert _library_links(other, document_id) == 1
        assert find_tracker_by_url(other, url).is_downloaded is True
    assert library.get_document_by_id(document_id)["has_pdf"] is False

    assert (
        service.queue_research_downloads(
            world.research_id, LIBRARY_COLLECTION_ID
        )
        == 0
    )
    assert download_all_new(db, monkeypatch)["queued"] == 0
    assert service.download_resource(resource_id) == (True, None)
    assert len(downloader.urls) == 1
    assert _tree(store.root) == set()


# ===========================================================================
# The deletion the sync exists for
# ===========================================================================


@pytest.mark.parametrize("tracker_path", ["kept", "lost"])
def test_filesystem_download_whose_file_was_removed_is_deleted(
    db, store, world, filesystem_mode, monkeypatch, tracker_path
):
    """A download that had only its file is deleted once the file is gone.

    No text was extracted and there is no blob, so after the user removed
    the PDF from disk nothing of the document is left. The sync deletes it
    so that the URL can be downloaded again. ``lost`` is the R1 sequence
    (the tracker path cleared by a "none"-mode re-download) with the file
    removed afterwards, where the no-path branch must still delete.
    """
    url = "https://example.com/removed-from-disk.pdf"
    resource_id = world.add_resource(url)
    downloader = StubDownloader()
    service = make_service(store, downloader, storage_mode="filesystem")
    monkeypatch.setattr(service, "_extract_text_from_pdf", lambda _data: None)
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        document = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        document_id = document.id
        stored = store.root / document.file_path
    assert stored.is_file()
    library = make_library_service(db, store, monkeypatch)
    if tracker_path == "lost":
        assert library.mark_for_redownload([document_id]) == 1
        service.settings._values["research_library.pdf_storage_mode"] = "none"
        assert service.download_resource(resource_id) == (True, None)
        with db.fresh() as other:
            assert find_tracker_by_url(other, url).file_path is None
    stored.unlink()

    stats = library.sync_library_with_filesystem()

    assert stats["files_missing"] == 1
    with db.fresh() as other:
        assert other.get(Document, document_id) is None
        assert _library_links(other, document_id) == 0
        tracker = find_tracker_by_url(other, url)
        assert tracker.file_path is None
        if tracker_path == "kept":
            assert tracker.is_downloaded is False
            assert stats["trackers_updated"] == 1
            assert [entry["id"] for entry in stats["missing_files"]] == [
                document_id
            ]

    # The point of the deletion: the URL downloads again.
    service.settings._values["research_library.pdf_storage_mode"] = "filesystem"
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        redownloaded = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        assert redownloaded.id != document_id
        assert redownloaded.status is DocumentStatus.COMPLETED
    assert stored.is_file()


def test_database_download_with_no_blob_and_no_text_is_deleted_and_reported(
    db, store, world, monkeypatch
):
    """A database-mode download with nothing left is deleted and reported.

    The document is a database-mode download that holds no blob and no
    text (see ``_add_database_row_without_pdf``). A second research
    downloaded the same bytes: the content-hash dedup linked its resource
    to the document and recorded ``"database"`` in the URL's tracker, as
    it does for a document labelled as stored in the database. Database
    storage leaves the document's ``file_path`` None, so while the tracker
    counted only for a document storing the same path it described no
    database document: the sync deleted this one, but neither reported it
    nor reset the tracker, which kept recording a PDF that no document
    holds. The tracker describes the document now. The sync deletes it,
    reports it as ``"database"`` and resets the tracker, and the URL
    downloads again. A file named ``database`` in the library root is not
    the document's PDF.
    """
    url = "https://example.com/database-row-with-nothing-left.pdf"
    resource_id = world.add_resource(url, downloaded=True)
    document_id = _add_database_row_without_pdf(
        db, world, resource_id, world.research_id, url, PDF_BYTES
    )
    _, linked_id = _cite_in_another_research(db, url)
    service = make_service(store, StubDownloader(), storage_mode="database")
    monkeypatch.setattr(service, "_extract_text_from_pdf", lambda _data: None)
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as other:
        assert other.get(ResearchResource, linked_id).document_id == (
            document_id
        )
        assert other.query(Document).count() == 1
        assert other.query(DocumentBlob).count() == 0
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == ("database", True)
    (store.root / "database").write_bytes(PDF_BYTES)
    library = make_library_service(db, store, monkeypatch)

    stats = library.sync_library_with_filesystem()

    assert (stats["files_found"], stats["files_missing"]) == (0, 1)
    assert stats["missing_files"] == [
        {
            "id": document_id,
            "title": "Sync Contract Paper",
            "path": "database",
            "url": url,
        }
    ]
    assert stats["trackers_updated"] == 1
    with db.fresh() as other:
        assert other.get(Document, document_id) is None
        assert _library_links(other, document_id) == 0
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == (None, False)

    # The point of the deletion: the URL downloads again.
    assert service.download_resource(resource_id) == (True, None)
    with db.fresh() as other:
        redownloaded = (
            other.query(Document).filter_by(resource_id=resource_id).one()
        )
        assert redownloaded.id != document_id
        assert (
            other.query(DocumentBlob)
            .filter_by(document_id=redownloaded.id)
            .count()
            == 1
        )
        tracker = find_tracker_by_url(other, url)
        assert (tracker.file_path, tracker.is_downloaded) == ("database", True)
    stats = library.sync_library_with_filesystem()
    assert (stats["files_found"], stats["files_missing"]) == (1, 0)


# ===========================================================================
# What the sync returns
# ===========================================================================


def test_sync_response_names_missing_files_by_their_stored_path(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    tmp_path,
):
    """The sync's response holds no absolute server path.

    ``POST /library/api/sync-library`` returns the sync's statistics, and
    each ``missing_files`` entry carried the absolute path its file
    resolved to on the server. That is the host's directory layout, which
    ``/library/api/check-downloads`` and the library listing withhold from
    the client (#6462). Every entry now gives the path stored for the file,
    relative to the library root: a download with text whose file is gone,
    reached through its tracker's path (``tracker-path``) or through its own
    after a "none"-mode re-download cleared the tracker's (``own-path``),
    and a download without text, which the sync deletes (``deleted``).
    """
    library = make_library_service(db, store, monkeypatch)
    service = make_service(store, StubDownloader(), storage_mode="filesystem")
    contents = {
        "tracker-path": TEXT_PDF_BYTES,
        "own-path": _text_pdf("Text of the paper downloaded again."),
        "deleted": PDF_BYTES,
    }
    stored = {}
    for case, content in contents.items():
        resource_id = world.add_resource(f"https://example.com/{case}.pdf")
        service.downloaders = [StubDownloader(content)]
        assert service.download_resource(resource_id) == (True, None)
        with db.fresh() as other:
            document = (
                other.query(Document).filter_by(resource_id=resource_id).one()
            )
            stored[document.id] = (case, document.file_path)
        if case == "own-path":
            assert library.mark_for_redownload([document.id]) == 1
            settings = service.settings._values
            settings["research_library.pdf_storage_mode"] = "none"
            assert service.download_resource(resource_id) == (True, None)
            settings["research_library.pdf_storage_mode"] = "filesystem"
    for _case, path in stored.values():
        (store.root / path).unlink()
    monkeypatch.setattr(
        library_router, "LibraryService", lambda username: library
    )

    stats = library_router.sync_library(None, username=USERNAME)

    body = json.dumps(stats)
    assert str(tmp_path) not in body, body
    assert stats["files_missing"] == 3
    assert {entry["id"]: entry["path"] for entry in stats["missing_files"]} == {
        document_id: path for document_id, (_case, path) in stored.items()
    }
    with db.fresh() as other:
        kept = {document.id for document in other.query(Document).all()}
    assert kept == {
        document_id
        for document_id, (case, _path) in stored.items()
        if case != "deleted"
    }


@pytest.mark.parametrize(
    "entrypoint", ["research-queue", "all-new", "direct", "research-count"]
)
def test_canonical_refill_ignores_retained_old_owner(
    db,
    store,
    world,
    one_download_session,
    filesystem_mode,
    monkeypatch,
    entrypoint,
):
    """A completed retained snapshot must not hide the resource's explicit target."""
    linked_url = "https://example.com/current-version.pdf"
    linked_id = world.add_resource(linked_url)
    downloader = StubDownloader(TEXT_PDF_BYTES)
    service = make_service(store, downloader, storage_mode="filesystem")
    # First extraction fails; both references still retain the real old PDF.
    original_extract = service._extract_text_from_pdf
    service._extract_text_from_pdf = lambda _data: None
    assert service.download_resource(linked_id) == (True, None)
    _, mirror_id = _cite_in_another_research(
        db, "https://mirror.example.org/old-version.pdf", query="old version"
    )
    assert service.download_resource(mirror_id) == (True, None)
    with db.fresh() as session:
        old_id = session.get(ResearchResource, linked_id).document_id
        assert session.get(ResearchResource, mirror_id).document_id == old_id
        assert session.get(Document, old_id).text_content is None
    service._extract_text_from_pdf = original_extract
    owner_url = "https://example.org/new-version.pdf"
    _, owner_id = _cite_in_another_research(db, owner_url, query="new version")
    downloader.content = REGENERATED_PDF_BYTES
    assert service.download_resource(owner_id) == (True, None)
    with db.fresh() as session:
        canonical_id = session.get(ResearchResource, owner_id).document_id
    library = make_library_service(db, store, monkeypatch)
    assert library.mark_for_redownload([old_id]) == 1
    assert service.download_resource(linked_id) == (True, None)
    with db.fresh() as session:
        assert (
            session.get(ResearchResource, linked_id).document_id == canonical_id
        )
        assert session.get(Document, old_id).status == DocumentStatus.PENDING
    # A later successful extraction through the old mirror completes the
    # retained snapshot, without changing the first resource's new link.
    downloader.content = TEXT_PDF_BYTES
    assert service.download_resource(mirror_id) == (True, None)
    with db.fresh() as session:
        old = session.get(Document, old_id)
        assert old.status == DocumentStatus.COMPLETED
        assert old.resource_id == linked_id
        assert (
            session.get(ResearchResource, linked_id).document_id == canonical_id
        )
        canonical = session.get(Document, canonical_id)
        assert canonical.text_content == REGENERATED_TEXT
        (store.root / canonical.file_path).unlink()
    assert library.sync_library_with_filesystem()["files_missing"] == 1
    with db.fresh() as session:
        assert document_awaits_pdf(session.get(Document, canonical_id))
        assert (
            library_utils.get_document_for_resource(
                session, session.get(ResearchResource, linked_id)
            ).id
            == canonical_id
        )
    downloader.content = REGENERATED_PDF_BYTES
    if entrypoint == "research-queue":
        queued = service.queue_research_downloads(
            world.research_id,
            LIBRARY_COLLECTION_ID,
            run_research_ids=frozenset([world.research_id]),
        )
        assert queued == 1, (
            "old owned document hides explicit target awaiting refill"
        )
    elif entrypoint == "all-new":
        # The canonical owner's URL is denied now, but the linked URL works.
        answer = download_all_new(db, monkeypatch, refused_urls={owner_url})
        with db.fresh() as session:
            pending = [
                q.resource_id
                for q in session.query(DownloadQueue).filter_by(
                    status=DocumentStatus.PENDING
                )
            ]
        assert pending == [linked_id], (answer, pending, linked_id)
    elif entrypoint == "research-count":
        row = next(
            row
            for row in library.get_research_list_with_stats()
            if row["id"] == world.research_id
        )
        assert row["downloaded_count"] == 0, row
    else:
        before = len(downloader.urls)
        assert service.download_resource(linked_id) == (True, None)
        with db.fresh() as session:
            assert not document_awaits_pdf(
                session.get(Document, canonical_id)
            ), (
                "success returned without refilling the canonical PDF",
                downloader.urls[before:],
            )