mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-18 15:00:30 +00:00
Forward list/get/patch/delete and reference-audio paths with safe path segment encoding, method passthrough, and empty-body GET/DELETE handling.
68 lines
2.1 KiB
Go
68 lines
2.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/xai"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBuildGrokVoiceURL_UsesAPIDefaultForCLIProxyBase(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{
|
|
"base_url": xai.DefaultCLIBaseURL,
|
|
},
|
|
}
|
|
url, err := buildGrokVoiceURL(account, nil, "tts")
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultBaseURL+"/tts", url)
|
|
|
|
url, err = buildGrokVoiceURL(account, nil, "realtime")
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultBaseURL+"/realtime", url)
|
|
}
|
|
|
|
func TestBuildGrokVoiceURL_EmptyBaseFallsBackToAPI(t *testing.T) {
|
|
account := &Account{
|
|
Platform: PlatformGrok,
|
|
Type: AccountTypeOAuth,
|
|
Credentials: map[string]any{},
|
|
}
|
|
url, err := buildGrokVoiceURL(account, nil, "stt")
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultBaseURL+"/stt", url)
|
|
}
|
|
|
|
func TestBuildGrokVoiceURL_RequiresEndpoint(t *testing.T) {
|
|
account := &Account{Platform: PlatformGrok, Type: AccountTypeOAuth}
|
|
_, err := buildGrokVoiceURL(account, nil, " ")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestBuildGrokVoiceURL_EncodesCustomVoicePathSegments(t *testing.T) {
|
|
account := &Account{Platform: PlatformGrok, Type: AccountTypeOAuth}
|
|
got, err := buildGrokVoiceURL(account, nil, "custom-voices/nlbqfwie/audio")
|
|
require.NoError(t, err)
|
|
require.Equal(t, xai.DefaultBaseURL+"/custom-voices/nlbqfwie/audio", got)
|
|
|
|
_, err = buildGrokVoiceURL(account, nil, "custom-voices/../audio")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestForwardGrokVoice_RejectsNonGrok(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
_, err := svc.ForwardGrokVoice(context.Background(), nil, &Account{Platform: PlatformOpenAI}, "tts", []byte(`{}`), "application/json")
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "not supported")
|
|
}
|
|
|
|
func TestForwardGrokVoice_RejectsUnknownEndpoint(t *testing.T) {
|
|
svc := &OpenAIGatewayService{}
|
|
_, err := svc.ForwardGrokVoice(context.Background(), nil, &Account{Platform: PlatformGrok}, "unknown", []byte(`{}`), "application/json")
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "unsupported")
|
|
}
|