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.

23.0 KB
Raw Blame
"""Status-code mapping for every branch of ``web/routers/library_delete.py``.

Ported from ``tests/research_library/deletion/routes/test_delete_routes_http.py``
and ``test_delete_routes_coverage.py``, both deleted in the Flask->FastAPI
migration.

Genuinely superseded, and therefore NOT re-ported here:

* every note/protected refusal (``is_note`` -> 403, ``protected`` -> 403) and
  its 404/200 discriminators --
  ``tests/security/test_library_rag_security_fastapi.py::TestDeleteDocumentNoteRefusal``
  and ``::TestSiblingDeleteRoutes403VsNotFound``, which drive them end to end
  against a real seeded database;
* every ``document_ids`` validation path on the four bulk routes (null body,
  missing key, non-list, empty list, hostile element types) --
  ``tests/web/routers/test_library_delete_hostile_input.py``;
* a protected collection type -> 409 --
  ``tests/security/test_library_notes_authz_fastapi.py::TestProtectedCollectionDeleteMapsTo409AtTheRoute``;
* the 401 for an unauthenticated caller --
  ``tests/security/test_unauthenticated_reachability_census.py``.

What is left is the ordinary-failure half of the same handlers, which no
test on the branch touches:

* ``delete_document_blob``'s THREE-way split. "not found" in the error
  string -> 404, anything else -> 400. The 403 note arm is covered
  elsewhere, but the 404-vs-400 discrimination below it is not, and
  collapsing it tells a client whose request was simply malformed that the
  document does not exist (or the reverse).
* ``delete_collection``'s 404-vs-400 split below the 409.
* ``delete_collection_index`` -- no coverage at all, at any layer.
* both deletion previews' "not found" -> 404.
* the ``except Exception -> handle_api_error`` arm on all eleven handlers.
  It is what stops a service crash from escaping as an unhandled 500 with a
  traceback body; ``handle_api_error`` substitutes a fixed message, and
  nothing on the branch checks that the internal detail does not survive.

FastAPI does not honour Flask's ``(body, status)`` tuple return -- it would
serialise the 2-tuple as an HTTP 200 with a ``[{...}, 404]`` array body --
so each of these branches has to construct a ``JSONResponse`` explicitly.
That makes "the status code is right" a real, breakable property here rather
than a formality, which is the shape of a bug this file exists to catch.
"""

import asyncio
import json
from unittest.mock import MagicMock, Mock, patch

import pytest
from starlette.requests import Request

from local_deep_research.web.routers import library_delete as delete_module
from local_deep_research.web.routers.library_delete import (
    delete_collection,
    delete_collection_index,
    delete_document,
    delete_document_blob,
    delete_documents_blobs_bulk,
    delete_documents_bulk,
    get_bulk_deletion_preview,
    get_collection_deletion_preview,
    get_document_deletion_preview,
    remove_document_from_collection,
    remove_documents_from_collection_bulk,
)

INTERNAL_DETAIL = "sqlite3.OperationalError: /srv/data/alice/library.db locked"


# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------


def _request(method="DELETE", path="/library/api/x", payload=None):
    body = json.dumps(
        payload if payload is not None else {"document_ids": ["d1"]}
    ).encode()

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

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


def _patch_services(document=None, collection=None, bulk=None):
    """Patch the three deletion service classes on the router module."""
    return [
        patch.object(
            delete_module,
            "DocumentDeletionService",
            Mock(return_value=document if document is not None else Mock()),
        ),
        patch.object(
            delete_module,
            "CollectionDeletionService",
            Mock(return_value=collection if collection is not None else Mock()),
        ),
        patch.object(
            delete_module,
            "BulkDeletionService",
            Mock(return_value=bulk if bulk is not None else Mock()),
        ),
    ]


