fix: add IndexError guard and error handling test

This commit is contained in:
2026-04-13 14:33:26 -04:00
parent b2a42e5fe6
commit d0a201476e
2 changed files with 29 additions and 1 deletions

View File

@@ -2,6 +2,8 @@ import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from companion.rag.search import SearchEngine
from companion.rag.vector_store import VectorStore
@@ -32,3 +34,24 @@ def test_search_returns_results(mock_embedder_cls):
results = engine.search("hello")
assert len(results) == 1
assert results[0]["source_file"] == "a.md"
@patch("companion.rag.search.OllamaEmbedder")
def test_search_raises_on_embedder_failure(mock_embedder_cls):
mock_embedder = MagicMock()
mock_embedder.embed.side_effect = RuntimeError("Connection failed")
mock_embedder_cls.return_value = mock_embedder
with tempfile.TemporaryDirectory() as tmp:
store = VectorStore(uri=tmp, dimensions=4)
engine = SearchEngine(
vector_store=store,
embedder_base_url="http://localhost:11434",
embedder_model="dummy",
embedder_batch_size=32,
default_top_k=5,
similarity_threshold=0.0,
hybrid_search_enabled=False,
)
with pytest.raises(RuntimeError, match="Failed to generate embedding"):
engine.search("hello")