| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545 |
- package service
- import (
- "context"
- "testing"
- "time"
- "github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
- "github.com/stretchr/testify/require"
- )
- func TestIsModelRateLimited(t *testing.T) {
- now := time.Now()
- future := now.Add(10 * time.Minute).Format(time.RFC3339)
- past := now.Add(-10 * time.Minute).Format(time.RFC3339)
- tests := []struct {
- name string
- account *Account
- requestedModel string
- expected bool
- }{
- {
- name: "official model ID hit - claude-sonnet-4-5",
- account: &Account{
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- expected: true,
- },
- {
- name: "official model ID hit via mapping - request claude-3-5-sonnet, mapped to claude-sonnet-4-5",
- account: &Account{
- Credentials: map[string]any{
- "model_mapping": map[string]any{
- "claude-3-5-sonnet": "claude-sonnet-4-5",
- },
- },
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "claude-3-5-sonnet",
- expected: true,
- },
- {
- name: "no rate limit - expired",
- account: &Account{
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5": map[string]any{
- "rate_limit_reset_at": past,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- expected: false,
- },
- {
- name: "no rate limit - no matching key",
- account: &Account{
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "gemini-3-flash": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- expected: false,
- },
- {
- name: "no rate limit - unsupported model",
- account: &Account{},
- requestedModel: "gpt-4",
- expected: false,
- },
- {
- name: "no rate limit - empty model",
- account: &Account{},
- requestedModel: "",
- expected: false,
- },
- {
- name: "gemini model hit",
- account: &Account{
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "gemini-3-pro-high": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "gemini-3-pro-high",
- expected: true,
- },
- {
- name: "antigravity platform - gemini-3-pro-preview mapped to gemini-3-pro-high",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "gemini-3-pro-high": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "gemini-3-pro-preview",
- expected: true,
- },
- {
- name: "antigravity platform - gemini family rate limit blocks mapped preview",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- antigravityGeminiModelRateLimitKey: map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "gemini-3-pro-preview",
- expected: true,
- },
- {
- name: "antigravity platform - gemini family rate limit does not block claude",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- antigravityGeminiModelRateLimitKey: map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- expected: false,
- },
- {
- name: "non-antigravity platform - gemini-3-pro-preview NOT mapped",
- account: &Account{
- Platform: PlatformGemini,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "gemini-3-pro-high": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "gemini-3-pro-preview",
- expected: false, // gemini 平台不走 antigravity 映射
- },
- {
- name: "antigravity platform - claude-opus-4-5-thinking mapped to opus-4-6-thinking",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-opus-4-6-thinking": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "claude-opus-4-5-thinking",
- expected: true,
- },
- {
- name: "no scope fallback - claude_sonnet should not match",
- account: &Account{
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude_sonnet": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "claude-3-5-sonnet-20241022",
- expected: false,
- },
- {
- name: "openai image generation family key blocks image model",
- account: &Account{
- Platform: PlatformOpenAI,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- openAIImageGenerationRateLimitKey: map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "gpt-image-2",
- expected: true,
- },
- {
- name: "openai image generation family key does not block text model",
- account: &Account{
- Platform: PlatformOpenAI,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- openAIImageGenerationRateLimitKey: map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- },
- requestedModel: "gpt-5.4",
- expected: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := tt.account.isModelRateLimitedWithContext(context.Background(), tt.requestedModel)
- if result != tt.expected {
- t.Errorf("isModelRateLimited(%q) = %v, want %v", tt.requestedModel, result, tt.expected)
- }
- })
- }
- }
- func TestIsModelRateLimited_OpenAIImageGenerationIntentBlocksTextModelImageTool(t *testing.T) {
- future := time.Now().Add(10 * time.Minute).Format(time.RFC3339)
- account := &Account{
- Platform: PlatformOpenAI,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- openAIImageGenerationRateLimitKey: map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- }
- require.False(t, account.isModelRateLimitedWithContext(context.Background(), "gpt-5.4"))
- require.True(t, account.isModelRateLimitedWithContext(WithOpenAIImageGenerationIntent(context.Background()), "gpt-5.4"))
- }
- func TestIsModelRateLimited_Antigravity_ThinkingAffectsModelKey(t *testing.T) {
- now := time.Now()
- future := now.Add(10 * time.Minute).Format(time.RFC3339)
- account := &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5-thinking": map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- }
- ctx := context.WithValue(context.Background(), ctxkey.ThinkingEnabled, true)
- if !account.isModelRateLimitedWithContext(ctx, "claude-sonnet-4-5") {
- t.Errorf("expected model to be rate limited")
- }
- }
- func TestGetModelRateLimitRemainingTime(t *testing.T) {
- now := time.Now()
- future10m := now.Add(10 * time.Minute).Format(time.RFC3339)
- future5m := now.Add(5 * time.Minute).Format(time.RFC3339)
- past := now.Add(-10 * time.Minute).Format(time.RFC3339)
- tests := []struct {
- name string
- account *Account
- requestedModel string
- minExpected time.Duration
- maxExpected time.Duration
- }{
- {
- name: "nil account",
- account: nil,
- requestedModel: "claude-sonnet-4-5",
- minExpected: 0,
- maxExpected: 0,
- },
- {
- name: "model rate limited - direct hit",
- account: &Account{
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5": map[string]any{
- "rate_limit_reset_at": future10m,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- minExpected: 9 * time.Minute,
- maxExpected: 11 * time.Minute,
- },
- {
- name: "model rate limited - via mapping",
- account: &Account{
- Credentials: map[string]any{
- "model_mapping": map[string]any{
- "claude-3-5-sonnet": "claude-sonnet-4-5",
- },
- },
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5": map[string]any{
- "rate_limit_reset_at": future5m,
- },
- },
- },
- },
- requestedModel: "claude-3-5-sonnet",
- minExpected: 4 * time.Minute,
- maxExpected: 6 * time.Minute,
- },
- {
- name: "expired rate limit",
- account: &Account{
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5": map[string]any{
- "rate_limit_reset_at": past,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- minExpected: 0,
- maxExpected: 0,
- },
- {
- name: "no rate limit data",
- account: &Account{},
- requestedModel: "claude-sonnet-4-5",
- minExpected: 0,
- maxExpected: 0,
- },
- {
- name: "no scope fallback",
- account: &Account{
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude_sonnet": map[string]any{
- "rate_limit_reset_at": future5m,
- },
- },
- },
- },
- requestedModel: "claude-3-5-sonnet-20241022",
- minExpected: 0,
- maxExpected: 0,
- },
- {
- name: "antigravity platform - claude-opus-4-5-thinking mapped to opus-4-6-thinking",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-opus-4-6-thinking": map[string]any{
- "rate_limit_reset_at": future5m,
- },
- },
- },
- },
- requestedModel: "claude-opus-4-5-thinking",
- minExpected: 4 * time.Minute,
- maxExpected: 6 * time.Minute,
- },
- {
- name: "antigravity platform - gemini family rate limit remaining",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- antigravityGeminiModelRateLimitKey: map[string]any{
- "rate_limit_reset_at": future10m,
- },
- },
- },
- },
- requestedModel: "gemini-3-pro-preview",
- minExpected: 9 * time.Minute,
- maxExpected: 11 * time.Minute,
- },
- {
- name: "antigravity platform - gemini family remaining ignored for claude",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- antigravityGeminiModelRateLimitKey: map[string]any{
- "rate_limit_reset_at": future10m,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- minExpected: 0,
- maxExpected: 0,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := tt.account.GetModelRateLimitRemainingTimeWithContext(context.Background(), tt.requestedModel)
- if result < tt.minExpected || result > tt.maxExpected {
- t.Errorf("GetModelRateLimitRemainingTime() = %v, want between %v and %v", result, tt.minExpected, tt.maxExpected)
- }
- })
- }
- }
- func TestGetRateLimitRemainingTime(t *testing.T) {
- now := time.Now()
- future15m := now.Add(15 * time.Minute).Format(time.RFC3339)
- future5m := now.Add(5 * time.Minute).Format(time.RFC3339)
- tests := []struct {
- name string
- account *Account
- requestedModel string
- minExpected time.Duration
- maxExpected time.Duration
- }{
- {
- name: "nil account",
- account: nil,
- requestedModel: "claude-sonnet-4-5",
- minExpected: 0,
- maxExpected: 0,
- },
- {
- name: "model rate limited - 15 minutes",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5": map[string]any{
- "rate_limit_reset_at": future15m,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- minExpected: 14 * time.Minute,
- maxExpected: 16 * time.Minute,
- },
- {
- name: "only model rate limited",
- account: &Account{
- Platform: PlatformAntigravity,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- "claude-sonnet-4-5": map[string]any{
- "rate_limit_reset_at": future5m,
- },
- },
- },
- },
- requestedModel: "claude-sonnet-4-5",
- minExpected: 4 * time.Minute,
- maxExpected: 6 * time.Minute,
- },
- {
- name: "neither rate limited",
- account: &Account{
- Platform: PlatformAntigravity,
- },
- requestedModel: "claude-sonnet-4-5",
- minExpected: 0,
- maxExpected: 0,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := tt.account.GetRateLimitRemainingTimeWithContext(context.Background(), tt.requestedModel)
- if result < tt.minExpected || result > tt.maxExpected {
- t.Errorf("GetRateLimitRemainingTime() = %v, want between %v and %v", result, tt.minExpected, tt.maxExpected)
- }
- })
- }
- }
- func TestIsModelRateLimited_AnthropicFableFamilyKey(t *testing.T) {
- now := time.Now()
- future := now.Add(48 * time.Hour).Format(time.RFC3339)
- account := &Account{
- Platform: PlatformAnthropic,
- Extra: map[string]any{
- modelRateLimitsKey: map[string]any{
- anthropicFableRateLimitKey: map[string]any{
- "rate_limit_reset_at": future,
- },
- },
- },
- }
- tests := []struct {
- requestedModel string
- expected bool
- }{
- {"claude-fable-5", true},
- {"claude-fable-5[1m]", true}, // 家族 key 覆盖变体
- {"Claude-Fable-5-20260601", true}, // 大小写不敏感
- {"claude-sonnet-4-6", false}, // 其他模型不受影响
- {"claude-opus-4-8", false},
- }
- for _, tc := range tests {
- t.Run(tc.requestedModel, func(t *testing.T) {
- got := account.isModelRateLimitedWithContext(context.Background(), tc.requestedModel)
- require.Equal(t, tc.expected, got)
- remaining := account.GetModelRateLimitRemainingTimeWithContext(context.Background(), tc.requestedModel)
- require.Equal(t, tc.expected, remaining > 0)
- })
- }
- }
- func TestIsAnthropicFableModel(t *testing.T) {
- require.True(t, isAnthropicFableModel("claude-fable-5"))
- require.True(t, isAnthropicFableModel("claude-fable-5[1m]"))
- require.True(t, isAnthropicFableModel("Claude-Fable-5"))
- require.False(t, isAnthropicFableModel("claude-sonnet-4-6"))
- require.False(t, isAnthropicFableModel(""))
- }
|