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.

41.9 KB
Raw Blame
"""ROW-LEVEL deletion cascade contracts, against a REAL on-disk SQLite DB.

Scope: what happens to the ROWS. The delete-route STATUS matrix
(200/403/404/400/409/500) is covered elsewhere and is deliberately NOT
duplicated here.

Why a real database rather than a ``MagicMock`` session: a mock cannot
show that a row is gone, that a deliberately-seeded sibling survived, or
that a ``WHERE`` clause is still present on a bulk ``DELETE``. Every
deletion assertion below is paired: the target must be gone AND a sibling
seeded for exactly that purpose must survive. "The row is gone" alone
passes just as happily for a delete that wiped the whole table.

Why ON-DISK and not ``:memory:``: in-memory SQLite is per-connection, so
a second connection cannot observe the first's committed state and any
cross-session assertion would pass vacuously. Every assertion here reads
through a FRESH connection to the same file, never through the session
the service mutated.

``PRAGMA foreign_keys = ON`` is applied to the test engine because
production applies it on every connection
(``sqlcipher_utils.apply_performance_pragmas``, wired in
``encrypted_db.LibraryDatabaseManager._apply_pragmas`` for both the
encrypted and unencrypted engines). Without it every ``ondelete=CASCADE``
in the library models is inert and the cascade under test would not
exist -- ``CascadeHelper.delete_document_completely`` issues raw
``Query.delete()`` calls and relies on the DB to clean up
``document_blobs`` / ``rag_document_status``.

The services are driven DIRECTLY. No FastAPI app is booted.

Covered:
  * deleting one document touches only its own rows, never another
    collection's;
  * a collection delete cascades to exactly the tables it is documented
    to cascade to -- asserted as an exact whole-database row census diff,
    "no more, no fewer", not a handful of spot checks;
  * protected/system collections (default_library, research_history,
    notes) are refused with every row intact, paired with a control
    user_collection of identical shape that IS deleted;
  * a note-backed Document cannot be hard-deleted (or blob-stripped, or
    bulk-deleted, or orphan-cascaded) through the generic document API --
    the invariant whose route-level guard test was deleted from main with
    no successor, and whose shadowing once made a note Document
    hard-deletable;
  * blob-only deletion frees the binary and leaves the row + metadata;
  * a failure partway through leaves NO orphans: the whole-database
    census is byte-identical before and after.
"""

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

import pytest
from sqlalchemy import create_engine, event, select
from sqlalchemy.orm import sessionmaker

from local_deep_research.constants import FILE_PATH_BLOB_DELETED
from local_deep_research.database.models import Base
from local_deep_research.database.models.download_tracker import (
    DownloadTracker,
)
from local_deep_research.research_library.utils import get_url_hash
from local_deep_research.database.models.library import (
    Collection,
    CollectionFolder,
    Document,
    DocumentBlob,
    DocumentChunk,
    DocumentCollection,
    DocumentStatus,
    EmbeddingProvider,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)
from local_deep_research.research_library.deletion.services.bulk_deletion import (
    BulkDeletionService,
)
from local_deep_research.research_library.deletion.services.collection_deletion import (
    PROTECTED_COLLECTION_TYPES,
    CollectionDeletionService,
)
from local_deep_research.research_library.deletion.services.document_deletion import (
    DocumentDeletionService,
)
from local_deep_research.research_library.deletion.utils.cascade_helper import (
    CascadeHelper,
)

USERNAME = "cascade_user"

_DOC_MOD = (
    "local_deep_research.research_library.deletion.services.document_deletion"
)
_COLL_MOD = (
    "local_deep_research.research_library.deletion.services.collection_deletion"
)
_SESSION_CTX = "local_deep_research.database.session_context"
_RAG_FACTORY = (
    "local_deep_research.research_library.services.rag_service_factory"
)


# ---------------------------------------------------------------------------
# Row census helpers
# ---------------------------------------------------------------------------

# Stable, human-readable identity per table. Tables not listed fall back to
# their primary key. Used so an expected-diff can be written in terms of the
# seeded ids instead of opaque autoincrement integers.
_IDENTITY = {
    "document_collections": ("document_id", "collection_id"),
    "document_chunks": ("collection_name", "source_id", "chunk_index"),
    "collection_folders": ("collection_id", "folder_path"),
    "rag_indices": ("collection_name", "index_hash"),
    "download_tracker": ("url_hash",),
}


def _identity_columns(table):
    names = _IDENTITY.get(table.name)
    if names is None:
        names = tuple(c.name for c in table.primary_key.columns)
    return names


def _pk_census(engine):
    """{table_name: {identity_tuple, ...}} for every non-empty table."""
    census = {}
    with engine.connect() as conn:
        for table in Base.metadata.sorted_tables:
            names = _identity_columns(table)
            if not names:
                continue
            cols = [table.c[n] for n in names]
            rows = conn.execute(select(*cols)).fetchall()
            if rows:
                census[table.name] = {tuple(r) for r in rows}
    return census


