Files
traefik-llmgateway/smart_router_size_test.go
T
lukaszraczyloandGitHub 4c61d19a72 feat(router): smart routers and per-member rate limits (#9)
Add smart routers: virtual models that score each request and route it to admin-defined model tiers. A retryable failure moves the request to a higher tier. The classifier v2 extracts prose, finds the original ask in long tool loops, adds an uncertainty margin and reads tool-output failures. Sessions, outcome signals, bounded calibration, adaptive selection within a tier, gated saturation downgrade, a first-byte timeout and an opt-in timeout fallback are included. Every new feature is off by default.

Add per-member group limits, a tokensPerMinute window that counts uncached input and output, rate-limit headers in three dialects, soft pacing before the hard limit, and a limitExemptKinds list so embedding and reranking traffic never counts toward or hits a limit.

Fix yaegi named-result state that made smartContentSize grow between calls.
2026-11-25 00:40:30 +00:00

111 lines
3.7 KiB
Go

package traefikllmgateway
import (
"encoding/json"
"strings"
"testing"
)
func TestSmartPromptSize_CountsToolPayloads(t *testing.T) {
t.Parallel()
blob := strings.Repeat("x", 4000)
tests := []struct {
req map[string]any
name string
endpoint string
minChars int
maxChars int
}{
{
name: "openai tool_calls string arguments",
req: chatMsgs(map[string]any{"role": "assistant", "content": "", "tool_calls": []any{map[string]any{"id": "c1", "function": map[string]any{"name": "f", "arguments": blob}}}}),
endpoint: cacheEndpointChat,
minChars: 4000, maxChars: 4100,
},
{
name: "openai tool_calls object arguments",
req: chatMsgs(map[string]any{"role": "assistant", "tool_calls": []any{map[string]any{"function": map[string]any{"arguments": map[string]any{"q": blob}}}}}),
endpoint: cacheEndpointChat,
minChars: 4000, maxChars: 4100,
},
{
name: "anthropic tool_use input",
req: chatMsgs(msg("assistant", []any{map[string]any{"type": "tool_use", "id": "t1", "name": "f", "input": map[string]any{"code": blob}}})),
endpoint: cacheEndpointMessages,
minChars: 4000, maxChars: 4100,
},
{
name: "tools schema on chat",
req: map[string]any{"messages": []any{}, "tools": []any{map[string]any{"type": "function", "function": map[string]any{"name": "f", "description": blob, "parameters": map[string]any{"type": "object", "properties": map[string]any{"a": map[string]any{"type": "string"}}}}}}},
endpoint: cacheEndpointChat,
minChars: 4000, maxChars: 4300,
},
{
name: "tools schema on messages",
req: map[string]any{"messages": []any{}, "tools": []any{map[string]any{"name": "f", "description": blob, "input_schema": map[string]any{"type": "object"}}}},
endpoint: cacheEndpointMessages,
minChars: 4000, maxChars: 4200,
},
{
name: "no tool payloads add nothing",
req: chatMsgs(msg("user", "hello")),
endpoint: cacheEndpointChat,
minChars: 5, maxChars: 5,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
chars, _ := smartPromptSize(tc.req, tc.endpoint)
if chars < tc.minChars || chars > tc.maxChars {
t.Errorf("chars = %d, want in [%d, %d]", chars, tc.minChars, tc.maxChars)
}
})
}
}
func TestSmartPromptSize_ToolSizeTracksSerializedLength(t *testing.T) {
t.Parallel()
input := map[string]any{"path": "/etc/hosts", "opts": map[string]any{"deep": []any{"a", "bb", true, nil, 12.5}}}
raw, err := json.Marshal(input)
if err != nil {
t.Fatal(err)
}
got := smartValueSize(input)
if got < len(raw)*7/10 || got > len(raw)*13/10 {
t.Errorf("smartValueSize = %d, serialized length = %d: want within 30%%", got, len(raw))
}
}
func TestSmartValueSize_BoundedWork(t *testing.T) {
t.Parallel()
deep := any("leaf")
for i := 0; i < 1000; i++ {
deep = []any{deep}
}
if got := smartValueSize(deep); got <= 0 || got > 2*smartSizeMaxDepth+16 {
t.Errorf("deep nesting size = %d, want a small bounded value", got)
}
wide := make([]any, 100000)
for i := range wide {
wide[i] = "abcd"
}
if got := smartValueSize(wide); got > (smartSizeMaxNodes+1)*8 {
t.Errorf("wide array size = %d, want capped by the node budget", got)
}
}
func TestSmartPromptBytes_IncludesToolsSchema(t *testing.T) {
t.Parallel()
base := chatMsgs(msg("user", "hello"))
withTools := map[string]any{
"messages": base["messages"],
"tools": []any{map[string]any{"type": "function", "function": map[string]any{"name": "f", "description": strings.Repeat("d", 8000)}}},
}
baseBytes, _ := smartPromptBytes(base, cacheEndpointChat)
toolBytes, _ := smartPromptBytes(withTools, cacheEndpointChat)
if diff := toolBytes - baseBytes; diff < 8000 {
t.Errorf("tools schema added %d bytes, want >= 8000", diff)
}
}