Files
magpie/internal/gateway/fastpick_test.go
T
yetone 2533ccdaf9 gui: a model with a fast mode is sent fast, or not, from its row in the agent's picker (#954, Gyarados4157) — Fast was only a routing group member's (Group.Fast), so a GPT on a ChatGPT account picked straight in Codex or OpenCode could only go fast through a group of one. A model provider.CanFast says has a fast mode (a ChatGPT account's GPT, an OpenAI key's Responses GPT/o-series, Cursor's -fast, Claude Opus on an Anthropic key) now has a bolt beside its star in the agent's picker and in the same picker opened from a provider's row; a click posts /api/agent-fast and switches it for that agent (settings FastPicks, by agent, "<provider>/<model>", moved by a provider rename and kept by the Settings page's save) without changing the pick or closing the picker; a refusal puts it back. The field shows a bolt after a model sent fast. The gateway asks for the vendor's fast mode (service_tier "priority" / speed "fast", as for a group member) on that agent's requests for that entry, every account and key of it, not its fallbacks, another agent's requests or the same model at another provider; the Routing trace marks the try Fast. A routing group is never switched: its members say it. zh/ja/de added. fast-pick (window and 440px panel, en/zh/ja/de, Chromium and WebKit; the bolt only on models with a fast mode, the post, the refusal, the provider row's picker) passes and fails without the fix; TestFastPick (gateway, Chat/Responses/Anthropic in, fails with the gateway hunk off), TestSetFastPick, TestFastPickOptions and the settings-keep test (fails without the carry) pass with -race; i18n-keys, gui-ja, gui-de, favorite-star, agent-models-picker, claude-picker-fold and click-scroll pass; go test ./internal/gui/ ./internal/provider/ ./internal/agent/ ./internal/gateway/ ./internal/settings/ pass.
2026-10-06 11:15:52 +08:00

75 lines
2.9 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// postAs sends body to path with agent's own token, as the agent wired to
// magpie does.
func postFrom(t *testing.T, s *Server, agent, path, body string) Route {
t.Helper()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", path, strings.NewReader(body))
req.Header.Set("Authorization", "Bearer "+TokenFor(agent))
if strings.HasSuffix(path, "/messages") {
req.Header.Set("anthropic-version", "2023-06-01")
}
s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%s: %d %s", path, rec.Code, rec.Body)
}
return lastRoute(s)
}
// A model switched fast in an agent's picker (#954) is asked for in its
// vendor's fast mode on that agent's requests, whichever API it speaks, and
// the trace says so; another agent's requests for it, and the agent's for
// the same model at another provider, go as they are.
func TestFastPick(t *testing.T) {
s, up := fasted(t)
if err := provider.SetFastPick("codex", "oa/gpt-6.1-sol", true); err != nil {
t.Fatal(err)
}
asks := []struct{ name, path, body string }{
{"chat", "/v1/chat/completions", `{"model":"oa/gpt-6.1-sol","messages":[{"role":"user","content":"hi"}]}`},
{"responses", "/v1/responses", `{"model":"oa/gpt-6.1-sol","input":"hi"}`},
{"anthropic", "/v1/messages", `{"model":"oa/gpt-6.1-sol","max_tokens":1024,"messages":[{"role":"user","content":"hi"}]}`},
}
for _, a := range asks {
r := postFrom(t, s, "codex", a.path, a.body)
host, path, _, body := up.last(t)
if host != "api.openai.com" || body["service_tier"] != "priority" || body["model"] != "gpt-6.1-sol" {
t.Fatalf("%s: sent to %s%s %v", a.name, host, path, body)
}
if len(r.Tries) != 1 || !r.Tries[0].Fast || len(r.Order) == 0 || !r.Order[0].Fast {
t.Fatalf("%s: traced %+v %+v", a.name, r.Tries, r.Order)
}
r = postFrom(t, s, "opencode", a.path, a.body)
if _, _, _, body := up.last(t); body["service_tier"] != nil || r.Tries[0].Fast {
t.Fatalf("%s: another agent's request went fast: %v %+v", a.name, body, r.Tries)
}
}
postFrom(t, s, "codex", "/v1/chat/completions", `{"model":"oa/gpt-6.1-mini","messages":[{"role":"user","content":"hi"}]}`)
if _, _, _, body := up.last(t); body["service_tier"] != nil {
t.Fatalf("a model not switched fast went fast: %v", body)
}
postFrom(t, s, "codex", "/v1/chat/completions", `{"model":"rl/gpt-6.1-sol","messages":[{"role":"user","content":"hi"}]}`)
if host, _, _, body := up.last(t); host != "relay.example" || body["service_tier"] != nil {
t.Fatalf("the same model at a relay went fast: %s %v", host, body)
}
// switched back: as it was
if err := provider.SetFastPick("codex", "oa/gpt-6.1-sol", false); err != nil {
t.Fatal(err)
}
postFrom(t, s, "codex", "/v1/responses", `{"model":"oa/gpt-6.1-sol","input":"hi"}`)
if _, _, _, body := up.last(t); body["service_tier"] != nil {
t.Fatalf("switched back, still fast: %v", body)
}
}