| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- package service
- import (
- "testing"
- "github.com/stretchr/testify/require"
- )
- func TestAccount_IsOpenAIPassthroughEnabled(t *testing.T) {
- t.Run("新字段开启", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeAPIKey,
- Extra: map[string]any{
- "openai_passthrough": true,
- },
- }
- require.True(t, account.IsOpenAIPassthroughEnabled())
- })
- t.Run("兼容旧字段", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_oauth_passthrough": true,
- },
- }
- require.True(t, account.IsOpenAIPassthroughEnabled())
- })
- t.Run("非OpenAI账号始终关闭", func(t *testing.T) {
- account := &Account{
- Platform: PlatformAnthropic,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_passthrough": true,
- },
- }
- require.False(t, account.IsOpenAIPassthroughEnabled())
- })
- t.Run("空额外配置默认关闭", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- }
- require.False(t, account.IsOpenAIPassthroughEnabled())
- })
- }
- func TestAccount_IsOpenAIOAuthPassthroughEnabled(t *testing.T) {
- t.Run("仅OAuth类型允许返回开启", func(t *testing.T) {
- oauthAccount := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_passthrough": true,
- },
- }
- require.True(t, oauthAccount.IsOpenAIOAuthPassthroughEnabled())
- apiKeyAccount := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeAPIKey,
- Extra: map[string]any{
- "openai_passthrough": true,
- },
- }
- require.False(t, apiKeyAccount.IsOpenAIOAuthPassthroughEnabled())
- })
- }
- func TestAccount_IsCodexCLIOnlyEnabled(t *testing.T) {
- t.Run("OpenAI OAuth 开启", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "codex_cli_only": true,
- },
- }
- require.True(t, account.IsCodexCLIOnlyEnabled())
- })
- t.Run("OpenAI OAuth 关闭", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "codex_cli_only": false,
- },
- }
- require.False(t, account.IsCodexCLIOnlyEnabled())
- })
- t.Run("字段缺失默认关闭", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{},
- }
- require.False(t, account.IsCodexCLIOnlyEnabled())
- })
- t.Run("类型非法默认关闭", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "codex_cli_only": "true",
- },
- }
- require.False(t, account.IsCodexCLIOnlyEnabled())
- })
- t.Run("非 OAuth 账号始终关闭", func(t *testing.T) {
- apiKeyAccount := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeAPIKey,
- Extra: map[string]any{
- "codex_cli_only": true,
- },
- }
- require.False(t, apiKeyAccount.IsCodexCLIOnlyEnabled())
- otherPlatform := &Account{
- Platform: PlatformAnthropic,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "codex_cli_only": true,
- },
- }
- require.False(t, otherPlatform.IsCodexCLIOnlyEnabled())
- })
- }
- func TestAccount_IsOpenAIResponsesWebSocketV2Enabled(t *testing.T) {
- t.Run("OAuth使用OAuth专用开关", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_oauth_responses_websockets_v2_enabled": true,
- },
- }
- require.True(t, account.IsOpenAIResponsesWebSocketV2Enabled())
- })
- t.Run("API Key使用API Key专用开关", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeAPIKey,
- Extra: map[string]any{
- "openai_apikey_responses_websockets_v2_enabled": true,
- },
- }
- require.True(t, account.IsOpenAIResponsesWebSocketV2Enabled())
- })
- t.Run("OAuth账号不会读取API Key专用开关", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_apikey_responses_websockets_v2_enabled": true,
- },
- }
- require.False(t, account.IsOpenAIResponsesWebSocketV2Enabled())
- })
- t.Run("分类型新键优先于兼容键", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_oauth_responses_websockets_v2_enabled": false,
- "responses_websockets_v2_enabled": true,
- "openai_ws_enabled": true,
- },
- }
- require.False(t, account.IsOpenAIResponsesWebSocketV2Enabled())
- })
- t.Run("分类型键缺失时回退兼容键", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeAPIKey,
- Extra: map[string]any{
- "responses_websockets_v2_enabled": true,
- },
- }
- require.True(t, account.IsOpenAIResponsesWebSocketV2Enabled())
- })
- t.Run("非OpenAI账号默认关闭", func(t *testing.T) {
- account := &Account{
- Platform: PlatformAnthropic,
- Type: AccountTypeAPIKey,
- Extra: map[string]any{
- "responses_websockets_v2_enabled": true,
- },
- }
- require.False(t, account.IsOpenAIResponsesWebSocketV2Enabled())
- })
- }
- func TestAccount_ResolveOpenAIResponsesWebSocketV2Mode(t *testing.T) {
- t.Run("default fallback to ctx_pool", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{},
- }
- require.Equal(t, OpenAIWSIngressModeCtxPool, account.ResolveOpenAIResponsesWebSocketV2Mode(""))
- require.Equal(t, OpenAIWSIngressModeCtxPool, account.ResolveOpenAIResponsesWebSocketV2Mode("invalid"))
- })
- t.Run("oauth mode field has highest priority", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModePassthrough,
- "openai_oauth_responses_websockets_v2_enabled": false,
- "responses_websockets_v2_enabled": false,
- },
- }
- require.Equal(t, OpenAIWSIngressModePassthrough, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeCtxPool))
- })
- t.Run("oauth mode supports http_bridge", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeHTTPBridge,
- },
- }
- require.Equal(t, OpenAIWSIngressModeHTTPBridge, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeCtxPool))
- })
- t.Run("legacy enabled maps to ctx_pool", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeAPIKey,
- Extra: map[string]any{
- "responses_websockets_v2_enabled": true,
- },
- }
- require.Equal(t, OpenAIWSIngressModeCtxPool, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeOff))
- })
- t.Run("shared/dedicated mode strings are compatible with ctx_pool", func(t *testing.T) {
- shared := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeShared,
- },
- }
- dedicated := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeDedicated,
- },
- }
- require.Equal(t, OpenAIWSIngressModeShared, shared.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeOff))
- require.Equal(t, OpenAIWSIngressModeDedicated, dedicated.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeOff))
- require.Equal(t, OpenAIWSIngressModeCtxPool, normalizeOpenAIWSIngressDefaultMode(OpenAIWSIngressModeShared))
- require.Equal(t, OpenAIWSIngressModeCtxPool, normalizeOpenAIWSIngressDefaultMode(OpenAIWSIngressModeDedicated))
- })
- t.Run("legacy disabled maps to off", func(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeAPIKey,
- Extra: map[string]any{
- "openai_apikey_responses_websockets_v2_enabled": false,
- "responses_websockets_v2_enabled": true,
- },
- }
- require.Equal(t, OpenAIWSIngressModeOff, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeCtxPool))
- })
- t.Run("non openai always off", func(t *testing.T) {
- account := &Account{
- Platform: PlatformAnthropic,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeDedicated,
- },
- }
- require.Equal(t, OpenAIWSIngressModeOff, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeDedicated))
- })
- }
- func TestAccount_OpenAIWSExtraFlags(t *testing.T) {
- account := &Account{
- Platform: PlatformOpenAI,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_ws_force_http": true,
- "openai_ws_allow_store_recovery": true,
- },
- }
- require.True(t, account.IsOpenAIWSForceHTTPEnabled())
- require.True(t, account.IsOpenAIWSAllowStoreRecoveryEnabled())
- off := &Account{Platform: PlatformOpenAI, Type: AccountTypeOAuth, Extra: map[string]any{}}
- require.False(t, off.IsOpenAIWSForceHTTPEnabled())
- require.False(t, off.IsOpenAIWSAllowStoreRecoveryEnabled())
- var nilAccount *Account
- require.False(t, nilAccount.IsOpenAIWSAllowStoreRecoveryEnabled())
- nonOpenAI := &Account{
- Platform: PlatformAnthropic,
- Type: AccountTypeOAuth,
- Extra: map[string]any{
- "openai_ws_allow_store_recovery": true,
- },
- }
- require.False(t, nonOpenAI.IsOpenAIWSAllowStoreRecoveryEnabled())
- }
|