def _full_census(engine):
    """Every column of every row, as sortable reprs -- catches in-place
    UPDATEs that a primary-key census cannot see."""
    census = {}
    with engine.connect() as conn:
        for table in Base.metadata.sorted_tables:
            rows = conn.execute(select(table)).fetchall()
            if rows:
                census[table.name] = sorted(repr(tuple(r)) for r in rows)
    return census


def _census_diff(before, after):
    """(removed, added) as {table: {identity, ...}}, empty tables dropped."""
    removed = {}
    added = {}
    for table in set(before) | set(after):
        gone = before.get(table, set()) - after.get(table, set())
        new = after.get(table, set()) - before.get(table, set())
        if gone:
            removed[table] = gone
        if new:
            added[table] = new
    return removed, added


# ---------------------------------------------------------------------------
# Seeding
# ---------------------------------------------------------------------------


def _add_document(
    session,
    *,
    doc_id,
    source_type_id,
    title,
    url=None,
    storage_mode="database",
):
    session.add(
        Document(
            id=doc_id,
            source_type_id=source_type_id,
            document_hash=hashlib.sha256(doc_id.encode()).hexdigest(),
            original_url=url,
            file_path=f"pdfs/{doc_id}.pdf",
            file_size=4096,
            file_type="pdf",
            mime_type="application/pdf",
            storage_mode=storage_mode,
            title=title,
            text_content=f"text-of-{doc_id}",
            status=DocumentStatus.COMPLETED,
        )
    )


def _add_blob(session, doc_id, payload):
    session.add(
        DocumentBlob(
            document_id=doc_id,
            pdf_binary=payload,
            blob_hash=hashlib.sha256(payload).hexdigest(),
        )
    )


def _link(session, doc_id, collection_id, *, indexed=True):
    session.add(
        DocumentCollection(
            document_id=doc_id,
            collection_id=collection_id,
            indexed=indexed,
            chunk_count=1,
        )
    )


def _add_chunk(session, doc_id, collection_id, index):
    name = f"collection_{collection_id}"
    text = f"chunk-{index}-of-{doc_id}-in-{collection_id}"
    session.add(
        DocumentChunk(
            chunk_hash=hashlib.sha256(text.encode()).hexdigest(),
            source_type="document",
            source_id=doc_id,
            collection_name=name,
            chunk_text=text,
            chunk_index=index,
            start_char=0,
            end_char=len(text),
            word_count=len(text.split()),
            embedding_id=str(uuid.uuid5(uuid.NAMESPACE_URL, text)),
            embedding_model="fake-model",
            embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
            embedding_dimension=8,
        )
    )


def _add_rag_index(session, collection_id):
    name = f"collection_{collection_id}"
    index = RAGIndex(
        collection_name=name,
        embedding_model="fake-model",
        embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
        embedding_dimension=8,
        index_path=f"/nonexistent/rag_indices/{collection_id}",
        index_hash=hashlib.sha256(name.encode()).hexdigest(),
        chunk_size=100,
        chunk_overlap=10,
    )
    session.add(index)
    session.flush()
    return index.id


def _add_rag_status(session, doc_id, collection_id, rag_index_id):
    session.add(
        RagDocumentStatus(
            document_id=doc_id,
            collection_id=collection_id,
            rag_index_id=rag_index_id,
            chunk_count=1,
        )
    )


def _add_collection(session, collection_id, name, collection_type):
    session.add(
        Collection(
            id=collection_id,
            name=name,
            collection_type=collection_type,
            is_default=(collection_type == "default_library"),
        )
    )


def _add_folder(session, collection_id, folder_path):
    session.add(
        CollectionFolder(
            collection_id=collection_id,
            folder_path=folder_path,
        )
    )


def _add_tracker(session, resource_id, url, doc_id):
    # Seed with the production hash: hand-rolling sha256(url.lower()) here
    # would pin the pre-B2 (#6758) scheme and silently diverge from the
    # cascade lookup under test.
    session.add(
        DownloadTracker(
            url=url,
            url_hash=get_url_hash(url),
            first_resource_id=resource_id,
            file_path=f"pdfs/{doc_id}.pdf",
            file_name=f"{doc_id}.pdf",
            is_downloaded=True,
            library_document_id=doc_id,
        )
    )


class _World:
    """Ids of everything seeded, so tests read as prose."""

    col_a = "col-alpha"
    col_b = "col-beta"
    col_c = "col-gamma"
    col_notes = "col-notes"
    col_library = "col-library"
    col_history = "col-history"

    # Alpha-only documents (orphans if Alpha goes away)
    a_only = "doc-a-only"
    a_only2 = "doc-a-only-2"
    # In Alpha AND Beta -- must survive an Alpha delete
    shared = "doc-shared"
    # Beta-only -- the sibling that must never be touched
    b_only = "doc-b-only"
    # Notes
    note_home = "doc-note-home"  # lives in the Notes collection
    note_c = "doc-note-in-gamma"  # note orphaned by a Gamma delete
    plain_c = "doc-plain-in-gamma"  # regular sibling in Gamma


