Files
sub2api/backend/internal/service/grok_audio_test.go
T
IanShaw027 35faaa6d21 feat(grok): register custom-voices CRUD and audio download gateway routes
Forward list/get/patch/delete and reference-audio paths with safe path segment
encoding, method passthrough, and empty-body GET/DELETE handling.
2026-08-08 08:48:38 +08:00

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")
}