openai_profit_control_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "math"
  6. "testing"
  7. "time"
  8. "github.com/Wei-Shaw/sub2api/internal/config"
  9. "github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
  10. "github.com/Wei-Shaw/sub2api/internal/pkg/timezone"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func profitControlTestGroup(id int64, margin, buffer float64) *Group {
  14. return &Group{
  15. ID: id,
  16. Platform: PlatformOpenAI,
  17. Status: StatusActive,
  18. Hydrated: true,
  19. RateMultiplier: 1.0,
  20. SubscriptionType: SubscriptionTypeStandard,
  21. ProfitControlEnabled: true,
  22. ProfitMinMargin: margin,
  23. ProfitSafetyBuffer: buffer,
  24. }
  25. }
  26. func profitControlTestCtx(group *Group) context.Context {
  27. return context.WithValue(context.Background(), ctxkey.Group, group)
  28. }
  29. func profitControlTestAccountWithRate(account *Account, rate float64) *Account {
  30. account.RateMultiplier = &rate
  31. return account
  32. }
  33. func TestResolveOpenAIProfitControlGate(t *testing.T) {
  34. svc := &OpenAIGatewayService{}
  35. groupID := int64(7)
  36. t.Run("nil group id yields no gate", func(t *testing.T) {
  37. require.Nil(t, svc.resolveOpenAIProfitControlGate(context.Background(), nil))
  38. })
  39. t.Run("no ctx group and no snapshot yields no gate", func(t *testing.T) {
  40. require.Nil(t, svc.resolveOpenAIProfitControlGate(context.Background(), &groupID))
  41. })
  42. t.Run("disabled group yields no gate", func(t *testing.T) {
  43. group := profitControlTestGroup(groupID, 0.3, 0)
  44. group.ProfitControlEnabled = false
  45. require.Nil(t, svc.resolveOpenAIProfitControlGate(profitControlTestCtx(group), &groupID))
  46. })
  47. t.Run("non openai or grok platform yields no gate even if enabled", func(t *testing.T) {
  48. group := profitControlTestGroup(groupID, 0.3, 0)
  49. group.Platform = PlatformAnthropic
  50. require.Nil(t, svc.resolveOpenAIProfitControlGate(profitControlTestCtx(group), &groupID))
  51. })
  52. t.Run("grok group routed through openai handler installs gate", func(t *testing.T) {
  53. group := profitControlTestGroup(groupID, 0.3, 0.05)
  54. group.Platform = PlatformGrok
  55. group.RateMultiplier = 0.5
  56. gate := svc.resolveOpenAIProfitControlGate(profitControlTestCtx(group), &groupID)
  57. require.NotNil(t, gate)
  58. require.Equal(t, PlatformGrok, gate.platform)
  59. require.InDelta(t, 0.5*(1-0.35), gate.threshold, 1e-12)
  60. })
  61. t.Run("ctx group id mismatch without snapshot yields no gate", func(t *testing.T) {
  62. group := profitControlTestGroup(groupID+1, 0.3, 0)
  63. require.Nil(t, svc.resolveOpenAIProfitControlGate(profitControlTestCtx(group), &groupID))
  64. })
  65. t.Run("threshold composes margin and buffer from downstream rate", func(t *testing.T) {
  66. group := profitControlTestGroup(groupID, 0.3, 0.05)
  67. group.RateMultiplier = 2.0
  68. gate := svc.resolveOpenAIProfitControlGate(profitControlTestCtx(group), &groupID)
  69. require.NotNil(t, gate)
  70. require.InDelta(t, 2.0*(1-0.35), gate.threshold, 1e-12)
  71. require.Equal(t, PlatformOpenAI, gate.platform)
  72. require.False(t, gate.pricingAt.IsZero())
  73. require.Equal(t, groupID, gate.groupID)
  74. })
  75. t.Run("threshold applies peak factor exactly like billing", func(t *testing.T) {
  76. group := profitControlTestGroup(groupID, 0.5, 0)
  77. group.SubscriptionType = SubscriptionTypeSubscription
  78. group.PeakRateEnabled = true
  79. group.PeakStart = "00:00"
  80. group.PeakEnd = "23:59"
  81. group.PeakRateMultiplier = 3.0
  82. gate := svc.resolveOpenAIProfitControlGate(profitControlTestCtx(group), &groupID)
  83. require.NotNil(t, gate)
  84. expected := group.RateMultiplier * group.PeakMultiplierAt(timezone.Now()) * 0.5
  85. require.InDelta(t, expected, gate.threshold, 1e-9)
  86. require.Equal(t, PlatformOpenAI, gate.platform)
  87. })
  88. }
  89. func TestOpenAIProfitControlVetoReason(t *testing.T) {
  90. now := time.Now()
  91. gateCtx := func(threshold float64) context.Context {
  92. return context.WithValue(context.Background(), openAIProfitControlGateCtxKey{}, &openAIProfitControlGate{
  93. threshold: threshold,
  94. pricingAt: now,
  95. })
  96. }
  97. t.Run("no gate admits everything", func(t *testing.T) {
  98. vetoed, reason := openAIProfitControlVetoReason(context.Background(), upstreamCostTestOAuthAccount(1))
  99. require.False(t, vetoed)
  100. require.Empty(t, reason)
  101. })
  102. t.Run("fresh rate below threshold admits", func(t *testing.T) {
  103. account := profitControlTestAccountWithRate(upstreamCostTestAccount(1, UpstreamBillingProbeStatusOK, 99, now.Add(-time.Minute), 30*time.Minute), 0.5)
  104. vetoed, _ := openAIProfitControlVetoReason(gateCtx(0.7), account)
  105. require.False(t, vetoed)
  106. })
  107. t.Run("rate exactly at threshold admits via epsilon", func(t *testing.T) {
  108. account := profitControlTestAccountWithRate(upstreamCostTestAccount(1, UpstreamBillingProbeStatusOK, 99, now.Add(-time.Minute), 30*time.Minute), 0.7)
  109. vetoed, _ := openAIProfitControlVetoReason(gateCtx(0.7), account)
  110. require.False(t, vetoed)
  111. })
  112. t.Run("rate within float noise above threshold admits", func(t *testing.T) {
  113. account := profitControlTestAccountWithRate(upstreamCostTestAccount(1, UpstreamBillingProbeStatusOK, 99, now.Add(-time.Minute), 30*time.Minute), 0.7+1e-12)
  114. vetoed, _ := openAIProfitControlVetoReason(gateCtx(0.7), account)
  115. require.False(t, vetoed)
  116. })
  117. t.Run("rate above threshold is vetoed", func(t *testing.T) {
  118. account := profitControlTestAccountWithRate(upstreamCostTestAccount(1, UpstreamBillingProbeStatusOK, 0.1, now.Add(-time.Minute), 30*time.Minute), 0.8)
  119. vetoed, reason := openAIProfitControlVetoReason(gateCtx(0.7), account)
  120. require.True(t, vetoed)
  121. require.Equal(t, openAIProfitFilterReasonThreshold, reason)
  122. })
  123. t.Run("zero threshold only admits free upstream", func(t *testing.T) {
  124. free := profitControlTestAccountWithRate(upstreamCostTestAccount(1, UpstreamBillingProbeStatusOK, 99, now.Add(-time.Minute), 30*time.Minute), 0)
  125. vetoed, _ := openAIProfitControlVetoReason(gateCtx(0), free)
  126. require.False(t, vetoed)
  127. paid := profitControlTestAccountWithRate(upstreamCostTestAccount(2, UpstreamBillingProbeStatusOK, 0, now.Add(-time.Minute), 30*time.Minute), 0.01)
  128. vetoed, reason := openAIProfitControlVetoReason(gateCtx(0), paid)
  129. require.True(t, vetoed)
  130. require.Equal(t, openAIProfitFilterReasonThreshold, reason)
  131. })
  132. t.Run("missing account rate is invalid", func(t *testing.T) {
  133. vetoed, reason := openAIProfitControlVetoReason(gateCtx(0.7), upstreamCostTestOAuthAccount(1))
  134. require.True(t, vetoed)
  135. require.Equal(t, openAIProfitFilterReasonInvalidAccountRate, reason)
  136. })
  137. t.Run("oauth account with manual rate is priceable", func(t *testing.T) {
  138. account := profitControlTestAccountWithRate(upstreamCostTestOAuthAccount(1), 0.2)
  139. vetoed, _ := openAIProfitControlVetoReason(gateCtx(0.7), account)
  140. require.False(t, vetoed)
  141. })
  142. t.Run("stale probe does not affect manual account rate", func(t *testing.T) {
  143. account := profitControlTestAccountWithRate(upstreamCostTestAccount(1, UpstreamBillingProbeStatusOK, 99, now.Add(-3*time.Hour), 30*time.Minute), 0.1)
  144. vetoed, _ := openAIProfitControlVetoReason(gateCtx(0.7), account)
  145. require.False(t, vetoed)
  146. })
  147. t.Run("negative and non-finite rates are invalid", func(t *testing.T) {
  148. for _, rate := range []float64{-1, math.NaN(), math.Inf(1)} {
  149. account := profitControlTestAccountWithRate(upstreamCostTestOAuthAccount(1), rate)
  150. vetoed, reason := openAIProfitControlVetoReason(gateCtx(0.7), account)
  151. require.True(t, vetoed)
  152. require.Equal(t, openAIProfitFilterReasonInvalidAccountRate, reason)
  153. }
  154. })
  155. }
  156. func TestProfitControlSchedulerFiltersCandidates(t *testing.T) {
  157. resetOpenAIAdvancedSchedulerSettingCacheForTest()
  158. defer resetOpenAIAdvancedSchedulerSettingCacheForTest()
  159. now := time.Now()
  160. cheap := upstreamCostTestAccount(1, UpstreamBillingProbeStatusOK, 0.3, now.Add(-time.Minute), 30*time.Minute)
  161. expensive := upstreamCostTestAccount(2, UpstreamBillingProbeStatusOK, 0.8, now.Add(-time.Minute), 30*time.Minute)
  162. oauth := upstreamCostTestOAuthAccount(3)
  163. profitControlTestAccountWithRate(cheap, 0.3)
  164. profitControlTestAccountWithRate(expensive, 0.8)
  165. for _, account := range []*Account{cheap, expensive, oauth} {
  166. account.Status = StatusActive
  167. account.Schedulable = true
  168. account.Concurrency = 5
  169. }
  170. cache := &upstreamCostTrackingConcurrencyCache{loadMap: map[int64]*AccountLoadInfo{
  171. cheap.ID: {AccountID: cheap.ID},
  172. expensive.ID: {AccountID: expensive.ID},
  173. oauth.ID: {AccountID: oauth.ID},
  174. }}
  175. cfg := &config.Config{}
  176. svc := &OpenAIGatewayService{
  177. accountRepo: schedulerTestOpenAIAccountRepo{accounts: []Account{*cheap, *expensive, *oauth}},
  178. cfg: cfg,
  179. rateLimitService: newOpenAIAdvancedSchedulerRateLimitService("true"),
  180. concurrencyService: NewConcurrencyService(cache),
  181. }
  182. groupID := int64(7)
  183. t.Run("unprofitable and invalid-rate accounts never win", func(t *testing.T) {
  184. // margin 0.5 → 阈值 0.5:expensive(0.8) 超阈值、oauth 倍率缺失,仅 cheap 可选。
  185. ctx := profitControlTestCtx(profitControlTestGroup(groupID, 0.5, 0))
  186. for i := 0; i < 5; i++ {
  187. selection, _, err := svc.SelectAccountWithScheduler(ctx, &groupID, "", "", "gpt-test", nil, OpenAIUpstreamTransportAny, false)
  188. require.NoError(t, err)
  189. require.NotNil(t, selection)
  190. require.Equal(t, cheap.ID, selection.Account.ID)
  191. if selection.ReleaseFunc != nil {
  192. selection.ReleaseFunc()
  193. }
  194. }
  195. })
  196. t.Run("all excluded surfaces standard no-available error with profit reasons", func(t *testing.T) {
  197. // margin+buffer 0.8 → 阈值 0.2:cheap/expensive 超阈值,oauth 倍率非法。
  198. ctx := profitControlTestCtx(profitControlTestGroup(groupID, 0.7, 0.1))
  199. selection, _, err := svc.SelectAccountWithScheduler(ctx, &groupID, "", "", "gpt-test", nil, OpenAIUpstreamTransportAny, false)
  200. require.Nil(t, selection)
  201. require.Error(t, err)
  202. require.True(t, errors.Is(err, ErrNoAvailableAccounts))
  203. require.Contains(t, err.Error(), openAIProfitFilterReasonThreshold+"=2")
  204. require.Contains(t, err.Error(), openAIProfitFilterReasonInvalidAccountRate+"=1")
  205. })
  206. t.Run("manually rated oauth account is admitted", func(t *testing.T) {
  207. // 阈值 0.2 排除两个 API Key;OAuth 手工倍率 0.1 可参与调度。
  208. profitControlTestAccountWithRate(oauth, 0.1)
  209. svc.accountRepo = schedulerTestOpenAIAccountRepo{accounts: []Account{*cheap, *expensive, *oauth}}
  210. ctx := profitControlTestCtx(profitControlTestGroup(groupID, 0.7, 0.1))
  211. selection, _, err := svc.SelectAccountWithScheduler(ctx, &groupID, "", "", "gpt-test", nil, OpenAIUpstreamTransportAny, false)
  212. require.NoError(t, err)
  213. require.NotNil(t, selection)
  214. require.Equal(t, oauth.ID, selection.Account.ID)
  215. if selection.ReleaseFunc != nil {
  216. selection.ReleaseFunc()
  217. }
  218. })
  219. t.Run("gate disabled keeps official behavior", func(t *testing.T) {
  220. group := profitControlTestGroup(groupID, 0.7, 0.1)
  221. group.ProfitControlEnabled = false
  222. selection, _, err := svc.SelectAccountWithScheduler(profitControlTestCtx(group), &groupID, "", "", "gpt-test", nil, OpenAIUpstreamTransportAny, false)
  223. require.NoError(t, err)
  224. require.NotNil(t, selection)
  225. if selection.ReleaseFunc != nil {
  226. selection.ReleaseFunc()
  227. }
  228. })
  229. }
  230. func TestValidateProfitControlConfig(t *testing.T) {
  231. require.NoError(t, ValidateProfitControlConfig(PlatformAnthropic, false, 0, 0))
  232. for _, platform := range []string{PlatformOpenAI, PlatformAnthropic, PlatformGemini, PlatformGrok, PlatformAntigravity} {
  233. require.NoError(t, ValidateProfitControlConfig(platform, true, 0.3, 0.05))
  234. require.NoError(t, ValidateProfitControlConfig(platform, true, 0, 0))
  235. }
  236. require.Error(t, ValidateProfitControlConfig(PlatformComposite, true, 0.3, 0))
  237. require.Error(t, ValidateProfitControlConfig(PlatformOpenAI, true, -0.1, 0))
  238. require.Error(t, ValidateProfitControlConfig(PlatformOpenAI, true, 1.0, 0))
  239. require.Error(t, ValidateProfitControlConfig(PlatformOpenAI, true, 0, 1.0))
  240. require.Error(t, ValidateProfitControlConfig(PlatformOpenAI, true, 0.6, 0.4))
  241. }
  242. func TestNormalizeProfitControlConfig(t *testing.T) {
  243. t.Run("unsupported platform resets everything", func(t *testing.T) {
  244. enabled, margin, buffer := NormalizeProfitControlConfig(PlatformComposite, true, 0.3, 0.1)
  245. require.False(t, enabled)
  246. require.Zero(t, margin)
  247. require.Zero(t, buffer)
  248. })
  249. t.Run("all five platforms retain configuration", func(t *testing.T) {
  250. for _, platform := range []string{PlatformOpenAI, PlatformAnthropic, PlatformGemini, PlatformGrok, PlatformAntigravity} {
  251. enabled, margin, buffer := NormalizeProfitControlConfig(platform, true, 0.3, 0.1)
  252. require.True(t, enabled)
  253. require.InDelta(t, 0.3, margin, 1e-12)
  254. require.InDelta(t, 0.1, buffer, 1e-12)
  255. }
  256. })
  257. t.Run("openai disabled keeps legal values and cleans dirty ones", func(t *testing.T) {
  258. enabled, margin, buffer := NormalizeProfitControlConfig(PlatformOpenAI, false, 0.3, 0.05)
  259. require.False(t, enabled)
  260. require.InDelta(t, 0.3, margin, 1e-12)
  261. require.InDelta(t, 0.05, buffer, 1e-12)
  262. _, margin, buffer = NormalizeProfitControlConfig(PlatformOpenAI, false, -1, 1.5)
  263. require.Zero(t, margin)
  264. require.Zero(t, buffer)
  265. })
  266. t.Run("openai enabled passes through for validation", func(t *testing.T) {
  267. enabled, margin, buffer := NormalizeProfitControlConfig(PlatformOpenAI, true, 0.3, 0.05)
  268. require.True(t, enabled)
  269. require.InDelta(t, 0.3, margin, 1e-12)
  270. require.InDelta(t, 0.05, buffer, 1e-12)
  271. })
  272. }