@pytest.fixture
def db(tmp_path, mocker):
    """Real on-disk SQLite DB + the deletion services wired to it.

    The patched ``get_user_db_session`` yields ONE long-lived session and
    never closes it, which is exactly what production does: the real
    context manager hands out a reused THREAD-LOCAL session and only
    rolls back on exception (``database/session_context.py``). Assertions
    never read through this session -- they use ``db.fresh()``, a brand
    new connection to the same file.
    """
    db_path = tmp_path / "cascade.db"
    engine = create_engine(f"sqlite:///{db_path}")

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

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

    @contextmanager
    def _shared_session(*_a, **_k):
        try:
            yield session
        except Exception:
            session.rollback()
            raise

    mocker.patch(f"{_DOC_MOD}.get_user_db_session", _shared_session)
    mocker.patch(f"{_COLL_MOD}.get_user_db_session", _shared_session)
    mocker.patch(f"{_SESSION_CTX}.get_user_db_session", _shared_session)
    mocker.patch(f"{_DOC_MOD}.capture_request_db_password", return_value=None)

    purged = []

    @contextmanager
    def _fake_rag_service(_username, collection_id=None, db_password=None):
        purged.append(collection_id)
        yield MagicMock()

    mocker.patch(f"{_RAG_FACTORY}.get_rag_service", _fake_rag_service)

    # --- seed -------------------------------------------------------------
    st_doc = SourceType(
        id=uuid.uuid4().hex, name="document", display_name="Document"
    )
    st_note = SourceType(id=uuid.uuid4().hex, name="note", display_name="Note")
    session.add_all([st_doc, st_note])

    w = _World()
    _add_collection(session, w.col_a, "Alpha", "user_collection")
    _add_collection(session, w.col_b, "Beta", "user_collection")
    _add_collection(session, w.col_c, "Gamma", "user_collection")
    _add_collection(session, w.col_notes, "Notes", "notes")
    _add_collection(session, w.col_library, "Library", "default_library")
    _add_collection(session, w.col_history, "History", "research_history")
    session.flush()

    rag_a = _add_rag_index(session, w.col_a)
    rag_b = _add_rag_index(session, w.col_b)
    rag_c = _add_rag_index(session, w.col_c)

    _add_folder(session, w.col_a, "/data/alpha-one")
    _add_folder(session, w.col_a, "/data/alpha-two")
    _add_folder(session, w.col_b, "/data/beta-one")

    _add_document(
        session,
        doc_id=w.a_only,
        source_type_id=st_doc.id,
        title="Alpha Only",
        url="https://example.test/a-only",
    )
    _add_document(
        session, doc_id=w.a_only2, source_type_id=st_doc.id, title="Alpha 2"
    )
    _add_document(
        session, doc_id=w.shared, source_type_id=st_doc.id, title="Shared"
    )
    _add_document(
        session,
        doc_id=w.b_only,
        source_type_id=st_doc.id,
        title="Beta Only",
        url="https://example.test/b-only",
    )
    _add_document(
        session, doc_id=w.note_home, source_type_id=st_note.id, title="Note"
    )
    _add_document(
        session, doc_id=w.note_c, source_type_id=st_note.id, title="Note C"
    )
    _add_document(
        session, doc_id=w.plain_c, source_type_id=st_doc.id, title="Plain C"
    )
    session.flush()

    _add_blob(session, w.a_only, b"%PDF-alpha-only-payload")
    _add_blob(session, w.b_only, b"%PDF-beta-only-payload")
    _add_blob(session, w.note_home, b"%PDF-note-payload")

    _link(session, w.a_only, w.col_a)
    _link(session, w.a_only2, w.col_a)
    _link(session, w.shared, w.col_a)
    _link(session, w.shared, w.col_b)
    _link(session, w.b_only, w.col_b)
    _link(session, w.note_home, w.col_notes)
    _link(session, w.note_c, w.col_c)
    _link(session, w.plain_c, w.col_c)

    _add_chunk(session, w.a_only, w.col_a, 0)
    _add_chunk(session, w.a_only, w.col_a, 1)
    _add_chunk(session, w.a_only2, w.col_a, 0)
    _add_chunk(session, w.shared, w.col_a, 0)
    _add_chunk(session, w.shared, w.col_b, 0)
    _add_chunk(session, w.b_only, w.col_b, 0)
    _add_chunk(session, w.b_only, w.col_b, 1)
    _add_chunk(session, w.note_home, w.col_notes, 0)
    _add_chunk(session, w.note_c, w.col_c, 0)
    _add_chunk(session, w.plain_c, w.col_c, 0)

    _add_rag_status(session, w.a_only, w.col_a, rag_a)
    _add_rag_status(session, w.a_only2, w.col_a, rag_a)
    _add_rag_status(session, w.shared, w.col_a, rag_a)
    _add_rag_status(session, w.shared, w.col_b, rag_b)
    _add_rag_status(session, w.b_only, w.col_b, rag_b)
    _add_rag_status(session, w.note_c, w.col_c, rag_c)
    _add_rag_status(session, w.plain_c, w.col_c, rag_c)

    research = ResearchHistory(
        id=uuid.uuid4().hex,
        query="q",
        mode="quick",
        status="completed",
        created_at="2026-01-01T00:00:00",
    )
    session.add(research)
    session.flush()
    res_a = ResearchResource(
        research_id=research.id,
        url="https://example.test/a-only",
        created_at="2026-01-01T00:00:00",
    )
    res_b = ResearchResource(
        research_id=research.id,
        url="https://example.test/b-only",
        created_at="2026-01-01T00:00:00",
    )
    session.add_all([res_a, res_b])
    session.flush()
    _add_tracker(session, res_a.id, "https://example.test/a-only", w.a_only)
    _add_tracker(session, res_b.id, "https://example.test/b-only", w.b_only)

    session.commit()

    class _DB:
        pass

    handle = _DB()
    handle.engine = engine
    handle.Session = Session
    handle.session = session
    handle.w = w
    handle.purged = purged
    handle.doc_source_type_id = st_doc.id
    handle.note_source_type_id = st_note.id
    handle.fresh = lambda: Session()
    handle.pk_census = lambda: _pk_census(engine)
    handle.full_census = lambda: _full_census(engine)

    yield handle

    session.close()
    engine.dispose()


