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.

53.9 KB
Raw Blame
"""Ported from ``tests/notes/test_notes_routes_review_fixes.py`` on main
(deleted by the FastAPI migration).

Route-boundary fixes that don't fit the service-level test files. Driven
against ``web/routers/notes.py``, ``web/routers/library_delete.py`` and
``web/routers/rag.py`` -- the FastAPI successors of ``notes_routes.py``,
``research_library/deletion/routes/delete_routes.py`` and
``research_library/routes/rag_routes.py``.

Successor audit
---------------
32 of the original 47 are fully superseded on the branch and are NOT
re-ported:

* ``TestNotesRateLimitsAreKeyedPerUser`` (6) ->
  ``tests/web/routers/test_notes_rate_limit_keys.py``, which is strictly
  stronger (six buckets, decorator wiring, and live cross-user enforcement).
* ``TestFullStackProtection`` + ``TestDocumentDeletionRefusesNotes`` (8) ->
  ``tests/research_library/test_deletion_cascade_contracts.py`` and
  ``tests/security/test_library_notes_authz_fastapi.py``.
* ``TestProtectedCollectionRouteReturns409`` 409/404 arms,
  ``TestGetNoteVersionsRequiresIsNote``, the four reorder shape/auth guards,
  ``TestUpdateCollectionRefusesSystemTypes``, most of
  ``TestCreateCollectionTypeAllowlist``, ``TestNoteSubResourceRoutes404OnUnknownId``
  and ``TestNoteVersionRoutesAreScopedToTheirOwnNote`` ->
  ``tests/security/test_library_notes_authz_fastapi.py``.
* ``TestListNotesExposesTotal`` -> ported into ``tests/notes/test_notes_api.py``
  alongside the sibling offset assertions.

What remains here is what nothing on the branch would go red for.

Plumbing translation
--------------------
Flask ``test_request_context`` + ``flask_session["username"]`` -> a direct
call on the unwrapped FastAPI handler with a dummy ``Request`` (query string
in the ASGI scope) and explicit ``username`` / ``body`` arguments. Handlers
return a plain dict (200) or a ``JSONResponse``; ``_unpack`` normalises both.
``_handler`` peels the slowapi rate-limit wrappers where main peeled
``@login_required``.
"""

import asyncio
import inspect
import json as _json
import uuid
from contextlib import ExitStack, contextmanager
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
from fastapi.responses import JSONResponse
from starlette.requests import Request

from local_deep_research.database.models import (
    Document,
    NoteSynthesis,
    NoteSynthesisSource,
    SourceType,
)

from tests.notes.helpers import _generate_hash

USERNAME = "testuser"


@pytest.fixture
def real_engine():
    """In-memory SQLite engine + sessionmaker with FK enforcement on.

    Returns ``(engine, Session)``. Ported verbatim from main -- the paging
    tests need *separate* sessions over one database, which the shared
    ``db_session`` fixture cannot provide.
    """
    from sqlalchemy import create_engine, event as sa_event
    from sqlalchemy.orm import sessionmaker
    from local_deep_research.database.models.base import Base

    engine = create_engine("sqlite:///:memory:")

    @sa_event.listens_for(engine, "connect")
    def enable_fks(dbapi_conn, connection_record):
        cur = dbapi_conn.cursor()
        cur.execute("PRAGMA foreign_keys=ON")
        cur.close()

    Base.metadata.create_all(engine)
    try:
        yield engine, sessionmaker(bind=engine)
    finally:
        engine.dispose()


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


def _handler(fn):
    while hasattr(fn, "__wrapped__"):
        fn = fn.__wrapped__
    return fn


def _unpack(response):
    if isinstance(response, JSONResponse):
        return _json.loads(response.body), response.status_code
    return response, 200


def _request(path="/", method="GET", query=""):
    return Request(
        {
            "type": "http",
            "method": method,
            "path": path,
            "query_string": query.encode(),
            "headers": [],
            "session": {"session_id": "sess-1"},
        }
    )


def _call(path, handler, *args, method="GET", json=None, username=USERNAME):
    raw_path, _, query = path.partition("?")
    fn = _handler(handler)
    kwargs = {"username": username}
    if "body" in inspect.signature(fn).parameters:
        kwargs["body"] = json if json is not None else {}
    if inspect.iscoroutinefunction(fn):
        return _unpack(
            asyncio.run(fn(_request(raw_path, method, query), *args, **kwargs))
        )
    return _unpack(fn(_request(raw_path, method, query), *args, **kwargs))


@contextmanager
def _patch_sessions(monkeypatch, session_factory):
    """Point both ``get_user_db_session`` import sites at ``session_factory``."""

    @contextmanager
    def _fake(username=None, password=None):
        with session_factory() as s:
            yield s

    monkeypatch.setattr(
        "local_deep_research.database.session_context.get_user_db_session",
        _fake,
    )
    monkeypatch.setattr(
        "local_deep_research.research_library.notes.services.note_service.get_user_db_session",
        _fake,
    )
    yield


# ---------------------------------------------------------------------------


