| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- //go:build unit
- package service
- import (
- "testing"
- "github.com/Wei-Shaw/sub2api/internal/pkg/xai"
- "github.com/stretchr/testify/require"
- )
- 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 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 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)
- }
- })
- }
- }
- func TestGetGrokBaseURLUsesSubscriptionProxyForOAuth(t *testing.T) {
- tests := []struct {
- name string
- account Account
- expected string
- }{
- {
- name: "oauth without base_url uses CLI subscription proxy",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{},
- },
- expected: xai.DefaultCLIBaseURL,
- },
- {
- name: "oauth stored official API endpoint is honored (manual endpoint switch)",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": xai.DefaultBaseURL,
- },
- },
- expected: xai.DefaultBaseURL,
- },
- {
- name: "oauth stored regional API endpoint is honored",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "https://us-west-2.api.x.ai/v1",
- },
- },
- expected: "https://us-west-2.api.x.ai/v1",
- },
- {
- name: "oauth stored CLI proxy is honored verbatim",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": xai.DefaultCLIBaseURL,
- },
- },
- expected: xai.DefaultCLIBaseURL,
- },
- {
- name: "oauth unparseable base_url falls back to CLI proxy",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "not a url",
- },
- },
- expected: xai.DefaultCLIBaseURL,
- },
- {
- name: "oauth explicit custom base_url redirects forwarding traffic",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "https://custom.example.com/v1",
- },
- },
- expected: "https://custom.example.com/v1",
- },
- {
- name: "oauth custom base_url with path prefix redirects forwarding traffic",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "https://relay.example.com/xai/v1",
- },
- },
- expected: "https://relay.example.com/xai/v1",
- },
- {
- name: "API key without base_url uses official credit-backed API",
- account: Account{
- Type: AccountTypeAPIKey,
- Platform: PlatformGrok,
- Credentials: map[string]any{},
- },
- expected: xai.DefaultBaseURL,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- require.Equal(t, tt.expected, tt.account.GetGrokBaseURL())
- })
- }
- }
- func TestGetGrokBaseURLHonorsOAuthCustomRegardlessOfUnsafeOverrides(t *testing.T) {
- t.Setenv(xai.EnvAllowUnsafeURLOverrides, "true")
- account := Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "https://custom.example.com/v1",
- },
- }
- require.Equal(t, "https://custom.example.com/v1", account.GetGrokBaseURL())
- }
- func TestGetGrokMediaBaseURLRedirectsCLIGatewayToOfficialAPI(t *testing.T) {
- tests := []struct {
- name string
- account Account
- expected string
- }{
- {
- name: "oauth without base_url uses official media API",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{},
- },
- expected: xai.DefaultBaseURL,
- },
- {
- name: "oauth stored CLI proxy is separated from the media API",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": xai.DefaultCLIBaseURL,
- },
- },
- expected: xai.DefaultBaseURL,
- },
- {
- name: "oauth stored CLI proxy variant is canonicalized to the media API",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "HTTPS://CLI-CHAT-PROXY.GROK.COM:443/%76%31/",
- },
- },
- expected: xai.DefaultBaseURL,
- },
- {
- name: "oauth unparseable base_url falls back to official media API",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "not a url",
- },
- },
- expected: xai.DefaultBaseURL,
- },
- {
- name: "oauth stored official API endpoint is honored (manual endpoint switch)",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": xai.DefaultBaseURL,
- },
- },
- expected: xai.DefaultBaseURL,
- },
- {
- name: "oauth stored regional API endpoint is honored for media",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "https://us-west-2.api.x.ai/v1",
- },
- },
- expected: "https://us-west-2.api.x.ai/v1",
- },
- {
- name: "oauth custom base_url redirects media traffic",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "https://custom.example.com/v1",
- },
- },
- expected: "https://custom.example.com/v1",
- },
- {
- name: "API key retains its configured media API",
- account: Account{
- Type: AccountTypeAPIKey,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "https://grok.example.com/v1",
- },
- },
- expected: "https://grok.example.com/v1",
- },
- {
- name: "non-Grok account has no Grok media base URL",
- account: Account{
- Type: AccountTypeOAuth,
- Platform: PlatformOpenAI,
- Credentials: map[string]any{},
- },
- expected: "",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- require.Equal(t, tt.expected, tt.account.GetGrokMediaBaseURL())
- })
- }
- }
- func TestGetGrokMediaBaseURLHonorsOAuthCustomRegardlessOfUnsafeOverrides(t *testing.T) {
- t.Setenv(xai.EnvAllowUnsafeURLOverrides, "true")
- account := Account{
- Type: AccountTypeOAuth,
- Platform: PlatformGrok,
- Credentials: map[string]any{
- "base_url": "https://custom.example.com/v1",
- },
- }
- require.Equal(t, "https://custom.example.com/v1", account.GetGrokMediaBaseURL())
- }
|