- windsurf_gateway_service: 添加上游延迟/TTFT/错误上下文记录 - endpoint: DeriveUpstreamEndpoint 添加 PlatformWindsurf 分支 - ops_error_logger: guessPlatformFromPath 添加 /windsurf/ 识别
221 lines
5.8 KiB
Go
221 lines
5.8 KiB
Go
//go:build unit
|
|
|
|
package service
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestGetBaseURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
account Account
|
|
expected string
|
|
}{
|
|
{
|
|
name: "non-apikey type returns empty",
|
|
account: Account{
|
|
Type: AccountTypeOAuth,
|
|
Platform: PlatformAnthropic,
|
|
},
|
|
expected: "",
|
|
},
|
|
{
|
|
name: "apikey without base_url returns default anthropic",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAnthropic,
|
|
Credentials: map[string]any{},
|
|
},
|
|
expected: "https://api.anthropic.com",
|
|
},
|
|
{
|
|
name: "apikey with custom base_url",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAnthropic,
|
|
Credentials: map[string]any{"base_url": "https://custom.example.com"},
|
|
},
|
|
expected: "https://custom.example.com",
|
|
},
|
|
{
|
|
name: "antigravity apikey auto-appends /antigravity",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{"base_url": "https://upstream.example.com"},
|
|
},
|
|
expected: "https://upstream.example.com/antigravity",
|
|
},
|
|
{
|
|
name: "antigravity apikey trims trailing slash before appending",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{"base_url": "https://upstream.example.com/"},
|
|
},
|
|
expected: "https://upstream.example.com/antigravity",
|
|
},
|
|
{
|
|
name: "antigravity apikey explicit sub2api upstream_type appends /antigravity",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://upstream.example.com",
|
|
"upstream_type": "sub2api",
|
|
},
|
|
},
|
|
expected: "https://upstream.example.com/antigravity",
|
|
},
|
|
{
|
|
name: "antigravity apikey newapi upstream_type does NOT append /antigravity",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://api.opusclaw.me",
|
|
"upstream_type": "newapi",
|
|
},
|
|
},
|
|
expected: "https://api.opusclaw.me",
|
|
},
|
|
{
|
|
name: "antigravity apikey newapi upstream_type trims trailing slash",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://api.opusclaw.me/",
|
|
"upstream_type": "newapi",
|
|
},
|
|
},
|
|
expected: "https://api.opusclaw.me",
|
|
},
|
|
{
|
|
name: "antigravity apikey upstream_type case-insensitive",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://api.opusclaw.me",
|
|
"upstream_type": "NewAPI",
|
|
},
|
|
},
|
|
expected: "https://api.opusclaw.me",
|
|
},
|
|
{
|
|
name: "antigravity non-apikey returns empty",
|
|
account: Account{
|
|
Type: AccountTypeOAuth,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{"base_url": "https://upstream.example.com"},
|
|
},
|
|
expected: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := tt.account.GetBaseURL()
|
|
if result != tt.expected {
|
|
t.Errorf("GetBaseURL() = %q, want %q", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetGeminiBaseURL(t *testing.T) {
|
|
const defaultGeminiURL = "https://generativelanguage.googleapis.com"
|
|
|
|
tests := []struct {
|
|
name string
|
|
account Account
|
|
expected string
|
|
}{
|
|
{
|
|
name: "apikey without base_url returns default",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformGemini,
|
|
Credentials: map[string]any{},
|
|
},
|
|
expected: defaultGeminiURL,
|
|
},
|
|
{
|
|
name: "apikey with custom base_url",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformGemini,
|
|
Credentials: map[string]any{"base_url": "https://custom-gemini.example.com"},
|
|
},
|
|
expected: "https://custom-gemini.example.com",
|
|
},
|
|
{
|
|
name: "antigravity apikey auto-appends /antigravity",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{"base_url": "https://upstream.example.com"},
|
|
},
|
|
expected: "https://upstream.example.com/antigravity",
|
|
},
|
|
{
|
|
name: "antigravity apikey trims trailing slash",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{"base_url": "https://upstream.example.com/"},
|
|
},
|
|
expected: "https://upstream.example.com/antigravity",
|
|
},
|
|
{
|
|
name: "antigravity apikey newapi upstream_type does NOT append /antigravity (gemini)",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{
|
|
"base_url": "https://api.opusclaw.me",
|
|
"upstream_type": "newapi",
|
|
},
|
|
},
|
|
expected: "https://api.opusclaw.me",
|
|
},
|
|
{
|
|
name: "antigravity oauth does NOT append /antigravity",
|
|
account: Account{
|
|
Type: AccountTypeOAuth,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{"base_url": "https://upstream.example.com"},
|
|
},
|
|
expected: "https://upstream.example.com",
|
|
},
|
|
{
|
|
name: "oauth without base_url returns default",
|
|
account: Account{
|
|
Type: AccountTypeOAuth,
|
|
Platform: PlatformAntigravity,
|
|
Credentials: map[string]any{},
|
|
},
|
|
expected: defaultGeminiURL,
|
|
},
|
|
{
|
|
name: "nil credentials returns default",
|
|
account: Account{
|
|
Type: AccountTypeAPIKey,
|
|
Platform: PlatformGemini,
|
|
},
|
|
expected: defaultGeminiURL,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := tt.account.GetGeminiBaseURL(defaultGeminiURL)
|
|
if result != tt.expected {
|
|
t.Errorf("GetGeminiBaseURL() = %q, want %q", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|