fix(rate-limits): treat empty policies as unlimited

This commit is contained in:
2026-05-24 22:28:25 +08:00
parent 6d99e26e2a
commit 71950d2b4f
9 changed files with 168 additions and 45 deletions
+37
View File
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/easyai/easyai-ai-gateway/apps/api/internal/clients"
"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
)
func TestTokenUsageAmountsUsesActualUsageForTPM(t *testing.T) {
@@ -27,3 +28,39 @@ func TestTokenUsageAmountsFallsBackToInputOutputTotal(t *testing.T) {
t.Fatalf("expected total token fallback 8, got %v", got["tpm_total"])
}
}
func TestEffectiveRateLimitPolicyTreatsEmptyRuntimePolicyAsUnlimited(t *testing.T) {
policy := effectiveRateLimitPolicy(store.RuntimeModelCandidate{
PlatformRateLimitPolicy: map[string]any{"rules": []any{
map[string]any{"metric": "rpm", "limit": 500},
}},
RuntimePolicySetID: "runtime-policy-1",
RuntimeRateLimitPolicy: map[string]any{"rules": []any{}},
})
if hasRules(policy) {
t.Fatalf("expected empty runtime policy to clear inherited limits, got %+v", policy)
}
}
func TestReservationsFromPolicySkipsNonPositiveLimits(t *testing.T) {
reservations := reservationsFromPolicy(
"platform_model",
"model-1",
"Model 1",
nil,
map[string]any{"rules": []any{
map[string]any{"metric": "rpm", "limit": -1},
map[string]any{"metric": "tpm_total", "limit": 0},
map[string]any{"metric": "concurrent", "limit": 2},
}},
map[string]any{"prompt": "hello"},
)
if len(reservations) != 1 {
t.Fatalf("expected only the positive concurrent rule to reserve, got %+v", reservations)
}
if reservations[0].Metric != "concurrent" || reservations[0].Limit != 2 {
t.Fatalf("expected concurrent reservation with limit 2, got %+v", reservations[0])
}
}