Files
magpie/internal/provider/migrate_ux_test.go
T
yetone 2da8fbe488 tests: every test that runs plugins on the bun on PATH runs it once first, through testenv.Bun, so a bun that has never run here doesn't time out internal/provider's and internal/plugin's plugin tests (#1306)
65dcc573 (ggbdpq's patch 1) warmed the bun on PATH before the root
package's one-minute plugin contexts. The same lookup of the bun on PATH
is in 40 more places in internal/provider (28), internal/gateway (8),
internal/plugin (3) and internal/gui (1), and in the root package's
TestPluginListSaysClash. Many of them then open a one- or two-minute
context (internal/provider's sign-in, fetch and move tests,
internal/plugin's sandbox), and there macOS's first-exec check of a
just-installed or upgraded bun landed inside the deadline. The rest
are covered the same way, so no test has to remember which is which.

testenv.Bun now does what the root package's pathBun did: it finds the
bun on PATH (skipping the test when there is none) and runs
`bun --version` before the test opens its context, once per bun per test
binary. pathBun is gone; every `exec.LookPath("bun")` + skip block in
the tests uses testenv.Bun instead. Three tests reach for the bun some
other way and are left as they were: TestHostRunsAsMagpieBun (7ddea2c2
covers its copy), the plugin_git test's realBun and netproxy's
TestBunThroughBridge, none of which runs the plugin host under a
deadline on the PATH bun. migrate_test.go needed two `err` declarations
that the removed block used to provide.

What was run (macOS arm64, bun 1.4.2), with a wrapper bun first on PATH
whose first exec sleeps 61s (marker-gated, then execs the real bun):
- Red, old code:
  - internal/provider TestPluginSignInTakesAPastedCallback: FAIL at
    61.1s, "pluginsignin_paste_test.go:36: starting plugins: context
    deadline exceeded".
  - internal/plugin TestFakePlugin: FAIL at 62.0s, "plugin_test.go:66:
    Plugins = [], starting plugins: context deadline exceeded".
  - internal/gateway TestPluginFailover passed on the old code too (its
    calls have no deadline that short), so the gateway is not shown red.
- Green, this change, same cold wrapper: the three tests passed (61.1s,
  62.9s, 61.2s).
- go test -tags nogui -count=1 for the root package and for
  internal/testenv, gateway, gui, plugin and provider, under a temp HOME
  with GOMODCACHE, GOCACHE and GOPATH pinned: all ok.
- gofmt -l on the changed files: clean. go vet of those packages for
  darwin (-tags nogui), GOOS=linux and GOOS=windows (-tags nogui), and
  GOOS=windows without the tag: clean.

Not run: a really cold bun on a loaded machine; the sleep stands in for
the check. Not run on Linux or Windows, where the warm-up is one extra
`bun --version` per test binary.
2026-10-10 10:22:59 +08:00

257 lines
9.2 KiB
Go

package provider
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/testenv"
"github.com/yetone/magpie/internal/zed"
)
// fakePlugin readies the test plugin's host for a move of fakeco, with
// the plugin not yet installed: the move's install adds it.
func fakePlugin(t *testing.T, inUse *[]string) (context.Context, string, *int) {
t.Helper()
bun := testenv.Bun(t)
claudeHome(t)
t.Setenv("MAGPIE_BUN", bun)
t.Cleanup(plugin.Settle)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
t.Cleanup(cancel)
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
fakeMover(t, inUse)
movers["fakeco"].pkg = abs
installs := 0
installPlugin = func(ctx context.Context, pkg, _ string) error {
installs++
if pluginListed(pkg) {
return nil
}
_, err := plugin.Add(ctx, pkg)
return err
}
return ctx, abs, &installs
}
func setFakeLogins(t *testing.T, ls ...savedLogin) {
t.Helper()
loginsMu.Lock()
defer loginsMu.Unlock()
if err := writeLogins(ls); err != nil {
t.Fatal(err)
}
}
// Every account lapsed: there's nothing to try the plugin with, so the
// move says so before installing anything.
func TestMoveNeedsAWorkingAccount(t *testing.T) {
claudeHome(t)
inUse := []string{"fake-1"}
fakeMover(t, &inUse)
installs := 0
installPlugin = func(context.Context, string, string) error { installs++; return nil }
l := fakeLogin("a@fake", "r-a", true, true)
l.Lapsed = "expired"
setFakeLogins(t, l)
err := Move(context.Background(), "fakeco")
if w := WhyOf(err); w == nil || w.Code != "lapsed" || installs != 0 {
t.Fatalf("Move with every account lapsed = %v (why %+v), %d installs", err, w, installs)
}
if m, _ := MigrationOf("fakeco"); m.State != MoveFailed || m.Why == nil || m.Why.Code != "lapsed" {
t.Fatalf("the failed move's record: %+v", m)
}
}
// A move that installed the plugin and then failed takes it away again,
// leaving no plugin provider saying it's signed out; one that got through
// keeps it, and going back takes it away when it serves nothing else. A
// plugin the user had installed stays either way.
func TestMoveTidiesItsPlugin(t *testing.T) {
inUse := []string{"fake-1", "gone-model"}
ctx, abs, _ := fakePlugin(t, &inUse)
setFakeLogins(t, fakeLogin("a@fake", "r-a", true, true))
err := Move(ctx, "fakeco")
if w := WhyOf(err); w == nil || w.Code != "unserved" || !strings.Contains(err.Error(), "gone-model") {
t.Fatalf("Move short of a model = %v (why %+v)", err, w)
}
if pluginListed(abs) {
t.Fatal("a failed move left the plugin it installed")
}
if s := fakeSaved(t); fakeToken(s["a@fake"]) != "r-a" {
t.Fatalf("the built-in's account after a failed move: %+v", s)
}
inUse = []string{"fake-1"}
if err := Move(ctx, "fakeco"); err != nil {
t.Fatal(err)
}
if m, _ := MigrationOf("fakeco"); !m.Installed || !pluginListed(abs) {
t.Fatalf("after moving: installed %v, listed %v", m.Installed, pluginListed(abs))
}
if err := MoveBack(ctx, "fakeco"); err != nil {
t.Fatal(err)
}
if pluginListed(abs) {
t.Fatal("moving back left the plugin the move installed")
}
if s := fakeSaved(t); len(s) != 1 || !strings.HasPrefix(fakeToken(s["a@fake"]), "r-a") {
t.Fatalf("the built-in's account moved back: %+v", s)
}
// the user's own plugin: a failed move leaves it, as does going back
if _, err := plugin.Add(ctx, abs); err != nil {
t.Fatal(err)
}
inUse = []string{"gone-model"}
if err := Move(ctx, "fakeco"); err == nil || !pluginListed(abs) {
t.Fatalf("a failed move onto the user's plugin: %v, listed %v", err, pluginListed(abs))
}
inUse = []string{"fake-1"}
os.Remove(migrationsPath())
if err := Move(ctx, "fakeco"); err != nil {
t.Fatal(err)
}
if err := MoveBack(ctx, "fakeco"); err != nil || !pluginListed(abs) {
t.Fatalf("moving back off the user's plugin: %v, listed %v", err, pluginListed(abs))
}
}
// The move proves each account through the vendor: one whose usage read
// the vendor refuses goes along marked and untried, as a lapsed one does,
// and the move fails when that leaves none tried; one the plugin couldn't
// reach its vendor for (its models hook keeping a list of its own) stops
// the move, as nothing proved it works.
func TestMoveProvesEachAccount(t *testing.T) {
inUse := []string{"fake-1"}
ctx, abs, _ := fakePlugin(t, &inUse)
if _, err := plugin.Add(ctx, abs); err != nil {
t.Fatal(err)
}
setFakeLogins(t, fakeLogin("z@fake", "r-gone", true, true))
if err := Move(ctx, "fakeco"); WhyOf(err) == nil || WhyOf(err).Code != "lapsed" || Moved("fakeco") {
t.Fatalf("Move with its one account refused = %v", err)
}
setFakeLogins(t, fakeLogin("a@fake", "r-a", true, true), fakeLogin("z@fake", "r-unreachable", false, true))
err := Move(ctx, "fakeco")
w := WhyOf(err)
if w == nil || w.Code != "account" || w.Args["user"] != "z@fake" || !strings.Contains(err.Error(), "couldn't reach FakeCo") {
t.Fatalf("Move with the vendor unreachable = %v (why %+v)", err, w)
}
if Moved("fakeco") || plugin.SignedIn("fakeco") || len(fakeSaved(t)) != 2 {
t.Fatal("a move with an account unproven moved")
}
loginsMu.Lock()
for _, l := range readLogins() {
if l.Agent == "plugin:fakeco" {
t.Errorf("a failed move left the plugin's row of %s", l.User)
}
}
loginsMu.Unlock()
setFakeLogins(t, fakeLogin("a@fake", "r-a", true, true), fakeLogin("z@fake", "r-gone", false, true))
if err := Move(ctx, "fakeco"); err != nil {
t.Fatalf("an account refused stopped the move: %v", err)
}
for _, l := range pluginLogins(mustPlugin(t)) {
if (l.User == "z@fake") != (l.Lapsed != "") {
t.Fatalf("moved %s: lapsed %q", l.User, l.Lapsed)
}
}
}
// A Zed account's list goes to the plugin with it, and comes back with it:
// the plugin serves the account's own models until it reads them, not the
// families it declares for a sign-in with none.
func TestZedMoveCarriesItsModels(t *testing.T) {
claudeHome(t)
list := `{"models":[{"provider":"anthropic","id":"claude-sonnet-4-5","display_name":"Claude Sonnet 4.5"}]}`
setFakeLogins(t, savedLogin{Agent: "zed", User: "z@example.com", On: true, First: true,
Auth: []byte(`{"userId":"u1","accessToken":"a","models":` + list + `}`)})
out, err := movers["zed"].out()
if err != nil || len(out) != 1 {
t.Fatal(out, err)
}
var r struct {
Models json.RawMessage `json:"models"`
}
if json.Unmarshal([]byte(str(out[0].Auth["refresh"])), &r) != nil || !strings.Contains(string(r.Models), "claude-sonnet-4-5") {
t.Fatalf("the refresh carries no list: %v", out[0].Auth["refresh"])
}
ls, _, err := movers["zed"].back(nil, "z@example.com", out[0].Auth)
if err != nil {
t.Fatal(err)
}
c, _ := zedSaved(ls[0])
if ms, err := zed.ParseModels(c.Models); err != nil || len(ms) != 1 || ms[0].ID != "claude-sonnet-4-5" {
t.Fatalf("back without its list: %s %v", c.Models, err)
}
}
// A moved provider shows the built-in's API host, which the move keeps,
// not plugin://<id>'s: "" for one that had none, as Zed's.
func TestMovedShowsBuiltinHost(t *testing.T) {
claudeHome(t)
pp := plugin.Provider{ID: "factory", Models: []plugin.Model{{ID: "m"}}}
if h := pluginProvider(pp, pluginLogin{}).Host(); h != "factory" {
t.Fatalf("a plugin's own provider: %q", h)
}
// the host the move kept, else (moved before it kept one) the built-in's
for host, want := range map[string]string{"llm.factory.test": "llm.factory.test", "": HostOf(factoryAPI)} {
_ = setMigration("factory", func(m *Migration) { *m = Migration{State: MovePlugin, Host: host} })
p := pluginProvider(pp, pluginLogin{})
if h := p.Host(); h != want || want == "" {
t.Fatalf("moved: %q, want %q", h, want)
}
if w := p.Where(); w != want {
t.Fatalf("moved calls go to %q, want %q", w, want)
}
}
// one that showed no host shows none
_ = setMigration("devin", func(m *Migration) { *m = Migration{State: MovePlugin} })
if h := pluginProvider(plugin.Provider{ID: "devin"}, pluginLogin{}).Host(); h != "" {
t.Fatalf("moved devin: %q", h)
}
}
// A built-in with no account signed in goes onto its plugin as it is: the
// plugin installed and the subscription its from then on, so the first
// sign-in is the plugin's. Going back takes the plugin it installed away.
// One with an account is moved.
func TestAdoptBeforeSigningIn(t *testing.T) {
inUse := []string{"fake-1"}
ctx, abs, installs := fakePlugin(t, &inUse)
if err := Adopt(ctx, "fakeco"); err != nil {
t.Fatal(err)
}
if m, _ := MigrationOf("fakeco"); m.State != MovePlugin || !m.Installed || !pluginListed(abs) || *installs != 1 {
t.Fatalf("adopted: %+v, listed %v, %d installs", m, pluginListed(abs), *installs)
}
if PluginID("fakeco") != "fakeco" {
t.Fatalf("the plugin's provider is %s", PluginID("fakeco"))
}
if err := Adopt(ctx, "fakeco"); err != nil || *installs != 1 {
t.Fatalf("adopting again: %v, %d installs", err, *installs)
}
if err := MoveBack(ctx, "fakeco"); err != nil {
t.Fatal(err)
}
if Moved("fakeco") || pluginListed(abs) {
t.Fatalf("back: moved %v, listed %v", Moved("fakeco"), pluginListed(abs))
}
setFakeLogins(t, fakeLogin("a@fake", "r-a", true, true))
if err := Adopt(ctx, "fakeco"); err != nil {
t.Fatal(err)
}
if !Moved("fakeco") || !plugin.SignedIn("fakeco") || len(fakeSaved(t)) != 0 {
t.Fatalf("adopting one signed in didn't move it: moved %v, saved %+v", Moved("fakeco"), fakeSaved(t))
}
}