Files
magpie/internal/gateway/trace_test.go
T
yetone d6096e2fb1 routing: a failed request whose route has no order opens when clicked (Discord, mythfish on v0.1.810: on the Routing page's 实时 list, clicking "11:28:08 AM • workbuddy-ai/deepseek-v4.1-flash • 400 · 失败 6.5 秒" did nothing; the request was refused with WorkBuddy AI 400 {"message":"WorkBuddy AI: Invalid request parameters","type":"invalid_request_error"}) — Route.Order has no omitempty and the trace's snapshots (Trace, the saved copy in update, sessionLatest) copied it with append([]Weighed(nil), r.Order...), so a route begun with no seats weighed reached the page as "order": null, while its tries were there. routing.js reads r.order as a list throughout: a row's click runs pick → sync → rebuild, whose setOf(r) is r.order.map(seat), and renderActs runs r.order.forEach, so pick threw (webkit: "null is not an object (evaluating 'r.order.map')") before the story was drawn and the click looked dead. Reproduced with a live trace fixture: a 400 with order null fails to open; the same 400 with order [] or a one-seat order, every failure kind, and an order whose seats don't match the try all open, so it is the null and not the 400 or the WorkBuddy error. The page now fills in a route's lists where routes come in (whole: order, tries and left default to []), for the live trace, a kept day's history and openRoute's fetched route (the tray panel's and Sessions' links), and the gateway writes Order as [] when it is empty in all three snapshots, so an older page reading a newer gateway is fine too. Tests: routing-failed-open.test.cjs (webkit, en and zh, live and a kept day: a request that answered and the report's 400 with "order": null; clicking the 400 presses its row, shows the story "How the request at … was routed" / "… 的请求是怎么路由的" with "It said: WorkBuddy AI: Invalid request parameters" / "原话:…", doesn't move the page, and the answered one opens again after; no page errors) fails all four on the old routing.js and passes; TestTraceOrderIsAList (a route begun with no order and refused with a 400: Trace's JSON has "order":[], sessionLatest's copy a non-nil Order) fails on the old trace.go with "order":null and passes. The routing-*.test.cjs and click-scroll GUI tests pass in webkit (routing-time's zh case failed once in a 4-way parallel run and passes on its own). go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not found: which gateway path began the reporter's route with no order — the plain chat path weighs at least the provider it was asked for — so the fix is to the shape, not to that path; not tried with the reporter's WorkBuddy AI account. The 400 itself (WorkBuddy refusing the request's parameters) is a separate question this doesn't touch.
2026-10-04 12:23:43 +08:00

140 lines
5.0 KiB
Go

package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// The trace tells what routing did as it did it: who was to answer in what
// order, each try and what it answered, and how long one that failed rests.
func TestTraceTellsTheRoute(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
restingUntil.Lock()
restingUntil.m = map[string]time.Time{}
restingUntil.Unlock()
up := &byKey{limited: map[string]bool{"k-personal": true}}
srv := httptest.NewServer(up)
defer srv.Close()
p := provider.Provider{ID: "plan", Name: "Plan", Chat: srv.URL + "/v1", Models: []string{"m1"},
Key: "k-personal", KeyName: "Personal", Keys: []provider.KeyAccount{{Name: "Team", Key: "k-team"}}}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
s := New()
send := func() {
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(chatReq))
req.Header.Set("session_id", "native-session")
req.Header.Set(SessionHeader, "chosen-session")
s.Handler().ServeHTTP(httptest.NewRecorder(), req)
}
// one waiting for a route hears of it
got := make(chan []Route, 1)
go func() {
got <- s.Trace(context.Background(), 0, 5*time.Second).Routes
}()
time.Sleep(50 * time.Millisecond)
send()
if rs := <-got; len(rs) != 1 {
t.Fatalf("waiting: %d routes", len(rs))
}
st := s.Trace(context.Background(), 0, 0)
r := st.Routes[len(st.Routes)-1]
if r.Session != "chosen-session" {
t.Fatalf("session %q", r.Session)
}
if !r.Done || r.Status != 200 || r.Provider != "plan" || len(r.Order) != 2 || r.Order[0].Who != "Personal" || r.Order[1].Kind != "key" {
t.Fatalf("route %+v", r)
}
if len(r.Tries) != 2 || r.Tries[0].Status != 429 || r.Tries[0].Fail != failRate || r.Tries[0].Rest == nil ||
r.Tries[0].Rest.By != "cooldown" || r.Tries[1].Status != 200 || r.Tries[1].Rest != nil {
t.Fatalf("tries %+v", r.Tries)
}
if recs := usage.Load(time.Time{}); len(recs) != 1 || recs[0].RouteID != r.ID || r.ID == 0 {
t.Fatalf("usage: %+v, route %d", recs, r.ID)
}
// the next finds the limited key resting, and says why
send()
st = s.Trace(context.Background(), st.Seq, 0)
r = st.Routes[len(st.Routes)-1]
if r.Order[0].Who != "Team" || r.Order[1].Rest == nil || r.Order[1].Rest.Status != 429 || len(r.Tries) != 1 {
t.Fatalf("resting %+v", r)
}
if st.Totals != (Totals{Requests: 2, Rerouted: 1}) {
t.Fatalf("totals %+v", st.Totals)
}
}
// A vendor's Retry-After reaches routing through a failed request: the
// account rests as long as it says, and the trace says so.
func TestRetryAfterRests(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
restingUntil.Lock()
restingUntil.m = map[string]time.Time{}
restingUntil.Unlock()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Header.Get("Authorization") == "Bearer k-slow" {
w.Header().Set("Retry-After", "300")
w.WriteHeader(429)
io.WriteString(w, `{"error":{"message":"Too many requests"}}`)
return
}
io.WriteString(w, `{"id":"x","choices":[]}`)
}))
defer srv.Close()
p := provider.Provider{ID: "ra", Name: "RA", Chat: srv.URL + "/v1", Models: []string{"m1"},
Key: "k-slow", KeyName: "Slow", Keys: []provider.KeyAccount{{Name: "Fast", Key: "k-fast"}}}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
s := New()
s.Handler().ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", "/v1/chat/completions",
strings.NewReader(`{"model":"ra/m1","messages":[{"role":"user","content":"hi"}]}`)))
rs := s.Trace(context.Background(), 0, 0).Routes
if len(rs) != 1 || len(rs[0].Tries) != 2 || rs[0].Tries[0].Rest == nil {
t.Fatalf("%+v", rs)
}
r := rs[0].Tries[0].Rest
if r.By != "retry-after" || time.Until(r.Until).Round(time.Minute) != 5*time.Minute {
t.Fatalf("rest %+v", r)
}
}
// A route weighed with no seats tells its order as [], not null: the
// Routing page reads it as a list, and a null one left a refused request
// that couldn't be opened (Discord, mythfish on v0.1.810).
func TestTraceOrderIsAList(t *testing.T) {
s := New()
r := s.trace.begin(Route{Model: "workbuddy-ai/deepseek-v4.1-flash"})
s.trace.update(r, func(r *Route) {
r.Tries = append(r.Tries, Try{ID: "workbuddy-ai", Status: 400, Error: "WorkBuddy AI: Invalid request parameters"})
r.Status, r.Error, r.Done = 400, "WorkBuddy AI: Invalid request parameters", true
})
st := s.Trace(context.Background(), 0, 0)
b, err := json.Marshal(st.Routes)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(b), `"order":[]`) || strings.Contains(string(b), `"order":null`) {
t.Fatalf("trace routes = %s, want order []", b)
}
if _, c := s.trace.sessionLatest(context.Background(), "", 0, 0); c == nil || c.Order == nil {
t.Fatalf("session's latest route = %+v, want a non-nil order", c)
}
}