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.

test(notes): cover router and AI-service error branches (#6145)

LearningCircuit2f94db744236
diff --git a/tests/notes/test_note_ai_service_edges.py b/tests/notes/test_note_ai_service_edges.py
new file mode 100644
index 0000000000..4197acf594
--- /dev/null
+++ b/tests/notes/test_note_ai_service_edges.py
@@ -0,0 +1,274 @@
+"""Cover isolated NoteAIService branches without databases or model backends."""
+
+from contextlib import contextmanager
+from types import SimpleNamespace
+from unittest.mock import MagicMock
+
+import pytest
+
+from local_deep_research.research_library.notes.services.note_ai_service import (
+ NoteAIService,
+)
+
+
+def _llm_returning(payload: str) -> MagicMock:
+ llm = MagicMock()
+ llm.invoke.return_value = SimpleNamespace(content=payload)
+ return llm
+
+
+def test_get_llm_temperature_override_is_not_cached(monkeypatch):
+ service = NoteAIService("alice")
+ first = MagicMock(name="first")
+ second = MagicMock(name="second")
+ get_llm = MagicMock(side_effect=[first, second])
+ monkeypatch.setattr(
+ "local_deep_research.config.llm_config.get_llm", get_llm
+ )
+ monkeypatch.setattr(service, "_build_settings_snapshot", lambda: {"x": 1})
+
+ assert service._get_llm(temperature=0) is first
+ assert service._get_llm(temperature=0) is second
+ assert service._llm is None
+
+
[email protected]("value", [None, "not-a-number", []])
+def test_coerce_confidence_rejects_unusable_values(value):
+ assert NoteAIService._coerce_confidence(value) == 0
+
+
+def test_common_prefix_scans_complete_equal_block():
+ # The divergence must sit INSIDE the second 64 KiB block, not on the
+ # block boundary itself (e.g. `shared + "a"` vs `shared + "b"`, which
+ # diverges at the very first byte of block two): at the boundary, a
+ # pure per-character scan and a block-then-char scan agree by
+ # construction, so deleting the block loop or the char scan entirely
+ # still returns the right answer. Fifty matching characters into the
+ # second block forces both stages to actually run.
+ shared = "x" * (1 << 16)
+ a = shared + "y" * 50 + "p"
+ b = shared + "y" * 50 + "q"
+ assert NoteAIService._common_prefix_len(a, b) == len(shared) + 50
+
+
+def test_fetch_and_project_note_edge_branches(monkeypatch):
+ session = MagicMock()
+
+ @contextmanager
+ def fake_session(_username):
+ yield session
+
+ monkeypatch.setattr(
+ "local_deep_research.research_library.notes.services.note_ai_service.get_user_db_session",
+ fake_session,
+ )
+ service = NoteAIService("alice")
+ monkeypatch.setattr(
+ service, "_get_note_source_type_id", lambda _session: None
+ )
+ assert service._fetch_document("missing") is None
+
+ document = SimpleNamespace(
+ id="n1", title="Title", text_content="Body", tags=None
+ )
+ monkeypatch.setattr(service, "_fetch_document", lambda _note_id: document)
+ assert service._get_note_content("n1") == "Body"
+ assert service._get_note("n1") == {
+ "id": "n1",
+ "title": "Title",
+ "content": "Body",
+ "tags": [],
+ }
+
+
+def test_simple_ai_methods_cover_empty_and_failure_paths(monkeypatch):
+ service = NoteAIService("alice")
+ monkeypatch.setattr(service, "_get_note_content", lambda _note_id: None)
+ assert service.extract_research_questions("missing") == []
+ assert service.summarize_changes("", "new") == "Note created"
+ assert service.summarize_changes("old", "") == "Changes made"
+
+ monkeypatch.setattr(service, "_get_note_content", lambda _note_id: "body")
+ monkeypatch.setattr(
+ service,
+ "semantic_search",
+ MagicMock(side_effect=RuntimeError("search unavailable")),
+ )
+ with pytest.raises(RuntimeError, match="search unavailable"):
+ service.find_similar_notes("n1")
+
+
+def test_grade_rejects_non_list_source_refs():
+ # A scalar (unwrapped) source_refs must be discarded rather than
+ # iterated. `source_refs: "0"` doesn't distinguish this: iterating a
+ # string yields characters, which the `isinstance(r, int)` filter
+ # already drops, so the anti-rubber-stamp downgrade fires either way
+ # and masks whether the non-list guard did anything. An unwrapped int
+ # is unmasked: without the guard, `for r in 1` raises TypeError instead
+ # of degrading to the same "unverified" verdict.
+ service = NoteAIService("alice")
+ service._get_llm = lambda temperature=None: _llm_returning(
+ '[{"verdict":"supported","confidence":80,'
+ '"reasoning":"r","source_refs":1}]'
+ )
+
+ verdict = service.grade_all_claims(["claim"], "report", ["source label"])[0]
+
+ assert verdict["verdict"] == "unverified"
+ assert verdict["confidence"] == 0
+ assert verdict["sources"] == []
+
+
+def test_grade_positional_fallback_for_unlabelled_item():
+ # The verdict object carries no "index" key and by_index is empty, so
+ # only the positional fallback (parsed[i] matched to claim i by
+ # position) can supply it. The citation is valid and non-empty, so the
+ # anti-rubber-stamp downgrade never fires — removing the fallback
+ # leaves `item == {}`, which flips verdict/confidence/sources rather
+ # than converging on the same downgraded result.
+ service = NoteAIService("alice")
+ service._get_llm = lambda temperature=None: _llm_returning(
+ '[{"verdict":"supported","confidence":90,'
+ '"reasoning":"ok","source_refs":[0]}]'
+ )
+
+ verdict = service.grade_all_claims(
+ ["claim"], "report", [{"title": "Source A"}]
+ )[0]
+
+ assert verdict["verdict"] == "supported"
+ assert verdict["confidence"] == 90
+ assert verdict["sources"] == [{"title": "Source A", "url": ""}]
+
+
+def test_grade_skips_non_dict_shown_source():
+ # Two refs: one resolves to a dict source (kept, so the anti-rubber-
+ # stamp downgrade never triggers) and one to a non-dict source, which
+ # `isinstance(shown_sources[r], dict)` must filter out. Without that
+ # guard the code calls `.get()` on a plain string and raises
+ # AttributeError instead of silently producing the same citation list.
+ service = NoteAIService("alice")
+ service._get_llm = lambda temperature=None: _llm_returning(
+ '[{"verdict":"supported","confidence":70,'
+ '"reasoning":"ok","source_refs":[0,1]}]'
+ )
+
+ verdict = service.grade_all_claims(
+ ["claim"], "report", [{"title": "Source A"}, "plain label"]
+ )[0]
+
+ assert verdict["verdict"] == "supported"
+ assert verdict["sources"] == [{"title": "Source A", "url": ""}]
+
+
+def test_similar_passages_empty_engine_result_and_failure(monkeypatch):
+ from local_deep_research.research_library.notes.services.note_service import (
+ NoteService,
+ )
+
+ @contextmanager
+ def fake_session(_username):
+ yield MagicMock()
+
+ class EmptyEngine:
+ def __init__(self, **_kwargs):
+ pass
+
+ def search(self, _query, *, limit):
+ return []
+
+ monkeypatch.setattr(
+ "local_deep_research.research_library.notes.services.note_ai_service.get_user_db_session",
+ fake_session,
+ )
+ monkeypatch.setattr(
+ NoteService,
+ "_get_or_create_notes_collection",
+ lambda _self, _session: "notes",
+ )
+ monkeypatch.setattr(
+ "local_deep_research.web_search_engines.engines.search_engine_collection.CollectionSearchEngine",
+ EmptyEngine,
+ )
+ service = NoteAIService("alice")
+ assert service.find_similar_passages("query") == []
+
+ monkeypatch.setattr(
+ EmptyEngine, "search", MagicMock(side_effect=RuntimeError("index down"))
+ )
+ with pytest.raises(RuntimeError, match="index down"):
+ service.find_similar_passages("query")
+
+
+def test_suggest_links_missing_success_and_failure(monkeypatch):
+ service = NoteAIService("alice")
+ monkeypatch.setattr(service, "_get_note", lambda _note_id: None)
+ assert service.suggest_links("missing") == []
+
+ monkeypatch.setattr(
+ service,
+ "_get_note",
+ lambda _note_id: {"title": "T", "content": "C"},
+ )
+ monkeypatch.setattr(
+ service,
+ "semantic_search",
+ lambda *_args, **_kwargs: [{"id": "n1"}, {"id": "n2"}],
+ )
+ assert service.suggest_links("n1") == [{"id": "n2"}]
+
+ monkeypatch.setattr(
+ service,
+ "semantic_search",
+ MagicMock(side_effect=RuntimeError("index down")),
+ )
+ with pytest.raises(RuntimeError, match="index down"):
+ service.suggest_links("n1")
+
+
+def test_semantic_diff_reraises_model_failure():
+ service = NoteAIService("alice")
+ service._llm = MagicMock()
+ service._llm.invoke.side_effect = RuntimeError("model down")
+
+ with pytest.raises(RuntimeError, match="model down"):
+ service.semantic_diff("old", "new")
+
+
+def test_summarize_synthesis_selects_summary_prompt_and_title(monkeypatch):
+ documents = [
+ SimpleNamespace(
+ id="a", title="Alpha", text_content="alpha source content"
+ ),
+ SimpleNamespace(
+ id="b", title="Beta", text_content="beta source content"
+ ),
+ ]
+ session = MagicMock()
+ session.query.return_value.filter.return_value.all.return_value = documents
+
+ @contextmanager
+ def fake_session(_username):
+ yield session
+
+ monkeypatch.setattr(
+ "local_deep_research.research_library.notes.services.note_ai_service.get_user_db_session",
+ fake_session,
+ )
+ service = NoteAIService("alice")
+ monkeypatch.setattr(
+ service, "_get_note_source_type_id", lambda _session: "note"
+ )
+ llm = _llm_returning("combined")
+ service._llm = llm
+
+ result = service.synthesize_notes(["a", "b"], "summarize")
+
+ assert result["success"] is True
+ assert result["content"] == "combined"
+ assert result["suggested_title"] == "Summary: Alpha + 1 more"
+ llm.invoke.assert_called_once()
+ prompt = llm.invoke.call_args.args[0]
+ assert prompt.startswith("Summarize the key points")
+ assert "alpha source content" in prompt
+ assert "beta source content" in prompt
diff --git a/tests/web/routers/test_notes_branch_edges.py b/tests/web/routers/test_notes_branch_edges.py
new file mode 100644
index 0000000000..ed01b78025
--- /dev/null
+++ b/tests/web/routers/test_notes_branch_edges.py
@@ -0,0 +1,353 @@
+"""Exercise notes-router branch outcomes with offline direct handler calls."""
+
+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 test_text_query_rejects_non_string():
+ with pytest.raises(ValueError, match="query must be a string"):
+ notes._clamp_text_query(3, "query")
+
+
+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()
+
+
+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"}),
+]
+
+
[email protected](("name", "path_args"), PASSTHROUGH_CASES)
+def test_body_gate_response_is_returned_verbatim(name, path_args):
+ response = JSONResponse({"gate": "stopped"}, status_code=413)
+
+ result = _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
+
+
[email protected]("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 = _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
+
+
+ ("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
+
+
+VALIDATION_ERROR_CASES = [
+ 'create_note|create_note|{"body":{"title":"t","content":"c"}}',
+ 'update_note|update_note|{"note_id":"n","body":{"title":"t"}}',
+ 'remove_note_from_collection|remove_from_collection|{"note_id":"n","collection_id":"c"}',
+ 'reorder_note_research|reorder_note_research|{"note_id":"n","body":{"research_ids":["r"]}}',
+ 'create_research_note|create_note_for_research|{"research_id":"r","body":{}}',
+ 'create_document_note|create_note_for_document|{"document_id":"d","body":{}}',
+ 'accept_suggested_link|accept_suggested_link|{"note_id":"n","body":{"target_note_id":"t"}}',
+]
+
+
[email protected]("case", VALIDATION_ERROR_CASES)
+def test_service_validation_errors_are_client_errors(monkeypatch, case):
+ name, method, raw_kwargs = case.split("|", 2)
+ 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
+ assert _payload(response)["error"] == "invalid note data"
+
+
+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"
+ )