package embed_test import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/mathiasbq/hyperguild/ingestion/internal/embed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestNew_EmptyURLReturnsNil(t *testing.T) { assert.Nil(t, embed.New("", "model")) } func TestEmbed_ReturnsVector(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/api/embed", r.URL.Path) var req map[string]any require.NoError(t, json.NewDecoder(r.Body).Decode(&req)) assert.Equal(t, "nomic", req["model"]) assert.Equal(t, "hello", req["input"]) _ = json.NewEncoder(w).Encode(map[string]any{ "embeddings": [][]float32{{0.1, 0.2, 0.3}}, }) })) defer srv.Close() c := embed.New(srv.URL, "nomic") require.NotNil(t, c) v, err := c.Embed(context.Background(), "hello") require.NoError(t, err) assert.Equal(t, []float32{0.1, 0.2, 0.3}, v) } func TestEmbed_StripsTrailingSlashFromURL(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "/api/embed", r.URL.Path) _ = json.NewEncoder(w).Encode(map[string]any{"embeddings": [][]float32{{1.0}}}) })) defer srv.Close() c := embed.New(srv.URL+"/", "nomic") _, err := c.Embed(context.Background(), "x") require.NoError(t, err) } func TestEmbed_PropagatesUpstreamError(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusBadGateway) })) defer srv.Close() c := embed.New(srv.URL, "m") _, err := c.Embed(context.Background(), "x") require.Error(t, err) } func TestEmbed_RejectsEmptyEmbeddingsArray(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _ = json.NewEncoder(w).Encode(map[string]any{"embeddings": [][]float32{}}) })) defer srv.Close() c := embed.New(srv.URL, "m") _, err := c.Embed(context.Background(), "x") require.Error(t, err) } func TestEmbed_RejectsEmptyText(t *testing.T) { c := embed.New("http://127.0.0.1:1", "m") _, err := c.Embed(context.Background(), "") require.Error(t, err) }