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.

13.5 KB
Raw Blame
"""Exercise notes-router branch outcomes with offline direct handler calls."""

import asyncio
import inspect
import json
from unittest.mock import AsyncMock, MagicMock

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

from local_deep_research.web.routers import notes


def _request(query: str = "") -> Request:
    return Request(
        {
            "type": "http",
            "method": "GET",
            "path": "/notes/test",
            "query_string": query.encode(),
            "headers": [],
            "client": ("127.0.0.1", 1234),
            "session": {"session_id": "sid"},
        }
    )


def _raw_handler(name: str):
    handler = getattr(notes, name)
    while hasattr(handler, "__wrapped__"):
        handler = handler.__wrapped__
    return handler


def _payload(response: JSONResponse):
    return json.loads(response.body)


def _call_handler(handler, *args, **kwargs):
    if inspect.iscoroutinefunction(handler):
        return asyncio.run(handler(*args, **kwargs))
    return handler(*args, **kwargs)


def test_text_query_rejects_non_string():
    with pytest.raises(ValueError, match="query must be a string"):
        notes._clamp_text_query(3, "query")


@pytest.mark.asyncio
async def test_large_json_parse_uses_worker_thread(monkeypatch):
    expected = {"ok": True}
    to_thread = AsyncMock(return_value=expected)
    monkeypatch.setattr(notes.asyncio, "to_thread", to_thread)

    result = await notes._parse_json_off_loop(
        bytearray(notes._JSON_OFFLOAD_THRESHOLD_BYTES)
    )

    assert result == expected
    to_thread.assert_awaited_once()


@pytest.mark.asyncio
async def test_invalid_content_length_is_ignored():
    sent = False

    async def receive():
        nonlocal sent
        if sent:
            return {"type": "http.disconnect"}
        sent = True
        return {"type": "http.request", "body": b"", "more_body": False}

    request = Request(
        {
            "type": "http",
            "method": "POST",
            "path": "/notes/test",
            "query_string": b"",
            "headers": [(b"content-length", b"invalid")],
        },
        receive,
    )

    assert await notes._notes_json_body(request) == {}


def test_auto_index_skips_missing_credentials_and_collection(monkeypatch):
    from local_deep_research.database.session_passwords import (
        session_password_store,
    )
    from local_deep_research.web.routers import rag

    trigger = MagicMock()
    monkeypatch.setattr(rag, "trigger_auto_index", trigger)
    monkeypatch.setattr(
        session_password_store, "get_session_password", lambda *_args: None
    )
    service = MagicMock()
    notes._trigger_note_auto_index("n1", service, "alice", "sid")
    trigger.assert_not_called()

    monkeypatch.setattr(
        session_password_store, "get_session_password", lambda *_args: "pw"
    )
    service.get_notes_collection_id.return_value = None
    notes._trigger_note_auto_index("n1", service, "alice", "sid")
    trigger.assert_not_called()


def test_auto_index_calls_worker_and_swallows_failures(
    monkeypatch, loguru_caplog_full
):
    from local_deep_research.database.session_passwords import (
        session_password_store,
    )
    from local_deep_research.web.routers import rag

    monkeypatch.setattr(
        session_password_store, "get_session_password", lambda *_args: "pw"
    )
    service = MagicMock()
    service.get_notes_collection_id.return_value = "notes"
    trigger = MagicMock()
    monkeypatch.setattr(rag, "trigger_auto_index", trigger)
    notes._trigger_note_auto_index("n1", service, "alice", "sid")
    trigger.assert_called_once_with(["n1"], "notes", "alice", "pw")

    trigger.side_effect = RuntimeError("worker unavailable")
    notes._trigger_note_auto_index("n1", service, "alice", "sid")

    # Assert the behaviour (an exception-level record, with the actual
    # traceback, was emitted) rather than pinning the `logger.exception`
    # attribute name — so this frame can move to
    # `logger.opt(exception=True).error(...)` without breaking the test.
    assert "Failed to trigger auto-indexing for note" in loguru_caplog_full.text
    assert "worker unavailable" in loguru_caplog_full.text
    assert "Traceback" in loguru_caplog_full.text


