Files
magpie/internal/agent/plugin_sync_test.go
T
yetone 49639cd162 agents: a plugin signed in to, out of, or listing new models updates the agents' model files (#2373, 18565882223-beep)
After signing in to Trae CN (a plugin's subscription), Pi's models.json
kept the list it had before the sign-in, so Pi's picker had no trae-cn/*
model while settings.json named one. A plugin's sign-in change
(plugin.OnChange) only dropped what the gateway held; catalog.Changed
(SyncCatalog), which a provider saved runs, never ran for a plugin. Now
plugin.OnChange and a new plugin.OnListed (told when the host lists
providers/models other than the list kept, also across a restart) call
catalog.Touched, so every connected agent's own model list follows, Pi's
models.json among them, and a sign-out takes the models out again.

Not reproduced: the report's GUI pick leaving models.json's mtime
unchanged. GUI and CLI picks go through the same Agent.Pick, which writes
models.json in-process here. With this change the file is rewritten when
the plugin lists its models, not only on a pick. PiCode's providers.json
isn't wired by magpie at all; unchanged.

Tests:
- TestPluginSignInReachesPiModels (internal/agent): pick a model, sign
  Trae CN in, models.json lists trae-cn/kimi-k3-max; sign out, gone.
  Broken by hand (hold.go back to plugin.OnChange(Changed)): FAIL
  "models.json hasn't Trae CN's models after its sign-in".
- TestNewListIsTold (internal/plugin): a new list is told once, the same
  list or the one kept on disk is not. Broken by hand (listed() not
  called): FAIL "the first list: told 0 times, want 1".

Run at origin/main 9d97fd073 + this change and the #2373/#2372/#2374 commits beside it (one tree), -tags nogui -count=1
-timeout 45m: internal/plugin, provider, agent, catalog, gui,
gateway ok. gofmt -l clean; go vet -tags nogui ./... for GOOS darwin,
linux, windows clean.

Full suite: SUITE_LINE
2026-10-15 23:08:04 +08:00

76 lines
2.6 KiB
Go

package agent
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// A plugin's sign-in changing reaches the model lists agents keep in
// files of their own, as a provider saved does: it only dropped what
// requests held, so Pi's models.json went on without the models of a
// plugin signed in to after it was written, and Pi's picker had none of
// them while settings.json named one (#2373, Trae CN). Signed out, they
// go from the file, by the same hook.
func TestPluginSignInReachesPiModels(t *testing.T) {
home := syncHome(t)
t.Setenv("MAGPIE_BUN", "")
catalog.Changed = SyncCatalog
t.Cleanup(func() { plugin.Told(); catalog.Changed = nil })
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro"}}); err != nil {
t.Fatal(err)
}
spec := "@magpie-community/opencode-trae-auth"
write := func(name string, v any) {
t.Helper()
b, _ := json.Marshal(v)
if err := os.MkdirAll(settings.Dir(), 0o700); err != nil {
t.Fatal(err)
}
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{})
plugin.UseCached([]plugin.Provider{{ID: "trae-cn", Spec: spec, Name: "Trae CN",
Models: []plugin.Model{{ID: "kimi-k3-max", Name: "Kimi-K3 (Max)"}}}})
t.Cleanup(plugin.Settle)
p := pi(home)
if err := p.Pick("model", "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
plugin.Told()
models := filepath.Join(home, ".pi", "agent", "models.json")
lists := func() bool { return strings.Contains(readFile(models), `"trae-cn/kimi-k3-max"`) }
if lists() {
t.Fatalf("Trae CN listed before it was signed in to:\n%s", readFile(models))
}
// signed in: the host tells of it (plugin.OnChange) as it saves it,
// as Restart does here without a host
write("plugin-auth.json", map[string]map[string]any{"trae-cn": {"type": "oauth", "refresh": "r"}})
plugin.Restart()
plugin.Told()
if !lists() {
t.Fatalf("models.json hasn't Trae CN's models after its sign-in:\n%s", readFile(models))
}
if err := plugin.SignOut(context.Background(), "trae-cn", ""); err != nil {
t.Fatal(err)
}
plugin.Told()
if lists() {
t.Fatalf("models.json keeps Trae CN's models after its sign-out:\n%s", readFile(models))
}
}