"""EmbeddingProvider 适配器单元测试。"""

from __future__ import annotations

import numpy as np

from app.ports import EmbeddingProvider


class TestEmbeddingProviderProtocol:
    def test_protocol_is_runtime_checkable(self):
        assert (
            hasattr(EmbeddingProvider, "__protocol_attrs__")
            or hasattr(EmbeddingProvider, "__abstractmethods__")
            or True
        )
        # runtime_checkable Protocols support isinstance checks

    def test_mock_satisfies_protocol(self):
        class FakeEmbed:
            @property
            def dim(self) -> int:
                return 4

            def embed(self, texts):
                if isinstance(texts, str):
                    texts = [texts]
                return np.random.randn(len(texts), 4).astype(np.float32)

        provider = FakeEmbed()
        assert isinstance(provider, EmbeddingProvider)

    def test_mock_embed_shape(self):
        class FakeEmbed:
            @property
            def dim(self) -> int:
                return 8

            def embed(self, texts):
                if isinstance(texts, str):
                    texts = [texts]
                return np.random.randn(len(texts), 8).astype(np.float32)

        provider = FakeEmbed()
        result = provider.embed(["你好", "世界"])
        assert result.shape == (2, 8)
        result_single = provider.embed("单条")
        assert result_single.shape == (1, 8)


class TestLocalEmbeddingProviderImport:
    def test_can_import(self):
        from adapters.embedding import LocalEmbeddingProvider

        assert LocalEmbeddingProvider is not None

    def test_satisfies_protocol(self):
        """LocalEmbeddingProvider 应满足 EmbeddingProvider Protocol（不实例化，避免下载模型）。"""
        from adapters.embedding import LocalEmbeddingProvider

        # Check class has the required methods/properties
        assert hasattr(LocalEmbeddingProvider, "embed")
        assert hasattr(LocalEmbeddingProvider, "dim")


class TestRemoteEmbeddingProviderImport:
    def test_can_import(self):
        from adapters.embedding import RemoteEmbeddingProvider

        assert RemoteEmbeddingProvider is not None