class TestSynthesizeRoutePersistsFilteredSources:
    """The ``synthesize_notes`` route must persist only the ids the AI
    service actually filtered through (``result['source_notes']``), not the
    raw client-supplied ``note_ids``.

    Pre-fix the route iterated ``data['note_ids']`` directly, so any non-note
    Document id got recorded as a ``NoteSynthesisSource`` row even though the
    LLM never saw it -- and a bogus id would FK-violate *after* create_note
    had already committed, leaving an orphan synthesized note.

    No branch test inspects the ``NoteSynthesisSource`` rows this route
    writes.
    """

    @pytest.fixture
    def patched(self, db_session, monkeypatch, note_source_type):
        @contextmanager
        def fake_session(username=None, password=None):
            yield db_session

        monkeypatch.setattr(
            "local_deep_research.database.session_context.get_user_db_session",
            fake_session,
        )
        monkeypatch.setattr(
            "local_deep_research.research_library.notes.services.note_service.get_user_db_session",
            fake_session,
        )
        return db_session

    def test_route_persists_only_filtered_source_ids(
        self, patched, note_source_type
    ):
        """End-to-end: client posts 3 ids; the AI service drops the bogus
        one; only the AI-filtered ids land in NoteSynthesisSource."""
        from local_deep_research.web.routers import notes as notes_routes

        note_a_id = str(uuid.uuid4())
        note_b_id = str(uuid.uuid4())
        result_doc_id = str(uuid.uuid4())
        for nid, title in (
            (note_a_id, "A"),
            (note_b_id, "B"),
            (result_doc_id, "synthesized"),
        ):
            patched.add(
                Document(
                    id=nid,
                    title=title,
                    text_content=title.lower(),
                    file_type="note",
                    file_size=1,
                    document_hash=_generate_hash(nid),
                    source_type_id=note_source_type.id,
                    tags=[],
                )
            )
        patched.commit()

        bogus_id = str(uuid.uuid4())
        raw_note_ids = [note_a_id, bogus_id, note_b_id]
        ai_result = {
            "source_notes": [
                {"id": note_a_id, "title": "A"},
                {"id": note_b_id, "title": "B"},
            ],
            "suggested_title": "synthesized",
            "content": "merged",
        }

        with (
            patch.object(notes_routes, "NoteAIService") as mock_ai_cls,
            patch.object(notes_routes, "NoteService") as mock_svc_cls,
        ):
            mock_ai = MagicMock()
            mock_ai.synthesize_notes = AsyncMock(return_value=ai_result)
            mock_ai_cls.return_value = mock_ai

            mock_svc = MagicMock()
            mock_svc.create_note.return_value = result_doc_id
            mock_svc_cls.return_value = mock_svc

            payload, status = _call(
                "/notes/api/notes/synthesize",
                notes_routes.synthesize_notes,
                method="POST",
                json={
                    "note_ids": raw_note_ids,
                    "synthesis_type": "merge",
                },
            )

        assert status == 201, payload
        assert payload["success"] is True

        persisted_ids = {
            s.source_document_id
            for s in patched.query(NoteSynthesisSource).all()
        }
        # Only the AI-filtered ids land -- NOT the raw client list.
        assert persisted_ids == {note_a_id, note_b_id}, (
            "Route persisted raw client note_ids instead of AI-filtered "
            "source_notes."
        )
        assert bogus_id not in persisted_ids

        # And exactly one NoteSynthesis row, against the right note.
        syntheses = patched.query(NoteSynthesis).all()
        assert len(syntheses) == 1
        assert syntheses[0].result_document_id == result_doc_id


class TestSynthesizeRouteStatusMapping:
    """``synthesize_notes`` maps three branches to distinct status codes
    that the persistence test above (a successful ``create_note=True``) never
    exercises:

      * create_note=False    -> 200, NO note created, no synthesis rows
      * result has 'error'   -> 400, message forwarded verbatim, no create
      * note_ids not a list  -> 400 before the AI service is constructed

    The branch's element-type test only ever passes LISTS, so the
    ``isinstance(note_ids, list)`` half is unpinned -- a raw string would be
    iterated character by character into one-char note ids.
    """

    def test_synthesize_create_note_false_returns_200_and_skips_create(
        self, db_session, monkeypatch, note_source_type
    ):
        from local_deep_research.web.routers import notes as notes_routes

        @contextmanager
        def fake_session(username=None, password=None):
            yield db_session

        monkeypatch.setattr(
            "local_deep_research.database.session_context.get_user_db_session",
            fake_session,
        )

        note_a_id = str(uuid.uuid4())
        note_b_id = str(uuid.uuid4())
        ai_result = {
            "source_notes": [{"id": note_a_id, "title": "A"}],
            "suggested_title": "T",
            "content": "merged",
        }

        with (
            patch.object(notes_routes, "NoteAIService") as mock_ai_cls,
            patch.object(notes_routes, "NoteService") as mock_svc_cls,
        ):
            mock_ai = MagicMock()
            mock_ai.synthesize_notes = AsyncMock(return_value=ai_result)
            mock_ai_cls.return_value = mock_ai
            mock_svc = MagicMock()
            mock_svc_cls.return_value = mock_svc

            payload, status = _call(
                "/notes/api/notes/synthesize",
                notes_routes.synthesize_notes,
                method="POST",
                json={
                    "note_ids": [note_a_id, note_b_id],
                    "synthesis_type": "merge",
                    "create_note": False,
                },
            )

            # create_note must NOT have been invoked.
            mock_svc.create_note.assert_not_called()

        assert status == 200, (
            f"create_note=False must map to 200, not 201; got {status}."
        )
        assert payload["success"] is True
        assert payload["result"] == ai_result
        assert "note_id" not in payload["result"], (
            "create_note=False must not stamp a note_id (creation skipped)."
        )

        # No persistence side effects at all.
        assert db_session.query(NoteSynthesis).count() == 0
        assert db_session.query(NoteSynthesisSource).count() == 0

    def test_synthesize_error_result_maps_to_400(self):
        """An AI result carrying 'error' is forwarded as a 400 with the
        message verbatim, and the creation block is short-circuited."""
        from local_deep_research.web.routers import notes as notes_routes

        with (
            patch.object(notes_routes, "NoteAIService") as mock_ai_cls,
            patch.object(notes_routes, "NoteService") as mock_svc_cls,
        ):
            mock_ai = MagicMock()
            mock_ai.synthesize_notes = AsyncMock(
                return_value={"error": "too few notes (need 2-5)"}
            )
            mock_ai_cls.return_value = mock_ai
            mock_svc = MagicMock()
            mock_svc_cls.return_value = mock_svc

            payload, status = _call(
                "/notes/api/notes/synthesize",
                notes_routes.synthesize_notes,
                method="POST",
                json={
                    "note_ids": [str(uuid.uuid4()), str(uuid.uuid4())],
                    "synthesis_type": "merge",
                },
            )

            # Error branch must return before any note is created.
            mock_svc.create_note.assert_not_called()

        assert status == 400, f"An 'error' result must map to 400, got {status}"
        assert payload["success"] is False
        assert payload["error"] == "too few notes (need 2-5)", (
            "The route must forward result['error'] verbatim."
        )

    def test_synthesize_non_list_note_ids_returns_400(self):
        """A truthy non-list ``note_ids`` (a raw string) passes the
        ``not data.get('note_ids')`` guard but must be rejected by the
        isinstance(list) guard with a 400 -- before the AI service is even
        constructed, so the string is never iterated char by char."""
        from local_deep_research.web.routers import notes as notes_routes

        with patch.object(notes_routes, "NoteAIService") as mock_ai_cls:
            payload, status = _call(
                "/notes/api/notes/synthesize",
                notes_routes.synthesize_notes,
                method="POST",
                json={
                    "note_ids": "note-a,note-b",
                    "synthesis_type": "merge",
                },
            )

            # The guard must fire before the AI service is built.
            mock_ai_cls.assert_not_called()

        assert status == 400
        assert payload["success"] is False
        assert payload["error"] == "note_ids must be a list of strings"


