|
| 1 | +//go:build unit |
| 2 | + |
| 3 | +package service |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +type updateAccountOveragesRepoStub struct { |
| 14 | + mockAccountRepoForGemini |
| 15 | + account *Account |
| 16 | + updateCalls int |
| 17 | +} |
| 18 | + |
| 19 | +func (r *updateAccountOveragesRepoStub) GetByID(ctx context.Context, id int64) (*Account, error) { |
| 20 | + return r.account, nil |
| 21 | +} |
| 22 | + |
| 23 | +func (r *updateAccountOveragesRepoStub) Update(ctx context.Context, account *Account) error { |
| 24 | + r.updateCalls++ |
| 25 | + r.account = account |
| 26 | + return nil |
| 27 | +} |
| 28 | + |
| 29 | +func TestUpdateAccount_DisableOveragesClearsAICreditsKey(t *testing.T) { |
| 30 | + accountID := int64(101) |
| 31 | + repo := &updateAccountOveragesRepoStub{ |
| 32 | + account: &Account{ |
| 33 | + ID: accountID, |
| 34 | + Platform: PlatformAntigravity, |
| 35 | + Type: AccountTypeOAuth, |
| 36 | + Status: StatusActive, |
| 37 | + Extra: map[string]any{ |
| 38 | + "allow_overages": true, |
| 39 | + "mixed_scheduling": true, |
| 40 | + modelRateLimitsKey: map[string]any{ |
| 41 | + "claude-sonnet-4-5": map[string]any{ |
| 42 | + "rate_limited_at": "2026-03-15T00:00:00Z", |
| 43 | + "rate_limit_reset_at": "2099-03-15T00:00:00Z", |
| 44 | + }, |
| 45 | + creditsExhaustedKey: map[string]any{ |
| 46 | + "rate_limited_at": "2026-03-15T00:00:00Z", |
| 47 | + "rate_limit_reset_at": time.Now().Add(5 * time.Hour).UTC().Format(time.RFC3339), |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + svc := &adminServiceImpl{accountRepo: repo} |
| 55 | + updated, err := svc.UpdateAccount(context.Background(), accountID, &UpdateAccountInput{ |
| 56 | + Extra: map[string]any{ |
| 57 | + "mixed_scheduling": true, |
| 58 | + modelRateLimitsKey: map[string]any{ |
| 59 | + "claude-sonnet-4-5": map[string]any{ |
| 60 | + "rate_limited_at": "2026-03-15T00:00:00Z", |
| 61 | + "rate_limit_reset_at": "2099-03-15T00:00:00Z", |
| 62 | + }, |
| 63 | + creditsExhaustedKey: map[string]any{ |
| 64 | + "rate_limited_at": "2026-03-15T00:00:00Z", |
| 65 | + "rate_limit_reset_at": time.Now().Add(5 * time.Hour).UTC().Format(time.RFC3339), |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }) |
| 70 | + |
| 71 | + require.NoError(t, err) |
| 72 | + require.NotNil(t, updated) |
| 73 | + require.Equal(t, 1, repo.updateCalls) |
| 74 | + require.False(t, updated.IsOveragesEnabled()) |
| 75 | + |
| 76 | + // 关闭 overages 后,AICredits key 应被清除 |
| 77 | + rawLimits, ok := repo.account.Extra[modelRateLimitsKey].(map[string]any) |
| 78 | + if ok { |
| 79 | + _, exists := rawLimits[creditsExhaustedKey] |
| 80 | + require.False(t, exists, "关闭 overages 时应清除 AICredits 限流 key") |
| 81 | + } |
| 82 | + // 普通模型限流应保留 |
| 83 | + require.True(t, ok) |
| 84 | + _, exists := rawLimits["claude-sonnet-4-5"] |
| 85 | + require.True(t, exists, "普通模型限流应保留") |
| 86 | +} |
| 87 | + |
| 88 | +func TestUpdateAccount_EnableOveragesClearsModelRateLimitsBeforePersist(t *testing.T) { |
| 89 | + accountID := int64(102) |
| 90 | + repo := &updateAccountOveragesRepoStub{ |
| 91 | + account: &Account{ |
| 92 | + ID: accountID, |
| 93 | + Platform: PlatformAntigravity, |
| 94 | + Type: AccountTypeOAuth, |
| 95 | + Status: StatusActive, |
| 96 | + Extra: map[string]any{ |
| 97 | + "mixed_scheduling": true, |
| 98 | + modelRateLimitsKey: map[string]any{ |
| 99 | + "claude-sonnet-4-5": map[string]any{ |
| 100 | + "rate_limited_at": "2026-03-15T00:00:00Z", |
| 101 | + "rate_limit_reset_at": "2099-03-15T00:00:00Z", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + svc := &adminServiceImpl{accountRepo: repo} |
| 109 | + updated, err := svc.UpdateAccount(context.Background(), accountID, &UpdateAccountInput{ |
| 110 | + Extra: map[string]any{ |
| 111 | + "mixed_scheduling": true, |
| 112 | + "allow_overages": true, |
| 113 | + }, |
| 114 | + }) |
| 115 | + |
| 116 | + require.NoError(t, err) |
| 117 | + require.NotNil(t, updated) |
| 118 | + require.Equal(t, 1, repo.updateCalls) |
| 119 | + require.True(t, updated.IsOveragesEnabled()) |
| 120 | + |
| 121 | + _, exists := repo.account.Extra[modelRateLimitsKey] |
| 122 | + require.False(t, exists, "开启 overages 时应在持久化前清掉旧模型限流") |
| 123 | +} |
0 commit comments