Files
magpie/internal/gateway/chat_reasoning_relay_test.go
T
yetone 8b89b2e36c tests: two waits that went red at load 80–100 leave room: the reasoning relay is timed by the vendor's clock, otel waits a minute for its export
Both went red in full runs of go test ./... on a machine at load 80–100.
Neither is a fault in what they test.

- TestChatReasoningRelayedAsItComes: "first thought after 924ms" against a
  700ms bound from before the request, while the thought still came ~1s
  ahead of the text. The bound timed the gateway's start, not the relay.
  slowThinker now records when it wrote each event. The test checks that
  the first thought reached the client before the vendor wrote the text.
  That is the guarantee, and a slow start no longer counts against it.
- plugins/otel TestOnExportsOffSendsNothing waited 10s for the first export.
  It now waits a minute, as other waits for a plugin's first work do.

Run: go test -count=5 for the relay test, all ok. Broken by hand:
refusesAfterThinking returns true, so the reasoning is held as Claude's is.
The test went red ("reached the client 489µs after the vendor wrote the
text"). Full go test ./... ran with the middleware point commit before this
one (see its body).
2026-10-15 13:40:27 +08:00

65 lines
2.4 KiB
Go

package gateway
import (
"bufio"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// #473: dsh (pi-ai, Chat Completions) on WorkBuddy AI's deepseek-v4.1-flash
// at max heard nothing for 300s and gave up. A Chat reply relayed to a Chat
// client hands on DeepSeek's reasoning_content as it comes, each piece
// while the model is still thinking, not once its text begins: the client
// hears from the model as long as the provider streams its thinking.
func TestChatReasoningRelayedAsItComes(t *testing.T) {
fresh(t)
think := `data: {"id":"c1","choices":[{"index":0,"delta":{"content":"","reasoning_content":"hmm"},"finish_reason":""}]}`
up := &slowThinker{events: []string{
`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning_content":""},"finish_reason":""}]}`,
think, think, think, think, think,
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":"done"},"finish_reason":""}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`,
`data: [DONE]`,
}, gap: 200 * time.Millisecond, done: make(chan time.Time, 1)}
vendor := httptest.NewServer(up)
t.Cleanup(vendor.Close)
if err := provider.Save(provider.Provider{ID: "wb", Name: "WorkBuddy AI", Key: "k", Chat: vendor.URL + "/v2", Models: []string{"deepseek-v4.1-flash"}}); err != nil {
t.Fatal(err)
}
gw := httptest.NewServer(New().Handler())
t.Cleanup(gw.Close)
res, err := http.Post(gw.URL+"/v1/chat/completions", "application/json",
strings.NewReader(`{"model":"wb/deepseek-v4.1-flash","stream":true,"reasoning_effort":"max","messages":[{"role":"user","content":"hi"}]}`))
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
var firstThought time.Time
rd := bufio.NewReader(res.Body)
for {
ln, err := rd.ReadString('\n')
if firstThought.IsZero() && strings.Contains(ln, `"reasoning_content":"hmm"`) {
firstThought = time.Now()
}
if err != nil {
break
}
}
if firstThought.IsZero() {
t.Fatal("no reasoning reached the client")
}
<-up.done
// by the vendor's own clock, so a slow start under load doesn't count:
// the first thought reaches the client before the vendor has even
// written the text, a second after it
if text := up.sent[6]; !firstThought.Before(text) {
t.Fatalf("reasoning held: the first thought reached the client %v after the vendor wrote the text", firstThought.Sub(text))
}
}