Files
Local-Cursor/internal/client/models_fetch_test.go
FuDie0915 188225fae6 Ð޸´ȫÏîĿÉó²éÎÊÌⲢ·¢²¼ v1.1.9
¸ÄÓñ¾»ú¶ÀÁ¢ CA£¬»ָ´ CLI TLS УÑ飬±£»¤ƾ¾ÝÓëÅäÖò¢·¢±£´档
Ð޸´ Cursor Êý¾ݡ¢»Ự·¾¶¡¢´úÀíÉúÃüÖÜÆڡ¢ģÐÍÇëÇóÓë Skills Îļþ²Ù×÷¡£
Ϊ¶ÀÁ¢ Tab ·þÎñÔö¼ӿͻ§¶˼øȨ£¬Éý¼¶ÒÀÀµÓ빤¾ßÁ´£¬ÍêÉƷ¢²¼ԪÐÅϢͬ²½¡£

Ñé֤£ºÁ½¸ö Go ģ¿éµIJâÊÔ/vet¡¢¹ؼüģ¿é race¡¢ǰ¶˲âÊÔ/Éú²ú¹¹½¨/Éó¼ơ¢
Go ¿ɴï©¶´ɨÃèÓë macOS/Linux ¿ÉÐÐģ¿齻²æ±àÒë¾ùͨ¹ý¡£
Windows ¸ôÀëÆô¶¯ͨ¹ý£»δÍê³ɵĵã»÷ðÑ̺ÍԭÉúƽ̨Ñé֤¼ûÉó²鱨¸档
2026-11-10 09:59:34 +08:00

77 lines
2.7 KiB
Go

package client
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestListRemoteModelsResolvesEndpointForOpenAIBaseURLShapes(t *testing.T) {
cases := []struct {
name string
adapterType string
baseURL string
wantPath string
}{
{"openai 裸 host 追加 /v1/models", "openai", "/api", "/api/v1/models"},
{"openai base 以 /v1 结尾仅追加 /models", "openai", "/v1", "/v1/models"},
{"openai base 内嵌 /v1/chat/completions 剥离方法段", "openai", "/v1/chat/completions", "/v1/models"},
{"openai base 内嵌 /v1/responses 剥离方法段", "openai", "/v1/responses", "/v1/models"},
{"anthropic 裸 host 追加 /v1/models", "anthropic", "/api", "/api/v1/models"},
{"anthropic base 以 /v1 结尾仅追加 /models", "anthropic", "/v1", "/v1/models"},
{"openai base 大小写混合时按 /V1 结尾去重", "openai", "/V1", "/V1/models"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var gotPath string
var gotAuth string
var gotAPIKey string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotAuth = r.Header.Get("Authorization")
gotAPIKey = r.Header.Get("x-api-key")
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": []map[string]any{{"id": "gpt-test"}},
})
}))
defer server.Close()
baseURL := server.URL + tc.baseURL
service := &ProxyService{}
result, err := service.ListRemoteModels(tc.adapterType, baseURL, "sk-test")
if err != nil {
t.Fatalf("ListRemoteModels() error = %v", err)
}
if gotPath != tc.wantPath {
t.Fatalf("请求路径 = %s, 期望 %s", gotPath, tc.wantPath)
}
if len(result.Models) != 1 || result.Models[0].ID != "gpt-test" {
t.Fatalf("模型解析结果异常: %+v", result.Models)
}
if tc.adapterType == "openai" {
if gotAuth != "Bearer sk-test" {
t.Fatalf("openai 期望 Authorization Bearer 头, got %q", gotAuth)
}
} else if gotAPIKey != "sk-test" {
t.Fatalf("anthropic 期望 x-api-key 头, got %q", gotAPIKey)
}
})
}
}
func TestBuildModelsEndpointDefaults(t *testing.T) {
if got := buildModelsEndpoint("openai", "https://example.com"); got != "https://example.com/v1/models" {
t.Fatalf("裸 host 解析 = %s", got)
}
if got := buildModelsEndpoint("anthropic", "https://example.com/v1/"); got != "https://example.com/v1/models" {
t.Fatalf("/v1 结尾解析 = %s", got)
}
if !strings.HasPrefix(buildModelsEndpoint("openai", "https://example.com/v1"), "https://example.com/v1/") {
t.Fatalf("/v1 结尾去重后应保留原路径前缀")
}
}