Files
magpie/internal/catalog/names_test.go
T
Kenoia 7df0a5bf32 catalog: a vendor's own model name is kept ahead of the catalog's, and fallback names never collide (#1188, fixes #1119)
Command Code's Flash Fast showed the catalog's "Flash", the same name as
plain Flash, so the picker had two "Flash" rows. An unprefixed `name`
from a vendor's /models now wins over the catalog fallback
(magpie_label and display_name still come first). A `Vendor: Model`
name such as OpenRouter's falls through to the catalog as before, so
OpenRouter's names don't change.

A catalog fallback that would give two ids one name keeps the id
instead. Exposed fills names only for unlisted picks and checks every
available name and id for a collision; listed picks keep exactly what
Available returned, Antigravity's id-only names among them.
TestNameOf's case-variant expectation changes on purpose: glm-5-turbo
and GLM-5-Turbo together keep their own ids.

Kenoia's PR head f538b19e, which conflicted with main in
internal/catalog/live.go and live_test.go; the conflict was resolved
keeping both main's live-name changes and this one.

Run on this tree (origin/main 848d2ea9 + the PR): gofmt -l clean;
go vet darwin, GOOS=linux -tags nogui and GOOS=windows on
internal/catalog and internal/provider; go test -tags nogui
./internal/catalog/ ok and ./internal/provider/ ok (805s, full
package, on the same change over 5b14ebf4), and -run Name|Catalog|Live
again on 848d2ea9. With the live.go fallback reverted, the new
live_test and model_names tests go red. Not tried against Command
Code's live /models with a real account.
2026-10-13 11:43:54 +08:00

116 lines
4.1 KiB
Go

package catalog
import (
"reflect"
"slices"
"strings"
"testing"
)
// A model is named as most of the providers serving it name it, matched
// without a vendor's prefix and in any case; a name that is only the id
// casts no vote.
func TestNameOf(t *testing.T) {
writeCatalog(t, `{
"zai": {"models": {"glm-5-turbo": {"id":"glm-5-turbo","name":"GLM-5-Turbo"}}},
"zai-coding-plan": {"models": {"glm-5-turbo": {"id":"glm-5-turbo","name":"GLM-5-Turbo"}}},
"a": {"models": {"z-ai/glm-5-turbo": {"id":"z-ai/glm-5-turbo","name":"GLM 5 Turbo"}}},
"b": {"models": {"glm-5-turbo": {"id":"glm-5-turbo","name":"glm-5-turbo"}},
"plain": {"id":"plain","name":"plain"}}
}`)
for id, want := range map[string]string{
"glm-5-turbo": "GLM-5-Turbo",
"GLM-5-Turbo": "GLM-5-Turbo",
"z-ai/glm-5-turbo": "GLM-5-Turbo",
"plain": "",
"unknown": "",
} {
if got := NameOf(id); got != want {
t.Errorf("NameOf(%q) = %q, want %q", id, got, want)
}
}
in := []Model{
{ID: "glm-5-turbo", Name: "glm-5-turbo"},
{ID: "GLM-5-Turbo"},
{ID: "glm-5-turbo", Name: "Turbo"}, // the list's own name stays
{ID: "plain", Name: "plain"},
}
var got []string
for _, m := range Named(in) {
got = append(got, m.ID+"|"+m.Name)
}
// These exact ids differ, so the shared name would hide that distinction.
// NameOf still matches either case; Named keeps each id in this list.
want := "glm-5-turbo|glm-5-turbo\nGLM-5-Turbo|GLM-5-Turbo\nglm-5-turbo|Turbo\nplain|plain"
if strings.Join(got, "\n") != want {
t.Fatalf("got\n%s\nwant\n%s", strings.Join(got, "\n"), want)
}
if in[0].Name != "glm-5-turbo" {
t.Fatalf("the list given was changed: %q", in[0].Name)
}
}
func TestNamedAvoidsCollisions(t *testing.T) {
writeCatalog(t, `{"vendor":{"models":{
"flash":{"id":"flash","name":"Flash"},
"fast":{"id":"fast","name":"Flash"},
"other":{"id":"other","name":"Other"},
"alias":{"id":"alias","name":"fast"}
}}}`)
for _, c := range []struct {
name string
in, want []Model
}{
{"supplemented", []Model{{ID: "flash", Name: "flash"}, {ID: "fast"}}, []Model{{ID: "flash", Name: "flash"}, {ID: "fast", Name: "fast"}}},
{"explicit", []Model{{ID: "flash", Name: "Flash"}, {ID: "fast", Name: "fast"}, {ID: "other"}}, []Model{{ID: "flash", Name: "Flash"}, {ID: "fast", Name: "fast"}, {ID: "other", Name: "Other"}}},
{"explicit duplicates", []Model{{ID: "flash", Name: "Vendor"}, {ID: "fast", Name: "Vendor"}}, []Model{{ID: "flash", Name: "Vendor"}, {ID: "fast", Name: "Vendor"}}},
{"fallback id", []Model{{ID: "alias"}, {ID: "fast"}}, []Model{{ID: "alias", Name: "alias"}, {ID: "fast", Name: "Flash"}}},
{"same id", []Model{{ID: "flash"}, {ID: "flash", Name: "flash"}}, []Model{{ID: "flash", Name: "Flash"}, {ID: "flash", Name: "Flash"}}},
{"unknown", []Model{{ID: "unknown", Name: "unknown"}}, []Model{{ID: "unknown", Name: "unknown"}}},
} {
t.Run(c.name, func(t *testing.T) {
for _, reverse := range []bool{false, true} {
in, want := slices.Clone(c.in), slices.Clone(c.want)
if reverse {
slices.Reverse(in)
slices.Reverse(want)
}
before := slices.Clone(in)
if got := Named(in); !reflect.DeepEqual(got, want) {
t.Errorf("Named(%+v) = %+v, want %+v", in, got, want)
}
if !reflect.DeepEqual(in, before) {
t.Errorf("input changed: %+v", in)
}
}
})
}
}
func TestDecorateAvoidsNameCollisions(t *testing.T) {
known := []Model{{ID: "flash", Name: "Flash", Context: 1000000}, {ID: "fast", Name: "Flash", Context: 1000000}}
for _, explicit := range []bool{false, true} {
in := []Model{{ID: "vendor/flash", Name: "vendor/flash"}, {ID: "vendor/fast", Name: "vendor/fast"}}
want := slices.Clone(in)
if explicit {
in[0].Name, want[0].Name = "Flash", "Flash"
}
for i := range want {
want[i].Context = 1000000
}
for _, reverse := range []bool{false, true} {
if reverse {
slices.Reverse(in)
slices.Reverse(want)
}
before := slices.Clone(in)
if got := Decorate(in, known); !reflect.DeepEqual(got, want) {
t.Errorf("Decorate(%+v) = %+v, want %+v", in, got, want)
}
if !reflect.DeepEqual(in, before) {
t.Errorf("input changed: %+v", in)
}
}
}
}