Files
yetone 02f1cee794 plugin: a plugin installs the vendor CLI it signs in through (niuniu_8964 on X)
agy-cli signs in and serves requests through Google's agy CLI. In magpie's
Docker image there is none, so its sign-in failed with "agy CLI not found"
and the user had to get a shell in the container and install it by hand,
where it lived outside the volume. Claude's sign-in already installs
claude when it is missing ("Installing Claude Code…"); a plugin had no way
to say it needs a CLI.

A plugin now declares its CLI as auth.cli = {name, manual, installed(),
install({signal})}. The host lists it as Provider.CLI and answers two new
calls, cliInstalled and installCli (run through the provider's proxy, and
given up when magpie gives up). pluginCLI returns that CLI when it isn't
installed, so the plugin's sign-in goes through the same "installing"
state as Claude's: the Providers sheet shows "Installing Antigravity CLI…",
then the sign-in link; a failed install names its cause and the plugin's
manual command. `magpie plugin login` does the same in a terminal. An
answer magpie can't get (host down, a timeout) counts as installed, so a
plugin is never reinstalled for a failed read.

The agy-cli side (installing agy into the data folder, which is on the
container's volume, and keeping that copy current) is in
magpie-community/plugins.

Run, at this head (rebased on 60ebaea03), on mica:
- go test ./internal/cli ./internal/plugin ./internal/provider ./internal/gui: ok
- GOOS=linux (-tags nogui) and GOOS=windows go vet of plugin, provider, cli: ok; gofmt clean
- TestPluginSignInInstallsItsCLI red on the old pluginSignIn (no CLI found,
  straight to waiting), green now. TestPluginInstallsItsCLI covers the host
  calls: listing, a refused install, an install given up, a good one.

Real use, magpie's Dockerfile built on xiaomi-gpu (Docker Desktop, linux
amd64), volume on /config, agy-cli loaded from its working tree:
- `magpie plugin login agy-cli 1`: "Installing it…", agy 1.5.9 downloaded
  to /config/data/magpie-agy-cli/agy, then agy's Google sign-in link.
- restart: agy still there and runs.
- `magpie web` in the container, agy removed, POST /api/plugin-signin:
  state installing ("Antigravity CLI"), then waiting with the link.
- agy.version set back to 1.5.0 and restarted: the plugin updated its copy
  a minute later.

Not tried: finishing a Google sign-in in the container (no keyring there;
agy-cli's file sign-in is the path), a real install on macOS, Windows or a
musl Linux, and the full suite.
2026-11-15 14:52:48 +08:00

98 lines
2.8 KiB
Go

package plugin
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// A plugin that runs its vendor's CLI says how to install it (auth.cli):
// it is listed with the CLI's name and manual, magpie asks whether it is
// there and has the plugin install it, and gives up an install it no
// longer waits for (niuniu_8964 on X: agy in magpie's container).
func TestPluginInstallsItsCLI(t *testing.T) {
sandbox(t)
home := os.Getenv("HOME")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
for _, name := range []string{"cliinstall", "importer"} {
abs, _ := filepath.Abs("testdata/" + name)
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
}
ps, err := Providers(ctx)
if err != nil {
t.Fatal(err)
}
var cli, other *Provider
for i := range ps {
switch ps[i].ID {
case "cliinstall":
cli = &ps[i]
case "importer":
other = &ps[i]
}
}
if cli == nil || other == nil {
t.Fatalf("providers = %+v", ps)
}
if cli.CLI == nil || cli.CLI.Name != "Fake CLI" || cli.CLI.Manual != "curl -fsSL https://fake.invalid/install.sh | bash" {
t.Fatalf("cliinstall's CLI = %+v", cli.CLI)
}
if other.CLI != nil {
t.Errorf("a plugin with no auth.cli names one: %+v", other.CLI)
}
if _, err := CLIInstalled(ctx, "importer"); err == nil {
t.Error("a plugin with no auth.cli answered whether its CLI is installed")
}
if in, err := CLIInstalled(ctx, "cliinstall"); err != nil || in {
t.Fatalf("before the install: %v, %v", in, err)
}
os.WriteFile(filepath.Join(home, "cli-mode"), []byte("fail"), 0o600)
if err := InstallCLI(ctx, "cliinstall"); err == nil || !strings.Contains(err.Error(), "the download was refused") {
t.Fatalf("a failing install = %v", err)
}
// given up, the plugin's install hears of it and the call returns
os.WriteFile(filepath.Join(home, "cli-mode"), []byte("hang"), 0o600)
hctx, hcancel := context.WithCancel(ctx)
done := make(chan error, 1)
go func() { done <- InstallCLI(hctx, "cliinstall") }()
time.Sleep(200 * time.Millisecond)
hcancel()
select {
case err := <-done:
if err == nil {
t.Fatal("a cancelled install returned no error")
}
case <-ctx.Done():
t.Fatal("a cancelled install never returned")
}
for range 200 {
if _, err := os.Stat(filepath.Join(home, "cli-aborted")); err == nil {
break
}
time.Sleep(20 * time.Millisecond)
}
if _, err := os.Stat(filepath.Join(home, "cli-aborted")); err != nil {
t.Error("the plugin's install never heard it was given up")
}
os.Remove(filepath.Join(home, "cli-mode"))
if err := InstallCLI(ctx, "cliinstall"); err != nil {
t.Fatal(err)
}
if in, err := CLIInstalled(ctx, "cliinstall"); err != nil || !in {
t.Fatalf("after the install: %v, %v", in, err)
}
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Fatal("ran out of time")
}
}