class TestProtectedCollectionRouteStatusMapping:
    """``DELETE /library/api/collections/{id}`` triages the service's
    ``deleted: False`` result three ways: 404 not-found, 409 protected type,
    else 400.

    The branch pins the 409 and 404 arms; the *else* arm -- an ordinary
    failure -- is unpinned, and collapsing it into 409 or 404 would tell the
    client a delete is impossible when it merely failed.
    """

    def _delete(self, result):
        from local_deep_research.web.routers import (
            library_delete as delete_routes,
        )

        with patch.object(
            delete_routes, "CollectionDeletionService"
        ) as mock_service_cls:
            mock_service = MagicMock()
            mock_service.delete_collection.return_value = result
            mock_service_cls.return_value = mock_service
            payload, status = _call(
                "/library/api/collections/abc",
                delete_routes.delete_collection,
                "abc",
                method="DELETE",
            )
        return payload, status, mock_service

    def test_route_still_returns_400_for_normal_failure(self):
        """Sanity: a non-protection delete failure still maps to 400."""
        payload, status, service = self._delete(
            {
                "deleted": False,
                "collection_id": "abc",
                "error": "Something else went wrong",
            }
        )
        assert status == 400, payload
        # The route must hand the path id straight to the service.
        service.delete_collection.assert_called_once_with(
            "abc", delete_orphaned_documents=True
        )

    def test_route_returns_404_for_not_found(self):
        _payload, status, _ = self._delete(
            {
                "deleted": False,
                "collection_id": "abc",
                "error": "Collection not found",
            }
        )
        assert status == 404

    def test_route_returns_409_for_protected_type(self):
        payload, status, _ = self._delete(
            {
                "deleted": False,
                "collection_id": "abc",
                "collection_name": "Notes",
                "collection_type": "notes",
                "error": (
                    "Cannot delete system collection 'Notes' (type=notes). "
                    "This collection holds first-class user data."
                ),
            }
        )
        assert status == 409, payload
        assert payload["success"] is False
        assert "system collection" in payload["error"].lower()


class TestReorderResearchRouteBoundary:
    """Route-only branches of ``POST /api/notes/{id}/research/reorder``.

    The shape guards and the ok==False message are pinned by
    ``tests/security/test_library_notes_authz_fastapi.py``; the service
    ``ValueError`` -> 400 mapping (e.g. the linked-research cap) is not, and
    without it the client gets an opaque 500 for a fixable request.
    """

    def _call_reorder(self, json_body, patch_service=None):
        from local_deep_research.web.routers import notes as notes_routes

        if patch_service is None:
            return _call(
                "/notes/api/notes/n1/research/reorder",
                notes_routes.reorder_note_research,
                "n1",
                method="POST",
                json=json_body,
            )
        with patch.object(notes_routes, "NoteService") as mock_svc_cls:
            mock_svc = MagicMock()
            patch_service(mock_svc)
            mock_svc_cls.return_value = mock_svc
            return _call(
                "/notes/api/notes/n1/research/reorder",
                notes_routes.reorder_note_research,
                "n1",
                method="POST",
                json=json_body,
            )

    def test_reorder_value_error_surfaces_as_400(self):
        def _raise(svc):
            svc.note_exists.return_value = True
            svc.reorder_note_research.side_effect = ValueError(
                "private validation detail"
            )

        payload, status = self._call_reorder(
            {"research_ids": ["a", "b"]}, patch_service=_raise
        )
        assert status == 400, payload
        assert payload["error"] == "Invalid research order"
        assert "private validation detail" not in payload["error"]

    def test_reorder_service_mismatch_returns_400_with_distinct_message(self):
        """Positive control for the above: the ok==False branch keeps its own
        message and does not collapse into the shape-guard text."""
        payload, status = self._call_reorder(
            {"research_ids": ["a", "b"]},
            patch_service=lambda svc: (
                setattr(svc.note_exists, "return_value", True),
                setattr(svc.reorder_note_research, "return_value", False),
            ),
        )
        assert status == 400, payload
        assert (
            payload["error"]
            == "research_ids do not match the note's linked research"
        )
        assert payload["error"] != "research_ids must be a non-empty list"


