grok_team_rate_limit_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //go:build unit
  2. package service
  3. import (
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestGrokTeamModelRateLimit_MarksAndFiltersSiblings(t *testing.T) {
  9. // Isolate from other tests by using unique team ids.
  10. team := "team-test-" + time.Now().Format("150405.000")
  11. a1 := &Account{
  12. ID: 101, Platform: PlatformGrok, Type: AccountTypeOAuth,
  13. Credentials: map[string]any{"team_id": team},
  14. }
  15. a2 := &Account{
  16. ID: 102, Platform: PlatformGrok, Type: AccountTypeOAuth,
  17. Credentials: map[string]any{"team_id": team},
  18. }
  19. other := &Account{
  20. ID: 103, Platform: PlatformGrok, Type: AccountTypeOAuth,
  21. Credentials: map[string]any{"team_id": team + "-other"},
  22. }
  23. noTeam := &Account{
  24. ID: 104, Platform: PlatformGrok, Type: AccountTypeOAuth,
  25. Credentials: map[string]any{},
  26. }
  27. now := time.Now()
  28. markGrokTeamModelRateLimit(a1, "grok-4.5", now.Add(5*time.Minute))
  29. require.True(t, isGrokTeamModelRateLimited(a1, "grok-4.5", now))
  30. require.True(t, isGrokTeamModelRateLimited(a2, "grok-4.5", now), "sibling with same team must cool")
  31. require.False(t, isGrokTeamModelRateLimited(a2, "grok-4.3", now), "other model stays pickable")
  32. require.False(t, isGrokTeamModelRateLimited(other, "grok-4.5", now))
  33. require.False(t, isGrokTeamModelRateLimited(noTeam, "grok-4.5", now))
  34. filtered := filterGrokTeamModelRateLimitedAccounts([]Account{*a1, *a2, *other, *noTeam}, "grok-4.5", now)
  35. require.Len(t, filtered, 2)
  36. ids := []int64{filtered[0].ID, filtered[1].ID}
  37. require.Contains(t, ids, int64(103))
  38. require.Contains(t, ids, int64(104))
  39. }
  40. func TestGrokTeamModelRateLimit_Expires(t *testing.T) {
  41. team := "team-expire-" + time.Now().Format("150405.000")
  42. a := &Account{
  43. ID: 201, Platform: PlatformGrok, Type: AccountTypeOAuth,
  44. Credentials: map[string]any{"team_id": team},
  45. }
  46. past := time.Now().Add(-time.Minute)
  47. markGrokTeamModelRateLimit(a, "grok-4.5", past)
  48. // mark clamps expired until into default TTL from "now" — use direct store inject via past+recheck
  49. // After mark with past, resolveGrokTeamRateLimitUntil path isn't used; mark uses now+default when until not after now.
  50. require.True(t, isGrokTeamModelRateLimited(a, "grok-4.5", time.Now()))
  51. }
  52. func TestGrokTeamModelRateLimitFilterUsesMappedUpstreamModel(t *testing.T) {
  53. now := time.Now()
  54. account := &Account{
  55. ID: 301,
  56. Platform: PlatformGrok,
  57. Type: AccountTypeOAuth,
  58. Credentials: map[string]any{
  59. "team_id": "team-mapped-301",
  60. "model_mapping": map[string]any{"gpt-*": "grok-4.5"},
  61. },
  62. }
  63. markGrokTeamModelRateLimit(account, "grok-4.5", now.Add(time.Hour))
  64. require.Empty(t, filterGrokTeamModelRateLimitedAccounts([]Account{*account}, "gpt-5", now))
  65. }