# ---------------------------------------------------------------------------
# Verification helpers -- all read through a FRESH connection
# ---------------------------------------------------------------------------


def _doc_ids(db):
    with db.fresh() as s:
        return {d.id for d in s.query(Document).all()}


def _collection_ids(db):
    with db.fresh() as s:
        return {c.id for c in s.query(Collection).all()}


def _link_pairs(db):
    with db.fresh() as s:
        return {
            (dc.document_id, dc.collection_id)
            for dc in s.query(DocumentCollection).all()
        }


def _chunk_keys(db):
    with db.fresh() as s:
        return {
            (c.collection_name, c.source_id, c.chunk_index)
            for c in s.query(DocumentChunk).all()
        }


def _blob_ids(db):
    with db.fresh() as s:
        return {b.document_id for b in s.query(DocumentBlob).all()}


def _rag_status_pairs(db):
    with db.fresh() as s:
        return {
            (r.document_id, r.collection_id)
            for r in s.query(RagDocumentStatus).all()
        }


def _rag_index_names(db):
    with db.fresh() as s:
        return {r.collection_name for r in s.query(RAGIndex).all()}


def _folder_keys(db):
    with db.fresh() as s:
        return {
            (f.collection_id, f.folder_path)
            for f in s.query(CollectionFolder).all()
        }


def _tracker_by_url(db, url):
    # Look up with the production hash (see _add_tracker): the old
    # hand-rolled sha256(url.lower()) no longer matches post-B2 (#6758).
    url_hash = get_url_hash(url)
    with db.fresh() as s:
        return s.query(DownloadTracker).filter_by(url_hash=url_hash).one()


def _get_doc(db, doc_id):
    with db.fresh() as s:
        return s.get(Document, doc_id)


# ===========================================================================
# 1. Single-document delete: only its own rows
# ===========================================================================


def test_delete_document_removes_only_its_own_rows(db):
    """Deleting Alpha's private document must not touch Beta's rows."""
    w = db.w
    service = DocumentDeletionService(USERNAME)

    result = service.delete_document(w.a_only)
    assert result["deleted"] is True, result

    # Target gone, across every table that referenced it.
    assert w.a_only not in _doc_ids(db)
    assert w.a_only not in _blob_ids(db)
    assert not [p for p in _link_pairs(db) if p[0] == w.a_only]
    assert not [k for k in _chunk_keys(db) if k[1] == w.a_only]
    assert not [p for p in _rag_status_pairs(db) if p[0] == w.a_only]

    # Deliberately-seeded sibling in ANOTHER collection: fully intact.
    # Without this half, a delete that wiped each table would still pass.
    assert w.b_only in _doc_ids(db)
    assert w.b_only in _blob_ids(db)
    assert (w.b_only, w.col_b) in _link_pairs(db)
    assert (f"collection_{w.col_b}", w.b_only, 0) in _chunk_keys(db)
    assert (f"collection_{w.col_b}", w.b_only, 1) in _chunk_keys(db)
    assert (w.b_only, w.col_b) in _rag_status_pairs(db)

    # Sibling inside the SAME collection is untouched too.
    assert w.a_only2 in _doc_ids(db)
    assert (f"collection_{w.col_a}", w.a_only2, 0) in _chunk_keys(db)

    # The collections themselves and their indices survive a document
    # delete -- deleting a document must never take its container.
    assert _collection_ids(db) == {
        w.col_a,
        w.col_b,
        w.col_c,
        w.col_notes,
        w.col_library,
        w.col_history,
    }
    assert _rag_index_names(db) == {
        f"collection_{w.col_a}",
        f"collection_{w.col_b}",
        f"collection_{w.col_c}",
    }
    assert _folder_keys(db) == {
        (w.col_a, "/data/alpha-one"),
        (w.col_a, "/data/alpha-two"),
        (w.col_b, "/data/beta-one"),
    }


