Files
magpie/internal/gateway/vision_enabled_test.go
T
yetone d5cae267a7 gateway: image recognition and web search pick only models the user enabled (JonahFu on X)
On a provider with a long list, AutoVision and the searcher picked their
small model from everything the vendor lists (Available), not from what
the user enabled. A Trae CN user's text-only model had its image sent to
OpenRouter's anthropic/claude-haiku-5.5, which they never enabled and
which OpenRouter refused for their region (403).

smallModel, and so AutoVision, searcherModel and the searcher's
first-model fallback, now chooses from Exposed: the user's picks, else
what agents are offered. With no enabled model that sees, a request
turned away says so ("none of the models enabled in magpie can see
images ... enable one that can, or pick one in Settings → Models → Image
recognition"); Vision off says it is off; a describer magpie picked
itself that fails is named as magpie's pick.

Not done: no fallback to another describer on a refusal. With the pick
limited to enabled models, a refusal is of a model the user chose, and
the error now names it and where to change it. Image generation's
AutoDrawer still picks from image models a provider lists: image models
aren't in the chat pickers, so limiting it to picks would turn drawing
off for nearly everyone.

Run: the two new tests fail on the old code (AutoVision =
router/google/gemini-3-flash, searcherModel = google/gemini-3-flash) and
pass now; go test ./... green except internal/gui's
TestDockJoinLeadsCommandTab, which reads the live desktop's Command-Tab
order and passes run alone; gofmt; GOOS=linux and windows go vet
./internal/gateway.
2026-10-12 00:01:28 +08:00

72 lines
2.6 KiB
Go

package gateway
import (
"net/http/httptest"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// routerUp is a router key whose vendor lists a small model that sees,
// anthropic/claude-haiku-5.5, which the user left unpicked: only "text",
// which can't see, is enabled (JonahFu on X: OpenRouter turned the unpicked
// Haiku away for their region, and they had never enabled it).
func routerUp(t *testing.T, picks ...string) (*Server, *eyeUp) {
t.Helper()
fresh(t)
u := &eyeUp{}
up := httptest.NewServer(u)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "router", Name: "Router", Chat: up.URL + "/v1", Key: "key", Models: picks}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("router", up.URL+"/v1", []catalog.Model{
{ID: "text", ImageInput: imageInputBool(false), Price: &catalog.Price{Input: 0.1, Output: 0.1}},
{ID: "anthropic/claude-haiku-5.5", Images: true, ImageInput: imageInputBool(true), Price: &catalog.Price{Input: 1, Output: 5}},
{ID: "google/gemini-3-flash", Images: true, ImageInput: imageInputBool(true), Price: &catalog.Price{Input: 0.3, Output: 2.5}},
}); err != nil {
t.Fatal(err)
}
return New(), u
}
func TestAutoVisionPicksOnlyAnEnabledModel(t *testing.T) {
s, u := routerUp(t, "text")
if m := AutoVision(); m != "" {
t.Fatalf("AutoVision = %q, a model the user never enabled", m)
}
code, body := postAs(t, s, "", `{"model":"router/text","messages":[{"role":"user","content":[{"type":"text","text":"read"},{"type":"image_url","image_url":{"url":"data:image/png;base64,aGVsbG8="}}]}]}`)
if code != 400 || !strings.Contains(body, "none of the models enabled in magpie can see images") {
t.Fatalf("%d %s", code, body)
}
if got := u.sent("anthropic/claude-haiku-5.5"); len(got) != 0 {
t.Fatalf("the unpicked model was asked to describe: %v", got)
}
// with one that sees enabled, that one describes, not the cheaper
// small model the user left out
routerUp(t, "text", "anthropic/claude-haiku-5.5")
if m := AutoVision(); m != "router/anthropic/claude-haiku-5.5" {
t.Fatalf("AutoVision = %q", m)
}
}
// The web-search side request's own pick is the same: a model the user
// enabled.
func TestSearcherModelIsAnEnabledModel(t *testing.T) {
routerUp(t, "text")
p, err := provider.Find("router")
if err != nil {
t.Fatal(err)
}
if m := searcherModel(*p); m != "text" {
t.Fatalf("searcherModel = %q, want the only enabled model", m)
}
if picked := slices.ContainsFunc(p.Exposed(), func(m catalog.Model) bool { return m.ID == "google/gemini-3-flash" }); picked {
t.Fatal("gemini is enabled")
}
}