Files
magpie/internal/gateway/grouppattern_test.go
T
yetone 5569275465 group: a routing group's members can be patterns, so a group follows a provider's catalog (#766: magpie group add my-free-pool models='openrouter/*:free,opencode-zen/*-free') — a group names its models one by one, so a pool of a vendor's free models went stale as the vendor added and dropped them. A group now keeps patterns apart from the models it names (Group.Match): a glob, * any run of characters (/ too) over the provider/model id in any case, or re:<regexp> over the whole id. groupsIn, which every reader goes through (Groups, FindGroup, /v1/models, MemberGroups, GroupsWith, RenameGroup, rules, Off, Pick, the GUI, TUI and CLI), puts after the named models every enabled provider's model a pattern matches, in catalog order, deduplicated, the named one keeping its place; a pattern never matches a group. What was matched is derived (Group.Matched) and never stored: store() and SaveGroup keep only the named members, so a save never freezes matches. Off and rules may name a matched model. A group with only patterns is allowed; one matching nothing routes nowhere and says so: magpie group <id> lists each pattern with how many models it matches now ("matches nothing now" in amber), the group list and the TUI too, and the GUI card shows a line per pattern with its count. CLI: models= takes patterns among the ids, models+= adds them (naming a matched model moves it to where it is named), models-=<pattern> drops one, models-= of a matched model is refused naming its pattern; the help documents patterns. GUI: the editor has a Patterns box (Add pattern / Remove, validated as the server does), matched models follow those named marked "by pattern" with their off switch and no Remove, and saves send members without the matched ones plus match; zh strings added. Tests: TestGroupPatterns, TestGroupPatternMembersOffAndRules, TestGroupPatternMatchingNothing, TestPatternSyntax (provider), TestPatternGroupRoutes (gateway: a request reaches a matched model, a model a provider adds joins, Off on a matched one, /v1/models lists the group), TestGroupPatternsCLI, and group-patterns.test.cjs (WebKit, en+zh); they fail on the old code (the Go ones don't build, the GUI one finds no pattern lines). GUI: group-patterns, group-found-off, group-member-fast, group-member-off, settings-groups, agent-model-groups, remote-group-served, routing-manual pass on WebKit; group-from-model-add has the same 2 failures (600x420) as origin/main. A request to a group whose patterns match nothing now is a 404 naming the group and its patterns (emptyGroupError), not 'magpie knows no model' and every model; TestEmptyPatternGroupSaysSo. With the owner's real config under a sandbox HOME: group/ds-pool of models='deepseek/*' listed deepseek-flash and deepseek-v4-pro by pattern, stored only the pattern, was in /v1/models and answered from deepseek-flash; none-pool of 'nobody/*' said its pattern matches nothing. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 16:52:15 +08:00

67 lines
2.3 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A group whose members are a pattern (#766) sends a request to a model the
// pattern matches, picks up one the vendor lists later without the group
// being saved again, and passes over a matched one switched off.
func TestPatternGroupRoutes(t *testing.T) {
fresh(t)
a, b := &keyed{}, &keyed{}
serveOn(t, "or", "ka", []string{"x", "y:free"}, a)
serveOn(t, "zen", "kb", []string{"z"}, b)
if err := provider.SaveGroup(provider.Group{Name: "Free", Members: []string{"*:free"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
const req = `{"model":"group/free","messages":[{"role":"user","content":"hi"}]}`
code, body := postAs(t, s, "", req)
if code != 200 || !strings.Contains(body, "from ka") || len(b.tried) != 0 {
t.Fatalf("to or/y:free: %d %s", code, body)
}
// another provider's free model, listed after the group was made
serveOn(t, "zen", "kb", []string{"z", "w:free"}, b)
g, ms, _ := provider.FindGroup("group/free")
if len(ms) != 2 || ms[1].Provider.ID != "zen" || ms[1].Model != "w:free" {
t.Fatalf("members: %+v", ms)
}
// the first switched off, the new one answers
g.Off = []string{"or/y:free"}
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
a.tried = nil
code, body = postAs(t, s, "", req)
if code != 200 || !strings.Contains(body, "from kb") || len(a.tried) != 0 {
t.Fatalf("to zen/w:free: %d %s, or tried %v", code, body, a.tried)
}
// and /v1/models lists the group
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
if !strings.Contains(rec.Body.String(), `"group/free"`) {
t.Fatal("group/free not listed")
}
}
// A group whose patterns match nothing served now is named as such, not
// answered with every model magpie has.
func TestEmptyPatternGroupSaysSo(t *testing.T) {
fresh(t)
if err := provider.SaveGroup(provider.Group{ID: "none", Name: "None", Match: []string{"nobody/*"}}); err != nil {
t.Fatal(err)
}
g, ok := emptyGroup("group/none")
if !ok {
t.Fatal("group/none not found as empty")
}
if msg := emptyGroupError(g); !strings.Contains(msg, "nobody/*") || !strings.Contains(msg, "match no model") {
t.Fatal(msg)
}
}