def test_delete_document_resets_only_its_own_download_tracker(db):
    """``update_download_tracker`` must match on THIS document's URL hash."""
    w = db.w
    DocumentDeletionService(USERNAME).delete_document(w.a_only)

    mine = _tracker_by_url(db, "https://example.test/a-only")
    assert mine.is_downloaded is False
    assert mine.file_path is None
    # documents.id FK is ondelete=SET NULL -- the tracker row survives with
    # a nulled link, it is not cascade-deleted.
    assert mine.library_document_id is None

    sibling = _tracker_by_url(db, "https://example.test/b-only")
    assert sibling.is_downloaded is True
    assert sibling.file_path == f"pdfs/{w.b_only}.pdf"
    assert sibling.library_document_id == w.b_only


def test_delete_document_purges_chunks_in_every_collection_it_lived_in(db):
    """A document in two collections loses both collections' chunk rows --
    and only its own."""
    w = db.w
    result = DocumentDeletionService(USERNAME).delete_document(w.shared)
    assert result["deleted"] is True, result
    assert result["chunks_deleted"] == 2, result

    assert not [k for k in _chunk_keys(db) if k[1] == w.shared]
    # Both collections were handed to the RAG purge, not just one.
    assert set(db.purged) == {w.col_a, w.col_b}

    # Siblings in BOTH of those collections keep their chunks.
    assert (f"collection_{w.col_a}", w.a_only, 0) in _chunk_keys(db)
    assert (f"collection_{w.col_b}", w.b_only, 0) in _chunk_keys(db)
    assert {w.a_only, w.a_only2, w.b_only} <= _doc_ids(db)


# ===========================================================================
# 2. The note-Document invariant (successor to main's deleted route test)
# ===========================================================================


def test_note_backed_document_cannot_be_hard_deleted(db):
    """A note-backed Document is refused by the generic document API, and
    every one of its rows survives.

    main's ``test_delete_document_route_resolution.py`` locked this at the
    routing layer and was deleted with no successor; its own docstring
    records that the shadowing it guarded against once made a note
    Document hard-deletable. This locks it at the row layer, where the
    damage would actually land.
    """
    w = db.w
    service = DocumentDeletionService(USERNAME)

    result = service.delete_document(w.note_home)
    assert result["deleted"] is False
    assert result["is_note"] is True

    # Nothing of the note was touched.
    assert w.note_home in _doc_ids(db)
    assert w.note_home in _blob_ids(db)
    assert (w.note_home, w.col_notes) in _link_pairs(db)
    assert (f"collection_{w.col_notes}", w.note_home, 0) in _chunk_keys(db)
    note = _get_doc(db, w.note_home)
    assert note.text_content == f"text-of-{w.note_home}"
    assert note.storage_mode == "database"

    # Control: the SAME service instance really can delete a non-note, so
    # the survival above is the guard working, not a broken harness.
    control = service.delete_document(w.a_only)
    assert control["deleted"] is True, control
    assert w.a_only not in _doc_ids(db)


def test_note_document_survives_bulk_delete_alongside_regular_documents(db):
    """The bulk endpoint loops over the same guarded method -- it must not
    amplify a gap. Note kept, regular sibling deleted, in one call."""
    w = db.w
    result = BulkDeletionService(USERNAME).delete_documents(
        [w.note_home, w.a_only2]
    )

    assert result["deleted"] == 1, result
    assert result["failed"] == 1, result
    assert [e["document_id"] for e in result["errors"]] == [w.note_home]

    assert w.note_home in _doc_ids(db)
    assert (f"collection_{w.col_notes}", w.note_home, 0) in _chunk_keys(db)
    assert w.a_only2 not in _doc_ids(db)


def test_note_document_blob_and_metadata_survive_blob_delete(db):
    """``delete_blob_only`` must not be a back door for rewriting a note's
    storage_mode/file_path."""
    w = db.w
    service = DocumentDeletionService(USERNAME)

    result = service.delete_blob_only(w.note_home)
    assert result["deleted"] is False
    assert result["is_note"] is True
    assert result["bytes_freed"] == 0

    assert w.note_home in _blob_ids(db)
    note = _get_doc(db, w.note_home)
    assert note.storage_mode == "database"
    assert note.file_path == f"pdfs/{w.note_home}.pdf"

    # Control: a regular document's blob IS strippable by this same call.
    control = service.delete_blob_only(w.a_only)
    assert control["deleted"] is True, control
    assert w.a_only not in _blob_ids(db)


def test_note_is_never_hard_deleted_by_collection_orphan_cascade(db):
    """Deleting Gamma orphans both its documents. The regular one is
    hard-deleted; the note must be skipped, not swept up."""
    w = db.w
    result = CollectionDeletionService(USERNAME).delete_collection(
        w.col_c, delete_orphaned_documents=True
    )
    assert result["deleted"] is True, result
    assert result["orphaned_documents_deleted"] == 1, result

    assert w.plain_c not in _doc_ids(db)
    assert w.note_c in _doc_ids(db)
    note = _get_doc(db, w.note_c)
    assert note.text_content == f"text-of-{w.note_c}"


