"""Tests for sagellm embedding interface."""

from __future__ import annotations

from sagellm.embedding_client import EmbeddingClient, LocalHashEmbedder
from sagellm.embedding_service import EmbeddingService


def test_embedding_client_empty_input_returns_empty() -> None:
    client = EmbeddingClient(base_url="http://127.0.0.1:8890")
    assert client.embed([]) == []


def test_embedding_service_normalizes_vectors() -> None:
    class _FakeClient:
        def embed(self, texts: list[str], model: str | None = None) -> list[list[float]]:
            return [[3.0, 4.0]]

    service = EmbeddingService(client=_FakeClient(), normalize=True)
    vectors = service.embed(["hello"])

    assert len(vectors) == 1
    assert abs(vectors[0][0] - 0.6) < 1e-6
    assert abs(vectors[0][1] - 0.8) < 1e-6


def test_embedding_service_can_disable_normalize() -> None:
    class _FakeClient:
        def embed(self, texts: list[str], model: str | None = None) -> list[list[float]]:
            return [[3.0, 4.0]]

    service = EmbeddingService(client=_FakeClient(), normalize=False)
    vectors = service.embed(["hello"])

    assert vectors == [[3.0, 4.0]]


def test_local_hash_embedder_dim_and_batch() -> None:
    embedder = LocalHashEmbedder(dim=256)
    vector = embedder.embed("hello")
    vectors = embedder.embed_batch(["hello", "world"])
    assert len(vector) == 256
    assert len(vectors) == 2
    assert embedder.get_dim() == 256


def test_local_hash_embedder_is_normalised() -> None:
    embedder = LocalHashEmbedder()
    vec = embedder.embed("sagellm rocks")
    norm = sum(v * v for v in vec) ** 0.5
    assert abs(norm - 1.0) < 1e-6


def test_local_hash_embedder_is_deterministic() -> None:
    embedder = LocalHashEmbedder()
    assert embedder.embed("abc") == embedder.embed("abc")