class TestNoteAIServiceRejectsNonNoteDocuments:
    """The six AI endpoints (summarize, key-concepts, research-questions,
    similar, related-research, suggest-links) all flow through
    ``NoteAIService._get_note_content`` / ``_get_note``. Pre-fix these
    helpers queried Document by id only, so a caller passing a PDF or
    research-result Document UUID would get the AI to summarize the wrong
    document type. Within-user scope only (the per-user encrypted DB rules
    out cross-user IDOR) but a real type confusion against the codebase's own
    ``_is_note`` contract.

    The branch's ``TestNoteAIServiceHelpers`` tests of these two methods are
    shadow tests: they query the DB directly and never call the methods.
    """

    def test_get_note_content_rejects_non_note_document(
        self, db_session, note_source_type, monkeypatch
    ):
        from local_deep_research.research_library.notes.services.note_ai_service import (
            NoteAIService,
        )

        # Seed a PDF Document (NOT a note).
        pdf_source_type = SourceType(
            id=str(uuid.uuid4()),
            name="document",
            display_name="Document",
        )
        db_session.add(pdf_source_type)
        db_session.commit()

        pdf_id = str(uuid.uuid4())
        db_session.add(
            Document(
                id=pdf_id,
                title="research-paper.pdf",
                text_content="full paper body",
                file_type="pdf",
                file_size=15,
                document_hash=_generate_hash(f"pdf-ai-{pdf_id}"),
                source_type_id=pdf_source_type.id,
                tags=[],
            )
        )
        db_session.commit()

        @contextmanager
        def _fake_session(username, password=None):
            yield db_session

        monkeypatch.setattr(
            "local_deep_research.research_library.notes.services.note_ai_service.get_user_db_session",
            _fake_session,
        )

        svc = NoteAIService("test_user")
        # The PDF exists and has content, but it's not a note. The AI
        # service must refuse it by returning None -- preventing the six
        # AI endpoints from quietly operating on non-note Documents.
        assert svc._get_note_content(pdf_id) is None
        assert svc._get_note(pdf_id) is None


class TestSuggestTagsRouteValidation:
    """``POST /api/notes/suggest-tags`` input guards live on the route, not
    the service: the existing_tags list-of-strings check (a non-list is
    interpolated downstream and 500s) and the ``_assert_content_size``
    wiring (a truthy non-string content passes the ``not content`` guard and
    must be rejected as a clean 400).

    ``tests/security/test_library_notes_authz_fastapi.py`` covers non-string
    ELEMENTS; it never passes a non-list, never passes a non-string
    ``content``, and never asserts the forwarded kwargs.
    """

    def _call_suggest(self, json_body, ai_setup=None):
        from local_deep_research.web.routers import notes as notes_routes

        with patch.object(notes_routes, "NoteAIService") as mock_ai_cls:
            mock_ai = MagicMock()
            # suggest_tags is awaited by the async route (#5854).
            mock_ai.suggest_tags = AsyncMock(return_value=[])
            if ai_setup is not None:
                ai_setup(mock_ai)
            mock_ai_cls.return_value = mock_ai
            payload, status = _call(
                "/notes/api/notes/suggest-tags",
                notes_routes.suggest_tags,
                method="POST",
                json=json_body,
            )
        return status, payload, mock_ai

    def test_suggest_tags_rejects_non_list_existing_tags(self):
        status, payload, mock_ai = self._call_suggest(
            {"content": "some note", "existing_tags": "not-a-list"}
        )
        assert status == 400, payload
        assert payload["success"] is False
        assert payload["error"] == "existing_tags must be a list of strings"
        mock_ai.suggest_tags.assert_not_called()

    def test_suggest_tags_content_size_check_wired(self):
        """A truthy non-string content (123) passes the ``not content`` guard
        and reaches ``_assert_content_size``, which raises ValueError ->
        mapped to a 400. Proves the size check is wired into THIS route."""
        status, payload, mock_ai = self._call_suggest({"content": 123})
        assert status == 400, payload
        assert payload["success"] is False
        assert payload["error"] == "Invalid tag request"
        mock_ai.suggest_tags.assert_not_called()

    def test_suggest_tags_unconfigured_llm_is_reported_as_model_not_configured_not_as_invalid_input(
        self,
    ):
        """A fresh install with no ``llm.model`` must not be told its request
        was invalid.

        ``NoteAIService.suggest_tags`` -> ``_get_llm`` -> ``get_llm`` raises
        ``LLMNotConfiguredError`` (a ``ValueError`` subclass) for every
        LLM-configuration failure, so a perfectly valid body fell into the
        ``except ValueError`` arm and came back as a request-validation 400
        carrying the exception's own setup text -- rendered verbatim by
        note-detail.js -- sending the user hunting through a body that was
        fine.

        The status stays 400 (an unconfigured integration is client state,
        not a server fault -- the same call ``web/routers/api.py``,
        ``followup.py`` and ``zotero.py`` make); what changes is that the
        body now SAYS which kind of 400 it is, via a fixed message and
        ``error_type: model_not_configured``. Deleting the
        ``except LLMNotConfiguredError`` arm makes this exception fall into
        the ``except ValueError`` arm below it: the status still matches, so
        the ``error``/``error_type`` assertions are the ones carrying this
        test.
        """
        from local_deep_research.config.llm_config import (
            LLM_NOT_CONFIGURED_CLIENT_MESSAGE,
            LLMNotConfiguredError,
        )

        leak = (
            "LLM model not configured; snapshot loaded from "
            "/srv/ldr-data/users/alice/settings.db"
        )
        status, payload, mock_ai = self._call_suggest(
            {"content": "some note"},
            ai_setup=lambda ai: setattr(
                ai.suggest_tags, "side_effect", LLMNotConfiguredError(leak)
            ),
        )
        assert status == 400, payload
        assert payload["success"] is False
        assert payload["error"] == LLM_NOT_CONFIGURED_CLIENT_MESSAGE
        assert payload["error_type"] == "model_not_configured"
        # A fixed constant, not the (path-bearing) exception text.
        assert "/srv/ldr-data" not in payload["error"]
        assert "settings.db" not in payload["error"]

    def test_suggest_tags_service_value_error_stays_a_bad_request(self):
        """Non-vacuity control for the branch above: an ordinary
        ``ValueError`` from the service is still the request-validation 400,
        with no exception text in the body. Without this, widening the
        configuration arm to catch every ``ValueError`` would go unnoticed.
        """
        leak = "tag row rejected: /srv/ldr-data/users/alice/library.db"
        status, payload, mock_ai = self._call_suggest(
            {"content": "some note"},
            ai_setup=lambda ai: setattr(
                ai.suggest_tags, "side_effect", ValueError(leak)
            ),
        )
        assert status == 400, payload
        assert payload["success"] is False
        assert payload["error"] == "Invalid tag request"
        # Both arms answer 400, so the classification -- not the status --
        # is what separates them. An ordinary ValueError must NOT be
        # labelled a configuration problem.
        assert "error_type" not in payload
        assert "/srv/ldr-data" not in payload["error"]

    def test_suggest_tags_valid_existing_tags_passes_to_service(self):
        """Positive control: the guard doesn't false-positive on valid input
        -- valid existing_tags reach the service and 200 is returned."""
        status, payload, mock_ai = self._call_suggest(
            {"content": "x", "existing_tags": ["a", "b"]},
            ai_setup=lambda ai: setattr(
                ai.suggest_tags, "return_value", ["t1"]
            ),
        )
        assert status == 200, payload
        assert payload["success"] is True
        assert payload["tags"] == ["t1"]
        mock_ai.suggest_tags.assert_called_once_with(
            content="x", existing_tags=["a", "b"]
        )


