Files
traefik-llmgateway/stream_hangup_shapes_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

117 lines
4.4 KiB
Go

package traefikllmgateway
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"
)
const hangupText40 = "0123456789012345678901234567890123456789"
func deltaFrames(n int, delta string) []string {
frames := make([]string, 0, n)
for i := 0; i < n; i++ {
frames = append(frames, fmt.Sprintf("data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":%s}]}\n\n", delta))
}
return frames
}
func hangupTokout(t *testing.T, frames []string) int64 {
t.Helper()
srv := hangupServer(t, "text/event-stream", frames)
defer srv.Close()
gw := newStreamBillingGateway(t, providerTypeOpenAI, srv.URL, "gpt-test")
body := map[string]any{"model": "gpt-test", "stream": true, "messages": []any{map[string]any{"role": "user", "content": "hi"}}}
gw.ServeHTTP(newRecordingWriter(), newUnifiedRequest(t, http.MethodPost, "/v1/chat/completions", "sk-alice", body))
_, out := aliceTokens(gw)
return out
}
func TestOpenAIStreamHangup_FunctionCallArgumentsBillEstimatedCompletion(t *testing.T) {
got := hangupTokout(t, deltaFrames(12, `{"function_call":{"arguments":"`+hangupText40+`"}}`))
if got != 120 {
t.Fatalf("tokout = %d, want 120", got)
}
}
func TestOpenAIStreamHangup_AudioTranscriptBillsEstimatedCompletion(t *testing.T) {
got := hangupTokout(t, deltaFrames(12, `{"audio":{"transcript":"`+hangupText40+`"}}`))
if got != 120 {
t.Fatalf("tokout = %d, want 120", got)
}
}
func TestOpenAIStreamHangup_RefusalBillsEstimatedCompletion(t *testing.T) {
got := hangupTokout(t, deltaFrames(12, `{"refusal":"`+hangupText40+`"}`))
if got != 120 {
t.Fatalf("tokout = %d, want 120", got)
}
}
func TestOpenAIStreamHangup_ArrayContentBillsEstimatedCompletion(t *testing.T) {
got := hangupTokout(t, deltaFrames(12, `{"content":[{"type":"text","text":"`+hangupText40+`"}]}`))
if got != 120 {
t.Fatalf("tokout = %d, want 120", got)
}
}
func TestOpenAIStreamHangup_AudioDataOnlyBillsChunkFloor(t *testing.T) {
got := hangupTokout(t, deltaFrames(12, `{"audio":{"data":"`+strings.Repeat("QUJD", 100)+`"}}`))
if got != 12 {
t.Fatalf("tokout = %d, want 12", got)
}
}
func TestOpenAIStreamHangup_ContentAndToolArgumentFiguresUnchanged(t *testing.T) {
frames := deltaFrames(6, `{"content":"`+hangupText40+`"}`)
frames = append(frames, deltaFrames(6, `{"tool_calls":[{"index":0,"id":"call_abcdef","type":"function","function":{"name":"lookup_weather","arguments":"`+hangupText40+`"}}]}`)...)
if got := hangupTokout(t, frames); got != 120 {
t.Fatalf("tokout = %d, want 120", got)
}
}
func TestOpenAIStreamHangup_RoleOnlyChunkBillsNoCompletion(t *testing.T) {
if got := hangupTokout(t, deltaFrames(12, `{"role":"assistant"}`)); got != 0 {
t.Fatalf("tokout = %d, want 0", got)
}
}
func TestChunkOutputBytes_SkipsMetadataKeysAndAudioData(t *testing.T) {
cases := []struct {
name string
delta string
want int64
}{
{"content", `{"content":"abcd"}`, 4},
{"role skipped", `{"role":"assistant","content":"ab"}`, 2},
{"null", `{"content":null}`, 0},
{"function_call", `{"function_call":{"name":"f","arguments":"12345"}}`, 5},
{"tool call metadata skipped", `{"tool_calls":[{"id":"call_1","type":"function","function":{"name":"fn","arguments":"xyz"}}]}`, 3},
{"audio data skipped", `{"audio":{"id":"a1","data":"QUJDRA==","transcript":"hey"}}`, 3},
{"array content", `{"content":[{"type":"text","text":"abc"},{"type":"text","text":"de"}]}`, 5},
{"refusal", `{"refusal":"nope"}`, 4},
{"reasoning", `{"reasoning_content":"ab","reasoning":"cde"}`, 5},
{"reasoning mirrored in details", `{"reasoning":"abcd","reasoning_details":[{"type":"reasoning.text","text":"abcd"}]}`, 4},
{"reasoning_content mirrored in reasoning", `{"reasoning_content":"abcd","reasoning":"abcd"}`, 4},
{"details alone", `{"reasoning_details":[{"type":"reasoning.text","text":"abcd"}]}`, 4},
{"non-string leaves", `{"content":[1,true,null]}`, 0},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
choice := []byte(`{"delta":` + c.delta + `}`)
if got, _ := chunkOutputBytes([]json.RawMessage{choice}); got != c.want {
t.Fatalf("chunkOutputBytes = %d, want %d", got, c.want)
}
})
}
}
func TestChunkOutputBytes_DepthCap(t *testing.T) {
deep := strings.Repeat(`{"x":`, 20) + `"abcd"` + strings.Repeat(`}`, 20)
if got, _ := chunkOutputBytes([]json.RawMessage{[]byte(`{"delta":` + deep + `}`)}); got != 0 {
t.Fatalf("deep delta counted %d bytes, want 0 past the depth cap", got)
}
}