Files
magpie/internal/provider/groupmatch_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

186 lines
6.6 KiB
Go

package provider
import (
"encoding/json"
"os"
"slices"
"strings"
"testing"
)
func patternHome(t *testing.T) {
t.Helper()
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
}
func routedIDs(t *testing.T, id string) []string {
t.Helper()
_, ms, ok := FindGroup(id)
if !ok {
t.Fatalf("no %s", id)
}
var out []string
for _, m := range ms {
out = append(out, m.ID)
}
return out
}
// A group's patterns (#766) find its members in the catalog each time it is
// read: the models they match follow the ones it names, in the catalog's
// order, a model named and matched both stays where it was named, and a
// model the vendor adds joins without the group being saved again.
func TestGroupPatterns(t *testing.T) {
patternHome(t)
if err := Save(Provider{ID: "or", Name: "OR", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"a:free", "b", "c:free"}}); err != nil {
t.Fatal(err)
}
if err := Save(Provider{ID: "zen", Name: "Zen", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x-free", "y"}}); err != nil {
t.Fatal(err)
}
// typed among the members, as the CLI and the Routing view may send it
if err := SaveGroup(Group{Name: "Free", Members: []string{"zen/y", "or/c:free", "or/*:free", "re:zen/.*-free"}}); err != nil {
t.Fatal(err)
}
g, _, _ := FindGroup("group/free")
if !slices.Equal(g.Match, []string{"or/*:free", "re:zen/.*-free"}) {
t.Fatalf("patterns: %v", g.Match)
}
want := []string{"zen/y", "or/c:free", "or/a:free", "zen/x-free"}
if !slices.Equal(g.Members, want) || !slices.Equal(g.Matched, []string{"or/a:free", "zen/x-free"}) {
t.Fatalf("members %v matched %v", g.Members, g.Matched)
}
if got := routedIDs(t, "group/free"); !slices.Equal(got, want) {
t.Fatalf("routed: %v", got)
}
// what was matched is not written into the group
b, _ := os.ReadFile(Path())
var f struct{ Groups []Group }
json.Unmarshal(b, &f)
if len(f.Groups) != 1 || !slices.Equal(f.Groups[0].Members, []string{"zen/y", "or/c:free"}) || len(f.Groups[0].Matched) != 0 {
t.Fatalf("stored: %+v", f.Groups)
}
// saved again as it was read, it stays so
if err := SaveGroup(g); err != nil {
t.Fatal(err)
}
if s, _ := StoredGroups(); !slices.Equal(s[0].Members, []string{"zen/y", "or/c:free"}) {
t.Fatalf("saved as read: %v", s[0].Members)
}
// renamed, too
if err := RenameGroup("free", "pool"); err != nil {
t.Fatal(err)
}
if s, _ := StoredGroups(); !slices.Equal(s[0].Members, []string{"zen/y", "or/c:free"}) || !slices.Equal(s[0].Match, g.Match) {
t.Fatalf("renamed: %+v", s[0])
}
// the vendor lists another free model: the group has it
if err := Save(Provider{ID: "or", Name: "OR", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"a:free", "b", "c:free", "d:free"}}); err != nil {
t.Fatal(err)
}
if got := routedIDs(t, "group/pool"); !slices.Equal(got, []string{"zen/y", "or/c:free", "or/a:free", "or/d:free", "zen/x-free"}) {
t.Fatalf("after the catalog changed: %v", got)
}
if hits := PatternHits(g); len(hits) != 2 || hits[0].Models != 3 || hits[1].Models != 1 {
t.Fatalf("hits: %+v", hits)
}
// /v1/models has the group
if !slices.ContainsFunc(Catalog(), func(e Entry) bool { return e.ID == "group/pool" }) {
t.Fatal("not in the catalog")
}
}
// A matched member may be switched off, picked, or ruled, like a named one;
// and named at an effort of its own it is not matched again.
func TestGroupPatternMembersOffAndRules(t *testing.T) {
patternHome(t)
if err := Save(Provider{ID: "or", Name: "OR", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"a:free", "b:free", "c"}}); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{Name: "P", Match: []string{"or/*:free"}, Off: []string{"or/a:free"}, Routing: Ordered,
Rules: []Rule{{Use: "or/b:free", Tokens: 10}}}); err != nil {
t.Fatal(err)
}
g, _, _ := FindGroup("group/p")
if !slices.Equal(g.Off, []string{"or/a:free"}) || len(g.Rules) != 1 {
t.Fatalf("saved: %+v", g)
}
if got := routedIDs(t, "group/p"); !slices.Equal(got, []string{"or/b:free"}) {
t.Fatalf("routed: %v", got)
}
// every matched one off is every one off
g.Off = g.Members
if err := SaveGroup(g); err == nil || !strings.Contains(err.Error(), "switched off") {
t.Fatalf("all off: %v", err)
}
// a matched one given an effort of its own is named, and not matched again
g, _, _ = FindGroup("group/p")
g.RenameMember("or/b:free", "or/b:free:high")
if err := SaveGroup(g); err != nil {
t.Fatal(err)
}
g, _, _ = FindGroup("group/p")
if !slices.Equal(g.Members, []string{"or/b:free:high", "or/a:free"}) || !slices.Equal(g.Matched, []string{"or/a:free"}) {
t.Fatalf("named at an effort: %v matched %v", g.Members, g.Matched)
}
}
// A pattern that matches nothing leaves the group there, empty but said so;
// a bad one is refused, and a group needs a model or a pattern.
func TestGroupPatternMatchingNothing(t *testing.T) {
patternHome(t)
if err := Save(Provider{ID: "or", Name: "OR", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"a"}}); err != nil {
t.Fatal(err)
}
if err := SaveGroup(Group{Name: "None", Match: []string{"or/*:free"}}); err != nil {
t.Fatal(err)
}
g, ms, ok := FindGroup("group/none")
if !ok || len(ms) != 0 || len(g.Members) != 0 {
t.Fatalf("found %v: %+v %v", ok, g, ms)
}
if hits := PatternHits(g); len(hits) != 1 || hits[0].Models != 0 {
t.Fatalf("hits: %+v", hits)
}
if err := SaveGroup(Group{Name: "Bad", Match: []string{"re:or/(a"}}); err == nil {
t.Fatal("a broken regexp was taken")
}
if err := SaveGroup(Group{Name: "Empty"}); err == nil {
t.Fatal("a group of nothing was taken")
}
// a pattern never matches a group, nor the group itself
if err := SaveGroup(Group{Name: "All", Match: []string{"*"}}); err != nil {
t.Fatal(err)
}
if g, _, _ := FindGroup("group/all"); !slices.Equal(g.Members, []string{"or/a"}) {
t.Fatalf("all: %v", g.Members)
}
}
func TestPatternSyntax(t *testing.T) {
for _, c := range []struct {
p, id string
ok bool
}{
{"openrouter/*:free", "openrouter/meta/llama-4:free", true},
{"openrouter/*:free", "openrouter/meta/llama-4", false},
{"*-free", "opencode-zen/grok-code-free", true},
{"opencode-zen/*", "opencode-zen/x", true},
{"opencode-zen/*", "opencode/x", false},
{"OpenRouter/*", "openrouter/x", true},
{"re:openrouter/.*:(free|beta)", "openrouter/x:beta", true},
{"re:openrouter/x", "openrouter/xy", false},
{"a.b*", "axb-1", false},
} {
if got := PatternMatches(c.p, c.id); got != c.ok {
t.Errorf("%s ~ %s: %v", c.p, c.id, got)
}
}
if IsPattern("openrouter/x:free") || !IsPattern("re:x") || !IsPattern("x*") {
t.Error("IsPattern")
}
}