account_anthropic_passthrough_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package service
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestAccount_IsAnthropicAPIKeyPassthroughEnabled(t *testing.T) {
  7. t.Run("Anthropic API Key 开启", func(t *testing.T) {
  8. account := &Account{
  9. Platform: PlatformAnthropic,
  10. Type: AccountTypeAPIKey,
  11. Extra: map[string]any{
  12. "anthropic_passthrough": true,
  13. },
  14. }
  15. require.True(t, account.IsAnthropicAPIKeyPassthroughEnabled())
  16. })
  17. t.Run("Anthropic API Key 关闭", func(t *testing.T) {
  18. account := &Account{
  19. Platform: PlatformAnthropic,
  20. Type: AccountTypeAPIKey,
  21. Extra: map[string]any{
  22. "anthropic_passthrough": false,
  23. },
  24. }
  25. require.False(t, account.IsAnthropicAPIKeyPassthroughEnabled())
  26. })
  27. t.Run("字段类型非法默认关闭", func(t *testing.T) {
  28. account := &Account{
  29. Platform: PlatformAnthropic,
  30. Type: AccountTypeAPIKey,
  31. Extra: map[string]any{
  32. "anthropic_passthrough": "true",
  33. },
  34. }
  35. require.False(t, account.IsAnthropicAPIKeyPassthroughEnabled())
  36. })
  37. t.Run("非 Anthropic API Key 账号始终关闭", func(t *testing.T) {
  38. oauth := &Account{
  39. Platform: PlatformAnthropic,
  40. Type: AccountTypeOAuth,
  41. Extra: map[string]any{
  42. "anthropic_passthrough": true,
  43. },
  44. }
  45. require.False(t, oauth.IsAnthropicAPIKeyPassthroughEnabled())
  46. openai := &Account{
  47. Platform: PlatformOpenAI,
  48. Type: AccountTypeAPIKey,
  49. Extra: map[string]any{
  50. "anthropic_passthrough": true,
  51. },
  52. }
  53. require.False(t, openai.IsAnthropicAPIKeyPassthroughEnabled())
  54. })
  55. }
  56. func TestAccount_GetAnthropicAPIKeyAuthScheme(t *testing.T) {
  57. tests := []struct {
  58. name string
  59. account *Account
  60. want string
  61. }{
  62. {
  63. name: "missing extra defaults to x-api-key",
  64. account: &Account{
  65. Platform: PlatformAnthropic,
  66. Type: AccountTypeAPIKey,
  67. },
  68. want: AnthropicAPIKeyAuthSchemeXAPIKey,
  69. },
  70. {
  71. name: "explicit bearer",
  72. account: &Account{
  73. Platform: PlatformAnthropic,
  74. Type: AccountTypeAPIKey,
  75. Extra: map[string]any{
  76. "anthropic_apikey_auth_scheme": AnthropicAPIKeyAuthSchemeAuthorizationBearer,
  77. },
  78. },
  79. want: AnthropicAPIKeyAuthSchemeAuthorizationBearer,
  80. },
  81. {
  82. name: "invalid value defaults to x-api-key",
  83. account: &Account{
  84. Platform: PlatformAnthropic,
  85. Type: AccountTypeAPIKey,
  86. Extra: map[string]any{
  87. "anthropic_apikey_auth_scheme": "bearer",
  88. },
  89. },
  90. want: AnthropicAPIKeyAuthSchemeXAPIKey,
  91. },
  92. {
  93. name: "non Anthropic API key defaults to x-api-key",
  94. account: &Account{
  95. Platform: PlatformOpenAI,
  96. Type: AccountTypeAPIKey,
  97. Extra: map[string]any{
  98. "anthropic_apikey_auth_scheme": AnthropicAPIKeyAuthSchemeAuthorizationBearer,
  99. },
  100. },
  101. want: AnthropicAPIKeyAuthSchemeXAPIKey,
  102. },
  103. }
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. require.Equal(t, tt.want, tt.account.GetAnthropicAPIKeyAuthScheme())
  107. })
  108. }
  109. }