def _call(handler, *args, document=None, collection=None, bulk=None, **kwargs):
    """Run a handler (sync or async) with the deletion services patched.

    ``username`` is the FAST002 ``Annotated[str, Depends(require_auth)]``
    parameter on every handler here -- Annotated carries no default (unlike
    the pre-conversion ``= Depends(require_auth)`` spelling), so a direct
    call bypassing FastAPI's own dependency resolution must supply it
    explicitly or every handler call raises ``TypeError: missing 1 required
    positional argument: 'username'``. None of these tests exercise
    username-dependent behavior, so a fixed stand-in is injected unless a
    caller overrides it.
    """
    kwargs.setdefault("username", "testuser")
    patches = _patch_services(document, collection, bulk)

    # The four bulk handlers hand their per-document loop to run_db_sync;
    # run it inline so the test drives the real service call.
    async def _inline_run_db_sync(fn, *a, **kw):
        return fn()

    patches.append(
        patch.object(delete_module, "run_db_sync", _inline_run_db_sync)
    )
    for p in patches:
        p.start()
    try:
        result = handler(*args, **kwargs)
        if asyncio.iscoroutine(result):
            result = asyncio.run(result)
        return result
    finally:
        for p in reversed(patches):
            p.stop()


def _body(response):
    return json.loads(response.body)


def _assert_scrubbed_500(response):
    assert response.status_code == 500, (
        f"a service exception must be caught and mapped to 500, got "
        f"{getattr(response, 'status_code', response)!r}"
    )
    body = _body(response)
    assert body["success"] is False
    rendered = json.dumps(body)
    assert INTERNAL_DETAIL not in rendered, (
        f"the internal exception text reached the client: {rendered}"
    )
    assert "/srv/data/alice" not in rendered


# ===========================================================================
# DELETE /library/api/document/{id}
# ===========================================================================


def test_a_missing_document_is_a_404():
    """The fall-through arm, below the note-refusal 403. It is the one the
    frontend reads as "this is gone, drop it from the list"."""
    service = Mock()
    service.delete_document.return_value = {
        "deleted": False,
        "error": "Not found",
    }

    response = _call(
        delete_document, _request(), "doc-missing", document=service
    )

    assert response.status_code == 404
    assert _body(response)["success"] is False


def test_a_deleted_document_reports_the_service_result():
    """Positive control: the same handler with ``deleted: True`` returns a
    plain 200 body carrying the service's own detail (chunk counts, bytes
    freed) merged alongside ``success``."""
    service = Mock()
    service.delete_document.return_value = {
        "deleted": True,
        "chunks_deleted": 5,
    }

    result = _call(delete_document, _request(), "doc-1", document=service)

    assert result == {"success": True, "deleted": True, "chunks_deleted": 5}
    service.delete_document.assert_called_once_with("doc-1")


# ===========================================================================
# DELETE /library/api/document/{id}/blob -- the 404-vs-400 split
# ===========================================================================


def test_a_blob_delete_for_a_missing_document_is_a_404():
    service = Mock()
    service.delete_blob_only.return_value = {
        "deleted": False,
        "error": "Document not found",
    }

    response = _call(
        delete_document_blob, _request(), "doc-1", document=service
    )

    assert response.status_code == 404
    assert _body(response)["success"] is False


def test_a_blob_delete_that_fails_for_any_other_reason_is_a_400():
    """The discriminator for the test above. The split is a substring test on
    the service's error message, so it is easy to collapse into a single
    status -- and then a document with no blob attached reads to the client
    as a document that does not exist."""
    service = Mock()
    service.delete_blob_only.return_value = {
        "deleted": False,
        "error": "No blob attached",
    }

    response = _call(
        delete_document_blob, _request(), "doc-1", document=service
    )

    assert response.status_code == 400, (
        "an error that is not 'not found' must be a 400, not the sibling 404"
    )
    assert _body(response)["success"] is False


def test_the_blob_not_found_match_is_case_insensitive():
    """``"not found" in result.get("error", "").lower()`` -- the service is
    free to capitalise its message."""
    service = Mock()
    service.delete_blob_only.return_value = {
        "deleted": False,
        "error": "Document Not Found",
    }

    response = _call(
        delete_document_blob, _request(), "doc-1", document=service
    )

    assert response.status_code == 404