# Every LLM-backed route in web/routers/notes.py, i.e. every route whose
# body transitively reaches ``NoteAIService._get_llm`` ->
# ``config.llm_config.get_llm``. Derived by hand from the service methods
# that call ``_get_llm`` (summarize_note, extract_research_questions,
# suggest_tags, extract_key_concepts, extract_claims, grade_all_claims,
# semantic_diff, synthesize_notes) and the routes that call them; keep the
# two in step when a new AI route lands.
#
# ``raise_at`` says where the typed error is injected:
#   "service"      -- on the ``NoteAIService`` method the route calls, which
#                     is exactly where the real ``_get_llm`` raises;
#   "grade_helper" -- on ``notes._grade_note_claims``, the module helper that
#                     owns the ``ai.grade_all_claims()`` call for the grade
#                     route. Driving that helper for real needs a completed
#                     research row, a link row and a stored report; the arm
#                     under test here is the route's, and the helper is the
#                     unit that raises through it.
_LLM_BACKED_NOTES_ROUTES = [
    (
        "summarize",
        "/notes/api/notes/n1/summarize",
        "summarize_note",
        ("n1",),
        {},
        "service",
    ),
    (
        "research-questions",
        "/notes/api/notes/n1/research-questions",
        "extract_research_questions",
        ("n1",),
        {},
        "service",
    ),
    (
        "suggest-tags",
        "/notes/api/notes/suggest-tags",
        "suggest_tags",
        (),
        {"content": "some note"},
        "service",
    ),
    (
        "key-concepts",
        "/notes/api/notes/n1/key-concepts",
        "extract_key_concepts",
        ("n1",),
        {},
        "service",
    ),
    (
        "fact-check",
        "/notes/api/notes/n1/fact-check",
        "fact_check_note",
        ("n1",),
        {},
        "service",
    ),
    (
        "fact-check-grade",
        "/notes/api/notes/n1/fact-check/r1/grade",
        "grade_note_fact_check",
        ("n1", "r1"),
        {"claims": ["the report says the sky is blue"]},
        "grade_helper",
    ),
    (
        "synthesize-preview",
        "/notes/api/notes/synthesize/preview",
        "preview_synthesis",
        (),
        {"note_ids": ["a", "b"], "synthesis_type": "merge"},
        "service",
    ),
    (
        "synthesize",
        "/notes/api/notes/synthesize",
        "synthesize_notes",
        (),
        {"note_ids": ["a", "b"], "synthesis_type": "merge"},
        "service",
    ),
    (
        "semantic-diff",
        "/notes/api/notes/n1/versions/semantic-diff?version1=v1&version2=current",
        "get_semantic_diff",
        ("n1",),
        None,
        "service",
    ),
]

# The ``NoteAIService`` methods that call ``_get_llm``.
_AI_METHODS_USING_THE_LLM = (
    "summarize_note",
    "extract_research_questions",
    "suggest_tags",
    "extract_key_concepts",
    "extract_claims",
    "grade_all_claims",
    "semantic_diff",
    "synthesize_notes",
)


class TestUnconfiguredLLMIsClassifiedOnEveryNotesAIRoute:
    """No LLM configured must read the same on ALL nine LLM-backed routes.

    ``suggest-tags`` was the only route with a typed arm; its eight siblings
    let ``LLMNotConfiguredError`` fall into ``except Exception`` and answered
    500 "An internal error occurred. Please try again or contact support."
    -- a server-fault message for a fresh install that has simply not chosen
    a model yet, and one that gives the user nothing to act on.

    The property: every route that can reach ``get_llm`` answers 400 with the
    fixed ``LLM_NOT_CONFIGURED_CLIENT_MESSAGE`` and
    ``error_type: model_not_configured``. Deleting the shared
    ``_llm_not_configured_response`` arm from any one route turns that row
    red (500 + "contact support"), and changing the status, message or
    ``error_type`` in the helper turns all nine red at once -- which is the
    point of routing them through one helper.
    """

    @pytest.fixture
    def unconfigured(self, monkeypatch):
        """Real routes, mocked service/session layer, ``get_llm`` unusable."""
        from local_deep_research.config.llm_config import (
            LLMNotConfiguredError,
        )

        leak = (
            "LLM model not configured; snapshot loaded from "
            "/srv/ldr-data/users/alice/settings.db"
        )

        @contextmanager
        def _fake_session(username=None, password=None):
            yield MagicMock()

        # semantic-diff reads the Document/NoteVersion rows itself before it
        # ever builds the AI service.
        monkeypatch.setattr(
            "local_deep_research.database.session_context.get_user_db_session",
            _fake_session,
        )
        return leak, LLMNotConfiguredError

    @pytest.mark.parametrize(
        "path, handler_name, args, body, raise_at",
        [row[1:] for row in _LLM_BACKED_NOTES_ROUTES],
        ids=[row[0] for row in _LLM_BACKED_NOTES_ROUTES],
    )
    def test_route_answers_model_not_configured(
        self, unconfigured, path, handler_name, args, body, raise_at
    ):
        from local_deep_research.config.llm_config import (
            LLM_NOT_CONFIGURED_CLIENT_MESSAGE,
        )
        from local_deep_research.research_library.notes.services.note_ai_service import (
            NoteAIService as RealNoteAIService,
        )
        from local_deep_research.web.routers import notes as notes_routes

        leak, error_cls = unconfigured

        ai = MagicMock()
        for method in _AI_METHODS_USING_THE_LLM:
            getattr(ai, method).side_effect = error_cls(leak)

        note_service = MagicMock()
        note_service.note_exists.return_value = True
        note_service._is_note.return_value = True

        with ExitStack() as patches:
            service_cls = patches.enter_context(
                patch.object(notes_routes, "NoteAIService", return_value=ai)
            )
            # The grade route slices its claim list with
            # ``NoteAIService.MAX_CLAIMS_PER_NOTE``; a MagicMock attribute
            # there is a TypeError, not a slice. Keep the real bound.
            service_cls.MAX_CLAIMS_PER_NOTE = (
                RealNoteAIService.MAX_CLAIMS_PER_NOTE
            )
            patches.enter_context(
                patch.object(
                    notes_routes, "NoteService", return_value=note_service
                )
            )
            if raise_at == "grade_helper":
                patches.enter_context(
                    patch.object(
                        notes_routes,
                        "_grade_note_claims",
                        side_effect=error_cls(leak),
                    )
                )
            payload, status = _call(
                path,
                getattr(notes_routes, handler_name),
                *args,
                method="GET" if body is None else "POST",
                json=body,
            )

        assert status == 400, payload
        assert payload["success"] is False
        assert payload["error"] == LLM_NOT_CONFIGURED_CLIENT_MESSAGE
        assert payload["error_type"] == "model_not_configured"
        # Never the exception's own setup text, which carries a settings path.
        assert "/srv/ldr-data" not in payload["error"]
        assert "settings.db" not in payload["error"]


