from __future__ import annotations

import numpy as np
import pytest

from idri import IdentifierEncoder, cosine_similarity, l2_distance


def test_cosine_similarity_for_normalized_vectors() -> None:
    enc = IdentifierEncoder()
    v = enc.encode("acme")
    assert cosine_similarity(v, v, assume_normalized=True) == pytest.approx(1.0, rel=1e-6)


def test_cosine_similarity_can_normalize_inputs() -> None:
    enc = IdentifierEncoder()
    v = enc.encode("invoice")
    scaled_a = v * 3.0
    scaled_b = v * 0.5
    score = cosine_similarity(scaled_a, scaled_b, assume_normalized=False)
    assert score == pytest.approx(1.0, rel=1e-6)


def test_cosine_similarity_shape_mismatch_raises() -> None:
    with pytest.raises(ValueError):
        cosine_similarity(np.zeros(32, dtype=np.float32), np.zeros(16, dtype=np.float32))


def test_l2_distance_for_identical_vectors_is_zero() -> None:
    enc = IdentifierEncoder()
    v = enc.encode("invoice")
    assert l2_distance(v, v) == pytest.approx(0.0, abs=1e-7)
