Files
sub2api/backend/internal/service/grok_team_rate_limit_test.go
T
李建琦 6d655c9903
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
2026-08-21 18:30:13 +08:00

76 lines
2.6 KiB
Go

//go:build unit
package service
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestGrokTeamModelRateLimit_MarksAndFiltersSiblings(t *testing.T) {
// Isolate from other tests by using unique team ids.
team := "team-test-" + time.Now().Format("150405.000")
a1 := &Account{
ID: 101, Platform: PlatformGrok, Type: AccountTypeOAuth,
Credentials: map[string]any{"team_id": team},
}
a2 := &Account{
ID: 102, Platform: PlatformGrok, Type: AccountTypeOAuth,
Credentials: map[string]any{"team_id": team},
}
other := &Account{
ID: 103, Platform: PlatformGrok, Type: AccountTypeOAuth,
Credentials: map[string]any{"team_id": team + "-other"},
}
noTeam := &Account{
ID: 104, Platform: PlatformGrok, Type: AccountTypeOAuth,
Credentials: map[string]any{},
}
now := time.Now()
markGrokTeamModelRateLimit(a1, "grok-4.5", now.Add(5*time.Minute))
require.True(t, isGrokTeamModelRateLimited(a1, "grok-4.5", now))
require.True(t, isGrokTeamModelRateLimited(a2, "grok-4.5", now), "sibling with same team must cool")
require.False(t, isGrokTeamModelRateLimited(a2, "grok-4.3", now), "other model stays pickable")
require.False(t, isGrokTeamModelRateLimited(other, "grok-4.5", now))
require.False(t, isGrokTeamModelRateLimited(noTeam, "grok-4.5", now))
filtered := filterGrokTeamModelRateLimitedAccounts([]Account{*a1, *a2, *other, *noTeam}, "grok-4.5", now)
require.Len(t, filtered, 2)
ids := []int64{filtered[0].ID, filtered[1].ID}
require.Contains(t, ids, int64(103))
require.Contains(t, ids, int64(104))
}
func TestGrokTeamModelRateLimit_Expires(t *testing.T) {
team := "team-expire-" + time.Now().Format("150405.000")
a := &Account{
ID: 201, Platform: PlatformGrok, Type: AccountTypeOAuth,
Credentials: map[string]any{"team_id": team},
}
past := time.Now().Add(-time.Minute)
markGrokTeamModelRateLimit(a, "grok-4.5", past)
// mark clamps expired until into default TTL from "now" — use direct store inject via past+recheck
// After mark with past, resolveGrokTeamRateLimitUntil path isn't used; mark uses now+default when until not after now.
require.True(t, isGrokTeamModelRateLimited(a, "grok-4.5", time.Now()))
}
func TestGrokTeamModelRateLimitFilterUsesMappedUpstreamModel(t *testing.T) {
now := time.Now()
account := &Account{
ID: 301,
Platform: PlatformGrok,
Type: AccountTypeOAuth,
Credentials: map[string]any{
"team_id": "team-mapped-301",
"model_mapping": map[string]any{"gpt-*": "grok-4.5"},
},
}
markGrokTeamModelRateLimit(account, "grok-4.5", now.Add(time.Hour))
require.Empty(t, filterGrokTeamModelRateLimitedAccounts([]Account{*account}, "gpt-5", now))
}