def test_note_cannot_be_unlinked_from_its_notes_collection(db):
    """The Notes collection is a note's permanent home; the generic
    collection-document API must refuse to unlink it there."""
    w = db.w
    service = DocumentDeletionService(USERNAME)

    result = service.remove_from_collection(w.note_home, w.col_notes)
    assert result["unlinked"] is False
    assert result["protected"] is True
    assert (w.note_home, w.col_notes) in _link_pairs(db)
    assert (f"collection_{w.col_notes}", w.note_home, 0) in _chunk_keys(db)

    # Control: the same call on a non-note link in a non-notes collection
    # does unlink.
    control = service.remove_from_collection(w.shared, w.col_a)
    assert control["unlinked"] is True, control
    assert (w.shared, w.col_a) not in _link_pairs(db)


# ===========================================================================
# 3. Blob-only deletion keeps the row and its metadata
# ===========================================================================


def test_blob_only_delete_frees_binary_and_keeps_row_and_metadata(db):
    """ "Remove the PDF to save space" must leave everything but the PDF."""
    w = db.w
    before = _get_doc(db, w.a_only)
    title, text, doc_hash = (
        before.title,
        before.text_content,
        before.document_hash,
    )

    result = DocumentDeletionService(USERNAME).delete_blob_only(w.a_only)
    assert result["deleted"] is True, result
    assert result["bytes_freed"] == len(b"%PDF-alpha-only-payload")
    assert result["storage_mode_updated"] is True

    # The binary is gone...
    assert w.a_only not in _blob_ids(db)
    # ...but the document row, its metadata, its collection links and its
    # chunk rows (i.e. its searchability) are all intact.
    after = _get_doc(db, w.a_only)
    assert after is not None
    assert after.title == title
    assert after.text_content == text
    assert after.document_hash == doc_hash
    assert after.storage_mode == "none"
    assert after.file_path == FILE_PATH_BLOB_DELETED
    assert (w.a_only, w.col_a) in _link_pairs(db)
    assert (f"collection_{w.col_a}", w.a_only, 0) in _chunk_keys(db)
    assert (w.a_only, w.col_a) in _rag_status_pairs(db)

    # Sibling blob untouched -- a blob delete without a WHERE clause would
    # have taken this one too.
    assert w.b_only in _blob_ids(db)
    with db.fresh() as s:
        sibling_blob = s.get(DocumentBlob, w.b_only)
        assert sibling_blob.pdf_binary == b"%PDF-beta-only-payload"
    assert _get_doc(db, w.b_only).storage_mode == "database"


# ===========================================================================
# 4. Collection delete: exactly these tables, no more, no fewer
# ===========================================================================


def test_delete_collection_cascade_is_exactly_the_documented_set(db):
    """Whole-database row census before/after.

    Asserting the EXACT diff is what makes this a "no more, no fewer"
    test: a cascade that reached one extra table, or stopped one table
    short, changes ``removed`` and fails here. Spot checks cannot do that.
    """
    w = db.w
    before = db.pk_census()

    result = CollectionDeletionService(USERNAME).delete_collection(
        w.col_a, delete_orphaned_documents=True
    )
    assert result["deleted"] is True, result

    removed, added = _census_diff(before, db.pk_census())

    col_a_name = f"collection_{w.col_a}"
    assert removed == {
        # the collection itself
        "collections": {(w.col_a,)},
        # its linked folders
        "collection_folders": {
            (w.col_a, "/data/alpha-one"),
            (w.col_a, "/data/alpha-two"),
        },
        # every membership row in it -- including the shared document's,
        # whose Beta membership must survive (checked below)
        "document_collections": {
            (w.a_only, w.col_a),
            (w.a_only2, w.col_a),
            (w.shared, w.col_a),
        },
        # every chunk row scoped to it
        "document_chunks": {
            (col_a_name, w.a_only, 0),
            (col_a_name, w.a_only, 1),
            (col_a_name, w.a_only2, 0),
            (col_a_name, w.shared, 0),
        },
        # its RAG index record
        "rag_indices": {
            (col_a_name, hashlib.sha256(col_a_name.encode()).hexdigest())
        },
        # its indexed-document markers (FK CASCADE off collections.id)
        "rag_document_status": {
            (w.a_only, w.col_a),
            (w.a_only2, w.col_a),
            (w.shared, w.col_a),
        },
        # documents orphaned by the unlink -- and ONLY those
        "documents": {(w.a_only,), (w.a_only2,)},
        # the orphans' blobs (FK CASCADE off documents.id)
        "document_blobs": {(w.a_only,)},
    }, removed
    assert added == {}, added

    # Explicit survivor checks: the sibling collection is complete.
    assert (w.shared, w.col_b) in _link_pairs(db)
    assert (f"collection_{w.col_b}", w.shared, 0) in _chunk_keys(db)
    assert (w.shared, w.col_b) in _rag_status_pairs(db)
    assert {w.shared, w.b_only, w.note_home} <= _doc_ids(db)
    assert w.b_only in _blob_ids(db)
    assert f"collection_{w.col_b}" in _rag_index_names(db)
    assert (w.col_b, "/data/beta-one") in _folder_keys(db)


