Files
magpie/internal/gateway/antigravity_pool_test.go
T
yetone 7e0fe83d4b provider: Listed as works for signed-in accounts and plugins (zeewell on X)
zeewell asked to list SuperGrok's grok/grok-4.6@500k as grok-4.6, and
Cursor, Devin and WorkBuddy models (all plugins) under ids of their own.
v0.1.2345's "Listed as" took ids only for key/relay providers
(takesModelIDs was p.Account == nil).

Why the restriction was there: the wire rewrite (Settings.Wires) turns
the request body's model back into the own id, but some code reads the
model by the vendor's id before that and got the listed one:
- plugin model lookup (pluginModel: the plugin's APIs, tiers, prices);
- the Claude subscription run (--model given to Claude Code);
- Code Assist/Antigravity variant choice (codeAssistBody, SentNameOnIn);
- account plan lists (fallback's Account.Lists, effort Levels/Tiers,
  AccountServes);
- moveModelRefs refused a provider with no providers.json entry, which
  is every plugin and account provider until a pick is set.
Each now translates with OwnModelID / the provider's ModelIDs, so the
upstream request still carries the vendor's or plugin's own id. Only a
decide-only provider refuses an id. The GUI box already follows
TakesModelIDs, so no GUI file changed.

Tests (red on the old code, each fix reverted by hand one at a time):
- TestModelIDRefused, TestModelIDAtAnAccount (takesModelIDs,
  AccountServes, SentNameOnIn);
- TestLocalModelIDAtAPlugin (Bun fake plugin: /v1/models, upstream
  path, X-Plugin-Model, body, usage; red on pluginModel and on
  moveModelRefs);
- TestLocalModelIDAtAnAntigravityAccount (codeAssistBody);
- TestLocalModelIDAtAClaudeAccount (claude got --model my-sonnet);
- antigravity pool test (fallback Lists, Levels).

Run on mote: gofmt clean; go vet darwin/linux/windows ok; go test
-tags nogui -count=1 -timeout 45m on internal/provider, gateway, cli,
gui, settings, access, usage, plugin: all ok except internal/gui's
TestSettingsSaveKeepsWhatItDoesNotSend (SearchAll), red on main since
b09349d17 and filed as #4514. After rebasing on 140efc8f7 (nothing new
under provider/gateway) the ModelID/LocalModelID/Antigravity tests
ran again: ok.

Real accounts, sandbox HOME, dev build on 127.0.0.1:3486: magpie model
id set grok/grok-4.6@500k -> grok-4.6, grok/grok-4.5@500k -> g45big,
cursor/composer-2.5 -> my-composer, devin/swe-2 -> my-swe,
workbuddy/deepseek-v4.1-flash -> ds-flash. /v1/models listed the new
ids. Requests for grok/grok-4.6, grok/g45big, cursor/my-composer and
devin/my-swe each answered 200 "pong", served as the vendor's id
(grok-4.5-build for g45big); usage records the listed id as model.
Plugin tokens were not rotated (no diff with live).

Not tried: WorkBuddy's real request (its own sign-in answers 401,
unrelated); the GUI box not clicked; no full ./... suite, no GUI suite
(no GUI file touched).
2026-10-31 18:38:31 +08:00

92 lines
3.2 KiB
Go

package gateway
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// Discovery and routing agree when a new model is available only to a
// secondary account, including the family id clients actually request.
func TestAntigravityPoolRoutesDiscoveredModels(t *testing.T) {
fresh(t)
var logins []map[string]any
for _, user := range []string{"old", "new"} {
logins = append(logins, map[string]any{
"agent": "antigravity", "user": user + "@example.com", "on": true,
"auth": map[string]any{"access_token": user, "refresh_token": "pool-test-" + user,
"project": "project-" + user, "expiry_date": time.Now().Add(time.Hour).UnixMilli()},
})
}
dir := filepath.Dir(provider.Path())
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "logins.json"), mustJSON(logins), 0o600); err != nil {
t.Fatal(err)
}
old := http.DefaultClient
http.DefaultClient = &http.Client{Transport: countTransport(func(r *http.Request) (*http.Response, error) {
var body string
switch {
case strings.HasSuffix(r.URL.Path, "latest-arm64-mac.yml"):
body = "version: 2.9.1\n"
case strings.HasSuffix(r.URL.Path, ":fetchAvailableModels"):
if r.Header.Get("Authorization") == "Bearer old" {
body = `{"models":{"claude-opus-4-6-thinking":{}}}`
} else {
body = `{"models":{"claude-opus-5-5-high":{},"claude-opus-5-5-low":{}}}`
}
default:
return nil, fmt.Errorf("unexpected request to %s", r.URL)
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body))}, nil
})}
t.Cleanup(func() { http.DefaultClient = old })
p, err := provider.Find("antigravity")
if err != nil {
t.Fatal(err)
}
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
for model, user := range map[string]string{
"claude-opus-5-5": "new@example.com", "claude-opus-5-5-high": "new@example.com",
"claude-opus-4-6-thinking": "old@example.com",
} {
on, aside, left := perKeyOf(*p, model, provider.Chat)
if len(on) != 1 || on[0].p.Account.User != user || len(aside) != 0 || len(left) != 1 {
t.Errorf("%s: eligible=%d, aside=%d, not listed=%d; expected only %s", model, len(on), len(aside), len(left), user)
}
if model == "claude-opus-5-5" && len(on) == 1 {
if !takesEffort(on[0], "high") || takesEffort(on[0], "xhigh") {
t.Error("routing did not use the account's own reasoning levels")
}
}
}
// listed by an id of the user's own (SetModelID), the model is still
// the one each account's own list names, levels and all
s := settings.Load()
s.ModelIDs = map[string]string{"antigravity/my-opus": "claude-opus-5-5"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
on, _, left := perKeyOf(*p, "my-opus", provider.Chat)
if len(on) != 1 || on[0].p.Account.User != "new@example.com" || len(left) != 1 {
t.Fatalf("my-opus: eligible=%d, not listed=%d; expected only new@example.com", len(on), len(left))
}
if !takesEffort(on[0], "high") || takesEffort(on[0], "xhigh") {
t.Error("my-opus: routing did not use the account's own reasoning levels")
}
}