Files
magpie/internal/gateway/trace_test.go
T
Zhu Wang 1adb2b571e feat(routing): group requests by session with names and estimated costs (#396)
* feat(routing): group requests by session with names and estimated costs

* fix(routing): address session view review feedback

Preserve Luna Reserve and other header fallbacks for ordinary Codex turn metadata; reuse metadata and the kind across trace and native accounting. Default to By request and persist the grouping choice. Store only token accounting fields in route history while retaining legacy JSON compatibility.

Add regressions for helper precedence, native kind accounting, history compatibility and grouping preferences.
2026-10-01 20:50:15 +08:00

116 lines
4.0 KiB
Go

package gateway
import (
"context"
"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)
}
}