test(vector-store): add epsilon tolerance edge case tests

Add comprehensive test coverage for near-zero norms, product
underflow, and floating point precision edge cases in
_cosine_similarity function.

Solution-ID: SOL-20251228113619
Issue-ID: ISS-1766921318981-11
Task-ID: T2
This commit is contained in:
catlog22
2025-12-28 21:37:59 +08:00
parent 6486c56850
commit af2ff54cb7
2 changed files with 71 additions and 11 deletions

View File

@@ -19,10 +19,12 @@ from typing import Any, Dict, List, Optional, Tuple
from codexlens.entities import SearchResult, SemanticChunk
from codexlens.errors import StorageError
from . import SEMANTIC_AVAILABLE
if SEMANTIC_AVAILABLE:
try:
import numpy as np
NUMPY_AVAILABLE = True
except ImportError:
np = None # type: ignore[assignment]
NUMPY_AVAILABLE = False
# Try to import ANN index (optional hnswlib dependency)
try:
@@ -37,7 +39,7 @@ logger = logging.getLogger(__name__)
def _cosine_similarity(a: List[float], b: List[float]) -> float:
"""Compute cosine similarity between two vectors."""
if not SEMANTIC_AVAILABLE:
if not NUMPY_AVAILABLE:
raise ImportError("numpy required for vector operations")
a_arr = np.array(a)
@@ -74,7 +76,7 @@ class VectorStore:
DEFAULT_DIM = 768
def __init__(self, db_path: str | Path) -> None:
if not SEMANTIC_AVAILABLE:
if not NUMPY_AVAILABLE:
raise ImportError(
"Semantic search dependencies not available. "
"Install with: pip install codexlens[semantic]"