account_openai_passthrough_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package service
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestAccount_IsOpenAIPassthroughEnabled(t *testing.T) {
  7. t.Run("新字段开启", func(t *testing.T) {
  8. account := &Account{
  9. Platform: PlatformOpenAI,
  10. Type: AccountTypeAPIKey,
  11. Extra: map[string]any{
  12. "openai_passthrough": true,
  13. },
  14. }
  15. require.True(t, account.IsOpenAIPassthroughEnabled())
  16. })
  17. t.Run("兼容旧字段", func(t *testing.T) {
  18. account := &Account{
  19. Platform: PlatformOpenAI,
  20. Type: AccountTypeOAuth,
  21. Extra: map[string]any{
  22. "openai_oauth_passthrough": true,
  23. },
  24. }
  25. require.True(t, account.IsOpenAIPassthroughEnabled())
  26. })
  27. t.Run("非OpenAI账号始终关闭", func(t *testing.T) {
  28. account := &Account{
  29. Platform: PlatformAnthropic,
  30. Type: AccountTypeOAuth,
  31. Extra: map[string]any{
  32. "openai_passthrough": true,
  33. },
  34. }
  35. require.False(t, account.IsOpenAIPassthroughEnabled())
  36. })
  37. t.Run("空额外配置默认关闭", func(t *testing.T) {
  38. account := &Account{
  39. Platform: PlatformOpenAI,
  40. Type: AccountTypeOAuth,
  41. }
  42. require.False(t, account.IsOpenAIPassthroughEnabled())
  43. })
  44. }
  45. func TestAccount_IsOpenAIOAuthPassthroughEnabled(t *testing.T) {
  46. t.Run("仅OAuth类型允许返回开启", func(t *testing.T) {
  47. oauthAccount := &Account{
  48. Platform: PlatformOpenAI,
  49. Type: AccountTypeOAuth,
  50. Extra: map[string]any{
  51. "openai_passthrough": true,
  52. },
  53. }
  54. require.True(t, oauthAccount.IsOpenAIOAuthPassthroughEnabled())
  55. apiKeyAccount := &Account{
  56. Platform: PlatformOpenAI,
  57. Type: AccountTypeAPIKey,
  58. Extra: map[string]any{
  59. "openai_passthrough": true,
  60. },
  61. }
  62. require.False(t, apiKeyAccount.IsOpenAIOAuthPassthroughEnabled())
  63. })
  64. }
  65. func TestAccount_IsCodexCLIOnlyEnabled(t *testing.T) {
  66. t.Run("OpenAI OAuth 开启", func(t *testing.T) {
  67. account := &Account{
  68. Platform: PlatformOpenAI,
  69. Type: AccountTypeOAuth,
  70. Extra: map[string]any{
  71. "codex_cli_only": true,
  72. },
  73. }
  74. require.True(t, account.IsCodexCLIOnlyEnabled())
  75. })
  76. t.Run("OpenAI OAuth 关闭", func(t *testing.T) {
  77. account := &Account{
  78. Platform: PlatformOpenAI,
  79. Type: AccountTypeOAuth,
  80. Extra: map[string]any{
  81. "codex_cli_only": false,
  82. },
  83. }
  84. require.False(t, account.IsCodexCLIOnlyEnabled())
  85. })
  86. t.Run("字段缺失默认关闭", func(t *testing.T) {
  87. account := &Account{
  88. Platform: PlatformOpenAI,
  89. Type: AccountTypeOAuth,
  90. Extra: map[string]any{},
  91. }
  92. require.False(t, account.IsCodexCLIOnlyEnabled())
  93. })
  94. t.Run("类型非法默认关闭", func(t *testing.T) {
  95. account := &Account{
  96. Platform: PlatformOpenAI,
  97. Type: AccountTypeOAuth,
  98. Extra: map[string]any{
  99. "codex_cli_only": "true",
  100. },
  101. }
  102. require.False(t, account.IsCodexCLIOnlyEnabled())
  103. })
  104. t.Run("非 OAuth 账号始终关闭", func(t *testing.T) {
  105. apiKeyAccount := &Account{
  106. Platform: PlatformOpenAI,
  107. Type: AccountTypeAPIKey,
  108. Extra: map[string]any{
  109. "codex_cli_only": true,
  110. },
  111. }
  112. require.False(t, apiKeyAccount.IsCodexCLIOnlyEnabled())
  113. otherPlatform := &Account{
  114. Platform: PlatformAnthropic,
  115. Type: AccountTypeOAuth,
  116. Extra: map[string]any{
  117. "codex_cli_only": true,
  118. },
  119. }
  120. require.False(t, otherPlatform.IsCodexCLIOnlyEnabled())
  121. })
  122. }
  123. func TestAccount_IsOpenAIResponsesWebSocketV2Enabled(t *testing.T) {
  124. t.Run("OAuth使用OAuth专用开关", func(t *testing.T) {
  125. account := &Account{
  126. Platform: PlatformOpenAI,
  127. Type: AccountTypeOAuth,
  128. Extra: map[string]any{
  129. "openai_oauth_responses_websockets_v2_enabled": true,
  130. },
  131. }
  132. require.True(t, account.IsOpenAIResponsesWebSocketV2Enabled())
  133. })
  134. t.Run("API Key使用API Key专用开关", func(t *testing.T) {
  135. account := &Account{
  136. Platform: PlatformOpenAI,
  137. Type: AccountTypeAPIKey,
  138. Extra: map[string]any{
  139. "openai_apikey_responses_websockets_v2_enabled": true,
  140. },
  141. }
  142. require.True(t, account.IsOpenAIResponsesWebSocketV2Enabled())
  143. })
  144. t.Run("OAuth账号不会读取API Key专用开关", func(t *testing.T) {
  145. account := &Account{
  146. Platform: PlatformOpenAI,
  147. Type: AccountTypeOAuth,
  148. Extra: map[string]any{
  149. "openai_apikey_responses_websockets_v2_enabled": true,
  150. },
  151. }
  152. require.False(t, account.IsOpenAIResponsesWebSocketV2Enabled())
  153. })
  154. t.Run("分类型新键优先于兼容键", func(t *testing.T) {
  155. account := &Account{
  156. Platform: PlatformOpenAI,
  157. Type: AccountTypeOAuth,
  158. Extra: map[string]any{
  159. "openai_oauth_responses_websockets_v2_enabled": false,
  160. "responses_websockets_v2_enabled": true,
  161. "openai_ws_enabled": true,
  162. },
  163. }
  164. require.False(t, account.IsOpenAIResponsesWebSocketV2Enabled())
  165. })
  166. t.Run("分类型键缺失时回退兼容键", func(t *testing.T) {
  167. account := &Account{
  168. Platform: PlatformOpenAI,
  169. Type: AccountTypeAPIKey,
  170. Extra: map[string]any{
  171. "responses_websockets_v2_enabled": true,
  172. },
  173. }
  174. require.True(t, account.IsOpenAIResponsesWebSocketV2Enabled())
  175. })
  176. t.Run("非OpenAI账号默认关闭", func(t *testing.T) {
  177. account := &Account{
  178. Platform: PlatformAnthropic,
  179. Type: AccountTypeAPIKey,
  180. Extra: map[string]any{
  181. "responses_websockets_v2_enabled": true,
  182. },
  183. }
  184. require.False(t, account.IsOpenAIResponsesWebSocketV2Enabled())
  185. })
  186. }
  187. func TestAccount_ResolveOpenAIResponsesWebSocketV2Mode(t *testing.T) {
  188. t.Run("default fallback to ctx_pool", func(t *testing.T) {
  189. account := &Account{
  190. Platform: PlatformOpenAI,
  191. Type: AccountTypeOAuth,
  192. Extra: map[string]any{},
  193. }
  194. require.Equal(t, OpenAIWSIngressModeCtxPool, account.ResolveOpenAIResponsesWebSocketV2Mode(""))
  195. require.Equal(t, OpenAIWSIngressModeCtxPool, account.ResolveOpenAIResponsesWebSocketV2Mode("invalid"))
  196. })
  197. t.Run("oauth mode field has highest priority", func(t *testing.T) {
  198. account := &Account{
  199. Platform: PlatformOpenAI,
  200. Type: AccountTypeOAuth,
  201. Extra: map[string]any{
  202. "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModePassthrough,
  203. "openai_oauth_responses_websockets_v2_enabled": false,
  204. "responses_websockets_v2_enabled": false,
  205. },
  206. }
  207. require.Equal(t, OpenAIWSIngressModePassthrough, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeCtxPool))
  208. })
  209. t.Run("oauth mode supports http_bridge", func(t *testing.T) {
  210. account := &Account{
  211. Platform: PlatformOpenAI,
  212. Type: AccountTypeOAuth,
  213. Extra: map[string]any{
  214. "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeHTTPBridge,
  215. },
  216. }
  217. require.Equal(t, OpenAIWSIngressModeHTTPBridge, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeCtxPool))
  218. })
  219. t.Run("legacy enabled maps to ctx_pool", func(t *testing.T) {
  220. account := &Account{
  221. Platform: PlatformOpenAI,
  222. Type: AccountTypeAPIKey,
  223. Extra: map[string]any{
  224. "responses_websockets_v2_enabled": true,
  225. },
  226. }
  227. require.Equal(t, OpenAIWSIngressModeCtxPool, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeOff))
  228. })
  229. t.Run("shared/dedicated mode strings are compatible with ctx_pool", func(t *testing.T) {
  230. shared := &Account{
  231. Platform: PlatformOpenAI,
  232. Type: AccountTypeOAuth,
  233. Extra: map[string]any{
  234. "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeShared,
  235. },
  236. }
  237. dedicated := &Account{
  238. Platform: PlatformOpenAI,
  239. Type: AccountTypeOAuth,
  240. Extra: map[string]any{
  241. "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeDedicated,
  242. },
  243. }
  244. require.Equal(t, OpenAIWSIngressModeShared, shared.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeOff))
  245. require.Equal(t, OpenAIWSIngressModeDedicated, dedicated.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeOff))
  246. require.Equal(t, OpenAIWSIngressModeCtxPool, normalizeOpenAIWSIngressDefaultMode(OpenAIWSIngressModeShared))
  247. require.Equal(t, OpenAIWSIngressModeCtxPool, normalizeOpenAIWSIngressDefaultMode(OpenAIWSIngressModeDedicated))
  248. })
  249. t.Run("legacy disabled maps to off", func(t *testing.T) {
  250. account := &Account{
  251. Platform: PlatformOpenAI,
  252. Type: AccountTypeAPIKey,
  253. Extra: map[string]any{
  254. "openai_apikey_responses_websockets_v2_enabled": false,
  255. "responses_websockets_v2_enabled": true,
  256. },
  257. }
  258. require.Equal(t, OpenAIWSIngressModeOff, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeCtxPool))
  259. })
  260. t.Run("non openai always off", func(t *testing.T) {
  261. account := &Account{
  262. Platform: PlatformAnthropic,
  263. Type: AccountTypeOAuth,
  264. Extra: map[string]any{
  265. "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModeDedicated,
  266. },
  267. }
  268. require.Equal(t, OpenAIWSIngressModeOff, account.ResolveOpenAIResponsesWebSocketV2Mode(OpenAIWSIngressModeDedicated))
  269. })
  270. }
  271. func TestAccount_OpenAIWSExtraFlags(t *testing.T) {
  272. account := &Account{
  273. Platform: PlatformOpenAI,
  274. Type: AccountTypeOAuth,
  275. Extra: map[string]any{
  276. "openai_ws_force_http": true,
  277. "openai_ws_allow_store_recovery": true,
  278. },
  279. }
  280. require.True(t, account.IsOpenAIWSForceHTTPEnabled())
  281. require.True(t, account.IsOpenAIWSAllowStoreRecoveryEnabled())
  282. off := &Account{Platform: PlatformOpenAI, Type: AccountTypeOAuth, Extra: map[string]any{}}
  283. require.False(t, off.IsOpenAIWSForceHTTPEnabled())
  284. require.False(t, off.IsOpenAIWSAllowStoreRecoveryEnabled())
  285. var nilAccount *Account
  286. require.False(t, nilAccount.IsOpenAIWSAllowStoreRecoveryEnabled())
  287. nonOpenAI := &Account{
  288. Platform: PlatformAnthropic,
  289. Type: AccountTypeOAuth,
  290. Extra: map[string]any{
  291. "openai_ws_allow_store_recovery": true,
  292. },
  293. }
  294. require.False(t, nonOpenAI.IsOpenAIWSAllowStoreRecoveryEnabled())
  295. }