"""
Tests for the LocalEmbeddingManager class.

Tests cover:
- LocalEmbeddingManager initialization and configuration
- Embeddings lazy initialization
"""

from unittest.mock import Mock, patch


class TestLocalEmbeddingManagerInit:
    """Tests for LocalEmbeddingManager initialization."""

    def test_init_with_defaults(self):
        """Initialize with default values."""
        from local_deep_research.web_search_engines.engines.local_embedding_manager import (
            LocalEmbeddingManager,
        )

        manager = LocalEmbeddingManager()

        assert manager.embedding_model == "all-MiniLM-L6-v2"
        assert manager.embedding_device == "cpu"
        assert manager.embedding_model_type == "sentence_transformers"
        assert manager._embeddings is None  # Lazy initialization

    def test_init_with_ollama(self):
        """Initialize with Ollama embeddings."""
        from local_deep_research.web_search_engines.engines.local_embedding_manager import (
            LocalEmbeddingManager,
        )

        manager = LocalEmbeddingManager(
            embedding_model_type="ollama",
            embedding_model="llama2",
            ollama_base_url="http://localhost:11434",
        )

        assert manager.embedding_model_type == "ollama"
        assert manager.embedding_model == "llama2"
        assert manager.ollama_base_url == "http://localhost:11434"

    def test_init_with_settings_snapshot(self):
        """Initialize with settings snapshot."""
        from local_deep_research.web_search_engines.engines.local_embedding_manager import (
            LocalEmbeddingManager,
        )

        settings = {"_username": "testuser"}
        manager = LocalEmbeddingManager(settings_snapshot=settings)

        assert manager.username == "testuser"
        assert manager.settings_snapshot == settings


class TestLocalEmbeddingManagerEmbeddings:
    """Tests for LocalEmbeddingManager embeddings property."""

    def test_embeddings_lazy_initialization(self):
        """Embeddings are lazily initialized."""
        from local_deep_research.web_search_engines.engines.local_embedding_manager import (
            LocalEmbeddingManager,
        )

        manager = LocalEmbeddingManager()

        assert manager._embeddings is None

        # Mock the embeddings initialization
        mock_embeddings = Mock()
        with patch.object(
            manager, "_initialize_embeddings", return_value=mock_embeddings
        ):
            embeddings = manager.embeddings

            assert embeddings is mock_embeddings
            assert manager._embeddings is mock_embeddings

    def test_embeddings_reuse(self):
        """Embeddings are reused after initialization."""
        from local_deep_research.web_search_engines.engines.local_embedding_manager import (
            LocalEmbeddingManager,
        )

        manager = LocalEmbeddingManager()

        mock_embeddings = Mock()
        manager._embeddings = mock_embeddings

        # Should return existing embeddings without reinitializing
        with patch.object(manager, "_initialize_embeddings") as mock_init:
            embeddings = manager.embeddings

            assert embeddings is mock_embeddings
            mock_init.assert_not_called()
