feat: add sub2api compatibility

This commit is contained in:
chick
2026-06-01 23:43:53 +08:00
parent 5f59b4bed3
commit f66540a273
6 changed files with 325 additions and 9 deletions
+51
View File
@@ -1,6 +1,7 @@
package service
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
@@ -61,6 +62,56 @@ func TestBuildModelChannelURLNormalizesArkPlanTaskPath(t *testing.T) {
}
}
func TestBuildModelChannelURLAcceptsVersionedSub2APIBaseURL(t *testing.T) {
got := BuildModelChannelURL(model.ModelChannel{BaseURL: "https://sub2api.example.com/v1"}, "/images/generations")
want := "https://sub2api.example.com/v1/images/generations"
if got != want {
t.Fatalf("BuildModelChannelURL = %q, want %q", got, want)
}
}
func TestNormalizePrivateSettingDefaultsSub2APIImageResponseFormatToURL(t *testing.T) {
setting := normalizePrivateSetting(model.PrivateSetting{Channels: []model.ModelChannel{{Compatibility: "sub2api"}}})
if got := setting.Channels[0].RequestOptions.ImageResponseFormat; got != "url" {
t.Fatalf("image response format = %q, want url", got)
}
}
func TestAdminTestChannelModelUsesImageGenerationProbeForImageModels(t *testing.T) {
var gotPath string
var gotPayload map[string]any
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
if err := json.NewDecoder(r.Body).Decode(&gotPayload); err != nil {
t.Fatalf("Decode request body: %v", err)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"url":"https://example.com/image.png"}]}`))
}))
defer server.Close()
result, err := testAdminChannelModel(model.ModelChannel{
Compatibility: "sub2api",
BaseURL: server.URL + "/v1",
APIKey: "test-key",
}, "gpt-image-2")
if err != nil {
t.Fatalf("testAdminChannelModel returned error: %v", err)
}
if result != "ok" {
t.Fatalf("result = %q, want ok", result)
}
if gotPath != "/v1/images/generations" {
t.Fatalf("path = %q, want /v1/images/generations", gotPath)
}
if gotPayload["model"] != "gpt-image-2" {
t.Fatalf("model = %#v", gotPayload["model"])
}
if gotPayload["response_format"] != "url" {
t.Fatalf("response_format = %#v, want url", gotPayload["response_format"])
}
}
func TestNormalizeSettingsPublishesEnabledChannelModelsAndRepairsDefaults(t *testing.T) {
settings := normalizeSettings(model.Settings{
Public: model.PublicSetting{