Files
magpie/internal/gateway/desktop_test.go
T
yetoneandggbdpq 7660399a12 gui: a provider's Claude model chip says the number Claude Desktop's menu shows beside it (#2578)
jerrychoi (#2578) has two providers serving the same Claude models, and
Claude Desktop's menu tells them apart only by a 10-digit number. The
provider editor now shows that number on the model's chip.

From ggbdpq's patch (dcb00beb5), changed after reading desktop.go and
Desktop 2.7032's app.asar:
- Desktop renames a row to its own catalog's name ("Opus 5.5") only
  when the id resolves into that catalog, which is the
  magpie-<n>.anthropic.<model> form claudeLooking gives a Claude model
  with reasoning levels. Every other row is named by our display_name
  (which already says the provider) or by its own id, and its number
  never shows in the menu. The patch put a number on nearly every chip;
  DesktopNumber now returns one only for the .anthropic. form ("" for
  mythos-magpie-<n>, anthropic/magpie-<n>, own ids and groups).
- The number shows only while Claude Desktop is installed, and only for
  a model Desktop is shown (CatalogFor("claude-desktop"), not the whole
  catalog, so a model hidden from Desktop gets none).
- The numbers are worked out once per Providers page build, not once
  per provider.
- The GUI test runs en and zh at 900px and en at 420px in both engines
  and checks the badge sits inside its chip.
- docs/subsystems/agent-wiring.md says why the number is there.

The badge reuses the muted .badge.ctx pill beside the context size. Its
tooltip is in zh, zh-TW, ja and de (ggbdpq's strings).

Broken by hand, each watched red then restored:
- ggbdpq's DesktopNumber: TestDesktopNumber red for glm (mythos), glm
  (anthropic/magpie) and a group led by Claude; the GUI Go test red for
  m2.
- the installed check removed: red "without Claude Desktop,
  claude-opus-5-5 says desktop ...".
- provider.Catalog() in place of CatalogFor: red for the hidden haiku.
- j.Desktop dropped: red for claude-opus-5-5.
- app.js without the badge: model-desktop-number.test.cjs 0 passed.

Run (macOS arm64, sandbox HOME, pinned GOMODCACHE/GOCACHE):
- model-desktop-number.test.cjs 6/6 in chromium and webkit (en, zh at
  900px, en at 420px); screenshots checked: zh at 900px shows "Opus 5.5
  [number]" as a muted pill, Sonnet none.
- Go, this tree, as 1160f2e7d before the bodies were reworded (with the
  #2576 commit, on origin/main ebf3f48f7):
  gofmt -l clean; GOOS=darwin, linux and windows go vet -tags nogui
  ./... clean; go test -tags nogui -timeout 45m ./... under pipefail,
  exit 0, 66 packages ok. The same was green at 18828ea1a (on
  6f950653f) and d6881341d (on ed0335f75).
- rebased onto e085d6e92 (six commits, gateway.go and i18n.js among
  them, no conflict) after that run: gofmt and vet of internal/gateway and
  internal/gui clean, and their Models, Desktop, Science and
  ProviderModelsSayDesktopNumber tests ok; the full suite was not rerun
  there.
- GUI, both engines, on this tree: every file that reaches the model
  chips or failed in a whole-suite try, 54 files (349 tests per engine).
  Reds: Codex predictions at 420px (#2624, red on main alone);
  provider-advanced-fold's "Advanced grows open" (#2552, a sampling
  flake under load); and browser launches past 180s. Each of the others
  passed when its file was rerun alone (codex-accounts-via 4/4 in
  webkit; provider-draft-picks and provider-fetch-again in chromium).
- Not done: a whole GUI suite to the end in both engines. Two tries at
  load ~300 (14 cores) failed mostly on browser launches timing out at
  180s, and in one of them another session checked out origin/main in
  the worktree mid-run, so those results were set aside.
Not tried: a real Claude Desktop with two providers of one model, and
the [1m] rows.

Co-authored-by: ggbdpq <67929842+ggbdpq@users.noreply.github.com>
2026-10-17 13:11:43 +08:00

173 lines
7.3 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Claude Desktop asks for anthropic/<id>: served by the model with that id
func TestAnthropicPrefixedModel(t *testing.T) {
f := &fake{reply: sse(
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m","type":"message","role":"assistant","model":"m1","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}`,
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)}
setup(t, provider.Anthropic, f)
code, body := post(t, "/v1/messages", `{"model":"anthropic/fake/m1","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || f.calls != 1 {
t.Fatalf("anthropic/fake/m1: %d %s, %d calls", code, body, f.calls)
}
var sent struct{ Model string }
if json.Unmarshal(f.got, &sent) != nil || sent.Model != "m1" {
t.Fatalf("sent upstream as %q", f.got)
}
if code, body := post(t, "/v1/messages", `{"model":"anthropic/nobody/m1","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`); code != 404 {
t.Fatalf("unknown: %d %s", code, body)
}
for _, id := range []string{"fake/m1", "anthropic/fake/m1"} {
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models/"+id, nil))
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"fake/m1"`) {
t.Fatalf("GET /v1/models/%s: %d %s", id, rec.Code, rec.Body)
}
}
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("HEAD", "/api/hello", nil))
if rec.Code != 200 {
t.Fatalf("HEAD /api/hello: %d", rec.Code)
}
req := httptest.NewRequest("GET", "/v1/models", nil)
req.Header.Set("x-api-key", Token+"-claude-desktop")
rec = httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
if !strings.Contains(rec.Body.String(), `"id":"`+aliasFor("fake/m1")+`"`) {
t.Fatalf("Claude Desktop's list: %s", rec.Body)
}
if claudeLooking(provider.Entry{ID: "devin/claude-opus-5-5"}) != "devin/claude-opus-5-5" {
t.Fatal("a Claude id was prefixed")
}
if got := unprefixed("claude-sonnet-4-5"); got != "claude-sonnet-4-5" {
t.Fatalf("unprefixed changed %q", got)
}
}
// Claude Desktop turns away any id naming another vendor's model, prefix
// or not (#148): deepseek, kimi, gpt, codex… get an alias it keeps, which
// magpie serves again, and it is known by its User-Agent without the key
func TestClaudeDesktopAliases(t *testing.T) {
for id, want := range map[string]bool{
"claude-sonnet-4-5": true, "anthropic/claude-x": true, "opus": true, "sonnet-4.5": true,
"anthropic/deepseek/deepseek-flash": false, "anthropic/kimi-k2": false, "anthropic/gpt-5": false,
"anthropic/openai/codex-mini": false, "fake/m1": false, "deepseek/claude-distill": false,
} {
if desktopAccepts(id) != want {
t.Errorf("desktopAccepts(%q) = %v", id, !want)
}
}
for _, id := range []string{"deepseek/deepseek-flash", "moonshot/kimi-k2.5", "openai/gpt-5.5", "codex/gpt-5-codex", "zai/glm-5"} {
a := claudeLooking(provider.Entry{ID: id})
if !desktopAccepts(a) || !strings.HasPrefix(a, desktopAlias) || a != claudeLooking(provider.Entry{ID: id}) {
t.Errorf("%s listed as %s", id, a)
}
}
f := &fake{reply: sse(
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m","type":"message","role":"assistant","model":"m1","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}`,
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)}
setup(t, provider.Anthropic, f)
req := httptest.NewRequest("GET", "/v1/models?limit=1000", nil)
req.Header.Set("x-api-key", Token)
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Claude/2.9939.2 Chrome/138.0.0.0 Electron/37.0.0 Safari/537.36")
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
var list struct {
Data []desktopRow `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil {
t.Fatal(err)
}
alias := aliasFor("fake/m1")
var row *desktopRow
for i := range list.Data {
if list.Data[i].ID == alias {
row = &list.Data[i]
}
if !desktopAccepts(list.Data[i].ID) {
t.Errorf("Desktop would drop %q", list.Data[i].ID)
}
}
if row == nil || row.DisplayName != "m1 · Fake" || row.Description != "" {
t.Fatalf("Claude Desktop by its User-Agent: %s", rec.Body)
}
for _, asked := range []string{alias, alias + "[1m]"} {
f.calls = 0
code, body := post(t, "/v1/messages", `{"model":"`+asked+`","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || f.calls != 1 {
t.Fatalf("%s: %d %s", asked, code, body)
}
}
rec = httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("GET", "/v1/models/"+alias, nil))
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"fake/m1"`) {
t.Fatalf("GET /v1/models/%s: %d %s", alias, rec.Code, rec.Body)
}
if _, ok := aliased(desktopAlias + "0000000000"); ok {
t.Fatal("an alias no model has resolved")
}
}
// Claude Desktop set up by magpie sends the key magpie gave it, from its
// own client and from the Claude Code its Code tab runs alike; without it,
// its Electron User-Agent still says who it is.
func TestClaudeDesktopKnown(t *testing.T) {
for _, c := range []struct{ key, ua string }{
{TokenFor("claude-desktop"), "Mozilla/5.0 (Macintosh) Claude/2.7032.0 Chrome/138.0 Electron/37.0 Safari/537.36"},
{TokenFor("claude-desktop"), "claude-cli/2.1.200 (external, claude-desktop)"},
{"", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Claude/2.7032.0 Chrome/138.0 Electron/37.0 Safari/537.36"},
} {
r := httptest.NewRequest("GET", "/v1/models", nil)
r.Header.Set("User-Agent", c.ua)
if c.key != "" {
r.Header.Set("Authorization", "Bearer "+c.key)
}
if got := agentOf(r); got != "claude-desktop" {
t.Errorf("%q %q: %s", c.key, c.ua, got)
}
}
}
type desktopRow struct {
ID string `json:"id"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
}
// DesktopNumber is the number Claude Desktop's menu says beside a model:
// only one it names after its own catalog, a Claude model with reasoning
// levels (magpie-<n>.anthropic.<model>), so the Providers page says it
// there alone (#2578). A row Desktop names by display_name, which says the
// provider, or by the model's own id, gets none.
func TestDesktopNumber(t *testing.T) {
opus := provider.Entry{ID: "a/claude-opus-5-5", Model: "claude-opus-5-5", Efforts: []string{"low"}}
n := DesktopNumber(opus)
if n != aliasNumber(opus.ID) || claudeLooking(opus) != "magpie-"+n+".anthropic.claude-opus-5-5" {
t.Errorf("an effort Claude model: DesktopNumber = %q, listed as %s", n, claudeLooking(opus))
}
for _, e := range []provider.Entry{
// mythos-magpie-<n>, named by display_name
{ID: "a/glm-5.3", Model: "glm-5.3", Efforts: []string{"low"}},
// anthropic/magpie-<n>, named by display_name
{ID: "a/glm-5.3", Model: "glm-5.3"},
// its own id, which carries no number
{ID: "a/claude-opus-5-5", Model: "claude-opus-5-5"},
// a group led by a Claude model keeps its own name (mythos-magpie-<n>)
{ID: "group/opus", Model: "claude-opus-5-5", Group: "opus", Efforts: []string{"low"}},
} {
if got := DesktopNumber(e); got != "" {
t.Errorf("%s (listed as %s): DesktopNumber = %q, want none", e.ID, claudeLooking(e), got)
}
}
}