def test_delete_collection_orphan_path_leaves_the_download_tracker_alone(db):
    """Documented asymmetry, pinned so a change is noticed.

    The document-delete path calls ``update_download_tracker``; the
    collection-delete orphan path calls ``delete_document_completely``
    directly and does NOT. The tracker row therefore keeps
    ``is_downloaded=True`` and its ``file_path`` after the document it
    described has been hard-deleted (only the FK link is nulled). This is
    pre-existing behaviour, identical to ``origin/main``.
    """
    w = db.w
    CollectionDeletionService(USERNAME).delete_collection(
        w.col_a, delete_orphaned_documents=True
    )
    assert w.a_only not in _doc_ids(db)

    tracker = _tracker_by_url(db, "https://example.test/a-only")
    assert tracker.library_document_id is None  # FK SET NULL fired
    assert tracker.is_downloaded is True  # but the flag was NOT reset
    assert tracker.file_path == f"pdfs/{w.a_only}.pdf"


def test_delete_collection_preserving_documents_keeps_orphan_rows(db):
    """``delete_orphaned_documents=False`` must unlink without deleting."""
    w = db.w
    before = db.pk_census()

    result = CollectionDeletionService(USERNAME).delete_collection(
        w.col_a, delete_orphaned_documents=False
    )
    assert result["deleted"] is True, result
    assert result["orphaned_documents_deleted"] == 0, result

    removed, added = _census_diff(before, db.pk_census())
    assert added == {}, added
    # No document and no blob may appear in the removed set at all.
    assert "documents" not in removed, removed
    assert "document_blobs" not in removed, removed

    assert {w.a_only, w.a_only2} <= _doc_ids(db)
    assert w.a_only in _blob_ids(db)
    kept = _get_doc(db, w.a_only)
    assert kept.text_content == f"text-of-{w.a_only}"
    # ...but they really are unlinked and their Alpha chunks really are gone.
    assert not [p for p in _link_pairs(db) if p[1] == w.col_a]
    assert not [k for k in _chunk_keys(db) if k[0] == f"collection_{w.col_a}"]
    # Sibling collection intact.
    assert (f"collection_{w.col_b}", w.b_only, 0) in _chunk_keys(db)


@pytest.mark.parametrize("protected_type", sorted(PROTECTED_COLLECTION_TYPES))
def test_protected_collections_are_refused_with_every_row_intact(
    db, protected_type
):
    """System collections must be refused, and a control user_collection of
    the same shape must still be deletable."""
    w = db.w
    target = {
        "default_library": w.col_library,
        "research_history": w.col_history,
        "notes": w.col_notes,
    }[protected_type]

    before = db.full_census()
    result = CollectionDeletionService(USERNAME).delete_collection(target)

    assert result["deleted"] is False, result
    assert result["collection_type"] == protected_type
    assert "Cannot delete system collection" in result["error"]
    # Not one row anywhere in the database changed.
    assert db.full_census() == before

    # Control: an ordinary collection with documents, chunks, folders and a
    # RAG index -- the same shape -- IS deleted by the same service. Without
    # this the refusal above could be a broken harness.
    control = CollectionDeletionService(USERNAME).delete_collection(w.col_a)
    assert control["deleted"] is True, control
    assert w.col_a not in _collection_ids(db)
    assert target in _collection_ids(db)


# ===========================================================================
# 5. Unlink (remove-from-collection) row contracts
# ===========================================================================


def test_remove_from_collection_unlinks_only_that_collection(db):
    """A document in two collections keeps everything belonging to the
    collection it was NOT removed from."""
    w = db.w
    result = DocumentDeletionService(USERNAME).remove_from_collection(
        w.shared, w.col_a
    )
    assert result["unlinked"] is True, result
    assert result["document_deleted"] is False, result

    assert w.shared in _doc_ids(db)
    assert (w.shared, w.col_a) not in _link_pairs(db)
    assert (w.shared, w.col_b) in _link_pairs(db)
    assert (f"collection_{w.col_a}", w.shared, 0) not in _chunk_keys(db)
    assert (f"collection_{w.col_b}", w.shared, 0) in _chunk_keys(db)
    assert (w.shared, w.col_a) not in _rag_status_pairs(db)
    assert (w.shared, w.col_b) in _rag_status_pairs(db)
    # Only the removed-from collection was purged, not every collection.
    assert db.purged == [w.col_a]

    # Siblings in the collection we unlinked from are untouched.
    assert (f"collection_{w.col_a}", w.a_only, 0) in _chunk_keys(db)
    assert (w.a_only, w.col_a) in _rag_status_pairs(db)


def test_remove_from_collection_orphan_delete_spares_siblings(db):
    """The orphan hard-delete must take exactly the orphan."""
    w = db.w
    before = db.pk_census()

    result = DocumentDeletionService(USERNAME).remove_from_collection(
        w.a_only, w.col_a
    )
    assert result["unlinked"] is True, result
    assert result["document_deleted"] is True, result

    removed, added = _census_diff(before, db.pk_census())
    assert added == {}, added
    assert removed["documents"] == {(w.a_only,)}, removed
    assert removed["document_blobs"] == {(w.a_only,)}, removed
    assert "collections" not in removed, removed
    assert "rag_indices" not in removed, removed

    assert {w.a_only2, w.shared, w.b_only} <= _doc_ids(db)
    assert (f"collection_{w.col_a}", w.a_only2, 0) in _chunk_keys(db)


