12345678910111213141516171819202122232425262728293031 | package llm
import (
"bytes"
"io"
"net/http"
"testing"
"time"
)
// Whatever a server streams, reading it does not panic, emits no more
// than it was sent, and gives at most the calls it allows.
func FuzzStream(f *testing.F) {
f.Add([]byte("data: " + delta(`{"content":"hi"}`) + "\n\ndata: [DONE]\n\n"))
f.Add([]byte("data: " + delta(`{"tool_calls":[{"index":128,"id":"x","function":{"name":"Read","arguments":"{}"}}]}`) + "\ndata: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n"))
f.Add([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"<think>a</think>b\"}}]}\ndata: {\"error\":{\"message\":\"boom\"}}\n"))
f.Add([]byte(": keep-alive\n\ndata: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":-1}]}}]}\ndata:[DONE]"))
f.Fuzz(func(t *testing.T, body []byte) {
c := New("http://unused", "", "m")
c.MaxTokens = 1000
resp := &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(body))}
emitted := 0
r, _, err := c.readStream(resp, time.Now(), func(text string, _ bool) { emitted += len(text) })
if err == nil && len(r.Calls) > 129 {
t.Fatalf("%d calls", len(r.Calls))
}
if emitted > len(body) {
t.Fatalf("emitted %d bytes from a %d-byte stream", emitted, len(body))
}
})
}
|