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.

19.1 KB
Raw Blame
"""Upload dedup semantics for ``POST /library/api/collections/{id}/upload``.

Ports the coverage main added for issue #5495 onto the FastAPI route.
Main's suite (``tests/research_library/routes/test_rag_routes_upload_coverage.py``)
targets the Flask blueprint this branch deleted, so none of it carries
over; these tests exercise the same behaviour through the real HTTP
route on ``web/routers/rag.py`` instead of mocking the session.

What is pinned here:

1. **Intra-batch duplicates.** When one request carries two files with
   identical bytes, the second occurrence must be reported under *its
   own* filename with status ``duplicate_in_batch`` and an ``id``
   pointing at the twin that was kept. Before the fix, the route
   re-ran the ``Document.document_hash`` lookup, found the row the
   first occurrence had just flushed, and reported the second file as
   ``already_in_collection`` *under the first occurrence's filename* --
   so the UI showed a book as skipped that had actually been added, and
   the duplicate the user picked vanished from the response entirely.

2. **The distinction that fix depends on.** A duplicate against a
   *pre-existing* library document is still ``already_in_collection``
   (same collection) or ``added_to_collection`` (new collection), never
   ``duplicate_in_batch`` -- and both now report the newly uploaded
   filename rather than the stored document's.

3. **PDF upgrade against the kept twin.** ``seen_hashes`` maps hash ->
   ``Document`` (not a bare marker) precisely so a later identical PDF
   in the same batch can still run ``_try_pdf_upgrade`` against the
   document the first occurrence kept.

4. **``_try_pdf_upgrade`` swallows upgrade failures**: the upload
   continues as a non-upgraded success rather than turning into a
   per-file error.

5. **Per-file SAVEPOINTs.** One file blowing up mid-write must not take
   the rest of the batch with it.
"""

import uuid
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient

from tests.web.routers.test_rag_pdf_storage_manager_user_scoping import (
    _minimal_pdf,
)


