Files
magpie/internal/agent/claude_alias_test.go
T
yetone 96367cb8f4 gui: Claude Code's model picker shows each model once (#496) — the rows magpie serves on the very Claude account Claude Code is signed in to fold into one, and models.dev's alias and dated id of one model are one row
Claude Code's picker had 37 rows where Codex's has 18. The Claude subscription added in magpie is, while Claude Code is signed in, the account Claude Code asks on its own, and both groups are titled "Claude Code", so every model showed twice (claude-opus-5-5, then "Claude Opus 5.5 · <account> · via magpie"); and models.dev lists claude-opus-4-5 and claude-opus-4-5-20251101 (haiku-4-5, sonnet-4-5 too) as two models. The via-magpie rows stay, as some use them (the [1m] fix, failover to their other accounts): viaMagpie now knows which provider is the agent's own sign-in (not a stand-in for a signed-out Claude Code, not paused; provider.Account.StandIn is new), and Claude Code's model field marks those options Same while its own group goes straight to Anthropic (not under a base URL of the user's, not for the tiers or a WSL distro). The picker folds them into one row, "13 models via magpie · <account> · the account Claude Code is signed in to" with a chevron, folded each time the picker opens; a click or Enter opens or closes it in place, the row staying under the pointer and the page not moving; the value set stays a row of its own, and Favorites never folds. claudeDated marks a dated Claude id whose undated id is in the same group as that one's Alias (one dated id per name only; [1m] kept), and the picker shows one row for the two: the alias, which a pick writes as before, or the dated one while that is the value set, so a choice made before still shows as picked. The option lists themselves are unchanged, so Spell, Reseat, renames and profiles still find every id. New strings have zh translations.

claude-picker-fold (Chromium and WebKit, en and zh, API fulfilled in memory) fails without the change in all 8 subtests and passes with it; TestClaudeDatedAlias covers the marking. model-pick, model-suffix-own, claude-direct, favorite-star, agent-models, unlisted-models and plain-names pass in both engines, click-scroll in WebKit; Chromium here only starts as the headless shell (the full Chrome for Testing bundle dies on icudtl.dat), where click-scroll fails the same way with the change undone. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui ./internal/agent ./internal/provider ./internal/gui (sandbox HOME) pass.
2026-10-02 11:08:14 +08:00

126 lines
5.2 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
)
// TestClaudeAliasShown (StringKe on Discord): a settings.json saying
// "model": "sonnet", one of Claude Code's own aliases, showed as a bare
// "sonnet" with no logo and matched none of the models offered. The alias
// is now offered, first, as the model Claude Code takes it for — the
// catalog's newest Sonnet, or the model the user's env gives the tier —
// with Claude's logo, and the file is left as it is.
func TestClaudeAliasShown(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(`{"anthropic":{"models":{
"claude-sonnet-5-5":{"id":"claude-sonnet-5-5","name":"Claude Sonnet 5.5","release_date":"2026-09-28"},
"claude-opus-5-5":{"id":"claude-opus-5-5","name":"Claude Opus 5.5","release_date":"2026-09-22"},
"claude-sonnet-5":{"id":"claude-sonnet-5","name":"Claude Sonnet 5","release_date":"2026-06-29"},
"claude-haiku-4-5-20251001":{"id":"claude-haiku-4-5-20251001","name":"Claude Haiku 4.5","release_date":"2025-10-15"},
"claude-haiku-4-5":{"id":"claude-haiku-4-5","name":"Claude Haiku 4.5 (latest)","release_date":"2025-10-15"}
}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
path := filepath.Join(home, ".claude", "settings.json")
a := claude(home)
for _, c := range []struct{ file, label, note string }{
{`{"model":"sonnet"}`, "sonnet · claude-sonnet-5-5", "Claude Sonnet 5.5"},
{`{"model":"opus[1m]"}`, "opus[1m] · claude-opus-5-5[1m]", "Claude Opus 5.5"},
{`{"model":"best"}`, "best · claude-opus-5-5", "Claude Opus 5.5"},
{`{"model":"haiku"}`, "haiku · claude-haiku-4-5", "Claude Haiku 4.5 (latest)"},
{`{"model":"opusplan"}`, "opusplan · claude-opus-5-5 / claude-sonnet-5-5", "Claude Opus 5.5 / Claude Sonnet 5.5"},
{`{"model":"default"}`, "", ""},
// the user's own env moves the tier, and the alias with it
{`{"model":"sonnet","env":{"ANTHROPIC_DEFAULT_SONNET_MODEL":"claude-sonnet-5"}}`, "sonnet · claude-sonnet-5", "claude-sonnet-5"},
} {
writeFile(t, path, c.file)
f := a.Field("model")
v := f.Get()
opts := f.Options(a.Values())
if len(opts) == 0 || opts[0].Value != v {
t.Fatalf("%s: the first option is not %q: %+v", c.file, v, opts)
}
o := opts[0]
if o.Label != c.label || o.Note != c.note || o.Icon != "claude-color" || o.Group != "Claude Code" || o.Direct != "Anthropic" {
t.Fatalf("%s: %+v, want label %q, note %q, Claude's logo, straight to Anthropic", c.file, o, c.label, c.note)
}
n := 0
for _, x := range opts {
if x.Value == v {
n++
}
}
if n != 1 {
t.Fatalf("%s: %q offered %d times", c.file, v, n)
}
// read, never written
if b, _ := os.ReadFile(path); string(b) != c.file {
t.Fatalf("reading wrote the file: %s", b)
}
}
// an id is no alias: offered once, as ever
writeFile(t, path, `{"model":"claude-sonnet-5-5"}`)
opts := a.Field("model").Options(a.Values())
if opts[0].Value != "claude-sonnet-5-5" || opts[0].Label != "" || opts[0].Direct != "Anthropic" {
t.Fatalf("an id got an alias's option: %+v", opts[0])
}
// through another endpoint of the user's, it is that one Claude Code asks
writeFile(t, path, `{"model":"sonnet","env":{"ANTHROPIC_BASE_URL":"https://relay.example.test"}}`)
opts = a.Field("model").Options(a.Values())
if opts[0].Direct != "relay.example.test" {
t.Fatalf("a relay's model said to go to %q", opts[0].Direct)
}
// picking one in magpie writes it as before
if err := a.Field("model").Set("claude-sonnet-5-5"); err != nil {
t.Fatal(err)
}
if v, _ := edit.GetJSON(path, "model"); v != "claude-sonnet-5-5" {
t.Fatalf("model = %q", v)
}
}
// TestClaudeDatedAlias (#496): models.dev lists an alias and its dated id
// as two models (claude-opus-4-5, claude-opus-4-5-20251101), and the
// picker showed both. The dated one now names its alias, for the picker to
// show one row; through magpie too, the [1m] mark and all. Two dated ids
// of one name, or a dated one with no alias beside it, are left alone.
func TestClaudeDatedAlias(t *testing.T) {
opts := claudeDated([]Option{
{Value: "claude-opus-4-5", Group: "Claude Code"},
{Value: "claude-opus-4-5-20251101", Group: "Claude Code"},
{Value: "claude/claude-sonnet-4-5[1m]", Group: "Claude Code"},
{Value: "claude/claude-sonnet-4-5-20250929[1m]", Group: "Claude Code"},
{Value: "claude-3-5-sonnet-20240620", Group: "Claude Code"},
{Value: "claude-3-7-sonnet", Group: "Other"},
{Value: "claude-3-7-sonnet-20250219", Group: "Other"},
{Value: "claude-3-7-sonnet-20250301", Group: "Other"},
{Value: "claude-haiku-4-5-20251001", Group: "Other"},
{Value: "claude-haiku-4-5", Group: "Elsewhere"},
})
want := map[string]string{
"claude-opus-4-5-20251101": "claude-opus-4-5",
"claude/claude-sonnet-4-5-20250929[1m]": "claude/claude-sonnet-4-5[1m]",
}
for _, o := range opts {
if o.Alias != want[o.Value] {
t.Errorf("%s: alias %q, want %q", o.Value, o.Alias, want[o.Value])
}
}
}