ratelimit_service_openai_image_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //go:build unit
  2. package service
  3. import (
  4. "bytes"
  5. "context"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/Wei-Shaw/sub2api/internal/config"
  13. "github.com/gin-gonic/gin"
  14. "github.com/stretchr/testify/require"
  15. )
  16. func TestIsOpenAIImageRateLimitError(t *testing.T) {
  17. imageBody := []byte(`{"error":{"message":"Rate limit reached for gpt-image-2-codex (for limit gpt-image) in organization org on input-images per min: Limit 4000, Used 4000. Please try again in 467ms."}}`)
  18. textBody := []byte(`{"error":{"message":"Rate limit reached for gpt-5.4 in organization org on tokens per min: Limit 30000, Used 30000. Please try again in 1s."}}`)
  19. require.True(t, isOpenAIImageRateLimitError(http.StatusTooManyRequests, imageBody))
  20. require.False(t, isOpenAIImageRateLimitError(http.StatusTooManyRequests, textBody))
  21. require.False(t, isOpenAIImageRateLimitError(http.StatusBadRequest, imageBody))
  22. }
  23. func TestRateLimitService_HandleOpenAIImageRateLimit_ParsesTryAgainCooldown(t *testing.T) {
  24. repo := &modelNotFoundAccountRepoStub{}
  25. svc := &RateLimitService{accountRepo: repo}
  26. account := &Account{ID: 201, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
  27. body := []byte(`{"error":{"type":"rate_limit_exceeded","message":"Rate limit reached for gpt-image-2-codex (for limit gpt-image) on input-images per min. Please try again in 2s."}}`)
  28. before := time.Now()
  29. handled := svc.HandleOpenAIImageRateLimit(context.Background(), account, http.StatusTooManyRequests, http.Header{}, body)
  30. require.True(t, handled)
  31. require.Len(t, repo.modelRateLimitCalls, 1)
  32. call := repo.modelRateLimitCalls[0]
  33. require.Equal(t, account.ID, call.accountID)
  34. require.Equal(t, openAIImageGenerationRateLimitKey, call.scope)
  35. require.Equal(t, openAIImageRateLimitReason, call.reason)
  36. require.WithinDuration(t, before.Add(2*time.Second), call.resetAt, time.Second)
  37. }
  38. func TestRateLimitService_HandleOpenAIImageRateLimit_DefaultsToOneMinute(t *testing.T) {
  39. repo := &modelNotFoundAccountRepoStub{}
  40. svc := &RateLimitService{accountRepo: repo}
  41. account := &Account{ID: 202, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
  42. body := []byte(`{"error":{"type":"rate_limit_exceeded","message":"Rate limit reached for gpt-image-2-codex (for limit gpt-image) on input-images per min."}}`)
  43. before := time.Now()
  44. handled := svc.HandleOpenAIImageRateLimit(context.Background(), account, http.StatusTooManyRequests, http.Header{}, body)
  45. require.True(t, handled)
  46. require.Len(t, repo.modelRateLimitCalls, 1)
  47. call := repo.modelRateLimitCalls[0]
  48. require.Equal(t, openAIImageGenerationRateLimitKey, call.scope)
  49. require.Equal(t, openAIImageRateLimitReason, call.reason)
  50. require.WithinDuration(t, before.Add(time.Minute), call.resetAt, time.Second)
  51. }
  52. func TestOpenAIGatewayService_HandleOpenAIAccountUpstreamError_ImageRateLimitDoesNotBlockWholeAccount(t *testing.T) {
  53. repo := &modelNotFoundAccountRepoStub{}
  54. svc := &OpenAIGatewayService{rateLimitService: &RateLimitService{accountRepo: repo}}
  55. account := &Account{ID: 203, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
  56. body := []byte(`{"error":{"type":"rate_limit_exceeded","message":"Rate limit reached for gpt-image-2-codex (for limit gpt-image) on input-images per min. Please try again in 1s."}}`)
  57. disabled := svc.handleOpenAIAccountUpstreamError(context.Background(), account, http.StatusTooManyRequests, http.Header{}, body, "gpt-image-2")
  58. require.False(t, disabled)
  59. require.Len(t, repo.modelRateLimitCalls, 1)
  60. require.Equal(t, openAIImageGenerationRateLimitKey, repo.modelRateLimitCalls[0].scope)
  61. _, wholeAccountBlocked := svc.openaiAccountRuntimeBlockUntil.Load(account.ID)
  62. require.False(t, wholeAccountBlocked)
  63. }
  64. func TestOpenAIGatewayServiceForwardImages_ImageRateLimitReturnsFailoverAndCoolsCapability(t *testing.T) {
  65. gin.SetMode(gin.TestMode)
  66. repo := &modelNotFoundAccountRepoStub{}
  67. body := []byte(`{"model":"gpt-image-2","prompt":"draw a cat"}`)
  68. errorBody := `{"error":{"type":"rate_limit_exceeded","message":"Rate limit reached for gpt-image-2-codex (for limit gpt-image) in organization org on input-images per min: Limit 4000, Used 4000. Please try again in 1s."}}`
  69. req := httptest.NewRequest(http.MethodPost, "/v1/images/generations", bytes.NewReader(body))
  70. req.Header.Set("Content-Type", "application/json")
  71. rec := httptest.NewRecorder()
  72. c, _ := gin.CreateTestContext(rec)
  73. c.Request = req
  74. svc := &OpenAIGatewayService{
  75. rateLimitService: &RateLimitService{accountRepo: repo},
  76. httpUpstream: &httpUpstreamRecorder{
  77. resp: &http.Response{
  78. StatusCode: http.StatusTooManyRequests,
  79. Header: http.Header{"X-Request-Id": []string{"req_img_rate_limited"}},
  80. Body: io.NopCloser(strings.NewReader(errorBody)),
  81. },
  82. },
  83. }
  84. parsed, err := svc.ParseOpenAIImagesRequest(c, body)
  85. require.NoError(t, err)
  86. account := &Account{
  87. ID: 204,
  88. Name: "openai-oauth",
  89. Platform: PlatformOpenAI,
  90. Type: AccountTypeOAuth,
  91. Credentials: map[string]any{
  92. "access_token": "token-123",
  93. },
  94. }
  95. result, err := svc.ForwardImages(context.Background(), c, account, body, parsed, "")
  96. require.Nil(t, result)
  97. var failoverErr *UpstreamFailoverError
  98. require.ErrorAs(t, err, &failoverErr)
  99. require.Equal(t, http.StatusTooManyRequests, failoverErr.StatusCode)
  100. require.Contains(t, string(failoverErr.ResponseBody), "input-images per min")
  101. require.Len(t, repo.modelRateLimitCalls, 1)
  102. require.Equal(t, openAIImageGenerationRateLimitKey, repo.modelRateLimitCalls[0].scope)
  103. }
  104. // issue #6171:上游"回文字没回图"是**这一轮**的结果(模型选择了说话),不是账号能力
  105. // 失效。它同时被判为可重试(502)并驱动 failover,若还写 30 分钟账号级冷却,一次闲聊
  106. // 回复就会沿号池把每个被重试到的账号依次冷却掉。冷却仍保留给结构化上游证据,见
  107. // TestOpenAIGatewayServiceForwardImages_StructuredUnavailableCoolsImageCapability。
  108. func TestOpenAIGatewayServiceForwardImages_TextFallbackDoesNotCoolImageCapability(t *testing.T) {
  109. gin.SetMode(gin.TestMode)
  110. repo := &modelNotFoundAccountRepoStub{}
  111. body := []byte(`{"model":"gpt-image-2","prompt":"draw a cat"}`)
  112. upstreamSSE := "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"r\",\"status\":\"completed\",\"model\":\"gpt-5.4-mini\",\"output\":[{\"type\":\"message\",\"content\":[{\"type\":\"output_text\",\"text\":\"Here's a polished image prompt for your request.\"}]}]}}\n\n"
  113. req := httptest.NewRequest(http.MethodPost, "/v1/images/generations", bytes.NewReader(body))
  114. req.Header.Set("Content-Type", "application/json")
  115. rec := httptest.NewRecorder()
  116. c, _ := gin.CreateTestContext(rec)
  117. c.Request = req
  118. svc := &OpenAIGatewayService{
  119. accountRepo: repo,
  120. httpUpstream: &httpUpstreamRecorder{
  121. resp: &http.Response{
  122. StatusCode: http.StatusOK,
  123. Header: http.Header{"Content-Type": []string{"text/event-stream"}},
  124. Body: io.NopCloser(strings.NewReader(upstreamSSE)),
  125. },
  126. },
  127. }
  128. parsed, err := svc.ParseOpenAIImagesRequest(c, body)
  129. require.NoError(t, err)
  130. account := &Account{
  131. ID: 205,
  132. Name: "openai-oauth",
  133. Platform: PlatformOpenAI,
  134. Type: AccountTypeOAuth,
  135. Credentials: map[string]any{
  136. "access_token": "token-123",
  137. },
  138. }
  139. result, err := svc.ForwardImages(context.Background(), c, account, body, parsed, "")
  140. require.Nil(t, result)
  141. require.Error(t, err)
  142. var failoverErr *UpstreamFailoverError
  143. require.ErrorAs(t, err, &failoverErr)
  144. require.False(t, failoverErr.RetryableOnSameAccount)
  145. // 换号行为不变:该判据仍足以放弃本账号重试这一次请求……
  146. require.Equal(t, http.StatusBadGateway, failoverErr.StatusCode)
  147. // ……但不再写任何账号级状态,否则重试会把冷却一路刷到整个号池。
  148. require.Empty(t, repo.modelRateLimitCalls,
  149. "模型回文字只说明这一轮没出图,不构成账号 30 分钟不可用的证据")
  150. }
  151. // 对照不变式:上游 error 帧点名 image_generation_unavailable 时仍写冷却,
  152. // 保证 #6171 的修复没有把这项能力保护整个废掉。
  153. func TestOpenAIGatewayServiceForwardImages_StructuredUnavailableCoolsImageCapability(t *testing.T) {
  154. gin.SetMode(gin.TestMode)
  155. repo := &modelNotFoundAccountRepoStub{}
  156. body := []byte(`{"model":"gpt-image-2","prompt":"draw a cat"}`)
  157. upstreamSSE := "data: {\"type\":\"response.failed\",\"response\":{\"id\":\"r\",\"error\":" +
  158. "{\"type\":\"upstream_error\",\"code\":\"image_generation_unavailable\"," +
  159. "\"message\":\"image generation tool is not available for this account\"}}}\n\n"
  160. req := httptest.NewRequest(http.MethodPost, "/v1/images/generations", bytes.NewReader(body))
  161. req.Header.Set("Content-Type", "application/json")
  162. rec := httptest.NewRecorder()
  163. c, _ := gin.CreateTestContext(rec)
  164. c.Request = req
  165. svc := &OpenAIGatewayService{
  166. accountRepo: repo,
  167. httpUpstream: &httpUpstreamRecorder{
  168. resp: &http.Response{
  169. StatusCode: http.StatusOK,
  170. Header: http.Header{"Content-Type": []string{"text/event-stream"}},
  171. Body: io.NopCloser(strings.NewReader(upstreamSSE)),
  172. },
  173. },
  174. }
  175. parsed, err := svc.ParseOpenAIImagesRequest(c, body)
  176. require.NoError(t, err)
  177. account := &Account{
  178. ID: 206,
  179. Name: "openai-oauth",
  180. Platform: PlatformOpenAI,
  181. Type: AccountTypeOAuth,
  182. Credentials: map[string]any{
  183. "access_token": "token-123",
  184. },
  185. }
  186. before := time.Now()
  187. result, err := svc.ForwardImages(context.Background(), c, account, body, parsed, "")
  188. require.Nil(t, result)
  189. require.Error(t, err)
  190. require.Len(t, repo.modelRateLimitCalls, 1)
  191. call := repo.modelRateLimitCalls[0]
  192. require.Equal(t, account.ID, call.accountID)
  193. require.Equal(t, openAIImageGenerationRateLimitKey, call.scope)
  194. require.Equal(t, openAIImagesOAuthUnavailableReason, call.reason)
  195. require.WithinDuration(t, before.Add(openAIImagesOAuthUnavailableDefaultCooldown), call.resetAt, time.Second)
  196. }
  197. func TestOpenAIGatewayService_CoolOpenAIImagesOAuthToolUsesConfiguredCooldown(t *testing.T) {
  198. accountRepo := &modelNotFoundAccountRepoStub{}
  199. settingRepo := newMockSettingRepo()
  200. settingRepo.data[SettingKeyOpenAIImagesOAuthUnavailableCooldownSettings] = `{"cooldown_minutes":7}`
  201. svc := &OpenAIGatewayService{
  202. accountRepo: accountRepo,
  203. settingService: NewSettingService(settingRepo, &config.Config{}),
  204. }
  205. before := time.Now()
  206. svc.coolOpenAIImagesOAuthTool(context.Background(), &Account{ID: 206, Platform: PlatformOpenAI, Type: AccountTypeOAuth})
  207. require.Len(t, accountRepo.modelRateLimitCalls, 1)
  208. require.WithinDuration(t, before.Add(7*time.Minute), accountRepo.modelRateLimitCalls[0].resetAt, time.Second)
  209. }
  210. func TestOpenAIGatewayServiceForwardImages_CapabilityLossCoolsImageScope(t *testing.T) {
  211. gin.SetMode(gin.TestMode)
  212. repo := &modelNotFoundAccountRepoStub{}
  213. body := []byte(`{"model":"gpt-image-2","prompt":"draw a cat"}`)
  214. errorBody := `{"error":{"message":"Tool choice 'image_generation' not found in 'tools' parameter.","param":"tool_choice","type":"invalid_request_error"}}`
  215. req := httptest.NewRequest(http.MethodPost, "/v1/images/generations", bytes.NewReader(body))
  216. req.Header.Set("Content-Type", "application/json")
  217. rec := httptest.NewRecorder()
  218. c, _ := gin.CreateTestContext(rec)
  219. c.Request = req
  220. svc := &OpenAIGatewayService{
  221. rateLimitService: &RateLimitService{accountRepo: repo},
  222. httpUpstream: &httpUpstreamRecorder{
  223. resp: &http.Response{
  224. StatusCode: http.StatusBadRequest,
  225. Header: http.Header{"X-Request-Id": []string{"req_img_capability_lost"}},
  226. Body: io.NopCloser(strings.NewReader(errorBody)),
  227. },
  228. },
  229. }
  230. parsed, err := svc.ParseOpenAIImagesRequest(c, body)
  231. require.NoError(t, err)
  232. account := &Account{
  233. ID: 205,
  234. Name: "openai-oauth",
  235. Platform: PlatformOpenAI,
  236. Type: AccountTypeOAuth,
  237. Credentials: map[string]any{
  238. "access_token": "token-123",
  239. },
  240. }
  241. before := time.Now()
  242. result, err := svc.ForwardImages(context.Background(), c, account, body, parsed, "")
  243. require.Nil(t, result)
  244. require.Error(t, err)
  245. require.Len(t, repo.modelRateLimitCalls, 1)
  246. call := repo.modelRateLimitCalls[0]
  247. require.Equal(t, account.ID, call.accountID)
  248. require.Equal(t, openAIImageGenerationRateLimitKey, call.scope)
  249. require.Equal(t, openAIImageCapabilityLossReason, call.reason)
  250. require.WithinDuration(t, before.Add(openAIImageCapabilityLossCooldown), call.resetAt, time.Second)
  251. }
  252. func TestOpenAIGatewayServiceHandleUpstreamError_PassthroughCapabilityLossDoesNotCool(t *testing.T) {
  253. repo := &modelNotFoundAccountRepoStub{}
  254. svc := &OpenAIGatewayService{rateLimitService: &RateLimitService{accountRepo: repo}}
  255. account := &Account{ID: 206, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
  256. body := []byte(`{"error":{"message":"Tool choice 'image_generation' not found in 'tools' parameter.","param":"tool_choice","type":"invalid_request_error"}}`)
  257. disabled := svc.handleOpenAIAccountUpstreamError(context.Background(), account, http.StatusBadRequest, http.Header{}, body, "gpt-5.5")
  258. require.False(t, disabled)
  259. require.Empty(t, repo.modelRateLimitCalls)
  260. _, wholeAccountBlocked := svc.openaiAccountRuntimeBlockUntil.Load(account.ID)
  261. require.False(t, wholeAccountBlocked)
  262. }
  263. func TestRateLimitServiceHandleOpenAIImageCapabilityLoss_IgnoresGenericBadRequest(t *testing.T) {
  264. repo := &modelNotFoundAccountRepoStub{}
  265. svc := &RateLimitService{accountRepo: repo}
  266. account := &Account{ID: 207, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
  267. body := []byte(`{"error":{"message":"Invalid type for input[0].arguments"}}`)
  268. handled := svc.HandleOpenAIImageCapabilityLoss(context.Background(), account, http.StatusBadRequest, body)
  269. require.False(t, handled)
  270. require.Empty(t, repo.modelRateLimitCalls)
  271. }
  272. func TestRateLimitServiceHandleOpenAIImageCapabilityLoss_RespectsPlatformAndErrorCodePolicy(t *testing.T) {
  273. body := []byte(`{"error":{"message":"Tool choice 'image_generation' not found in 'tools' parameter.","param":"tool_choice","type":"invalid_request_error"}}`)
  274. t.Run("non_openai_platform", func(t *testing.T) {
  275. repo := &modelNotFoundAccountRepoStub{}
  276. svc := &RateLimitService{accountRepo: repo}
  277. account := &Account{ID: 208, Platform: PlatformAnthropic, Type: AccountTypeOAuth}
  278. handled := svc.HandleOpenAIImageCapabilityLoss(context.Background(), account, http.StatusBadRequest, body)
  279. require.False(t, handled)
  280. require.Empty(t, repo.modelRateLimitCalls)
  281. })
  282. t.Run("custom_error_code_policy_excludes_400", func(t *testing.T) {
  283. repo := &modelNotFoundAccountRepoStub{}
  284. svc := &RateLimitService{accountRepo: repo}
  285. account := &Account{
  286. ID: 209,
  287. Platform: PlatformOpenAI,
  288. Type: AccountTypeAPIKey,
  289. Credentials: map[string]any{
  290. "custom_error_codes_enabled": true,
  291. "custom_error_codes": []any{float64(http.StatusTooManyRequests)},
  292. },
  293. }
  294. require.False(t, account.ShouldHandleErrorCode(http.StatusBadRequest))
  295. handled := svc.HandleOpenAIImageCapabilityLoss(context.Background(), account, http.StatusBadRequest, body)
  296. require.False(t, handled)
  297. require.Empty(t, repo.modelRateLimitCalls)
  298. })
  299. }
  300. func TestIsOpenAIImageCapabilityLossError(t *testing.T) {
  301. capabilityLossBody := []byte(`{"error":{"message":"Tool choice 'image_generation' not found in 'tools' parameter.","param":"tool_choice","type":"invalid_request_error"}}`)
  302. genericBadRequestBody := []byte(`{"error":{"message":"Invalid type for input[0].arguments"}}`)
  303. require.True(t, isOpenAIImageCapabilityLossError(http.StatusBadRequest, capabilityLossBody))
  304. require.False(t, isOpenAIImageCapabilityLossError(http.StatusBadRequest, genericBadRequestBody))
  305. require.False(t, isOpenAIImageCapabilityLossError(http.StatusTooManyRequests, capabilityLossBody))
  306. }