53 lines
1.8 KiB
Go
53 lines
1.8 KiB
Go
package openai
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/sdk/api/handlers"
|
|
sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
|
|
)
|
|
|
|
func TestForwardResponsesStreamSeparatesDataOnlySSEChunks(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
base := handlers.NewBaseAPIHandlers(&sdkconfig.SDKConfig{}, nil)
|
|
h := NewOpenAIResponsesAPIHandler(base)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(recorder)
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", nil)
|
|
|
|
flusher, ok := c.Writer.(http.Flusher)
|
|
if !ok {
|
|
t.Fatalf("expected gin writer to implement http.Flusher")
|
|
}
|
|
|
|
data := make(chan []byte, 2)
|
|
errs := make(chan *interfaces.ErrorMessage)
|
|
data <- []byte("data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"arguments\":\"{}\"}}")
|
|
data <- []byte("data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp-1\",\"output\":[]}}")
|
|
close(data)
|
|
close(errs)
|
|
|
|
h.forwardResponsesStream(c, flusher, func(error) {}, data, errs)
|
|
body := recorder.Body.String()
|
|
parts := strings.Split(strings.TrimSpace(body), "\n\n")
|
|
if len(parts) != 2 {
|
|
t.Fatalf("expected 2 SSE events, got %d. Body: %q", len(parts), body)
|
|
}
|
|
|
|
expectedPart1 := "data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"arguments\":\"{}\"}}"
|
|
if parts[0] != expectedPart1 {
|
|
t.Errorf("unexpected first event.\nGot: %q\nWant: %q", parts[0], expectedPart1)
|
|
}
|
|
|
|
expectedPart2 := "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp-1\",\"output\":[]}}"
|
|
if parts[1] != expectedPart2 {
|
|
t.Errorf("unexpected second event.\nGot: %q\nWant: %q", parts[1], expectedPart2)
|
|
}
|
|
}
|