Files
magpie/internal/provider/plugin_served_models_test.go
T
yetone 72109ea0eb provider: a plugin provider offers the models of every account switched on, not only the first's (#2554, willywang; LoKi)
pluginAccounts built the provider's catalog from its first login only, so
a model only a later account serves never showed or reached an agent.
Routing already sends each request to an account whose own list has the
model (perKeyBarred, Account.Lists), so the provider now offers the union
of the first account and every other account switched on, in catalog
order. An account switched off adds nothing; an account with no list of
its own means every model, as before. Reported by willywang, confirmed
again on v0.1.1320 by LoKi (lokic7123-star).

Run at the stacked tree (this commit with #2695 and #2862, on ef6f3749d):
- gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui, and
  GOOS=windows: ok.
- set -o pipefail; go test -count=1 -timeout 45m ./... under the suite
  lock: every package ok (SUITE_EXIT 0).
Red on the old code: TestPluginProviderOffersEveryOnAccountsModels, "the
provider offers \"a\", want \"a b\"". With the On check removed it offers
"a b c" and goes red too.
Not tried: real multi-account plugin sign-ins; the GUI's model list was
not looked at in the app.
2026-10-22 20:51:08 +08:00

88 lines
2.8 KiB
Go

package provider
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// A plugin provider offers the models any account switched on serves,
// not the first account's alone: a model only a later account lists is
// in the provider's catalog, and an account switched off adds nothing
// (#2554, willywang; LoKi saw it again on 0.1.1320). Each account keeps
// its own list for routing.
func TestPluginProviderOffersEveryOnAccountsModels(t *testing.T) {
claudeHome(t)
t.Setenv("MAGPIE_BUN", "")
spec := "@example/opencode-acme-auth"
write := func(name string, v any) {
t.Helper()
b, _ := json.Marshal(v)
os.MkdirAll(settings.Dir(), 0o700)
if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil {
t.Fatal(err)
}
}
write("plugins.json", map[string]any{"plugins": []map[string]any{{"spec": spec}}})
write("plugin-auth.json", map[string]map[string]any{
"acme": {"type": "api", "key": "sk-acme-0001"},
"acme#2": {"type": "api", "key": "sk-acme-0002"},
"acme#3": {"type": "api", "key": "sk-acme-0003"},
})
plugin.UseCached([]plugin.Provider{{ID: "acme", Spec: spec, Name: "Acme",
Methods: []plugin.Method{{Type: "api", Label: "API key"}},
Accounts: []plugin.Account{
{Key: "acme", Type: "api", Models: []string{"a"}},
{Key: "acme#2", Type: "api", Models: []string{"a", "b"}},
{Key: "acme#3", Type: "api", Models: []string{"c"}},
},
Models: listedModels(t, `[{"id": "a", "name": "A"}, {"id": "b", "name": "B"}, {"id": "c", "name": "C"}]`),
}})
t.Cleanup(func() { plugin.UseCached(nil) })
saveLogins(t,
savedLogin{Agent: "plugin:acme", User: "one", Home: "acme", On: true},
savedLogin{Agent: "plugin:acme", User: "two", Home: "acme#2", On: true},
savedLogin{Agent: "plugin:acme", User: "three", Home: "acme#3"},
)
ids := func(p Provider) string {
var out []string
for _, m := range p.Available() {
out = append(out, m.ID)
}
return strings.Join(out, " ")
}
ps := pluginAccounts()
i := slices.IndexFunc(ps, func(p Provider) bool { return p.ID == "acme" })
if i < 0 {
t.Fatalf("no acme provider in %v", ps)
}
if got := ids(ps[i]); got != "a b" {
t.Fatalf("the provider offers %q, want %q", got, "a b")
}
if !ps[i].Account.Lists("a") || ps[i].Account.Lists("b") {
t.Fatal("the first account routes past its own list")
}
per := map[string]string{}
for _, l := range pluginLogins(mustCached(t, "acme")) {
per[l.acct.Key] = ids(pluginProvider(mustCached(t, "acme"), l))
}
if per["acme"] != "a" || per["acme#2"] != "a b" || per["acme#3"] != "c" {
t.Fatalf("each account's own models: %v", per)
}
}
func mustCached(t *testing.T, id string) plugin.Provider {
t.Helper()
pp, ok := PluginOf(id)
if !ok {
t.Fatalf("no plugin %s", id)
}
return pp
}