130 lines
4.4 KiB
Go
130 lines
4.4 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
func TestRemoteAPIProviderErrorDoesNotExposeImagePayload(t *testing.T) {
|
|
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
|
|
secret := "TOPSECRETIMAGEPAYLOAD"
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(`{"error":{"message":"data:image/png;base64,` + secret + `"}}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
model, err := NewRemoteAPIChat(&ChatConfig{
|
|
Source: types.ModelSourceRemote,
|
|
BaseURL: server.URL + "/v1",
|
|
ModelName: "vision-test",
|
|
APIKey: "test-key",
|
|
Provider: "openai",
|
|
SupportsVision: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = model.Chat(context.Background(), []Message{{
|
|
Role: "user", Content: "inspect", Images: []string{"data:image/png;base64," + secret},
|
|
}}, nil)
|
|
if err == nil {
|
|
t.Fatal("expected provider error")
|
|
}
|
|
if strings.Contains(err.Error(), secret) || strings.Contains(strings.ToLower(err.Error()), "data:image") {
|
|
t.Fatalf("provider error leaked image payload: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestProcessRawHTTPStreamPrematureEOFFailsClosed(t *testing.T) {
|
|
body := "data: {\"choices\":[{\"delta\":{\"content\":\"partial\"}}]}\n\n"
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
|
|
stream := make(chan types.StreamResponse)
|
|
model := &RemoteAPIChat{modelName: "cloud-vision"}
|
|
go model.processRawHTTPStream(context.Background(), resp, stream, nil)
|
|
|
|
var got []types.StreamResponse
|
|
for chunk := range stream {
|
|
got = append(got, chunk)
|
|
}
|
|
if len(got) == 0 || got[len(got)-1].ResponseType != types.ResponseTypeError {
|
|
t.Fatalf("stream = %#v; want terminal error on premature EOF", got)
|
|
}
|
|
if got[len(got)-1].Content != "chat stream ended before completion" {
|
|
t.Fatalf("terminal error = %q", got[len(got)-1].Content)
|
|
}
|
|
}
|
|
|
|
func TestProcessRawHTTPStreamFinishReasonThenEOFCompletes(t *testing.T) {
|
|
body := "data: {\"choices\":[{\"delta\":{\"content\":\"answer\"},\"finish_reason\":\"stop\"}]}\n\n"
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
|
|
stream := make(chan types.StreamResponse)
|
|
model := &RemoteAPIChat{modelName: "cloud-vision"}
|
|
go model.processRawHTTPStream(context.Background(), resp, stream, nil)
|
|
|
|
var got []types.StreamResponse
|
|
for chunk := range stream {
|
|
got = append(got, chunk)
|
|
}
|
|
if len(got) == 0 {
|
|
t.Fatal("expected stream chunks")
|
|
}
|
|
last := got[len(got)-1]
|
|
if last.ResponseType == types.ResponseTypeError || !last.Done || last.FinishReason != "stop" {
|
|
t.Fatalf("stream = %#v; want successful stop completion", got)
|
|
}
|
|
}
|
|
|
|
func TestProcessRawHTTPStreamFinishReasonThenMalformedPacketFailsWithoutSuccessTerminal(t *testing.T) {
|
|
body := "data: {\"choices\":[{\"delta\":{\"content\":\"partial\"}}]}\n\n" +
|
|
"data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n" +
|
|
"data: {not-json}\n\n"
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
|
|
stream := make(chan types.StreamResponse, 8)
|
|
model := &RemoteAPIChat{modelName: "stream-test"}
|
|
go model.processRawHTTPStream(context.Background(), resp, stream, nil)
|
|
|
|
successTerminals, errorTerminals := 0, 0
|
|
for item := range stream {
|
|
if item.Done && item.ResponseType == types.ResponseTypeAnswer {
|
|
successTerminals++
|
|
}
|
|
if item.Done && item.ResponseType == types.ResponseTypeError {
|
|
errorTerminals++
|
|
}
|
|
}
|
|
if successTerminals != 0 || errorTerminals != 1 {
|
|
t.Fatalf("terminal counts success=%d error=%d; want 0/1", successTerminals, errorTerminals)
|
|
}
|
|
}
|
|
|
|
func TestProcessRawHTTPStreamEmitsExactlyOneSuccessTerminal(t *testing.T) {
|
|
body := "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\n" +
|
|
"data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n" +
|
|
"data: [DONE]\n\n"
|
|
resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))}
|
|
stream := make(chan types.StreamResponse, 8)
|
|
model := &RemoteAPIChat{modelName: "stream-test"}
|
|
go model.processRawHTTPStream(context.Background(), resp, stream, nil)
|
|
|
|
successTerminals := 0
|
|
for item := range stream {
|
|
if item.Done && item.ResponseType == types.ResponseTypeAnswer {
|
|
successTerminals++
|
|
}
|
|
if item.ResponseType == types.ResponseTypeError {
|
|
t.Fatalf("unexpected stream error: %#v", item)
|
|
}
|
|
}
|
|
if successTerminals != 1 {
|
|
t.Fatalf("success terminals = %d, want 1", successTerminals)
|
|
}
|
|
}
|