mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
package traefikllmgateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestOpenAIForwardStream_MalformedChoicesKeepUsage(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
body string
|
|
}{
|
|
{"tool_calls object", `data: {"choices":[{"delta":{"tool_calls":{"function":{"arguments":"x"}}}}],"usage":{"prompt_tokens":7,"completion_tokens":9}}` + "\n\n"},
|
|
{"choices object", `data: {"choices":{"delta":{}},"usage":{"prompt_tokens":7,"completion_tokens":9}}` + "\n\n"},
|
|
{"choice not object", `data: {"choices":["x"],"usage":{"prompt_tokens":7,"completion_tokens":9}}` + "\n\n"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
rec := httptest.NewRecorder()
|
|
u, err := (&openaiAdapter{}).forwardStream(rec, strings.NewReader(tt.body+"data: [DONE]\n\n"), false)
|
|
if err != nil {
|
|
t.Fatalf("forwardStream: %v", err)
|
|
}
|
|
if u.prompt != 7 || u.completion != 9 {
|
|
t.Fatalf("usage = %d/%d, want 7/9", u.prompt, u.completion)
|
|
}
|
|
if !strings.Contains(rec.Body.String(), `"choices"`) {
|
|
t.Errorf("chunk with choices was suppressed: %q", rec.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|