gcli2api / internal /pool /model_rate_test.go
a3216's picture
feat: 同步上游 320f5d4;扩展模型(多免费渠道/自定义映射/访问密钥/模型白名单)
e48a2d8
Raw History Blame Contribute Delete
2.01 kB
package pool
import (
"path/filepath"
"testing"
"time"
"github.com/misakano7545/workbuddy2api-panel/internal/auth"
)
func TestModelRateCooldownsIndependentAndPersisted(t *testing.T) {
fp := filepath.Join(t.TempDir(), "state.json")
p := New(fp)
p.Add(&auth.Auth{UID: "u1"})
p.SetSoftRateMax(2 * time.Minute)
p.CooldownSoftForModel("u1", time.Minute, time.Time{}, "kimi", "429 model rate limit")
first, _ := p.Status("u1")
p.CooldownSoftForModel("u1", time.Hour, time.Time{}, "kimi", "429 model rate limit")
again, _ := p.Status("u1")
if !first.RateLimitedModels[0].Until.Equal(again.RateLimitedModels[0].Until) {
t.Fatal("repeat failure extended active cooldown")
}
p.CooldownSoftForModel("u1", time.Hour, time.Time{}, "deepseek", "429 model rate limit")
p.NoteSuccess("u1")
p.Flush()
q := New(fp)
q.Add(&auth.Auth{UID: "u1"})
s, _ := q.Status("u1")
if s.Cooling || len(s.RateLimitedModels) != 2 {
t.Fatalf("reload status=%+v", s)
}
for _, m := range []string{"kimi", "deepseek"} {
if q.PickExcludingForModel(nil, m) != nil || q.PickByUIDForModel("u1", m) != nil {
t.Fatalf("%s cooldown bypassed", m)
}
}
if q.PickExcludingForModel(nil, "glm") == nil {
t.Fatal("unrelated model blocked")
}
q.CooldownUntilTomorrow4AM("u1", "余额不足")
if q.PickExcludingForModel(nil, "glm") != nil {
t.Fatal("account credit exhaustion must still block every model")
}
}
func TestFallbackCannotBypassModelRateCooldown(t *testing.T) {
p := New("")
p.Add(&auth.Auth{UID: "u1"})
p.CooldownSoftForModel("u1", time.Minute, time.Time{}, "kimi", "429 model rate limit")
// 账号另有降权时会进入全冷却兜底,仍必须遵守模型冷却。
p.mu.Lock()
p.byUID["u1"].degradeUntil = time.Now().Add(10 * time.Minute)
p.mu.Unlock()
if p.PickExcludingForModel(nil, "kimi") != nil {
t.Fatal("fallback bypassed the active model cooldown")
}
if p.PickExcludingForModel(nil, "deepseek") == nil {
t.Fatal("fallback should remain available for an unrelated model")
}
}