Files
traefik-llmgateway/passthrough_estimate_log_test.go
T
lukaszraczyloandGitHub f1b198bce4 fix(security): audit run-4 fixes and run-3 leftovers (#26)
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
2026-11-29 10:50:36 +00:00

44 lines
1.4 KiB
Go

package traefikllmgateway
import (
"io"
"net/http"
"strings"
"testing"
)
func estimateLogOf(t *testing.T, usageChunk string) string {
t.Helper()
gw := newResidualGateway(t, providerTypeOpenAI, "gpt-5", func(w http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(io.Discard, r.Body)
w.Header().Set("Content-Type", "text/event-stream")
for i := 0; i < 5; i++ {
_, _ = io.WriteString(w, sseTestChunk)
}
if usageChunk != "" {
_, _ = io.WriteString(w, usageChunk)
}
})
return captureStderr(t, func() {
residualPost(gw, "/up/v1/native-endpoint", `{"model":"gpt-5","stream":true,"messages":[]}`)
})
}
func TestPassthroughSSE_EstimateLogNamesOnlyEstimatedParts(t *testing.T) {
t.Run("completion only", func(t *testing.T) {
out := estimateLogOf(t, "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":42,\"completion_tokens\":0}}\n\n")
if !strings.Contains(out, "logged as estimated (completion tokens estimated from the stream event count)") {
t.Errorf("log = %q, want a completion-only estimate", out)
}
if strings.Contains(out, "prompt tokens estimated") {
t.Errorf("log claims the prompt was estimated: %q", out)
}
})
t.Run("prompt and completion", func(t *testing.T) {
out := estimateLogOf(t, "")
if !strings.Contains(out, "prompt tokens estimated from the request size") || !strings.Contains(out, "completion tokens estimated from the stream event count") {
t.Errorf("log = %q, want both parts named", out)
}
})
}