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.

9.9 KB
Raw Blame
"""Cover isolated NoteAIService branches without databases or model backends."""

import asyncio
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock

import pytest

from local_deep_research.research_library.notes.services.note_ai_service import (
    NoteAIService,
)


def _llm_returning(payload: str) -> MagicMock:
    # The AI methods are async (#6232) and reach the model through
    # ``ainvoke``; ``invoke`` stays wired for the sync change-summary twin.
    llm = MagicMock()
    response = SimpleNamespace(content=payload)
    llm.invoke.return_value = response
    llm.ainvoke = AsyncMock(return_value=response)
    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


@pytest.mark.parametrize("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 asyncio.run(service.extract_research_questions("missing")) == []
    assert asyncio.run(service.summarize_changes("", "new")) == "Note created"
    assert asyncio.run(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 = asyncio.run(
        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 = asyncio.run(
        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 = asyncio.run(
        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.ainvoke = AsyncMock(side_effect=RuntimeError("model down"))

    with pytest.raises(RuntimeError, match="model down"):
        asyncio.run(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 = asyncio.run(service.synthesize_notes(["a", "b"], "summarize"))

    assert result["success"] is True
    assert result["content"] == "combined"
    assert result["suggested_title"] == "Summary: Alpha + 1 more"
    llm.ainvoke.assert_called_once()
    prompt = llm.ainvoke.call_args.args[0]
    assert prompt.startswith("Summarize the key points")
    assert "alpha source content" in prompt
    assert "beta source content" in prompt