openai_sticky_compat_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/Wei-Shaw/sub2api/internal/config"
  6. "github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestGetStickySessionAccountID_FallbackToLegacyKey(t *testing.T) {
  10. beforeFallbackTotal, beforeFallbackHit, _ := openAIStickyCompatStats()
  11. cache := &stubGatewayCache{
  12. sessionBindings: map[string]int64{
  13. "openai:legacy-hash": 42,
  14. },
  15. }
  16. svc := &OpenAIGatewayService{
  17. cache: cache,
  18. cfg: &config.Config{
  19. Gateway: config.GatewayConfig{
  20. OpenAIWS: config.GatewayOpenAIWSConfig{
  21. SessionHashReadOldFallback: true,
  22. },
  23. },
  24. },
  25. }
  26. ctx := withOpenAILegacySessionHash(context.Background(), "legacy-hash")
  27. accountID, err := svc.getStickySessionAccountID(ctx, nil, "new-hash")
  28. require.NoError(t, err)
  29. require.Equal(t, int64(42), accountID)
  30. afterFallbackTotal, afterFallbackHit, _ := openAIStickyCompatStats()
  31. require.Equal(t, beforeFallbackTotal+1, afterFallbackTotal)
  32. require.Equal(t, beforeFallbackHit+1, afterFallbackHit)
  33. }
  34. func TestSetStickySessionAccountID_DualWriteOldEnabled(t *testing.T) {
  35. _, _, beforeDualWriteTotal := openAIStickyCompatStats()
  36. cache := &stubGatewayCache{sessionBindings: map[string]int64{}}
  37. svc := &OpenAIGatewayService{
  38. cache: cache,
  39. cfg: &config.Config{
  40. Gateway: config.GatewayConfig{
  41. OpenAIWS: config.GatewayOpenAIWSConfig{
  42. SessionHashDualWriteOld: true,
  43. },
  44. },
  45. },
  46. }
  47. ctx := withOpenAILegacySessionHash(context.Background(), "legacy-hash")
  48. err := svc.setStickySessionAccountID(ctx, nil, "new-hash", 9, openaiStickySessionTTL)
  49. require.NoError(t, err)
  50. require.Equal(t, int64(9), cache.sessionBindings["openai:new-hash"])
  51. require.Equal(t, int64(9), cache.sessionBindings["openai:legacy-hash"])
  52. _, _, afterDualWriteTotal := openAIStickyCompatStats()
  53. require.Equal(t, beforeDualWriteTotal+1, afterDualWriteTotal)
  54. }
  55. func TestSetStickySessionAccountID_DualWriteOldDisabled(t *testing.T) {
  56. cache := &stubGatewayCache{sessionBindings: map[string]int64{}}
  57. svc := &OpenAIGatewayService{
  58. cache: cache,
  59. cfg: &config.Config{
  60. Gateway: config.GatewayConfig{
  61. OpenAIWS: config.GatewayOpenAIWSConfig{
  62. SessionHashDualWriteOld: false,
  63. },
  64. },
  65. },
  66. }
  67. ctx := withOpenAILegacySessionHash(context.Background(), "legacy-hash")
  68. err := svc.setStickySessionAccountID(ctx, nil, "new-hash", 9, openaiStickySessionTTL)
  69. require.NoError(t, err)
  70. require.Equal(t, int64(9), cache.sessionBindings["openai:new-hash"])
  71. _, exists := cache.sessionBindings["openai:legacy-hash"]
  72. require.False(t, exists)
  73. }
  74. func TestSnapshotOpenAICompatibilityFallbackMetrics(t *testing.T) {
  75. before := SnapshotOpenAICompatibilityFallbackMetrics()
  76. ctx := context.WithValue(context.Background(), ctxkey.ThinkingEnabled, true)
  77. _, _ = ThinkingEnabledFromContext(ctx)
  78. after := SnapshotOpenAICompatibilityFallbackMetrics()
  79. require.GreaterOrEqual(t, after.MetadataLegacyFallbackTotal, before.MetadataLegacyFallbackTotal+1)
  80. require.GreaterOrEqual(t, after.MetadataLegacyFallbackThinkingEnabledTotal, before.MetadataLegacyFallbackThinkingEnabledTotal+1)
  81. }