Files
magpie/internal/provider/stream_required_test.go
T
yetone 1f99f6f0b5 provider, gateway: a backend that only streams is tested and served (Cyrus_4433 on X: claude-relay-service "400 · Stream must be set to true")
A CRS Responses endpoint, like Codex's own backend, turns away a request that
doesn't ask for stream:true. The editor's Test models and protocol detection
send one small body, so every model of such a relay was red; an agent that sent
stream:false got the same 400 through the gateway.

Probe: probeAt asks once more with stream:true when a 400/422 says the stream
must be set (provider.StreamRequired, WithStream). Chosen over streaming every
probe so a vendor that takes one body keeps being asked for one; every probe
(model test, per-model check, detect, key check) goes through probeAt.
Gateway: passthrough marks the model stream-only on that 400 for a request
asking one body and returns done=false; attempt then translates on the same
API (always streamed upstream, whole reply to the client), and asks so at once
from then on, as a refused custom tool is.

Run (mote): new tests built from the reporter's literal error, red on the old
code (TestProbeStreamsWhereTheBackendOnlyStreams, TestDetectFindsAStreamOnlyBackend,
TestWholeReplyFromAStreamOnlyBackend), green on the new; go vet darwin, windows,
linux nogui; go test ./internal/provider ./internal/gateway ok. Each fix broken
by hand (probe retry stashed, gateway hook stashed): red. Not tried: a real CRS
relay; the rest of ./... (started after the tag).
2026-11-11 10:37:13 +08:00

119 lines
3.7 KiB
Go

package provider
import (
"context"
"io"
"net/http"
"net/http/httptest"
"path/filepath"
"sync"
"testing"
"github.com/tidwall/gjson"
)
// crsStreamOnly is a claude-relay-service Responses endpoint as
// Cyrus_4433 on X met it: "2 个模型中有 2 个没有响应;gpt-6-sol: 400 ·
// Stream must be set to true", for a request that didn't ask for a stream.
func crsStreamOnly(t *testing.T, plain bool) (*httptest.Server, func() []string) {
t.Helper()
var mu sync.Mutex
var got []string
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
mu.Lock()
got = append(got, string(b))
mu.Unlock()
if !gjson.GetBytes(b, "stream").Bool() {
rw.WriteHeader(http.StatusBadRequest)
if plain {
_, _ = io.WriteString(rw, "Stream must be set to true")
} else {
_, _ = io.WriteString(rw, `{"error":{"message":"Stream must be set to true","type":"invalid_request_error"}}`)
}
return
}
rw.Header().Set("Content-Type", "text/event-stream")
_, _ = io.WriteString(rw, "event: response.created\ndata: {\"type\":\"response.created\",\"response\":{\"id\":\"r\"}}\n\n"+
"event: response.output_text.delta\ndata: {\"type\":\"response.output_text.delta\",\"delta\":\"hi\"}\n\n")
}))
t.Cleanup(srv.Close)
return srv, func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), got...) }
}
func sandboxHome(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
}
// A model's test on a backend that only streams is asked once more
// streamed, and is green: the editor's Test models, one model at a time.
func TestProbeStreamsWhereTheBackendOnlyStreams(t *testing.T) {
for _, plain := range []bool{false, true} {
sandboxHome(t)
srv, asked := crsStreamOnly(t, plain)
p := Provider{ID: "relay", Name: "My Relay", Responses: srv.URL + "/v1", Key: "k", Models: []string{"gpt-6-sol", "gpt-6.1-sol"}}
rs := p.TestModels(context.Background(), []string{"gpt-6-sol", "gpt-6.1-sol"})
if len(rs) == 0 {
t.Fatalf("plain=%v: no results", plain)
}
for _, r := range rs {
if !r.OK {
t.Errorf("plain=%v: %+v", plain, r)
}
}
on, off := 0, 0
for _, b := range asked() {
if gjson.Get(b, "stream").Bool() {
on++
} else {
off++
}
}
if on != 2 || off != 2 {
t.Errorf("plain=%v: %d streamed, %d not: %v", plain, on, off, asked())
}
}
}
// Protocol detection probes the same way, so a stream-only Responses
// endpoint is found.
func TestDetectFindsAStreamOnlyBackend(t *testing.T) {
sandboxHome(t)
srv, _ := crsStreamOnly(t, false)
p := Provider{ID: "relay", Name: "My Relay", Responses: srv.URL + "/v1", Key: "k"}
ds, err := p.Detect(context.Background(), srv.URL+"/v1", "gpt-6-sol")
if err != nil {
t.Fatal(err)
}
ok := false
for _, d := range ds {
if d.Protocol == Responses && d.OK {
ok = true
}
}
if !ok {
t.Errorf("%+v", ds)
}
}
func TestStreamRequired(t *testing.T) {
for msg, want := range map[string]bool{
"Stream must be set to true": true,
`{"error":{"message":"stream must be true"}}`: true,
"400 · Stream must be set to true": true,
"This endpoint only supports streaming": true,
"streaming is required": true,
"max_tokens must be at most 8": false,
"stream_options is only allowed when stream is true.": false,
"model not found": false,
} {
if got := StreamRequired([]byte(msg)); got != want {
t.Errorf("%q: %v", msg, got)
}
}
}