openai_account_scheduler_ws_snapshot_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/Wei-Shaw/sub2api/internal/config"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestOpenAIGatewayService_SelectAccountWithScheduler_UsesWSPassthroughSnapshotFlags(t *testing.T) {
  10. ctx := context.Background()
  11. groupID := int64(10105)
  12. account := &Account{
  13. ID: 35001,
  14. Platform: PlatformOpenAI,
  15. Type: AccountTypeOAuth,
  16. Status: StatusActive,
  17. Schedulable: true,
  18. Concurrency: 10,
  19. GroupIDs: []int64{groupID},
  20. Extra: map[string]any{
  21. "openai_oauth_responses_websockets_v2_mode": OpenAIWSIngressModePassthrough,
  22. },
  23. }
  24. snapshotCache := &openAISnapshotCacheStub{
  25. snapshotAccounts: []*Account{account},
  26. accountsByID: map[int64]*Account{account.ID: account},
  27. }
  28. cfg := &config.Config{}
  29. cfg.Gateway.OpenAIWS.Enabled = true
  30. cfg.Gateway.OpenAIWS.OAuthEnabled = true
  31. cfg.Gateway.OpenAIWS.APIKeyEnabled = true
  32. cfg.Gateway.OpenAIWS.ResponsesWebsocketsV2 = true
  33. cfg.Gateway.OpenAIWS.ModeRouterV2Enabled = true
  34. cfg.Gateway.OpenAIWS.IngressModeDefault = OpenAIWSIngressModeCtxPool
  35. svc := &OpenAIGatewayService{
  36. accountRepo: schedulerTestOpenAIAccountRepo{accounts: []Account{*account}},
  37. cache: &schedulerTestGatewayCache{},
  38. cfg: cfg,
  39. rateLimitService: newOpenAIAdvancedSchedulerRateLimitService("true"),
  40. schedulerSnapshot: &SchedulerSnapshotService{cache: snapshotCache},
  41. concurrencyService: NewConcurrencyService(schedulerTestConcurrencyCache{}),
  42. }
  43. selection, decision, err := svc.SelectAccountWithScheduler(
  44. ctx,
  45. &groupID,
  46. "",
  47. "session_hash_ws_passthrough",
  48. "gpt-5.1",
  49. nil,
  50. OpenAIUpstreamTransportResponsesWebsocketV2,
  51. false,
  52. )
  53. require.NoError(t, err)
  54. require.NotNil(t, selection)
  55. require.NotNil(t, selection.Account)
  56. require.Equal(t, account.ID, selection.Account.ID)
  57. require.Equal(t, openAIAccountScheduleLayerLoadBalance, decision.Layer)
  58. }