Files
magpie/groups_pattern_cli_test.go
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

65 lines
2.2 KiB
Go

package main
import (
"fmt"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// models= takes patterns among the ids (#766): the group keeps the
// patterns, not what they match, and models+= / models-= add and drop them.
func TestGroupPatternsCLI(t *testing.T) {
groupsHome(t)
g, err := addGroup("Pool", []string{"models=b/gpt-5.5,a/*"})
if err != nil {
t.Fatal(err)
}
// named first, then matched in the catalog's order
if !slices.Equal(g.Members, []string{"b/gpt-5.5", "a/m", "a/only-a", "a/claude-opus-5-5"}) || !slices.Equal(g.Match, []string{"a/*"}) {
t.Fatalf("added: %v %v", g.Members, g.Match)
}
gs := storedGroups(t)
if fmt.Sprint(gs[0]["members"]) != "[b/gpt-5.5]" || fmt.Sprint(gs[0]["match"]) != "[a/*]" {
t.Fatalf("stored: %v", gs)
}
// a matched model can't be dropped by id: the pattern would bring it back
if _, err := setGroup("pool", []string{"models-=a/m"}); err == nil || !strings.Contains(err.Error(), "a/*") {
t.Fatalf("dropped a matched model: %v", err)
}
// named, it moves to where it is named
if g, err = setGroup("pool", []string{"models+=a/only-a,re:b/vendor/.*"}); err != nil {
t.Fatal(err)
}
if !slices.Equal(g.Members, []string{"b/gpt-5.5", "a/only-a", "a/m", "a/claude-opus-5-5", "b/vendor/m"}) || len(g.Match) != 2 {
t.Fatalf("models+=: %v %v", g.Members, g.Match)
}
if g, err = setGroup("pool", []string{"models-=a/*"}); err != nil {
t.Fatal(err)
}
if !slices.Equal(g.Members, []string{"b/gpt-5.5", "a/only-a", "b/vendor/m"}) || !slices.Equal(g.Match, []string{"re:b/vendor/.*"}) {
t.Fatalf("models-= a pattern: %v %v", g.Members, g.Match)
}
// a pattern alone, matching nothing: the group stays, said so
if g, err = setGroup("pool", []string{"models=zzz/*"}); err != nil {
t.Fatal(err)
}
if len(g.Members) != 0 || !slices.Equal(g.Match, []string{"zzz/*"}) {
t.Fatalf("models=zzz/*: %+v", g)
}
if _, ms, ok := provider.FindGroup("group/pool"); !ok || len(ms) != 0 {
t.Fatalf("routed: %v %+v", ok, ms)
}
if err := showGroup(g); err != nil {
t.Fatal(err)
}
if _, err := addGroup("Bad", []string{"models=re:a/(m"}); err == nil {
t.Fatal("a broken regexp was taken")
}
if !strings.Contains(groupUsage, "re:<regexp>") {
t.Fatal("help says nothing of patterns")
}
}