PASSTHROUGH_CASES = [
    # create_note and update_note were the only two body-gate sites missing
    # from this list. Removing either handler's own
    # `if isinstance(body, JSONResponse): return body` line makes its row
    # here fail — but not the same way: update_note then calls
    # `data.get("pinned")` on the JSONResponse and crashes with
    # AttributeError like every other site below; create_note's very next
    # line is `if not isinstance(data, dict) or not data:`, which a
    # JSONResponse satisfies, so it silently answers a plausible
    # 400 "No data provided" instead of the 413 gate response — the
    # `result is response` assertion below still fails, just without a
    # crash to announce it.
    ("create_note", {}),
    ("update_note", {"note_id": "n"}),
    ("add_note_to_collection", {"note_id": "n"}),
    ("link_research_to_note", {"note_id": "n"}),
    ("patch_note_research", {"note_id": "n", "research_id": "r"}),
    ("reorder_note_research", {"note_id": "n"}),
    ("create_research_note", {"research_id": "r"}),
    ("save_research_as_note", {"research_id": "r"}),
    ("create_research_annotation", {"research_id": "r"}),
    ("create_document_note", {"document_id": "d"}),
    ("create_document_annotation", {"document_id": "d"}),
    ("index_note_to_collection", {"note_id": "n"}),
    ("summarize_note", {"note_id": "n"}),
    ("extract_research_questions", {"note_id": "n"}),
    ("suggest_tags", {}),
    ("extract_key_concepts", {"note_id": "n"}),
    ("accept_suggested_link", {"note_id": "n"}),
    ("similar_passages", {"note_id": "n"}),
    ("fact_check_note", {"note_id": "n"}),
    ("grade_note_fact_check", {"note_id": "n", "research_id": "r"}),
    ("resolve_link", {}),
    ("preview_synthesis", {}),
    ("synthesize_notes", {}),
    ("restore_note_version", {"note_id": "n", "version_id": "v"}),
]


@pytest.mark.parametrize(("name", "path_args"), PASSTHROUGH_CASES)
def test_body_gate_response_is_returned_verbatim(name, path_args):
    response = JSONResponse({"gate": "stopped"}, status_code=413)

    result = _call_handler(
        _raw_handler(name),
        request=_request(),
        username="alice",
        body=response,
        **path_args,
    )

    assert result is response


SERVICE_ERROR_CASES = [
    "list_notes||{}",
    "semantic_search_notes|q=topic|{}",
    "ask_context||{}",
    "search_notes_for_linking|query=x|{}",
    'get_note||{"note_id":"n"}',
    'update_note||{"note_id":"n","body":{"title":"x"}}',
    'delete_note||{"note_id":"n"}',
    'get_note_collections||{"note_id":"n"}',
    'add_note_to_collection||{"note_id":"n","body":{"collection_id":"c"}}',
    'remove_note_from_collection||{"note_id":"n","collection_id":"c"}',
    'get_note_research||{"note_id":"n"}',
    'link_research_to_note||{"note_id":"n","body":{"research_id":"r"}}',
    'patch_note_research||{"note_id":"n","research_id":"r","body":{}}',
    'reorder_note_research||{"note_id":"n","body":{"research_ids":["r"]}}',
    'get_research_notes||{"research_id":"r"}',
    'create_research_note||{"research_id":"r","body":{}}',
    'delete_research_annotation||{"research_id":"r","note_id":"n"}',
    'get_document_notes||{"document_id":"d"}',
    'create_document_note||{"document_id":"d","body":{}}',
    'get_document_annotations||{"document_id":"d"}',
    'delete_document_annotation||{"document_id":"d","note_id":"n"}',
    'index_note_to_collection||{"note_id":"n","body":{"collection_id":"c"}}',
    'get_similar_notes||{"note_id":"n"}',
    'summarize_note||{"note_id":"n","body":{}}',
    'extract_research_questions||{"note_id":"n","body":{}}',
    'suggest_tags||{"body":{"content":"x"}}',
    'extract_key_concepts||{"note_id":"n","body":{}}',
    'get_backlinks||{"note_id":"n"}',
    'get_outgoing_links||{"note_id":"n"}',
    'get_suggested_links||{"note_id":"n"}',
    'accept_suggested_link||{"note_id":"n","body":{"target_note_id":"t"}}',
    'get_unlinked_mentions||{"note_id":"n"}',
    'similar_passages||{"note_id":"n","body":{"text":"x"}}',
    'fact_check_note||{"note_id":"n","body":{}}',
    'resolve_link||{"body":{"link_text":"x"}}',
    'preview_synthesis||{"body":{"note_ids":["a","b"]}}',
    'synthesize_notes||{"body":{"note_ids":["a","b"]}}',
]


class _ServiceUnavailableError(RuntimeError):
    pass


