Files
magpie/internal/gateway/chat_usage_only_test.go
T
yetone 28b8244d47 gateway: a Chat reply with only a usage frame is an empty reply, asked again (codealc on X)
Copilot's gemini-4-carbon now and then answers a Chat request 200 with
completion_tokens above 0, no content or tool delta and finish_reason null
(~855 bytes: the usage frame). thoughtOnly never saw it (no reasoning in
it), the Chat decoder gave no stop, and the Responses encoder closed it
with an empty response.completed, so Codex stopped without a word.

A Chat upstream's reply that said nothing and never gave a finish_reason now
takes the empty-reply path (#667): asked again in place, the error when it
stays empty. A reply with text and no finish_reason, and one ending with a
stop and no text, go as before.

Run: TestChatUsageOnlyReplyIsEmpty plus the gateway empty/length/thinking
tests via mote-run. Broken by hand: without the `voids && stop == ""`
condition the new test is red with the empty response.completed.
Fixture: role chunk, then a choices:[] chunk with usage, then [DONE].
Not tried: the real Copilot account (the empty reply is intermittent).
2026-11-12 14:17:12 +08:00

65 lines
3.0 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// Copilot's gemini-4-carbon now and then answers a Chat request 200 with
// only a usage frame (about 855 bytes down, completion_tokens above 0, no
// content or tool delta and finish_reason null), which went to Codex as an
// empty response.completed: the turn stopped without a word (codealc on X).
// thoughtOnly never saw it, there being no reasoning in it. Such a reply is
// an empty one: asked again in place, and the error when it comes empty
// every time. A reply that said something with no finish_reason, and one
// that ended with a stop and no text, go as they did.
func TestChatUsageOnlyReplyIsEmpty(t *testing.T) {
fresh(t)
pause := retryPause
retryPause = time.Millisecond
t.Cleanup(func() { retryPause = pause })
var calls atomic.Int32
var reply string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.ReadAll(r.Body)
calls.Add(1)
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, reply)
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "cp", Name: "Copilot", Key: "k", Models: []string{"gem"}, Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
role := `data: {"id":"c1","object":"chat.completion.chunk","model":"gem","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}`
usage := `data: {"id":"c1","object":"chat.completion.chunk","model":"gem","choices":[],"usage":{"prompt_tokens":900,"completion_tokens":24,"total_tokens":924}}`
ask := func() (int, string) {
calls.Store(0)
return post(t, "/v1/responses", `{"model":"cp/gem","stream":true,"input":"hi"}`)
}
reply = sse(role, usage, `data: [DONE]`)
code, body := ask()
if code != 200 || !strings.Contains(body, "an empty reply") || strings.Contains(body, "response.completed") || calls.Load() < 2 {
t.Errorf("a usage-only reply answered %d after %d calls, not the empty-reply error: %s", code, calls.Load(), body)
}
text := `data: {"id":"c1","object":"chat.completion.chunk","model":"gem","choices":[{"index":0,"delta":{"content":"hello"},"finish_reason":null}]}`
reply = sse(role, text, usage, `data: [DONE]`)
if code, body = ask(); code != 200 || !strings.Contains(body, "hello") || !strings.Contains(body, "response.completed") || calls.Load() != 1 {
t.Errorf("a reply with text and no finish_reason: %d after %d calls: %s", code, calls.Load(), body)
}
stopped := `data: {"id":"c1","object":"chat.completion.chunk","model":"gem","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":900,"completion_tokens":2}}`
reply = sse(role, stopped, `data: [DONE]`)
if code, body = ask(); code != 200 || !strings.Contains(body, "response.completed") || strings.Contains(body, "an empty reply") || calls.Load() != 1 {
t.Errorf("a reply that ended with a stop and no text: %d after %d calls: %s", code, calls.Load(), body)
}
}