Files
magpie/internal/cli/plugin_middleware_cli_test.go
T
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00

71 lines
2.9 KiB
Go

package cli
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/middleware"
"github.com/yetone/magpie/internal/plugin"
)
// magpie plugin says which plugins are gateway middleware and their hooks,
// or why one didn't load; a middleware plugin has no provider to sign in
// to, so the list had nothing under it.
func TestPluginListSaysMiddleware(t *testing.T) {
groupsHome(t)
dir := t.TempDir()
good := filepath.Join(dir, "alias.middleware.js")
bad := filepath.Join(dir, "bad.middleware.js")
os.WriteFile(good, []byte("export const events = [\"message_start\"]\nexport function onRequest(b, ctx) {}\nexport function onEvent(e, ctx) {}\n"), 0o644)
os.WriteFile(bad, []byte("export function onRequest( {\n"), 0o644)
for _, spec := range []string{good, bad} {
if _, err := plugin.Add(context.Background(), spec); err != nil {
t.Fatal(err)
}
}
middleware.Reload()
out, err := stdoutOf(t, func() error { return listPlugins(context.Background(), false) })
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "gateway middleware onRequest, onEvent") {
t.Errorf("the middleware's hooks aren't listed:\n%s", out)
}
if !strings.Contains(out, "didn't load: ") || !strings.Contains(out, "SyntaxError") {
t.Errorf("the broken middleware doesn't say why:\n%s", out)
}
js, _ := stdoutOf(t, func() error { return listPlugins(context.Background(), true) })
if !strings.Contains(js, `"middleware"`) || !strings.Contains(js, `"message_start"`) {
t.Errorf("--json has no middleware:\n%s", js)
}
}
// magpie plugin off, on and rm take a plugin's short name, as options
// does: `magpie plugin off think-tags` said there was no such plugin.
func TestPluginOnOffRmShortName(t *testing.T) {
groupsHome(t)
dir := filepath.Join(t.TempDir(), "middleware-zz-tags")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"name": "@magpie-community/middleware-zz-tags", "magpie": {"middleware": "./zz.middleware.js"}}`), 0o644)
os.WriteFile(filepath.Join(dir, "zz.middleware.js"), []byte("export function onRequest(b) {}\n"), 0o644)
if _, err := plugin.Add(context.Background(), dir); err != nil {
t.Fatal(err)
}
off := func() bool { return plugin.Load().Plugins[0].Off }
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "off", "zz-tags"}) }); err != nil || !off() {
t.Fatalf("off by short name: err %v, off %v", err, off())
}
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "on", "zz-tags"}) }); err != nil || off() {
t.Fatalf("on by short name: err %v, off %v", err, off())
}
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "rm", "zz-tags"}) }); err != nil || len(plugin.Load().Plugins) != 0 {
t.Fatalf("rm by short name: err %v, left %v", err, plugin.Load().Plugins)
}
if err := pluginCmd([]string{"plugin", "off", "nothing-here"}); err == nil {
t.Error("a name no plugin has was taken")
}
}