Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
111 lines
2.9 KiB
Go
111 lines
2.9 KiB
Go
package aiprovider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/platformsettings"
|
|
)
|
|
|
|
func TestTestPlatformRole_unknown(t *testing.T) {
|
|
t.Parallel()
|
|
svc := NewService(nil, EnvConfig{})
|
|
res, err := svc.TestPlatformRole(context.Background(), "nope")
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if res["status"] != "failed" {
|
|
t.Fatalf("status=%v", res["status"])
|
|
}
|
|
}
|
|
|
|
func TestTestPlatformRole_skippedWhenUnset(t *testing.T) {
|
|
t.Parallel()
|
|
svc := NewService(nil, EnvConfig{})
|
|
svc.Platform = platformsettings.NewService(nil, platformsettings.EnvConfig{})
|
|
res, err := svc.TestPlatformRole(context.Background(), RoleSupport)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res["status"] != "skipped" {
|
|
t.Fatalf("status=%v message=%v", res["status"], res["message"])
|
|
}
|
|
}
|
|
|
|
func TestTestPlatformRole_chatProbeOK(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/chat/completions" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
auth := r.Header.Get("Authorization")
|
|
if !strings.HasPrefix(auth, "Bearer sk-test-") {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"choices": []map[string]any{
|
|
{"message": map[string]any{"content": "ok"}},
|
|
},
|
|
"usage": map[string]any{"total_tokens": 1},
|
|
})
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
plat := platformsettings.NewService(nil, platformsettings.EnvConfig{
|
|
OpenAIAPIKey: "sk-test-platform",
|
|
OpenAIBaseURL: srv.URL + "/v1",
|
|
OpenAIModel: "test-model",
|
|
})
|
|
svc := NewService(nil, EnvConfig{})
|
|
svc.Platform = plat
|
|
|
|
res, err := svc.TestPlatformRole(context.Background(), RoleProcessing)
|
|
if err != nil {
|
|
t.Fatalf("err=%v res=%v", err, res)
|
|
}
|
|
if res["status"] != "ok" {
|
|
t.Fatalf("status=%v message=%v", res["status"], res["message"])
|
|
}
|
|
if msg, _ := res["message"].(string); strings.Contains(strings.ToLower(msg), "sk-") {
|
|
t.Fatalf("message must not leak key fragments: %q", msg)
|
|
}
|
|
}
|
|
|
|
func TestTestPlatformRole_embedProbeOK(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/embeddings" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"data": []map[string]any{
|
|
{"embedding": []float32{0.1, 0.2}, "index": 0},
|
|
},
|
|
})
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
plat := platformsettings.NewService(nil, platformsettings.EnvConfig{
|
|
OpenAIEmbeddingAPIKey: "sk-test-embed",
|
|
OpenAIEmbeddingBaseURL: srv.URL + "/v1",
|
|
OpenAIEmbeddingModel: "text-embedding-3-small",
|
|
})
|
|
svc := NewService(nil, EnvConfig{})
|
|
svc.Platform = plat
|
|
|
|
res, err := svc.TestPlatformRole(context.Background(), RoleVectorization)
|
|
if err != nil {
|
|
t.Fatalf("err=%v res=%v", err, res)
|
|
}
|
|
if res["status"] != "ok" {
|
|
t.Fatalf("status=%v message=%v", res["status"], res["message"])
|
|
}
|
|
}
|