def test_a_successful_blob_delete_reports_the_bytes_freed():
    service = Mock()
    service.delete_blob_only.return_value = {
        "deleted": True,
        "bytes_freed": 1024,
    }

    result = _call(delete_document_blob, _request(), "doc-1", document=service)

    assert result == {"success": True, "deleted": True, "bytes_freed": 1024}


# ===========================================================================
# DELETE /library/api/collections/{id}
# ===========================================================================


def test_deleting_a_missing_collection_is_a_404():
    service = Mock()
    service.delete_collection.return_value = {
        "deleted": False,
        "error": "Collection not found",
    }

    response = _call(
        delete_collection, _request(), "coll-missing", collection=service
    )

    assert response.status_code == 404
    assert _body(response)["success"] is False


def test_a_collection_refused_for_another_reason_is_a_400():
    """Discriminator: the 404 arm is a substring match on the error, and the
    409 arm above it keys on ``collection_type``. A refusal that is neither
    must land on 400 rather than borrowing one of them."""
    service = Mock()
    service.delete_collection.return_value = {
        "deleted": False,
        "error": "Cannot delete default collection",
        "collection_type": "user",
    }

    response = _call(
        delete_collection, _request(), "coll-1", collection=service
    )

    assert response.status_code == 400, (
        "a non-not-found, non-protected refusal must be 400"
    )
    assert _body(response)["success"] is False


def test_deleting_a_collection_with_unreachable_vector_store_is_a_503():
    """The vector-store-unreachable shape (``retryable: True``, set by
    ``CollectionDeletionService`` when ``RemoteVectorStoreDropError``
    propagates from the cascade -- see #5765) must be a 503, not fall
    through to the 404/400 branches below it. The collection was not
    deleted (the service rolled back), so a client retrying this exact
    request can succeed once the vector store is reachable again (the drop
    is idempotent) -- a 503 says so; a
    404 would read as "already gone" to a naive retry/cleanup layer, which
    would then mark it deleted while the RAGIndex rows and remote vectors
    both survive.
    """
    service = Mock()
    service.delete_collection.return_value = {
        "deleted": False,
        "error": (
            "Vector store unreachable — the collection was not deleted. "
            "Retry once the vector store is available."
        ),
        "vector_store_error": True,
        "retryable": True,
    }

    response = _call(
        delete_collection, _request(), "coll-1", collection=service
    )

    assert response.status_code == 503, (
        f"a retryable vector-store-unreachable failure must be 503, got "
        f"{response.status_code}"
    )
    body = _body(response)
    assert body["success"] is False
    assert body["retryable"] is True


def test_deleting_a_collection_asks_for_orphan_cleanup():
    """``delete_orphaned_documents=True`` is the difference between deleting
    a collection and leaving its documents behind as unreachable rows."""
    service = Mock()
    service.delete_collection.return_value = {"deleted": True}

    result = _call(delete_collection, _request(), "coll-1", collection=service)

    assert result == {"success": True, "deleted": True}
    service.delete_collection.assert_called_once_with(
        "coll-1", delete_orphaned_documents=True
    )


# ===========================================================================
# DELETE /library/api/collections/{id}/index -- no coverage at any layer
# ===========================================================================


def test_deleting_only_the_index_leaves_the_collection():
    """The "rebuild my index" button. It must call
    ``delete_collection_index_only`` -- calling the full delete instead would
    destroy the user's collection to rebuild its index."""
    service = Mock()
    service.delete_collection_index_only.return_value = {
        "deleted": True,
        "chunks_deleted": 12,
    }

    result = _call(
        delete_collection_index, _request(), "coll-1", collection=service
    )

    assert result == {"success": True, "deleted": True, "chunks_deleted": 12}
    service.delete_collection_index_only.assert_called_once_with("coll-1")
    service.delete_collection.assert_not_called()


def test_deleting_the_index_of_a_missing_collection_is_a_404():
    service = Mock()
    service.delete_collection_index_only.return_value = {
        "deleted": False,
        "error": "Not found",
    }

    response = _call(
        delete_collection_index,
        _request(),
        "coll-missing",
        collection=service,
    )

    assert response.status_code == 404
    assert _body(response)["success"] is False


