package com.agent.management.service.impl;

import org.junit.jupiter.api.Test;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.ObjectProvider;

import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

class VectorStoreServiceImplTest {
    @Test
    void toleratesMissingOptionalMilvusMetadata() {
        EmbeddingBridgeClient bridge = mock(EmbeddingBridgeClient.class);
        @SuppressWarnings("unchecked") ObjectProvider<VectorStore> provider = mock(ObjectProvider.class);
        Map<String, Object> hit = new LinkedHashMap<>();
        hit.put("text", "chunk");
        hit.put("score", 0.8);
        hit.put("document_id", null);
        hit.put("category_id", null);
        when(bridge.retrieve("q", 5, "hybrid", "")).thenReturn(Map.of("hits", List.of(hit)));

        var result = new VectorStoreServiceImpl(provider, bridge).similaritySearch("q", 5, 0.0, null);

        assertThat(result).hasSize(1);
        assertThat(result.get(0).getText()).isEqualTo("chunk");
        assertThat(result.get(0).getMetadata()).doesNotContainKeys("documentId", "categoryId");
    }
}