@pytest.fixture(scope="module")
def auth_client():
    """Authenticated test client (same bootstrap as
    test_collection_upload_http.py's fixture)."""
    from local_deep_research.web.fastapi_app import app

    c = TestClient(app, raise_server_exceptions=False)

    user = f"test_dedup_{uuid.uuid4().hex[:8]}"
    pw = "TestPassword123!"  # noqa: S105

    def _csrf():
        c.get("/auth/login")
        r = c.get("/auth/csrf-token")
        return r.json().get("csrf_token", "") if r.status_code == 200 else ""

    c.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    resp = c.post(
        "/auth/login",
        data={"username": user, "password": pw, "csrf_token": _csrf()},
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(
            f"Auth bootstrap broken: login returned {resp.status_code} "
            f"(expected 302): {resp.text[:300]}"
        )

    csrf_resp = c.get("/auth/csrf-token")
    if csrf_resp.status_code == 200:
        token = csrf_resp.json().get("csrf_token")
        if token:
            c.headers.update({"X-CSRFToken": token})

    yield c

    c.post("/auth/logout", follow_redirects=False)


def _new_collection(client) -> str:
    resp = client.post(
        "/library/api/collections",
        json={"name": f"dedup-{uuid.uuid4().hex[:8]}"},
    )
    assert resp.status_code == 200, resp.text
    return resp.json()["collection"]["id"]


def _upload(client, collection_id, files, **form):
    """POST a multipart upload; ``files`` is [(filename, bytes, mime)]."""
    return client.post(
        f"/library/api/collections/{collection_id}/upload",
        files=[("files", f) for f in files],
        data=form,
    )


def _ok_body(resp):
    assert resp.status_code == 200, f"{resp.status_code}: {resp.text}"
    body = resp.json()
    assert body.get("success") is True, body
    return body


def _by_filename(body):
    """Index the ``uploaded`` entries by filename.

    Asserts the filenames are distinct: the #5495 bug produced two
    entries carrying the SAME filename, which is exactly what this
    indexing must not paper over.
    """
    entries = body["uploaded"]
    names = [e["filename"] for e in entries]
    assert len(set(names)) == len(names), (
        f"duplicate filenames in `uploaded`: {names} -- a second entry is "
        "being reported under an earlier file's name (#5495)"
    )
    return {e["filename"]: e for e in entries}


def _collection_filenames(client, collection_id):
    resp = client.get(f"/library/api/collections/{collection_id}/documents")
    assert resp.status_code == 200, resp.text
    return sorted(d["filename"] for d in resp.json().get("documents", []))


# ---------------------------------------------------------------------------
# 1. Intra-batch duplicates
# ---------------------------------------------------------------------------


@pytest.mark.timeout(180)
def test_intra_batch_duplicate_reported_under_own_filename(auth_client):
    collection_id = _new_collection(auth_client)
    content = f"the same book bytes {uuid.uuid4().hex}\n".encode()

    body = _ok_body(
        _upload(
            auth_client,
            collection_id,
            [
                ("book-a.txt", content, "text/plain"),
                ("book-b.txt", content, "text/plain"),
            ],
            storage_mode="database",
        )
    )

    entries = _by_filename(body)
    assert set(entries) == {"book-a.txt", "book-b.txt"}, entries

    first, second = entries["book-a.txt"], entries["book-b.txt"]
    assert first["status"] == "uploaded", first
    assert second["status"] == "duplicate_in_batch", (
        f"second copy reported as {second['status']!r}; an identical file "
        "earlier in the SAME request must not be labelled as a hit against "
        "a pre-existing library document (#5495)"
    )
    # The duplicate points at the twin that was actually kept, so the UI
    # can link it to the document that exists.
    assert second["id"] == first["id"], (entries, "id must be the kept twin")

    statuses = {e["status"] for e in body["uploaded"]}
    assert "already_in_collection" not in statuses, body
    assert body["errors"] == [], body
    assert body["summary"] == {"total": 2, "successful": 2, "failed": 0}, body

    # Exactly one Document was created, under the first occurrence's name.
    assert _collection_filenames(auth_client, collection_id) == ["book-a.txt"]


@pytest.mark.timeout(180)
def test_intra_batch_duplicate_survives_three_copies(auth_client):
    """Every later copy is its own entry -- the second occurrence does not
    become the reference for the third."""
    collection_id = _new_collection(auth_client)
    content = f"triplicate {uuid.uuid4().hex}\n".encode()

    body = _ok_body(
        _upload(
            auth_client,
            collection_id,
            [
                ("t1.txt", content, "text/plain"),
                ("t2.txt", content, "text/plain"),
                ("t3.txt", content, "text/plain"),
            ],
        )
    )
    entries = _by_filename(body)
    assert set(entries) == {"t1.txt", "t2.txt", "t3.txt"}, entries
    assert entries["t1.txt"]["status"] == "uploaded", entries
    assert entries["t2.txt"]["status"] == "duplicate_in_batch", entries
    assert entries["t3.txt"]["status"] == "duplicate_in_batch", entries
    assert (
        entries["t2.txt"]["id"]
        == entries["t3.txt"]["id"]
        == entries["t1.txt"]["id"]
    ), entries
    assert _collection_filenames(auth_client, collection_id) == ["t1.txt"]


@pytest.mark.timeout(180)
def test_distinct_bytes_in_one_batch_are_not_deduped(auth_client):
    """Control for the above: different content must NOT be collapsed."""
    collection_id = _new_collection(auth_client)
    tag = uuid.uuid4().hex

    body = _ok_body(
        _upload(
            auth_client,
            collection_id,
            [
                ("d1.txt", f"first {tag}\n".encode(), "text/plain"),
                ("d2.txt", f"second {tag}\n".encode(), "text/plain"),
            ],
        )
    )
    entries = _by_filename(body)
    assert [entries[n]["status"] for n in ("d1.txt", "d2.txt")] == [
        "uploaded",
        "uploaded",
    ], entries
    assert entries["d1.txt"]["id"] != entries["d2.txt"]["id"], entries
    assert _collection_filenames(auth_client, collection_id) == [
        "d1.txt",
        "d2.txt",
    ]


# ---------------------------------------------------------------------------
# 2. Duplicates against a PRE-EXISTING library document
# ---------------------------------------------------------------------------


@pytest.mark.timeout(180)
def test_second_request_same_collection_is_already_in_collection(auth_client):
    collection_id = _new_collection(auth_client)
    content = f"pre-existing doc {uuid.uuid4().hex}\n".encode()

    first = _ok_body(
        _upload(
            auth_client,
            collection_id,
            [("orig.txt", content, "text/plain")],
        )
    )
    kept_id = first["uploaded"][0]["id"]
    assert first["uploaded"][0]["status"] == "uploaded", first

    second = _ok_body(
        _upload(
            auth_client,
            collection_id,
            [("renamed.txt", content, "text/plain")],
        )
    )
    entry = second["uploaded"][0]
    assert entry["status"] == "already_in_collection", (
        f"a duplicate of a document uploaded in an EARLIER request must "
        f"stay 'already_in_collection', got {entry['status']!r}"
    )
    assert entry["id"] == kept_id, (entry, kept_id)
    # The uploaded filename, not the stored document's -- otherwise the
    # user sees 'orig.txt' reported for a file they never re-picked.
    assert entry["filename"] == "renamed.txt", entry

    # No second Document row was created.
    assert _collection_filenames(auth_client, collection_id) == ["orig.txt"]


@pytest.mark.timeout(180)
def test_same_bytes_into_another_collection_is_added_to_collection(
    auth_client,
):
    first_collection = _new_collection(auth_client)
    second_collection = _new_collection(auth_client)
    content = f"cross-collection {uuid.uuid4().hex}\n".encode()

    first = _ok_body(
        _upload(
            auth_client,
            first_collection,
            [("shared.txt", content, "text/plain")],
        )
    )
    kept_id = first["uploaded"][0]["id"]

    second = _ok_body(
        _upload(
            auth_client,
            second_collection,
            [("shared-copy.txt", content, "text/plain")],
        )
    )
    entry = second["uploaded"][0]
    assert entry["status"] == "added_to_collection", entry
    assert entry["id"] == kept_id, (entry, kept_id)
    assert entry["filename"] == "shared-copy.txt", entry

    # The existing Document really was linked into the new collection.
    assert _collection_filenames(auth_client, second_collection) == [
        "shared.txt"
    ]


@pytest.mark.timeout(180)
def test_preexisting_hit_and_intra_batch_dupe_in_one_batch(auth_client):
    """Both mechanisms in a single request must stay distinguishable."""
    collection_id = _new_collection(auth_client)
    old = f"already there {uuid.uuid4().hex}\n".encode()
    new = f"brand new {uuid.uuid4().hex}\n".encode()

    seeded = _ok_body(
        _upload(auth_client, collection_id, [("old.txt", old, "text/plain")])
    )
    old_id = seeded["uploaded"][0]["id"]

    body = _ok_body(
        _upload(
            auth_client,
            collection_id,
            [
                ("old-again.txt", old, "text/plain"),
                ("new-1.txt", new, "text/plain"),
                ("new-2.txt", new, "text/plain"),
            ],
        )
    )
    entries = _by_filename(body)
    assert entries["old-again.txt"]["status"] == "already_in_collection", (
        entries
    )
    assert entries["old-again.txt"]["id"] == old_id, entries
    assert entries["new-1.txt"]["status"] == "uploaded", entries
    assert entries["new-2.txt"]["status"] == "duplicate_in_batch", entries
    assert entries["new-2.txt"]["id"] == entries["new-1.txt"]["id"], entries


# ---------------------------------------------------------------------------
# 3./4. PDF upgrade against the kept twin, and its failure handling
# ---------------------------------------------------------------------------


def _patch_upgrade(result=None, exc=None):
    """Patch ``PDFStorageManager.upgrade_to_pdf``, recording its calls."""
    from local_deep_research.research_library.services.pdf_storage_manager import (  # noqa: E501
        PDFStorageManager,
    )

    calls = []

    def spy(self, document, pdf_content, session):
        calls.append({"document_id": document.id, "content": pdf_content})
        if exc is not None:
            raise exc
        return result

    return patch.object(PDFStorageManager, "upgrade_to_pdf", spy), calls


@pytest.mark.timeout(180)
def test_intra_batch_duplicate_pdf_upgrades_the_kept_twin(auth_client):
    """``seen_hashes`` must hold the Document, not a bare marker, so the
    later copy can be upgraded against the twin that was kept."""
    collection_id = _new_collection(auth_client)
    pdf_bytes = _minimal_pdf(f"dupe {uuid.uuid4().hex[:8]}")

    patcher, calls = _patch_upgrade(result=True)
    with patcher:
        body = _ok_body(
            _upload(
                auth_client,
                collection_id,
                [
                    ("copy-a.pdf", pdf_bytes, "application/pdf"),
                    ("copy-b.pdf", pdf_bytes, "application/pdf"),
                ],
                pdf_storage="database",
            )
        )

    entries = _by_filename(body)
    assert entries["copy-a.pdf"]["status"] == "uploaded", entries
    assert entries["copy-b.pdf"]["status"] == "duplicate_in_batch", entries
    assert entries["copy-b.pdf"]["pdf_upgraded"] is True, entries

    # Exactly one upgrade attempt -- from the intra-batch branch -- and it
    # targeted the document the first occurrence created, with the bytes
    # of the file the user actually uploaded second.
    assert len(calls) == 1, calls
    assert calls[0]["document_id"] == entries["copy-a.pdf"]["id"], (
        calls,
        entries,
    )
    assert calls[0]["content"] == pdf_bytes, "wrong bytes passed to upgrade"


@pytest.mark.timeout(180)
def test_intra_batch_duplicate_non_pdf_skips_upgrade(auth_client):
    """Magic-byte guard: a non-PDF duplicate must not reach the manager."""
    collection_id = _new_collection(auth_client)
    content = f"plain text twin {uuid.uuid4().hex}\n".encode()

    patcher, calls = _patch_upgrade(result=True)
    with patcher:
        body = _ok_body(
            _upload(
                auth_client,
                collection_id,
                [
                    ("txt-a.txt", content, "text/plain"),
                    ("txt-b.txt", content, "text/plain"),
                ],
                pdf_storage="database",
            )
        )

    entries = _by_filename(body)
    assert entries["txt-b.txt"]["status"] == "duplicate_in_batch", entries
    assert entries["txt-b.txt"]["pdf_upgraded"] is False, entries
    assert calls == [], calls


@pytest.mark.timeout(180)
def test_pdf_upgrade_exception_does_not_fail_the_upload(auth_client):
    """``_try_pdf_upgrade`` logs and swallows: the duplicate is still a
    (non-upgraded) success, not a per-file error."""
    collection_id = _new_collection(auth_client)
    pdf_bytes = _minimal_pdf(f"boom {uuid.uuid4().hex[:8]}")

    patcher, calls = _patch_upgrade(exc=RuntimeError("blob store offline"))
    with patcher:
        body = _ok_body(
            _upload(
                auth_client,
                collection_id,
                [
                    ("raise-a.pdf", pdf_bytes, "application/pdf"),
                    ("raise-b.pdf", pdf_bytes, "application/pdf"),
                ],
                pdf_storage="database",
            )
        )

    assert len(calls) == 1, calls
    entries = _by_filename(body)
    assert entries["raise-b.pdf"]["status"] == "duplicate_in_batch", entries
    assert entries["raise-b.pdf"]["pdf_upgraded"] is False, entries
    assert body["errors"] == [], (
        "an upgrade failure must be swallowed, not turned into a per-file error"
    )
    assert body["summary"] == {"total": 2, "successful": 2, "failed": 0}, body


# ---------------------------------------------------------------------------
# 5. Per-file SAVEPOINTs
# ---------------------------------------------------------------------------


@pytest.mark.timeout(180)
def test_failing_file_does_not_poison_the_rest_of_the_batch(auth_client):
    """A DB error on one file must be contained by that file's SAVEPOINT.

    The failure is injected as a **failed flush** (a NOT NULL violation on
    ``document_collections.document_id``). SQLAlchemy deactivates the
    session's transaction after a flush error, so every later statement
    raises ``PendingRollbackError`` until something rolls back. With the
    per-file ``begin_nested()``/``sp.rollback()`` only the bad file is
    lost; without it the remaining files -- and the final
    ``db_session.commit()`` -- all run on a poisoned transaction and the
    whole batch goes with it.
    """
    from local_deep_research.database.models.library import (
        DocumentCollection,
    )
    from local_deep_research.web.routers import rag as rag_module

    collection_id = _new_collection(auth_client)
    tag = uuid.uuid4().hex
    real_ensure = rag_module.ensure_in_collection
    calls = {"n": 0}

    def flaky_ensure(session, document_id, collection_id_):
        calls["n"] += 1
        if calls["n"] == 2:
            session.add(
                DocumentCollection(
                    document_id=None,  # NOT NULL -> IntegrityError on flush
                    collection_id=collection_id_,
                    indexed=False,
                )
            )
            session.flush()
        return real_ensure(session, document_id, collection_id_)

    with patch.object(rag_module, "ensure_in_collection", flaky_ensure):
        body = _ok_body(
            _upload(
                auth_client,
                collection_id,
                [
                    ("keep-1.txt", f"one {tag}\n".encode(), "text/plain"),
                    ("boom.txt", f"two {tag}\n".encode(), "text/plain"),
                    ("keep-2.txt", f"three {tag}\n".encode(), "text/plain"),
                ],
            )
        )

    assert calls["n"] == 3, (
        "the third file never reached ensure_in_collection -- the batch "
        "aborted after the failure instead of continuing"
    )
    entries = _by_filename(body)
    assert set(entries) == {"keep-1.txt", "keep-2.txt"}, body
    assert entries["keep-1.txt"]["status"] == "uploaded", body
    assert entries["keep-2.txt"]["status"] == "uploaded", body
    assert [e["filename"] for e in body["errors"]] == ["boom.txt"], body
    assert body["summary"] == {"total": 3, "successful": 2, "failed": 1}, body

    # And the surviving files are really committed, not just reported.
    assert _collection_filenames(auth_client, collection_id) == [
        "keep-1.txt",
        "keep-2.txt",
    ]