Files
magpie/internal/provider/model_names_test.go
T
yetone e0bf3e61ed catalog: a model the provider's catalog doesn't list is named as the other providers name it (John on Discord #feedback: GLM Coding Plan and ZCode name the same models in different case, in the provider list and the group picker) — the Coding Plan's /models (open.bigmodel.cn and api.z.ai, read with a real key) gives ids alone, glm-5-turbo, glm-5.3-flashx …; catalog.Decorate names only those Zhipu's models.dev entry (zhipuai) lists, so glm-5-turbo kept its id as its name beside ZCode's GLM-5-Turbo. models.dev's load now keeps each model's name as most providers serving it give it (a name that is only the id casts no vote), catalog.NameOf reads it by bare id in any case, and catalog.Named fills it in for a model still named by its id: in Available's live list and for a pick the list doesn't have. Ids, and what is sent upstream, are unchanged; a name the user gave stays theirs (Catalog's Name, with the new one as Default); an Azure provider is left out, its deployments named as their owner named them. TestModelNamedAsElsewhere (the Coding Plan's real ids, a user name, an Azure deployment) fails without the provider change and passes; TestNameOf is new. TestSearcherChosen read whatever catalog an earlier test had left loaded, and now resets it: with none, its lists' models go by their ids, as it expects. go test ./internal/catalog/ ./internal/provider/ ./internal/gateway/ ./internal/gui/ . pass; -race -count=5 for the two new tests; go vet, linux(nogui)/windows builds pass. No GUI change (the GUI and the group picker show name || id).
2026-10-06 13:09:40 +08:00

90 lines
3.2 KiB
Go

package provider
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// John (Discord #feedback): a GLM Coding Plan's models read glm-5-turbo
// where ZCode's read GLM-5-Turbo. The plan's list gives ids alone, and
// Zhipu's catalog (zhipuai) doesn't list glm-5-turbo, so it kept its id as
// its name. A model Zhipu's catalog doesn't list is now named as the other
// providers serving it name it; the id sent upstream stays, a name the
// user gave stays theirs, and an Azure deployment keeps the name its owner
// gave it.
func TestModelNamedAsElsewhere(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), []byte(`{
"zhipuai": {"models": {"glm-5.3": {"id":"glm-5.3","name":"GLM-5.3"}}},
"zai": {"models": {"glm-5-turbo": {"id":"glm-5-turbo","name":"GLM-5-Turbo"}, "gpt-x": {"id":"gpt-x","name":"GPT-X"}}},
"zai-coding-plan": {"models": {"glm-5.3-flashx": {"id":"glm-5.3-flashx","name":"GLM-5.3-FlashX"}}}
}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
// as https://open.bigmodel.cn/api/coding/paas/v4/models lists them
listed := []catalog.Model{{ID: "glm-5.3", Name: "glm-5.3"}, {ID: "glm-5-turbo", Name: "glm-5-turbo"}, {ID: "glm-5.3-flashx", Name: "glm-5.3-flashx"}, {ID: "glm-9", Name: "glm-9"}}
if err := catalog.SaveLive("zhipu", "", listed); err != nil {
t.Fatal(err)
}
if err := Save(Provider{ID: "zhipu", Name: "Zhipu GLM", Preset: "zhipu", Catalog: "zhipuai", Key: "test-key", Chat: "https://open.bigmodel.cn/api/coding/paas/v4",
Models: []string{"glm-5.3", "glm-5-turbo", "glm-5.3-flashx", "glm-9", "gpt-x"}}); err != nil {
t.Fatal(err)
}
p, err := Find("zhipu")
if err != nil {
t.Fatal(err)
}
var got []string
for _, m := range p.Exposed() {
got = append(got, m.ID+"|"+m.Name)
}
want := []string{
"glm-5.3|GLM-5.3",
"glm-5-turbo|GLM-5-Turbo",
"glm-5.3-flashx|GLM-5.3-FlashX",
"glm-9|glm-9", // no provider names it
"gpt-x|GPT-X", // picked, not in the list
}
if strings.Join(got, "\n") != strings.Join(want, "\n") {
t.Fatalf("got\n%s\nwant\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
}
if err := SetModelName("zhipu/glm-5-turbo", "turbo mine"); err != nil {
t.Fatal(err)
}
byID := map[string]Entry{}
for _, e := range Catalog() {
byID[e.ID] = e
}
if e := byID["zhipu/glm-5-turbo"]; e.Name != "turbo mine" || e.Default != "GLM-5-Turbo" || e.Model != "glm-5-turbo" {
t.Errorf("named by the user: %+v", e)
}
if err := catalog.SaveLive("my-azure", "", []catalog.Model{{ID: "gpt-x", Name: "gpt-x"}}); err != nil {
t.Fatal(err)
}
if err := Save(Provider{ID: "my-azure", Name: "Azure", Preset: AzurePreset, Key: "test-key", Chat: "https://me.openai.azure.com/openai/v1", Models: []string{"gpt-x"}}); err != nil {
t.Fatal(err)
}
a, err := Find("my-azure")
if err != nil {
t.Fatal(err)
}
if ms := a.Exposed(); len(ms) != 1 || ms[0].Name != "gpt-x" {
t.Errorf("an Azure deployment was renamed: %+v", ms)
}
}