Files
magpie/internal/agent/autoref_test.go
T
yetone 9b833205f7 groups: a found group's id and name come from models.dev's one spelling (XYenon)
XYenon on Discord (#feedback, 10-30): autoGroups() and spelledIn() picked
a found group's name and its public id apart, so a group read
glm-5-3-flash beside auto/glm-5.3-flash. The id also followed the order
the providers were added in.

Now:
- spelledIn takes the spelling models.dev gives the model most
  (catalog.Spelling, the normal index normalFor already reads): every
  member spelling of GLM-5.3 Flash (glm-5-3-flash, Ark's
  glm-5-3-flash-260828, glm-5.3-flash) gives auto/glm-5.3-flash. A model
  models.dev doesn't know keeps the old pick (first listed member
  spelling, else the first member's).
- autoGroups names the group from the same choice: catalog.NameOf of
  that spelling, else a vendor's own name, else the spelling itself.
  A found group the user changed keeps the name they saved.
- Old ids keep working. group/auto-<slug> doesn't change, and any
  auto/<spelling> is still the group (CanonicalRef: gateway, key
  allow-lists, fallbacks, members). PublicRef now takes an old
  auto/<spelling> to the current one too, so hidden, picked and ordered
  models saved by it hold, and a pick of it written now (Agent.Apply)
  is written as listed. An agent field magpie set to an old spelling
  moves on the next SyncCatalog (renameFoundGroups), as group/auto-
  values already did, because OpenCode checks its model against the list
  magpie writes. A value the user set stays as it is and is still served.

Tests (on mote, via mote-run):
- TestFoundGroupIDAndNameAreOneSpelling (5 member orders/spellings, the
  user's name kept), TestFoundGroupUnknownToModelsDevNamedByItsID,
  TestFoundGroupMovedOffAnOldSpelling. The catalog fixtures use
  models.dev's real spellings and names (glm-5.3-flash "GLM-5.3-Flash",
  volcengine's glm-5-3-flash "GLM 5.3 Flash").
- Broken by hand, one file reverted at a time: group.go reverted, the
  test is red (named "GLM 5.3 Flash" beside auto/glm-5.3-flash);
  provider/autoref.go reverted, red (id auto/glm-5-3-flash with Ark first,
  PublicRef of the old spelling unchanged); agent/autoref.go reverted,
  TestFoundGroupMovedOffAnOldSpelling red (field stays
  magpie/auto/glm-5-3-flash).
- go test -timeout 45m: internal/catalog, provider, agent, tui, cli,
  usageapi ok. internal/gui: everything ok except TestMainWindowCorners/
  macOS_26_SDK, which is #3949 (AppKit on macOS 27, nothing to do with
  this change). internal/gateway, codexcat, sessions ok.
- gofmt -l clean; go vet darwin, linux -tags nogui, windows clean.
- No GUI file changed, so the GUI suite wasn't run. Names reach it
  through /api/groups.

Not tried: a real models.dev sync with real providers in a sandbox app.
Groups whose first provider spelled a model differently from models.dev
(e.g. claude-opus-4.8 from a relay vs Anthropic's claude-opus-4-8) get
the models.dev spelling once. Magpie-set agent fields move with it, and
values the user set keep resolving. A key or fallback chip saved under
the old spelling still shows it in the editor (it still works).
2026-10-30 03:09:29 +08:00

155 lines
5.5 KiB
Go

package agent
import (
"encoding/json"
"os"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// An agent magpie set to a found group by its old id, group/auto-<slug>,
// is moved onto the group's auto/<model> on the next catalog sync, its
// other settings kept (ZeroClover on X); one whose field the user set
// themselves, or changed since magpie set it, keeps their value.
func TestFoundGroupRenamedOnlyWhereMagpieSetIt(t *testing.T) {
syncHome(t)
// relay and spare both serve glm-4.6: magpie finds the group
if err := provider.Save(provider.Provider{ID: "spare", Name: "Spare", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"glm-4.6"}}); err != nil {
t.Fatal(err)
}
if got := provider.PublicRef("group/auto-glm-4-6"); got != "auto/glm-4.6" {
t.Fatalf("the found group is listed as %q", got)
}
a, err := Find("opencode")
if err != nil {
t.Fatal(err)
}
if err := a.Apply("model", "relay/glm-4.6"); err != nil {
t.Fatal(err)
}
set := a.Values()["model"]
if set == "" {
t.Fatalf("nothing set: %v", a.Values())
}
// what an older magpie wrote and recorded: the group by its old id,
// beside a key of the user's own
old := "magpie/group/auto-glm-4-6"
if err := edit.SetJSON(a.Path, edit.KV{Path: "model", Value: old}, edit.KV{Path: "theme", Value: "mine"}); err != nil {
t.Fatal(err)
}
record(a.ID, "model", old)
SyncCatalog()
if got := a.Values()["model"]; got != "magpie/auto/glm-4.6" {
t.Fatalf("magpie's own value became %q, want magpie/auto/glm-4.6\n%s", got, readFile(a.Path))
}
if got := appliedOf(a.ID).Fields["model"]; got != "magpie/auto/glm-4.6" {
t.Errorf("applied.json holds %q", got)
}
var doc map[string]any
if err := json.Unmarshal([]byte(readFile(a.Path)), &doc); err != nil || doc["theme"] != "mine" {
t.Errorf("the user's own key: %v %v", doc["theme"], err)
}
if d := a.Drift(); d != nil {
t.Errorf("drift after the move: %+v", d)
}
// the user's own value, the group by its old id at an effort of their
// choosing, where magpie had set the group plain: theirs, kept
theirs := "magpie/group/auto-glm-4-6:high"
if err := edit.SetJSON(a.Path, edit.KV{Path: "model", Value: theirs}); err != nil {
t.Fatal(err)
}
record(a.ID, "model", old)
SyncCatalog()
if got := a.Values()["model"]; got != theirs {
t.Fatalf("the user's %q became %q", theirs, got)
}
// and one magpie never set: no record of the field at all
appliedMu.Lock()
m := appliedLoad()
delete(m, a.ID)
appliedSave(m)
appliedMu.Unlock()
if err := edit.SetJSON(a.Path, edit.KV{Path: "model", Value: old}); err != nil {
t.Fatal(err)
}
SyncCatalog()
if got := a.Values()["model"]; got != old {
t.Fatalf("the user's own %q became %q", old, got)
}
}
// A found group's auto/<model> follows the spelling models.dev gives the
// model most (XYenon on Discord), so one magpie listed before as
// auto/glm-5-3-flash is auto/glm-5.3-flash now. An agent magpie set to the
// old spelling is moved onto the new one on the next catalog sync, as one
// set to group/auto-<slug> is: OpenCode checks its model against the list
// magpie writes, which names the group by its new spelling only. One the
// user set to it themselves keeps their value, which is still the group.
func TestFoundGroupMovedOffAnOldSpelling(t *testing.T) {
syncHome(t)
os.WriteFile(catalog.CachePath(), []byte(`{
"zai": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash"}}},
"fireworks": {"models": {"glm-5.3-flash": {"id":"glm-5.3-flash","name":"GLM-5.3-Flash"}}}}`), 0o644)
catalog.Reset()
for id, m := range map[string]string{"ark": "glm-5-3-flash", "spare": "glm-5.3-flash"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{m}}); err != nil {
t.Fatal(err)
}
}
if got := provider.PublicRef("group/auto-glm-5-3-flash"); got != "auto/glm-5.3-flash" {
t.Fatalf("the found group is listed as %q", got)
}
a, err := Find("opencode")
if err != nil {
t.Fatal(err)
}
if err := a.Apply("model", "spare/glm-5.3-flash"); err != nil {
t.Fatal(err)
}
// what magpie wrote and recorded while the group was listed by the
// first provider's spelling
old := "magpie/auto/glm-5-3-flash"
if err := edit.SetJSON(a.Path, edit.KV{Path: "model", Value: old}); err != nil {
t.Fatal(err)
}
record(a.ID, "model", old)
SyncCatalog()
if got := a.Values()["model"]; got != "magpie/auto/glm-5.3-flash" {
t.Fatalf("magpie's own value became %q, want magpie/auto/glm-5.3-flash\n%s", got, readFile(a.Path))
}
if got := appliedOf(a.ID).Fields["model"]; got != "magpie/auto/glm-5.3-flash" {
t.Errorf("applied.json holds %q", got)
}
// set as it is listed now: left alone
SyncCatalog()
if got := a.Values()["model"]; got != "magpie/auto/glm-5.3-flash" {
t.Errorf("the group as it is listed became %q", got)
}
// the user's own: no record of the field
appliedMu.Lock()
m := appliedLoad()
delete(m, a.ID)
appliedSave(m)
appliedMu.Unlock()
if err := edit.SetJSON(a.Path, edit.KV{Path: "model", Value: old}); err != nil {
t.Fatal(err)
}
SyncCatalog()
if got := a.Values()["model"]; got != old {
t.Fatalf("the user's own %q became %q", old, got)
}
// and a pick of the old spelling written now is the group as listed
if err := a.Apply("model", "magpie/auto/glm-5-3-flash"); err != nil {
t.Fatal(err)
}
if got := a.Values()["model"]; got != "magpie/auto/glm-5.3-flash" {
t.Errorf("a pick of the old spelling was written as %q", got)
}
}