ux-research-agent-slackbot / tests /test_vector_store.py
debug-log
Claude Opus 4.6
feat: Insight Repository β€” GitLab μ΄ˆκΈ°ν™” + HF μžλ™ 배포
501bc8c
Raw History Blame Contribute Delete
8.17 kB
"""Tests for VectorStore text construction, hashing, and hybrid search."""
import pytest
from unittest.mock import patch, MagicMock
from src.engine.vector_store import (
_build_internal_text,
_build_baymard_text,
_build_nng_text,
VectorStore,
)
from src.engine.retriever import Retriever
# ─── Text construction tests ─────────────────────────────────────────────────
class TestBuildInternalText:
def test_basic_fields(self):
item = {
"metadata": {
"λ¦¬μ„œμΉ˜ 제λͺ©": "μˆ™μ†Œ μ˜ˆμ•½ μ‚¬μš©μ„± ν…ŒμŠ€νŠΈ",
"νƒœκ·Έ": "μˆ™μ†Œ, μ˜ˆμ•½",
"도메인": "μˆ™μ†Œ",
"방법둠": "UT",
},
"상세 정보": {
"μ£Όμš” λ‚΄μš© μš”μ•½": "μˆ™μ†Œ μ˜ˆμ•½ ν”„λ‘œμ„ΈμŠ€ κ°œμ„  ν•„μš”",
},
}
text = _build_internal_text(item, max_len=2000)
assert "μˆ™μ†Œ μ˜ˆμ•½ μ‚¬μš©μ„± ν…ŒμŠ€νŠΈ" in text
assert "μˆ™μ†Œ, μ˜ˆμ•½" in text
assert "UT" in text
assert "κ°œμ„  ν•„μš”" in text
def test_empty_fields(self):
item = {"metadata": {}, "상세 정보": {}}
text = _build_internal_text(item, max_len=2000)
assert text == ""
def test_truncation(self):
item = {
"metadata": {"λ¦¬μ„œμΉ˜ 제λͺ©": "A" * 3000},
"상세 정보": {},
}
text = _build_internal_text(item, max_len=100)
assert len(text) <= 100
class TestBuildBaymardText:
def test_basic_fields(self):
item = {
"title": "Product Image Quality",
"topic": "Product Page",
"issue": "Users cannot assess product quality",
"advice": "Use high-resolution images",
}
text = _build_baymard_text(item, max_len=2000)
assert "Product Image Quality" in text
assert "Product Page" in text
assert "high-resolution" in text
def test_truncation(self):
item = {"title": "T", "topic": "", "issue": "X" * 3000, "advice": ""}
text = _build_baymard_text(item, max_len=200)
assert len(text) <= 200
class TestBuildNngText:
def test_basic_fields(self):
item = {
"title": "Usability Heuristics",
"description": "10 heuristics for UI design",
"topics": ["Usability", "Heuristics"],
"contents": ["Visibility of system status", "Match between system and real world"],
}
text = _build_nng_text(item, max_len=2000)
assert "Usability Heuristics" in text
assert "10 heuristics" in text
assert "Visibility" in text
def test_contents_as_string(self):
item = {
"title": "Test",
"description": "",
"topics": [],
"contents": "Some string content",
}
text = _build_nng_text(item, max_len=2000)
assert "Some string content" in text
def test_truncation(self):
item = {
"title": "T",
"description": "D" * 3000,
"topics": [],
"contents": [],
}
text = _build_nng_text(item, max_len=150)
assert len(text) <= 150
# ─── Data hash tests ──────────────────────────────────────────────────────────
class TestComputeDataHash:
def _make_store(self, internal, baymard, nng):
"""Create a VectorStore without actually initializing ChromaDB."""
store = VectorStore.__new__(VectorStore)
store.internal_data = internal
store.baymard_data = baymard
store.nng_data = nng
return store
def test_consistent_hash(self):
data_i = [{"id": "RES-001"}, {"id": "RES-010"}]
data_b = [{"id": 1}, {"id": 5}]
data_n = [{"id": "article-1"}, {"id": "article-3"}]
store = self._make_store(data_i, data_b, data_n)
h1 = store._compute_data_hash()
h2 = store._compute_data_hash()
assert h1 == h2
def test_different_data_different_hash(self):
store_a = self._make_store(
[{"id": "RES-001"}], [{"id": 1}], [{"id": "a-1"}]
)
store_b = self._make_store(
[{"id": "RES-001"}, {"id": "RES-002"}], [{"id": 1}], [{"id": "a-1"}]
)
assert store_a._compute_data_hash() != store_b._compute_data_hash()
def test_empty_data(self):
store = self._make_store([], [], [])
h = store._compute_data_hash()
assert isinstance(h, str) and len(h) == 64
# ─── Hybrid search integration test ──────────────────────────────────────────
class TestHybridSearch:
@pytest.fixture
def retriever(self):
"""Create a Retriever with mock data and a mock VectorStore."""
with patch("src.engine.retriever.load_internal_research", return_value=[]), \
patch("src.engine.retriever.load_baymard_guidelines", return_value=[]), \
patch("src.engine.retriever.load_nng_articles", return_value=[]):
r = Retriever.__new__(Retriever)
r.internal_data = [
{
"id": "RES-001",
"metadata": {
"λ¦¬μ„œμΉ˜ 제λͺ©": "μˆ™μ†Œ μ˜ˆμ•½ μ‚¬μš©μ„± ν…ŒμŠ€νŠΈ",
"νƒœκ·Έ": "μˆ™μ†Œ",
"λ‹΄λ‹Ή λ¦¬μ„œμ²˜": "Shane",
"방법둠": "UT",
"도메인": "μˆ™μ†Œ",
"λ‚ μ§œ": "2023-06",
"결과 링크": "",
},
"상세 정보": {"μ£Όμš” λ‚΄μš© μš”μ•½": "μ˜ˆμ•½ ν”„λ‘œμ„ΈμŠ€ κ°œμ„  ν•„μš”"},
},
{
"id": "RES-002",
"metadata": {
"λ¦¬μ„œμΉ˜ 제λͺ©": "νŽœμ…˜ 이미지 ν’ˆμ§ˆ 쑰사",
"νƒœκ·Έ": "이미지",
"λ‹΄λ‹Ή λ¦¬μ„œμ²˜": "Judy",
"방법둠": "Survey",
"도메인": "μˆ™μ†Œ",
"λ‚ μ§œ": "2024-01",
"결과 링크": "",
},
"상세 정보": {"μ£Όμš” λ‚΄μš© μš”μ•½": "사진정보 λΆ€μ‘± 뢈만"},
},
]
r.baymard_data = []
r.nng_data = []
# Mock VectorStore
mock_vs = MagicMock()
mock_vs.semantic_search.return_value = [
{"doc_id": "RES-002", "source": "INTERNAL", "score": 0.85},
{"doc_id": "RES-001", "source": "INTERNAL", "score": 0.72},
]
r.vector_store = mock_vs
return r
def test_hybrid_returns_results(self, retriever):
results = retriever.hybrid_search(
keywords=["νŽœμ…˜"], query="νŽœμ…˜ 사진정보 λΆ€μ‘±", k=5
)
assert len(results) > 0
assert all("match_type" in r for r in results)
def test_hybrid_match_types(self, retriever):
results = retriever.hybrid_search(
keywords=["μˆ™μ†Œ"], query="μˆ™μ†Œ μ˜ˆμ•½ μ‚¬μš©μ„±", k=5
)
match_types = {r["match_type"] for r in results}
# Should contain at least one hybrid or fuzzy match
assert match_types & {"Hybrid Match", "Fuzzy Match", "Semantic Match"}
def test_fallback_when_no_vector_store(self, retriever):
retriever.vector_store = None
results = retriever.hybrid_search(
keywords=["μˆ™μ†Œ"], query="μˆ™μ†Œ μ˜ˆμ•½", k=5
)
# Should still return fuzzy results
assert all(r.get("match_type") == "Fuzzy Match" for r in results)
def test_source_filter(self, retriever):
results = retriever.hybrid_search(
keywords=["μˆ™μ†Œ"], query="μˆ™μ†Œ μ˜ˆμ•½", source="INTERNAL", k=5
)
assert all(r.get("source") == "INTERNAL" for r in results)