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 (7ddea2c2covers 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.
136 lines
4.5 KiB
Go
136 lines
4.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// magpie accounts add, signing in to a plugin's provider removed from
|
|
// magpie, brings it back, as the window's sign-in and a built-in's do.
|
|
func TestAccountsAddBringsBackARemovedPlugin(t *testing.T) {
|
|
ctx := fakeCoHome(t)
|
|
if _, err := provider.PluginAPIKey(ctx, "fakeco", 0, nil, "sk-one-key-1111"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.Delete("fakeco"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := provider.Find("fakeco"); err == nil {
|
|
t.Fatal("removed provider still listed")
|
|
}
|
|
// the API key way (1), then the key
|
|
piped(t, "1", "sk-two-key-2222")
|
|
if out, err := said(t, func() error { return accountsCmd([]string{"accounts", "add", "fakeco"}) }); err != nil {
|
|
t.Fatalf("%v: %s", err, out)
|
|
}
|
|
if _, err := provider.Find("fakeco"); err != nil {
|
|
t.Fatalf("signed in again, but not listed: %v", err)
|
|
}
|
|
}
|
|
|
|
// magpie plugin login, signing in again to an account whose sign-in the
|
|
// vendor turned away, takes its lapsed mark off, as the window's sign-in
|
|
// does, rather than the account saying "sign in again" until a request or
|
|
// a usage read goes through.
|
|
func TestPluginLoginTakesTheLapseOff(t *testing.T) {
|
|
ctx := fakeCoHome(t)
|
|
noBrowser(t)
|
|
// the browser's way (2), at work (2) on the team "gone", whose usage
|
|
// says its sign-in has expired, then the code the page shows
|
|
login := func() {
|
|
t.Helper()
|
|
piped(t, "2", "2", "gone", "good")
|
|
if out, err := said(t, func() error { return pluginCmd([]string{"plugin", "login", "fakeco"}) }); err != nil {
|
|
t.Fatalf("%v: %s", err, out)
|
|
}
|
|
}
|
|
login()
|
|
provider.LoginUsage(ctx, "fakeco")
|
|
if ls := provider.Logins("fakeco"); len(ls) != 1 || ls[0].Lapsed == "" {
|
|
t.Fatalf("the vendor's refusal didn't mark the account: %+v", ls)
|
|
}
|
|
login()
|
|
if ls := provider.Logins("fakeco"); len(ls) != 1 || ls[0].Lapsed != "" {
|
|
t.Fatalf("signed in again, but still lapsed: %+v", ls)
|
|
}
|
|
}
|
|
|
|
// An account signed in again is read afresh: the reading kept from the
|
|
// sign-in the vendor refused doesn't answer for the new one, saying its
|
|
// sign-in has expired for up to a minute after it was renewed.
|
|
func TestPluginLoginReadsUsageAfresh(t *testing.T) {
|
|
var status atomic.Int32
|
|
status.Store(http.StatusUnauthorized)
|
|
vendor := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(int(status.Load()))
|
|
fmt.Fprint(w, "Fake Pro")
|
|
}))
|
|
defer vendor.Close()
|
|
t.Setenv("FAKE_USAGE", vendor.URL)
|
|
ctx := fakeCoHome(t)
|
|
noBrowser(t)
|
|
login := func() {
|
|
t.Helper()
|
|
piped(t, "2", "2", "team", "good")
|
|
if out, err := said(t, func() error { return pluginCmd([]string{"plugin", "login", "fakeco"}) }); err != nil {
|
|
t.Fatalf("%v: %s", err, out)
|
|
}
|
|
}
|
|
login()
|
|
if q := provider.LoginUsage(ctx, "fakeco")["team@fake"]; q.Error == "" {
|
|
t.Fatalf("the vendor's 401 read clean: %+v", q)
|
|
}
|
|
status.Store(http.StatusOK)
|
|
login()
|
|
if q := provider.LoginUsage(ctx, "fakeco")["team@fake"]; q.Error != "" || q.Plan != "Fake Pro" {
|
|
t.Fatalf("signed in again, but the usage is the old sign-in's: %+v", q)
|
|
}
|
|
}
|
|
|
|
// fakeCoHome is a HOME of the test's own with the plugin host's test
|
|
// plugin (FakeCo) added, as TestAccountsOfAPlugin sets one up.
|
|
func fakeCoHome(t *testing.T) context.Context {
|
|
t.Helper()
|
|
bun := testenv.Bun(t)
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(home, ".claude"))
|
|
t.Setenv("MAGPIE_BUN", bun)
|
|
t.Cleanup(plugin.Settle)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
t.Cleanup(cancel)
|
|
abs, _ := filepath.Abs("internal/plugin/testdata/fake/index.js")
|
|
if _, err := plugin.Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := plugin.Providers(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
// noBrowser has the sign-in pages magpie opens go nowhere. It swaps the
|
|
// opener rather than putting a do-nothing open first on PATH: a script
|
|
// written now is a program macOS checks on its first run, which took 25 to
|
|
// 55 seconds while other test binaries ran, and ate the minute the plugin
|
|
// calls have (#1284).
|
|
func noBrowser(t *testing.T) {
|
|
t.Helper()
|
|
old := openInBrowser
|
|
openInBrowser = func(string) {}
|
|
t.Cleanup(func() { openInBrowser = old })
|
|
}
|