class TestUnconfiguredLLMThroughTheRealServiceChain:
    """End-to-end over the chain the route actually runs.

    Every other test here injects at a mocked ``NoteAIService``, which pins
    the route's ``except`` arm but assumes the service really does let
    ``LLMNotConfiguredError`` out of ``suggest_tags``. It does not: each AI
    method wraps its body in ``except Exception: logger.exception(...);
    raise``, and a future ``except ValueError: return []`` there would
    swallow the configuration failure and hand the user an empty tag list
    with a 200 -- with every mocked-service test still green.

    So this one patches only ``config.llm_config.get_llm`` (plus the DB
    session and settings manager that ``_build_settings_snapshot`` needs)
    and drives the REAL ``NoteAIService`` through the real route.
    """

    def test_real_note_ai_service_surfaces_the_typed_error_to_the_route(
        self, monkeypatch
    ):
        from local_deep_research.config import llm_config
        from local_deep_research.web.routers import notes as notes_routes

        leak = (
            "LLM model not configured. Please open Settings, choose an LLM "
            "provider, and select a model name. Snapshot read from "
            "/srv/ldr-data/users/alice/settings.db"
        )

        def _unconfigured_get_llm(*args, **kwargs):
            raise llm_config.LLMNotConfiguredError(leak)

        monkeypatch.setattr(llm_config, "get_llm", _unconfigured_get_llm)

        # Only the DB/session layer is mocked: _build_settings_snapshot opens
        # the per-user encrypted DB to build the snapshot it hands get_llm.
        @contextmanager
        def _fake_session(username=None, password=None):
            yield MagicMock()

        monkeypatch.setattr(
            "local_deep_research.research_library.notes.services."
            "note_ai_service.get_user_db_session",
            _fake_session,
        )
        monkeypatch.setattr(
            "local_deep_research.settings.manager.SettingsManager",
            lambda session: MagicMock(get_settings_snapshot=lambda: {}),
        )

        payload, status = _call(
            "/notes/api/notes/suggest-tags",
            notes_routes.suggest_tags,
            method="POST",
            json={"content": "a genuinely valid note body"},
        )

        assert status == 400, payload
        assert payload["success"] is False
        assert payload["error"] == llm_config.LLM_NOT_CONFIGURED_CLIENT_MESSAGE
        assert payload["error_type"] == "model_not_configured"
        assert "/srv/ldr-data" not in payload["error"]


class TestCreateCollectionTypeAllowlist:
    """``POST /library/api/collections`` must reject system collection types.

    Pre-fix the route passed a user-supplied ``type`` straight to the model,
    so ``{"type": "notes"}`` created an impostor Notes collection:
    undeletable under PROTECTED_COLLECTION_TYPES and able to
    nondeterministically win ``_get_or_create_notes_collection``'s unordered
    ``.first()`` lookup, homing new notes in the wrong collection.

    The branch pins the plain rejections and the default. What it does NOT
    pin is that the allowlist is EXACT MATCH: if a ``.strip().lower()``
    normalisation is ever added, ``"notes "`` slips through and recreates the
    impostor. That leg is ported here.

    Plumbing note: on the branch the endpoint is ``async def
    create_collection`` and immediately hands off to the synchronous
    ``_create_collection_sync(data, username)`` via ``run_db_sync``. Every
    validation guard -- including this allowlist -- lives in the sync half,
    ahead of any DB session, so the test drives that directly instead of
    building an event loop and a fake session around the async shell.
    """

    def _post_create(self, payload):
        from local_deep_research.web.routers import rag as rag_routes

        return _unpack(rag_routes._create_collection_sync(payload, USERNAME))

    @pytest.mark.parametrize(
        "bad", ["User_Uploads", " user_uploads ", "USER_COLLECTION", "notes "]
    )
    def test_allowlist_is_exact_match_no_normalization(self, bad):
        """Case / whitespace variants must NOT be normalized in. Critically
        ``'notes '`` must not slip through to create an undeletable impostor
        Notes collection."""
        payload, status = self._post_create({"name": "Trap", "type": bad})
        assert status == 400, (
            f"type={bad!r} must be rejected (no normalization), got {status}: "
            f"{payload}"
        )
        assert payload["success"] is False
        assert "type" in str(payload["error"]).lower()

    @pytest.mark.parametrize("good", ["user_uploads", "user_collection"])
    def test_allowlisted_types_are_accepted_and_round_trip(self, good):
        """Positive control: both allowlisted types create successfully and
        round-trip their collection_type -- a blanket reject would make the
        no-normalization test above pass vacuously."""
        from datetime import datetime, timezone
        from local_deep_research.web.routers import rag as rag_routes

        @contextmanager
        def _fake_session(username, password=None):
            fake_db = MagicMock()
            # No existing collection with this name.
            fake_db.query.return_value.filter_by.return_value.first.return_value = None

            def _add(obj):
                # Stamp the column default a real INSERT would set (the
                # MagicMock session never performs one).
                if getattr(obj, "created_at", None) is None:
                    obj.created_at = datetime.now(timezone.utc)

            fake_db.add.side_effect = _add
            yield fake_db

        with patch(
            "local_deep_research.database.session_context.get_user_db_session",
            _fake_session,
        ):
            payload, status = _unpack(
                rag_routes._create_collection_sync(
                    {"name": f"C {good}", "type": good}, USERNAME
                )
            )

        assert status == 200, payload
        assert payload["success"] is True
        assert payload["collection"]["collection_type"] == good

    def test_omitted_type_defaults_to_user_uploads(self):
        from datetime import datetime, timezone
        from local_deep_research.web.routers import rag as rag_routes

        @contextmanager
        def _fake_session(username, password=None):
            fake_db = MagicMock()
            fake_db.query.return_value.filter_by.return_value.first.return_value = None
            fake_db.add.side_effect = lambda obj: setattr(
                obj, "created_at", datetime.now(timezone.utc)
            )
            yield fake_db

        with patch(
            "local_deep_research.database.session_context.get_user_db_session",
            _fake_session,
        ):
            payload, status = _unpack(
                rag_routes._create_collection_sync(
                    {"name": "No Type"}, USERNAME
                )
            )

        assert status == 200, payload
        assert payload["collection"]["collection_type"] == "user_uploads"


