import numpy as np import pytest from agent_ai.embedding_index import build_faiss_index, search_faiss def test_build_and_search_faiss_index(): embeddings = np.array([ [1.0, 0.0, 0.0, 0.0], [0.9, 0.1, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.9, 0.1, 0.0], [0.0, 0.0, 1.0, 0.0] ], dtype=np.float32) # Construire l'index index = build_faiss_index(embeddings) # Requête : un vecteur proche du premier query = np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float32) distances, indices = search_faiss(index, query, top_k=3) # Vérifier la forme des résultats assert distances.shape == (1, 3) assert indices.shape == (1, 3) # Le vecteur le plus proche doit être lui-même (distance max = 1 après normalisation) assert indices[0, 0] == 0 assert np.isclose(distances[0, 0], 0.0, atol=1e-5) # Les distances doivent être décroissantes (cosine similarity décroissante) assert distances[0, 0] <= distances[0, 1] <= distances[0, 2] def test_search_batch_queries(): embeddings = np.random.rand(10, 8).astype(np.float32) index = build_faiss_index(embeddings) queries = np.array([ embeddings[0], embeddings[1] ]) distances, indices = search_faiss(index, queries, top_k=2) assert distances.shape == (2, 2) assert indices.shape == (2, 2)