Files
magpie/internal/gateway/reasoning_speed_fallback_test.go
T
yetoneandcongee949 296b63f8d6 usage, gateway: tests for the #1463 rebase: a priced row keeps its reasoning evidence, and a summary after an endpoint fallback stays hidden
Follow-up to congee949's rebased dfc24632
(#1463, streamed reasoning speed).

- TestPricedRowKeepsReasoningEvidence: the Requests page reads gateway rows
  through blockPricer, which sets swapKnown (64) on every row. The patch
  put ReasoningHidden on bit 64 too; on main that would have read every
  priced row as hidden, so a tool-only reply with no reasoning, and paced
  visible reasoning, lost their speed on the Requests tab. The rebase gave
  ReasoningHidden its own bit (256, Flags widened to uint16); this test
  prices visible, tool-only and hidden rows, raw and packed, and checks
  the flag and the speed read back.
- TestSummaryAfterEndpointFallbackStaysHidden: a Messages client, a model
  answered on /responses after /chat/completions said it isn't served
  there. call.To still reads Chat, so hidesReasoning(call.To) doesn't see
  the summary; only streamTranslated's summaryUpstream marking does. With
  no reasoning count the 1500 output tokens were otherwise timed from the
  first summary delta. Nothing covered that marking before.
- reasoning-speed.test.cjs: the "no reasoning: 500 in 5 s" comment had
  been joined onto the redacted row; put back on its own row.
- docs/subsystems/gui-shell.md: the flag bit, the fallback marking, the
  two tests.

Run: go test -tags nogui -count=1 ./internal/usage/ and the gateway's
reasoning tests; reasoning-speed.test.cjs in Chromium and WebKit (en, zh,
zh-TW, ja, de), 10/10.
Broken by hand: rowReasoningHidden = 64 turns TestPricedRowKeepsReasoningEvidence
red (visible and tool-only rows read hidden, 0 tok/s); summaryUpstream
forced false turns TestSummaryAfterEndpointFallbackStaysHidden red (1500
tokens in 314 ms).

Co-authored-by: congee949 <c86474321@gmail.com>
2026-10-13 06:43:50 +08:00

60 lines
2.7 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A model served only on /responses is asked on Chat first and answered on
// Responses (forwardTranslated's endpoint fallback, Copilot's newest
// models): the try's protocol still reads Chat, so the summary the reply
// streamed is only known for one through the upstream that sent it
// (streamTranslated's summaryUpstream). With no reasoning count, a summary
// that went unmarked would time the hidden reasoning's tokens as a tool
// call's speed (#1463).
func TestSummaryAfterEndpointFallbackStaysHidden(t *testing.T) {
fresh(t)
summary := &paced{events: []string{
`data: {"type":"response.created","response":{"id":"r1","status":"in_progress","output":[]}}`,
`~data: {"type":"response.reasoning_summary_text.delta","delta":"first summary"}`,
`~data: {"type":"response.reasoning_summary_text.delta","delta":"second summary"}`,
`~data: {"type":"response.reasoning_summary_text.delta","delta":"third summary"}`,
`data: {"type":"response.output_item.added","item":{"type":"function_call","call_id":"c1","name":"check"}}`,
`data: {"type":"response.function_call_arguments.delta","delta":"{}"}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":1500}}}`,
}, gap: 150 * time.Millisecond}
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v1/chat/completions" {
io.ReadAll(r.Body)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error":{"message":"model \"m1\" is not accessible via the /chat/completions endpoint","code":"unsupported_api_for_model"}}`)
return
}
summary.ServeHTTP(w, r)
}))
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Chat: up.URL + "/v1", Responses: up.URL + "/v1", Key: "k", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
s := New()
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"fake/m1","stream":true,"max_tokens":300,"messages":[{"role":"user","content":"hi"}]}`)))
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "check") {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
u := lastUsage(t)
if u.TTFT <= 0 || u.Output != 1500 {
t.Fatalf("the reply wasn't timed as a stream: %+v", u)
}
if n, w := u.Decode(); !u.ReasoningHidden || n != 0 || w != 0 {
t.Fatalf("a Responses summary was timed as the reply's speed (%d tokens in %d ms): %+v", n, w, u)
}
}