class TestGetNoteVersionsOffsetPagination:
    """``get_note_versions`` accepts an ``offset`` query param so older
    versions are reachable when total > limit. Pre-fix the route only had
    ``limit``, so versions 1 through (total - limit) were unreachable.

    Nothing on the branch pages this route: the pagination census covers
    ``list_notes`` only.
    """

    def _seed(self, Session, tied=False):
        from datetime import datetime, timedelta, timezone
        from local_deep_research.database.models import NoteVersion

        with Session() as session:
            note_st = SourceType(
                id=str(uuid.uuid4()), name="note", display_name="Note"
            )
            session.add(note_st)
            session.commit()

            note_id = str(uuid.uuid4())
            session.add(
                Document(
                    id=note_id,
                    title="paged",
                    text_content="body",
                    file_type="note",
                    file_size=4,
                    document_hash=_generate_hash(f"paged-{note_id}"),
                    source_type_id=note_st.id,
                    tags=[],
                )
            )

            base = datetime(2025, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
            for i in range(25):
                session.add(
                    NoteVersion(
                        id=str(uuid.uuid4()),
                        document_id=note_id,
                        title=f"v{i + 1}",
                        content=f"body{i + 1}",
                        tags=[],
                        change_type="manual_save",
                        content_hash=_generate_hash(f"pv-{i}-{note_id}"),
                        created_at=base
                        if tied
                        else base + timedelta(seconds=i),
                    )
                )
            session.commit()
        return note_id

    def test_offset_returns_older_versions_with_stable_numbering(
        self, real_engine, monkeypatch
    ):
        from local_deep_research.web.routers import notes as notes_routes

        _engine, Session = real_engine
        note_id = self._seed(Session)

        with _patch_sessions(monkeypatch, Session):
            # Page 1: default limit=20, offset=0 -> newest 20.
            body, status = _call(
                "/notes/api/notes/x/versions",
                notes_routes.get_note_versions,
                note_id,
            )
            assert status == 200, body
            assert body["success"] is True
            assert body["total"] == 25
            assert len(body["versions"]) == 20
            # Newest version is global #25; the 20th returned is global #6.
            assert body["versions"][0]["version_number"] == 25
            assert body["versions"][-1]["version_number"] == 6

            # Page 2: offset=20 -> versions 5, 4, 3, 2, 1.
            body, status = _call(
                "/notes/api/notes/x/versions?offset=20&limit=10",
                notes_routes.get_note_versions,
                note_id,
            )
            assert status == 200, body
            assert body["total"] == 25
            assert body["offset"] == 20
            # 5 versions remain (25 - 20 = 5), so 5 returned despite limit=10.
            assert len(body["versions"]) == 5
            assert [v["version_number"] for v in body["versions"]] == [
                5,
                4,
                3,
                2,
                1,
            ]

    def test_invalid_offset_returns_400(self, real_engine, monkeypatch):
        from local_deep_research.web.routers import notes as notes_routes

        _engine, Session = real_engine

        with _patch_sessions(monkeypatch, Session):
            body, status = _call(
                "/notes/api/notes/x/versions?offset=banana",
                notes_routes.get_note_versions,
                "does-not-matter",
            )

        assert status == 400, body
        assert "offset" in body["error"].lower()

    def test_versions_paging_no_duplicate_or_skipped_id_when_created_at_ties(
        self, real_engine, monkeypatch
    ):
        """When every version shares the same created_at second, the
        ``created_at.desc()`` ordering ties for every row and the
        ``id.desc()`` secondary sort is the sole discriminator. Drop it and
        SQLite's OFFSET/LIMIT row order becomes unstable across the separate
        page queries -- a row repeats on one page and another is skipped.
        This pages through all 25 and asserts no duplicate / no skipped id.
        """
        from local_deep_research.web.routers import notes as notes_routes

        _engine, Session = real_engine
        note_id = self._seed(Session, tied=True)

        all_ids = []
        all_version_numbers = []
        with _patch_sessions(monkeypatch, Session):
            for offset in (0, 10, 20):
                body, status = _call(
                    f"/notes/api/notes/x/versions?offset={offset}&limit=10",
                    notes_routes.get_note_versions,
                    note_id,
                )
                assert status == 200, body
                assert body["total"] == 25
                all_ids.extend(v["id"] for v in body["versions"])
                all_version_numbers.extend(
                    v["version_number"] for v in body["versions"]
                )

        # No id repeats across pages and none is skipped.
        assert len(all_ids) == 25
        assert len(set(all_ids)) == 25, (
            "A version id repeated or was skipped across pages -- the "
            "id.desc() tiebreaker was dropped, leaving paging unstable when "
            "created_at ties."
        )
        # Positional numbering stays contiguous 25..1 regardless of tie order.
        assert all_version_numbers == list(range(25, 0, -1))

    def test_versions_query_declares_the_id_desc_tiebreaker(self):
        """The tiebreaker is OUTPUT-INVISIBLE on SQLite, so pin it
        structurally.

        ``test_versions_paging_no_duplicate_or_skipped_id_when_created_at_ties``
        above is the behavioural probe main shipped, and it is the right
        assertion -- but it was mutation-checked here and SURVIVED deleting
        ``NoteVersion.id.desc()``: for this row set SQLite's OFFSET/LIMIT scan
        happens to stay in rowid order even with a fully tied sort key, so no
        row repeats or is skipped. The instability the tiebreaker prevents is
        a query-planner-dependent property no fixture can force deterministically.

        Assert the ordering clause itself instead, the way
        ``tests/web/test_pagination_clamping_census.py`` asserts
        output-invisible clamps: walk the handler's AST and require the
        ``order_by`` that sorts by ``created_at`` to also carry
        ``NoteVersion.id.desc()``. Deleting the tiebreaker fails HERE even
        when the behavioural probe cannot see it.
        """
        import ast
        import inspect
        import textwrap

        from local_deep_research.web.routers import notes as notes_routes

        src = textwrap.dedent(
            inspect.getsource(_handler(notes_routes.get_note_versions))
        )

        checked = 0
        for node in ast.walk(ast.parse(src)):
            if not (
                isinstance(node, ast.Call)
                and isinstance(node.func, ast.Attribute)
                and node.func.attr == "order_by"
            ):
                continue
            args = [ast.unparse(a) for a in node.args]
            if not any("created_at" in a for a in args):
                continue
            checked += 1
            assert "NoteVersion.id.desc()" in args, (
                "get_note_versions orders by created_at without the "
                f"NoteVersion.id.desc() tiebreaker (order_by args: {args}). "
                "With created_at ties, OFFSET/LIMIT paging can repeat or "
                "skip rows depending on the query plan."
            )

        assert checked == 1, (
            "expected exactly one created_at ordering in get_note_versions; "
            f"found {checked}. The structural probe stopped observing the "
            "clause it was written to guard."
        )


class TestFlattenedValueErrorArmsWithholdServiceText:
    """Three route arms replace a service ``ValueError`` with a fixed string.

    ``create_note`` and ``update_note`` -> "Invalid note data";
    ``create_document_annotation`` -> "Invalid annotation data". Each used to
    return ``{"error": str(e)}``, so whatever the service raised -- a
    SQLAlchemy constraint message carrying an absolute DB path, or any future
    exception that reaches those handlers -- went straight to the browser.

    Why these three need their own tests: the same two strings are asserted
    elsewhere in the suite, but always against a DIFFERENT route
    (``create_research_note`` in ``test_research_notes_routes.py``,
    ``create_research_annotation`` in ``test_notes_router_fastapi.py``,
    ``create_document_note`` in ``test_notes_api.py``). Reverting any ONE of
    the three arms below to ``str(e)`` left the whole suite green.

    Each test drives the arm through the service call it guards, so restoring
    ``{"error": str(e)}`` on that arm fails both the equality assertion and
    the leak assertions.
    """

    # Deliberately shaped like the exception the arms are meant to contain:
    # a server path, SQL text and a credential in one message.
    LEAK = (
        "IntegrityError at /srv/ldr-data/users/alice/library.db "
        "[SQL: INSERT INTO documents] sql credentials password=hunter2"
    )

    def _assert_withheld(self, payload, status, expected):
        assert status == 400, payload
        assert payload == {"success": False, "error": expected}
        assert "/srv/ldr-data" not in payload["error"]
        assert "INSERT INTO" not in payload["error"]
        assert "hunter2" not in payload["error"]

    def test_create_note_withholds_service_exception_text(self):
        from local_deep_research.web.routers import notes as notes_routes

        with patch.object(notes_routes, "NoteService") as cls:
            cls.return_value.create_note.side_effect = ValueError(self.LEAK)
            payload, status = _call(
                "/notes/api/notes",
                notes_routes.create_note,
                method="POST",
                json={"title": "T", "content": "C"},
            )

        self._assert_withheld(payload, status, "Invalid note data")

    def test_update_note_withholds_service_exception_text(self):
        from local_deep_research.web.routers import notes as notes_routes

        note_id = str(uuid.uuid4())
        with patch.object(notes_routes, "NoteService") as cls:
            cls.return_value.update_note.side_effect = ValueError(self.LEAK)
            payload, status = _call(
                f"/notes/api/notes/{note_id}",
                notes_routes.update_note,
                note_id,
                method="PUT",
                json={"content": "C"},
            )

        self._assert_withheld(payload, status, "Invalid note data")

    def test_create_document_annotation_withholds_service_exception_text(
        self, monkeypatch
    ):
        from local_deep_research.web.routers import notes as notes_routes

        document_id = str(uuid.uuid4())

        class _Row:
            id = document_id
            title = "A Document"

        @contextmanager
        def _fake_session(username=None, password=None):
            db = MagicMock()
            db.query.return_value.filter_by.return_value.first.return_value = (
                _Row()
            )
            yield db

        monkeypatch.setattr(
            "local_deep_research.database.session_context.get_user_db_session",
            _fake_session,
        )

        with patch.object(notes_routes, "NoteService") as cls:
            cls.return_value.create_note_for_document.side_effect = ValueError(
                self.LEAK
            )
            payload, status = _call(
                f"/notes/api/documents/{document_id}/annotations",
                notes_routes.create_document_annotation,
                document_id,
                method="POST",
                json={"comment": "a comment", "quote": "a quote"},
            )

        self._assert_withheld(payload, status, "Invalid annotation data")