Files
yetone 2f135ed4d5 plugin, gui, docs: Plugins says which plugins are providers and which are middleware, lists the community's ready-made middleware, and edits a middleware's options in its row (the owner) — the Plugins page still read as if every plugin were a subscription, and nothing showed what a middleware does or let you set its options without editing plugins.json. Every Discover card and Installed row now carries a Provider or Middleware chip (both for a plugin that is both), also for one that is off (isMiddleware/middlewareOnly from the server, read from its files); Discover splits into Subscriptions and Gateway middleware ("runs in magpie's gateway on what every agent sends and gets back, whichever provider serves it"), the latter from market entries with "kind": "middleware" and no providers or icon (the built-in market.json lists the five new @magpie-community/middleware-* packages: param-override, model-map, system-prompt, word-guard, think-tags, mostly New API's channel settings with the same JSON). A middleware row has Options: a JSON editor in the row, starting from the package's magpie.options example when none are set, which validates it is an object, saves (POST /api/plugins/options) for the next request, clears, and cancels with Esc. magpie plugin options takes a short name (model-map for @magpie-community/middleware-model-map; plugin.ShortName, also in SetOptions). en/zh/ja/de. Docs: usemagpie.ai/docs/plugins (en/zh/ja) gains Ready-made middleware and Options and the registry's kind; reference.md, both READMEs and docs/subsystems/gateway-middleware.md likewise. Tested with a real DeepSeek account and real Claude Code through magpie web --gateway in a sandbox HOME, with all five installed and options set by short name: a mapped model named back, max_tokens capped, a prompt appended for claude only, word-guard's 400 and param-override's 403 in the agent's shape, <think> stripped from Chat and Responses streams and a whole Anthropic reply, reasoning_content moved into the text; /api/plugins showed the kinds and the example. TestSetOptionsShortName is new; TestBuiltinMarket checks a middleware entry has no providers or icon and no other kind. plugin-middleware.test.cjs (Chromium and WebKit, en and zh: the chips, Discover's two sections, the options editor with its errors, no select, no scroll, no left border) fails without the change; the other plugin tests and click-scroll pass in WebKit (plugin-market's section name updated; plugin-move fails only under parallel load and passes alone). go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 20:50:09 +08:00

178 lines
5.3 KiB
Go

package plugin
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
// The list built in is what a magpie that can't reach the market shows:
// every entry a real package name, said in English and Chinese, once.
func TestBuiltinMarket(t *testing.T) {
l, err := parseMarket(builtinMarket)
if err != nil {
t.Fatal(err)
}
seen := map[string]bool{}
for _, x := range l {
if seen[x.Package] {
t.Errorf("%s listed twice", x.Package)
}
seen[x.Package] = true
if x.Summary["en"] == "" || x.Summary["zh"] == "" {
t.Errorf("%s: summary needs en and zh", x.Package)
}
if !x.Community {
t.Errorf("%s: only magpie's community's plugins are listed", x.Package)
}
// gateway middleware signs in to nothing
if x.Kind == "middleware" {
if len(x.Providers) != 0 || x.Replaces != "" {
t.Errorf("%s: middleware with providers", x.Package)
}
} else if len(x.Providers) == 0 || x.Icon == "" {
t.Errorf("%s: needs providers and an icon", x.Package)
}
if x.Kind != "" && x.Kind != "middleware" {
t.Errorf("%s: kind %q", x.Package, x.Kind)
}
}
if len(l) < 10 {
t.Errorf("%d listed", len(l))
}
}
// The market is fetched; one that fails is the copy fetched last, and
// with none, the one built in. "off" never asks.
func TestMarketSources(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", t.TempDir())
reset := func() { marketMu.Lock(); marketList = nil; marketMu.Unlock() }
up := true
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !up {
http.Error(w, "down", 500)
return
}
w.Write([]byte(`{"version":1,"plugins":[{"package":"opencode-x-auth","name":"X","community":true},{"package":"not a name","community":true},{"package":"opencode-other-auth","name":"Other"}]}`))
}))
defer srv.Close()
t.Setenv("MAGPIE_PLUGIN_MARKET", srv.URL)
ctx := context.Background()
reset()
if l := Market(ctx); len(l) != 1 || l[0].Name != "X" {
t.Fatalf("fetched: %+v", l)
}
up = false
reset()
if l := Market(ctx); len(l) != 1 || l[0].Package != "opencode-x-auth" {
t.Fatalf("down, the copy kept: %+v", l)
}
t.Setenv("XDG_CACHE_HOME", t.TempDir())
reset()
builtin, _ := parseMarket(builtinMarket)
if l := Market(ctx); len(l) != len(builtin) {
t.Fatalf("down, nothing kept: %d, want the %d built in", len(l), len(builtin))
}
up = true
t.Setenv("MAGPIE_PLUGIN_MARKET", "off")
reset()
if l := Market(ctx); len(l) != len(builtin) {
t.Fatalf("off: %d", len(l))
}
reset()
}
// A plugin's provider shows its vendor's icon: the one its package is
// listed with, else that of a listed plugin for the same provider.
func TestMarketIcon(t *testing.T) {
t.Setenv("XDG_CACHE_HOME", t.TempDir())
marketMu.Lock()
marketList = nil
marketMu.Unlock()
for _, c := range []struct{ spec, id, want string }{
{"@cognitionai/opencode-devin@0.3.1", "devin", "devin"},
{"opencode-gemini-auth@2.0.1", "google", "gemini-color"},
{"opencode-copilot-auth", "github-copilot", "githubcopilot"},
{"some-unlisted-plugin", "anthropic", "claude-color"},
{"opencode-antigravity-auth", "google", "antigravity-color"},
{"/Users/me/plugins/packages/zed", "zed", "zed"},
{"some-unlisted-plugin", "commandcode-plan", "commandcode"},
{"some-unlisted-plugin", "nobody", ""},
} {
if got := Icon(c.spec, c.id); got != c.want {
t.Errorf("Icon(%q, %q) = %q, want %q", c.spec, c.id, got, c.want)
}
}
}
// A plugin added from a folder is read from that folder, where npm has
// nothing to say of it: README.md whatever its case, else a markdown or
// text one beside it, and a folder with none says so.
func TestReadmeFromFolder(t *testing.T) {
ctx := context.Background()
dir := t.TempDir()
write := func(name, body string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
if _, err := Readme(ctx, dir); err == nil {
t.Fatal("a folder with no README answered")
}
if _, err := Readme(ctx, filepath.Join(dir, "not-there")); err == nil {
t.Fatal("a folder that isn't there answered")
}
// the lesser names are there first: the README.md is still the one read
write("README.txt", "a text one")
write("README", "an unsuffixed one")
write("readme.md", "# the plugin's own\n\nInstall it.\n")
p, err := Readme(ctx, dir)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(p.Readme, "# the plugin's own") {
t.Errorf("read %q, want the README.md", p.Readme)
}
if p.Updated.IsZero() {
t.Error("no modified time")
}
// whatever case the folder spells it in, and through file://
if err := os.Remove(filepath.Join(dir, "readme.md")); err != nil {
t.Fatal(err)
}
write("ReadMe.MD", "# spelled otherwise\n")
for _, spec := range []string{dir, "file://" + dir, filepath.Join(dir, ".")} {
p, err := Readme(ctx, spec)
if err != nil {
t.Fatalf("%s: %v", spec, err)
}
if !strings.HasPrefix(p.Readme, "# spelled otherwise") {
t.Errorf("%s: read %q, want the ReadMe.MD", spec, p.Readme)
}
}
// the text one, once there is no markdown at all
if err := os.Remove(filepath.Join(dir, "ReadMe.MD")); err != nil {
t.Fatal(err)
}
if err := os.Remove(filepath.Join(dir, "README")); err != nil {
t.Fatal(err)
}
p, err = Readme(ctx, dir)
if err != nil {
t.Fatal(err)
}
if p.Readme != "a text one" {
t.Errorf("read %q, want the README.txt", p.Readme)
}
}