# ===========================================================================
# 6. Failure partway leaves no orphans
# ===========================================================================


def test_collection_delete_failure_partway_leaves_no_orphans(db, mocker):
    """Fail between the first and second orphan hard-delete.

    By then the service has already deleted the chunk rows, the RAGIndex
    row, every membership row, the collection row and one whole document.
    The rollback must put every one of those back: the full-column census
    is compared, so a half-applied cascade (an orphaned blob, a stranded
    chunk, a nulled tracker link) fails here.
    """
    w = db.w
    real_delete = CascadeHelper.delete_document_completely
    calls = {"n": 0, "armed": True}

    def _fail_on_second(session, document_id):
        calls["n"] += 1
        if calls["armed"] and calls["n"] == 2:
            raise RuntimeError("injected mid-cascade failure")
        return real_delete(session, document_id)

    mocker.patch.object(
        CascadeHelper,
        "delete_document_completely",
        side_effect=_fail_on_second,
    )

    before = db.full_census()
    result = CollectionDeletionService(USERNAME).delete_collection(
        w.col_a, delete_orphaned_documents=True
    )

    assert result["deleted"] is False, result
    assert calls["n"] == 2, "the failure must land mid-cascade, not before"
    assert db.full_census() == before

    # And the control: with the injection disarmed (the session patches
    # stay in place), the very same call succeeds -- proving the census
    # equality above was the rollback, not a service that never started.
    calls["armed"] = False
    control = CollectionDeletionService(USERNAME).delete_collection(
        w.col_a, delete_orphaned_documents=True
    )
    assert control["deleted"] is True, control
    assert w.col_a not in _collection_ids(db)


def test_document_delete_failure_leaves_the_document_whole(db, mocker):
    """A failure inside the document delete must not leave a document whose
    blob or chunks were taken while the row itself survived."""
    w = db.w
    real_delete = CascadeHelper.delete_document_completely
    armed = {"on": True}

    def _maybe_fail(session, document_id):
        if armed["on"]:
            raise RuntimeError("injected failure")
        return real_delete(session, document_id)

    mocker.patch.object(
        CascadeHelper, "delete_document_completely", side_effect=_maybe_fail
    )

    before = db.full_census()
    result = DocumentDeletionService(USERNAME).delete_document(w.a_only)

    assert result["deleted"] is False, result
    assert "error" in result
    assert db.full_census() == before
    assert db.purged == [], "no RAG purge may run for a failed delete"

    armed["on"] = False
    control = DocumentDeletionService(USERNAME).delete_document(w.a_only)
    assert control["deleted"] is True, control
    assert w.a_only not in _doc_ids(db)
    assert w.b_only in _doc_ids(db)


def test_concurrent_loser_reports_not_found_and_deletes_nothing(db, mocker):
    """When the row-count delete affects 0 rows (a cross-process race won by
    someone else), the service must report not-found and must not purge."""
    w = db.w
    mocker.patch.object(
        CascadeHelper, "delete_document_completely", return_value=False
    )

    before = db.full_census()
    result = DocumentDeletionService(USERNAME).delete_document(w.a_only)

    assert result["deleted"] is False, result
    assert result["error"] == "Document not found"
    # The blob/link deletes staged by the real helper are stubbed out here,
    # so nothing should have been committed at all.
    assert db.full_census() == before
    assert db.purged == []


# ===========================================================================
# 7. Route-level successor: one owner for DELETE /library/api/document/<id>
# ===========================================================================


def test_exactly_one_route_owns_the_document_delete_path():
    """Successor to main's deleted ``test_delete_document_route_resolution``.

    That test existed because two Flask blueprints once declared the same
    ``DELETE /library/api/document/<id>``; the unguarded ``LibraryService``
    handler won registration order and made the note guard dead code. The
    FastAPI port must not reintroduce a second owner -- FastAPI resolves
    to the first match and would silently shadow the guarded handler in
    exactly the same way.
    """
    import importlib
    import pkgutil

    import local_deep_research.web.routers as routers_pkg

    owners = []
    seen = set()
    for module_info in pkgutil.iter_modules(routers_pkg.__path__):
        module = importlib.import_module(
            f"{routers_pkg.__name__}.{module_info.name}"
        )
        router = getattr(module, "router", None)
        if router is None:
            continue
        for route in router.routes:
            for method in getattr(route, "methods", None) or set():
                key = (method, route.path)
                seen.add(key)
                if key == ("DELETE", "/library/api/document/{document_id}"):
                    owners.append(
                        f"{module.__name__}:{route.endpoint.__name__}"
                    )

    # Sanity: the sweep really did find routes (a broken import loop would
    # otherwise make the assertion below vacuously true).
    assert ("DELETE", "/library/api/document/{document_id}/blob") in seen

    assert owners == [
        "local_deep_research.web.routers.library_delete:delete_document"
    ], (
        "Exactly one router may own DELETE /library/api/document/<id>, and "
        f"it must be the note-guarded deletion router. Found: {owners}"
    )