def test_deleting_the_index_with_unreachable_vector_store_is_a_503():
    """Same 503 mapping as ``delete_collection`` above, for the sibling
    endpoint -- see #5765."""
    service = Mock()
    service.delete_collection_index_only.return_value = {
        "deleted": False,
        "error": (
            "Vector store unreachable — the index was not deleted. Retry "
            "once the vector store is available."
        ),
        "vector_store_error": True,
        "retryable": True,
    }

    response = _call(
        delete_collection_index, _request(), "coll-1", collection=service
    )

    assert response.status_code == 503, (
        f"a retryable vector-store-unreachable failure must be 503, got "
        f"{response.status_code}"
    )
    body = _body(response)
    assert body["success"] is False
    assert body["retryable"] is True


_PERMANENT_DROP_FAILURE = {
    "deleted": False,
    "error": (
        "The vector store could not drop the collection — the index was "
        "not deleted. Retrying will not help until the vector store "
        "configuration or permissions are fixed; see the server log."
    ),
    "vector_store_error": True,
    "retryable": False,
}


def test_deleting_a_collection_with_a_permanent_drop_failure_is_a_500():
    """A drop that fails for a reason a retry cannot fix (auth, missing
    configuration, a store with no drop support -- #5765) is neither a 503
    (which invites a retry) nor a 404 (which reads as "already gone"), nor
    the generic 400 for a bad request."""
    service = Mock()
    service.delete_collection.return_value = dict(_PERMANENT_DROP_FAILURE)

    response = _call(
        delete_collection, _request(), "coll-1", collection=service
    )

    assert response.status_code == 500
    body = _body(response)
    assert body["success"] is False
    assert body["retryable"] is False


def test_deleting_the_index_with_a_permanent_drop_failure_is_a_500():
    service = Mock()
    service.delete_collection_index_only.return_value = dict(
        _PERMANENT_DROP_FAILURE
    )

    response = _call(
        delete_collection_index, _request(), "coll-1", collection=service
    )

    assert response.status_code == 500
    body = _body(response)
    assert body["success"] is False
    assert body["retryable"] is False


# ===========================================================================
# The two deletion previews
# ===========================================================================


def test_a_document_preview_for_a_missing_document_is_a_404():
    """The preview is what the confirm dialog renders. A missing document
    must not render an empty "you are about to delete" dialog."""
    service = Mock()
    service.get_deletion_preview.return_value = {"found": False}

    response = _call(
        get_document_deletion_preview,
        _request(method="GET"),
        "doc-missing",
        document=service,
    )

    assert response.status_code == 404
    assert _body(response) == {
        "success": False,
        "error": "Document not found",
    }


def test_a_collection_preview_for_a_missing_collection_is_a_404():
    service = Mock()
    service.get_deletion_preview.return_value = {"found": False}

    response = _call(
        get_collection_deletion_preview,
        _request(method="GET"),
        "coll-missing",
        collection=service,
    )

    assert response.status_code == 404
    assert _body(response) == {
        "success": False,
        "error": "Collection not found",
    }


@pytest.mark.parametrize(
    "handler,kind",
    [
        (get_document_deletion_preview, "document"),
        (get_collection_deletion_preview, "collection"),
    ],
    ids=["document", "collection"],
)
def test_a_found_preview_carries_the_service_detail(handler, kind):
    """Positive control for the two 404s: ``found`` is what decides, and a
    found preview passes the service's numbers straight through."""
    service = Mock()
    service.get_deletion_preview.return_value = {
        "found": True,
        "total_size": 5000,
    }

    result = _call(handler, _request(method="GET"), "x", **{kind: service})

    assert result == {"success": True, "found": True, "total_size": 5000}


# ===========================================================================
# The bulk routes' happy paths
# ===========================================================================


def test_bulk_delete_reports_the_service_result():
    service = Mock()
    service.delete_documents.return_value = {"deleted_count": 3}

    result = _call(
        delete_documents_bulk,
        _request(payload={"document_ids": ["d1", "d2", "d3"]}),
        bulk=service,
    )

    assert result == {"success": True, "deleted_count": 3}
    service.delete_documents.assert_called_once_with(["d1", "d2", "d3"])


