feat(ml): fuse two embedding models and protect the ML service with a token

This commit is contained in:
shira committed 2026-10-11 14:40:52 +03:00
1 parent bc00d6901f
commit b24e5ff906
63 files changed
+2287 -276

No files matched your search

@@ -14,12 +14,17 @@ import (
"lincodex/backend/internal/port/outbound"
)
// testToken is a syntactically valid service token of the tests.
const testToken = "test-ml-service-token-0123456789abcdef"
type fakeService struct {
mutex sync.Mutex
requests int
batches []int
models []string
respond func(writer http.ResponseWriter, kind string, texts []string)
// authorizations records the Authorization header of every request.
authorizations []string
respond func(writer http.ResponseWriter, kind string, texts []string)
}
func (service *fakeService) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
@@ -33,6 +38,7 @@ func (service *fakeService) ServeHTTP(writer http.ResponseWriter, request *http.
service.requests++
service.batches = append(service.batches, len(body.Texts))
service.models = append(service.models, body.Model)
service.authorizations = append(service.authorizations, request.Header.Get("Authorization"))
service.mutex.Unlock()
service.respond(writer, body.Kind, body.Texts)
}
@@ -53,7 +59,7 @@ func newTestEmbedder(t *testing.T, service *fakeService, clock func() time.Time)
t.Helper()
server := httptest.NewServer(service)
t.Cleanup(server.Close)
embedder, err := NewEmbedder(Settings{BaseURL: server.URL, Timeout: time.Second, FailureThreshold: 2, OpenDuration: time.Minute}, clock)
embedder, err := NewEmbedder(Settings{BaseURL: server.URL, Token: testToken, Timeout: time.Second, FailureThreshold: 2, OpenDuration: time.Minute}, clock)
if err != nil {
t.Fatalf("NewEmbedder() error = %v", err)
}
@@ -77,6 +83,25 @@ func TestEmbedSplitsBatchesAndChecksTheResponse(t *testing.T) {
if strings.Join(service.models, ",") != "example/model,example/model" {
t.Fatalf("requested models = %v, want the named model in every batch", service.models)
}
for _, header := range service.authorizations {
if header != "Bearer "+testToken {
t.Fatalf("Authorization = %q, want the bearer token on every request", header)
}
}
}
func TestEmbedTreatsARefusedTokenAsUnavailableWithoutQuotingIt(t *testing.T) {
service := &fakeService{respond: func(writer http.ResponseWriter, _ string, _ []string) {
writer.Header().Set("Content-Type", "application/problem+json")
writer.Header().Set("WWW-Authenticate", "Bearer")
writer.WriteHeader(http.StatusUnauthorized)
_, _ = writer.Write([]byte(`{"type":"urn:lincodex:problem:unauthenticated","title":"x","status":401,"code":"unauthenticated"}`))
}}
embedder := newTestEmbedder(t, service, time.Now)
_, _, err := embedder.Embed(context.Background(), "example/model", outbound.EmbeddingQuery, []string{"a"})
if !errors.Is(err, outbound.ErrEmbedderUnavailable) || strings.Contains(err.Error(), testToken) {
t.Fatalf("error = %v, want ErrEmbedderUnavailable without the token", err)
}
}
func TestEmbedRejectsResponsesThatBreakTheContract(t *testing.T) {
@@ -138,8 +163,11 @@ func TestEmbedNeverQuotesTextsInErrors(t *testing.T) {
func TestNewEmbedderRejectsUnsafeURLs(t *testing.T) {
for _, raw := range []string{"", "ftp://ml:8000", "http://user:secret@ml:8000", "ml:8000"} {
if _, err := NewEmbedder(Settings{BaseURL: raw, Timeout: time.Second, FailureThreshold: 1, OpenDuration: time.Second}, time.Now); err == nil {
if _, err := NewEmbedder(Settings{BaseURL: raw, Token: testToken, Timeout: time.Second, FailureThreshold: 1, OpenDuration: time.Second}, time.Now); err == nil {
t.Errorf("NewEmbedder(%q) accepted the URL", raw)
}
}
if _, err := NewEmbedder(Settings{BaseURL: "http://ml:8000", Timeout: time.Second, FailureThreshold: 1, OpenDuration: time.Second}, time.Now); err == nil {
t.Error("NewEmbedder() accepted a client without the service token")
}
}