64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package store
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestEffectiveModelRateLimitPolicyTreatsModelRulesAsAuthoritative(t *testing.T) {
|
|
policy := effectiveModelRateLimitPolicy(
|
|
map[string]any{"rules": []any{
|
|
map[string]any{"metric": "rpm", "limit": 500},
|
|
map[string]any{"metric": "tpm_total", "limit": 100000},
|
|
}},
|
|
map[string]any{"rules": []any{
|
|
map[string]any{"metric": "rpm", "limit": 120},
|
|
map[string]any{"metric": "tpm_total", "limit": 240000},
|
|
map[string]any{"metric": "concurrent", "limit": 6},
|
|
}},
|
|
map[string]any{},
|
|
map[string]any{"rules": []any{
|
|
map[string]any{"metric": "rpm", "limit": 30},
|
|
map[string]any{"metric": "concurrent", "limit": 2},
|
|
}},
|
|
)
|
|
|
|
if got := rateLimitForMetric(policy, "rpm"); got != 30 {
|
|
t.Fatalf("expected model rpm limit to win, got %v", got)
|
|
}
|
|
if got := rateLimitForMetric(policy, "concurrent"); got != 2 {
|
|
t.Fatalf("expected model concurrent limit to win, got %v", got)
|
|
}
|
|
if got := rateLimitForMetric(policy, "tpm_total"); got != 0 {
|
|
t.Fatalf("expected missing model tpm limit to mean unlimited, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestPriorityDemotionRecordFromEventPayloadKeepsReason(t *testing.T) {
|
|
createdAt := time.Date(2026, 5, 12, 9, 30, 0, 0, time.UTC)
|
|
record := priorityDemotionRecordFromEventPayload("event-1", "task-1", "fallback message", map[string]any{
|
|
"platformId": "platform-1",
|
|
"platformModelId": "platform-model-1",
|
|
"reason": "priority_demote_policy",
|
|
"errorCode": "rate_limit",
|
|
"errorMessage": "upstream 429 rate limit",
|
|
"category": "rate_limit",
|
|
"statusCode": float64(429),
|
|
"policySource": "gateway_runner_policies.priority_demote_policy",
|
|
"policy": "priorityDemotePolicy",
|
|
"policyRule": "categories",
|
|
"matchedValue": "rate_limit",
|
|
"dynamicPriority": float64(1511),
|
|
}, createdAt)
|
|
|
|
if record.Reason != "priority_demote_policy" || record.ErrorMessage != "upstream 429 rate limit" {
|
|
t.Fatalf("expected demotion reason and error message to survive, got %+v", record)
|
|
}
|
|
if record.StatusCode != 429 || record.DynamicPriority != 1511 {
|
|
t.Fatalf("expected numeric demotion metadata, got %+v", record)
|
|
}
|
|
if !record.CreatedAt.Equal(createdAt) {
|
|
t.Fatalf("expected createdAt %s, got %s", createdAt, record.CreatedAt)
|
|
}
|
|
}
|