Files
magpie/internal/gateway/custom_name_label_test.go
T
yetone 4547dafe95 models: Provider in model names has a third way, a name the user gave used just as they wrote it (#92 marsxxl: v0.1.591 的 Codex 下拉里自定义名称又带上了 · WorkBuddy / · Claude Code) — 559ce021 (#203) had a custom name keep " · <provider>" after it like any other, so "Opus 5.5" read "Opus 5.5 · Claude Code" in every list Labels feeds (Codex's model_catalog_json and the gateway's Codex /models, Pi's, OpenCode's, ZCode's, OMP's, Muse's), while #92 had been promised the name as written. Both are kept: the setting is now Off / Not on names I set (自定义名称不带供应商) / On. On stays the default and keeps #203's behaviour, so nobody's list changes unasked; own leaves a name the user gave as it is and a vendor's name with its provider's; off is #335's plain names. Stored as plainOwnNames beside plainNames, so a settings.json of before reads as it did (plainNames: true is off). POST /api/settings/plain-names takes mode on|own|off (a body of on alone still works), magpie model suffix on|own|off, a Settings save keeps it, and /v1/models' magpie_label now follows the setting through Labels as the agents' lists do. The row's hint is one line that fits (en, zh), and its zh name says 供应商 as the rest of the app. Prompts and headers are unchanged. TestSuffixMode, TestSuffixModesReachAgentFiles (Codex model_catalog_json and Pi models.json), TestModelsListSuffixModes (/v1/models magpie_label) and model-suffix-own.test.cjs (Chromium and WebKit, en and zh) fail without the change; TestModelName and TestModelNameAndLevelsReachAgentFiles keep #203's assertions. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass (TestConfiguredModelsAdvertiseImageInput load-flaky, passes alone); plain-names, click-scroll, old-webkit pass.
2026-10-01 23:20:57 +08:00

70 lines
2.2 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// /v1/models' magpie_label names a model as the agents' lists do, by the
// Provider in model names setting: on, as by default, a name the user gave
// a model has its provider's after it like any other (#203); own, that
// name is just as they wrote it and the vendor's keep theirs (#92: "Opus
// 5.5", not "Opus 5.5 · Claude Code"); off, none has it (#335).
// display_name is the name alone throughout.
func TestModelsListSuffixModes(t *testing.T) {
fresh(t)
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
data := `{"a":{"models":{"sol":{"id":"sol","name":"Sol"}}},"b":{"models":{"luna":{"id":"luna","name":"Luna"}}}}`
if err := os.WriteFile(catalog.CachePath(), []byte(data), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
for id, m := range map[string]string{"a": "sol", "b": "luna"} {
if err := provider.Save(provider.Provider{ID: id, Name: "Vendor " + strings.ToUpper(id), Catalog: id, Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{m}}); err != nil {
t.Fatal(err)
}
}
if err := provider.SetModelName("a/sol", "Sol 5"); err != nil {
t.Fatal(err)
}
list := func() map[string]string {
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models", nil))
var out struct {
Data []struct {
ID string `json:"id"`
Name string `json:"display_name"`
Label string `json:"magpie_label"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
got := map[string]string{}
for _, m := range out.Data {
got[m.ID] = m.Name + "|" + m.Label
}
return got
}
for _, c := range []struct{ mode, sol, luna string }{
{provider.SuffixOn, "Sol 5|Sol 5 · Vendor A", "Luna|Luna · Vendor B"},
{provider.SuffixOwn, "Sol 5|Sol 5", "Luna|Luna · Vendor B"},
{provider.SuffixOff, "Sol 5|Sol 5", "Luna|Luna"},
} {
if err := provider.SetSuffixMode(c.mode); err != nil {
t.Fatal(err)
}
if got := list(); got["a/sol"] != c.sol || got["b/luna"] != c.luna {
t.Fatalf("%s: %v", c.mode, got)
}
}
}