scheduler_mode1_projection_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package repository
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/Wei-Shaw/sub2api/internal/service"
  6. "github.com/alicebob/miniredis/v2"
  7. "github.com/redis/go-redis/v9"
  8. "github.com/stretchr/testify/require"
  9. "testing"
  10. )
  11. func TestMode1SchedulerProjectionRetainsPolicyAndProxy(t *testing.T) {
  12. a := service.Account{ID: 1, Platform: service.PlatformOpenAI, Type: service.AccountTypeOAuth, Concurrency: 100, Extra: map[string]any{
  13. "codex_fingerprint_mode": "device", "codex_fingerprint_seed": "11111111-1111-4111-8111-111111111111", "enable_tls_fingerprint": true, "tls_fingerprint_builtin": "nodejs24", "proxy_mode": "random",
  14. "anti_degrade": map[string]any{"enabled": true, "mode": "mode1", "policy_version": 2, "max_concurrency": 4},
  15. }}
  16. projected := buildSchedulerMetadataAccount(a)
  17. for _, key := range []string{"anti_degrade", "proxy_mode", "enable_tls_fingerprint", "tls_fingerprint_builtin", "codex_fingerprint_seed"} {
  18. require.Equal(t, a.Extra[key], projected.Extra[key], key)
  19. }
  20. require.Equal(t, 100, projected.Mode1EffectiveConcurrency())
  21. }
  22. func TestAccountProtectionSchedulerRetainsIndependentIntegrityMode(t *testing.T) {
  23. for _, mode := range []string{"off", "observe", "enforce"} {
  24. a := service.Account{ID: 2, Platform: service.PlatformOpenAI, Type: service.AccountTypeOAuth, Extra: map[string]any{"anti_degradation": true, "request_integrity_mode": mode, "anti_degrade": map[string]any{"enabled": true, "mode": "legacy"}}}
  25. projected := buildSchedulerMetadataAccount(a)
  26. require.Equal(t, mode, projected.RequestIntegrityMode())
  27. }
  28. }
  29. func TestMode1ConcurrencySlotsRemainBoundedAndRelease(t *testing.T) {
  30. r := miniredis.RunT(t)
  31. client := redis.NewClient(&redis.Options{Addr: r.Addr()})
  32. t.Cleanup(func() { client.Close() })
  33. cache := NewConcurrencyCache(client, 15, 900)
  34. a := &service.Account{ID: 1, Concurrency: 4, Extra: map[string]any{"anti_degrade": map[string]any{"enabled": true, "mode": "mode1", "policy_version": 2, "max_concurrency": 99}}}
  35. ctx := context.Background()
  36. for i := 0; i < 4; i++ {
  37. ok, err := cache.AcquireAccountSlot(ctx, a.ID, a.Mode1EffectiveConcurrency(), fmt.Sprint(i))
  38. require.NoError(t, err)
  39. require.True(t, ok)
  40. }
  41. ok, err := cache.AcquireAccountSlot(ctx, a.ID, a.Mode1EffectiveConcurrency(), "excess")
  42. require.NoError(t, err)
  43. require.False(t, ok)
  44. require.NoError(t, cache.ReleaseAccountSlot(ctx, a.ID, "0"))
  45. ok, err = cache.AcquireAccountSlot(ctx, a.ID, a.Mode1EffectiveConcurrency(), "replacement")
  46. require.NoError(t, err)
  47. require.True(t, ok)
  48. }