@pytest.mark.parametrize("case", SERVICE_ERROR_CASES)
def test_service_failures_use_notes_error_boundary(monkeypatch, case):
    err = _ServiceUnavailableError()

    class BrokenService:
        def __init__(self, _username):
            raise err

    sentinel = {"handled": True}
    boundary = MagicMock(return_value=sentinel)
    monkeypatch.setattr(notes, "NoteService", BrokenService)
    monkeypatch.setattr(notes, "NoteAIService", BrokenService)
    monkeypatch.setattr(notes, "handle_api_error", boundary)

    name, query, raw_kwargs = case.split("|", 2)
    result = _call_handler(
        _raw_handler(name),
        request=_request(query),
        username="alice",
        **json.loads(raw_kwargs),
    )

    assert result is sentinel
    # The exact instance the constructor raised must reach the boundary
    # unwrapped/unreplaced — `isinstance(..., RuntimeError)` would also
    # pass for a fresh exception the handler fabricated along the way.
    assert boundary.call_args.args[1] is err


@pytest.mark.parametrize(
    ("body", "message"),
    [
        ({"comment": 123, "quote": "q"}, "comment must be a string"),
        ({"comment": "x"}, "quote is required"),
        ({"comment": "x" * 5001, "quote": "q"}, "comment exceeds"),
        ({"comment": "x", "quote": "q" * 1001}, "quote exceeds"),
    ],
)
def test_annotation_validation_limits(body, message):
    with pytest.raises(ValueError, match=message):
        notes._validated_annotation_fields(body)


def test_comment_title_truncates_long_single_line():
    title = notes._comment_note_title("word " * 30)
    assert title.startswith("Comment: ")
    assert title.endswith("…")
    # 60-char slice rstrips its trailing space before the ellipsis is added
    assert len(title) == len("Comment: ") + 60


# route | service method | kwargs JSON | fixed client-safe message the
# route answers with. The service's own ValueError text is logged at the
# route and never echoed to the client.
VALIDATION_ERROR_CASES = [
    'create_note|create_note|{"body":{"title":"t","content":"c"}}'
    "|Invalid note data",
    'update_note|update_note|{"note_id":"n","body":{"title":"t"}}'
    "|Invalid note data",
    "remove_note_from_collection|remove_from_collection"
    '|{"note_id":"n","collection_id":"c"}'
    "|Note cannot be removed from this collection",
    "reorder_note_research|reorder_note_research"
    '|{"note_id":"n","body":{"research_ids":["r"]}}'
    "|Invalid research order",
    "create_research_note|create_note_for_research"
    '|{"research_id":"r","body":{}}'
    "|Invalid note data",
    "create_document_note|create_note_for_document"
    '|{"document_id":"d","body":{}}'
    "|Invalid document note data",
    "accept_suggested_link|accept_suggested_link"
    '|{"note_id":"n","body":{"target_note_id":"t"}}'
    "|Suggested link cannot be accepted",
]


@pytest.mark.parametrize("case", VALIDATION_ERROR_CASES)
def test_service_validation_errors_are_client_errors(monkeypatch, case):
    name, method, raw_kwargs, expected_message = case.split("|", 3)
    service = MagicMock()
    getattr(service, method).side_effect = ValueError("invalid note data")
    service.note_exists.return_value = True
    monkeypatch.setattr(notes, "NoteService", lambda _username: service)

    response = _raw_handler(name)(
        request=_request(), username="alice", **json.loads(raw_kwargs)
    )

    assert response.status_code == 400
    payload = _payload(response)
    assert payload["error"] == expected_message
    assert "invalid note data" not in payload["error"]


def test_link_research_integrity_error_classification(monkeypatch):
    from sqlalchemy.exc import IntegrityError

    service = MagicMock()
    monkeypatch.setattr(notes, "NoteService", lambda _username: service)
    boundary = MagicMock(return_value={"handled": True})
    monkeypatch.setattr(notes, "handle_api_error", boundary)

    service.link_research_to_note.side_effect = IntegrityError(
        "statement", {}, RuntimeError("FOREIGN KEY constraint failed")
    )
    missing = _raw_handler("link_research_to_note")(
        request=_request(),
        note_id="n",
        username="alice",
        body={"research_id": "r"},
    )
    assert missing.status_code == 404
    assert _payload(missing)["error"] == "Research run not found"

    service.link_research_to_note.side_effect = IntegrityError(
        "statement", {}, RuntimeError("display_order collision")
    )
    handled = _raw_handler("link_research_to_note")(
        request=_request(),
        note_id="n",
        username="alice",
        body={"research_id": "r"},
    )
    assert handled == {"handled": True}

    service.link_research_to_note.side_effect = IntegrityError(
        "statement",
        {},
        RuntimeError(
            "UNIQUE constraint failed: note_research.note_id, "
            "note_research.research_id"
        ),
    )
    duplicate = _raw_handler("link_research_to_note")(
        request=_request(),
        note_id="n",
        username="alice",
        body={"research_id": "r"},
    )
    assert duplicate.status_code == 409
    assert (
        _payload(duplicate)["error"] == "Research already linked to this note"
    )