def test_bulk_preview_forwards_the_requested_operation():
    """``operation`` decides whether the preview describes deleting the
    documents or only their blobs -- two very different confirm dialogs. It
    defaults to ``"delete"``."""
    service = Mock()
    service.get_bulk_preview.return_value = {"total_size": 5000}

    result = _call(
        get_bulk_deletion_preview,
        _request(
            method="POST",
            payload={"document_ids": ["d1"], "operation": "delete_blobs"},
        ),
        bulk=service,
    )

    assert result == {"success": True, "total_size": 5000}
    service.get_bulk_preview.assert_called_once_with(["d1"], "delete_blobs")


def test_bulk_preview_defaults_to_the_delete_operation():
    service = Mock()
    service.get_bulk_preview.return_value = {"total_size": 1}

    _call(
        get_bulk_deletion_preview,
        _request(method="POST", payload={"document_ids": ["d1"]}),
        bulk=service,
    )

    service.get_bulk_preview.assert_called_once_with(["d1"], "delete")


def test_bulk_collection_removal_passes_the_collection_id():
    service = Mock()
    service.remove_documents_from_collection.return_value = {"removed_count": 2}

    result = _call(
        remove_documents_from_collection_bulk,
        _request(payload={"document_ids": ["d1", "d2"]}),
        "coll-1",
        bulk=service,
    )

    assert result == {"success": True, "removed_count": 2}
    service.remove_documents_from_collection.assert_called_once_with(
        ["d1", "d2"], "coll-1"
    )


# ===========================================================================
# `except Exception -> handle_api_error` on every handler
# ===========================================================================


def _service_that_raises(method):
    service = MagicMock()
    getattr(service, method).side_effect = RuntimeError(INTERNAL_DETAIL)
    return service


@pytest.mark.parametrize(
    "handler,args,kind,method",
    [
        (delete_document, ("doc-1",), "document", "delete_document"),
        (delete_document_blob, ("doc-1",), "document", "delete_blob_only"),
        (
            get_document_deletion_preview,
            ("doc-1",),
            "document",
            "get_deletion_preview",
        ),
        (
            remove_document_from_collection,
            ("coll-1", "doc-1"),
            "document",
            "remove_from_collection",
        ),
        (delete_collection, ("coll-1",), "collection", "delete_collection"),
        (
            delete_collection_index,
            ("coll-1",),
            "collection",
            "delete_collection_index_only",
        ),
        (
            get_collection_deletion_preview,
            ("coll-1",),
            "collection",
            "get_deletion_preview",
        ),
        (delete_documents_bulk, (), "bulk", "delete_documents"),
        (delete_documents_blobs_bulk, (), "bulk", "delete_blobs"),
        (
            remove_documents_from_collection_bulk,
            ("coll-1",),
            "bulk",
            "remove_documents_from_collection",
        ),
        (get_bulk_deletion_preview, (), "bulk", "get_bulk_preview"),
    ],
    ids=lambda v: getattr(v, "__name__", v if isinstance(v, str) else ""),
)
def test_a_service_crash_is_a_scrubbed_500_not_a_traceback(
    handler, args, kind, method
):
    """Every handler wraps its body in ``except Exception ->
    handle_api_error``. Without it the exception escapes to the ASGI layer,
    which answers 500 with whatever the server is configured to render --
    and the message here carries a database path.
    """
    service = _service_that_raises(method)

    response = _call(handler, _request(), *args, **{kind: service})

    _assert_scrubbed_500(response)


def test_a_generic_index_delete_failure_is_a_500_not_a_404():
    """The service's generic failure (it rolled back) is not "already
    gone": a 404 would let a cleanup layer mark the index deleted."""
    service = Mock()
    service.delete_collection_index_only.return_value = {
        "deleted": False,
        "collection_id": "coll-1",
        "error": "Failed to delete collection index",
    }

    response = _call(
        delete_collection_index, _request(), "coll-1", collection=service
    )

    assert response.status_code == 500
    assert _body(response)["success"] is False