Files
magpie/internal/plugin/market_test.go
T
volcanic fc248e381f plugins: the folder a plugin lives in is picked, not typed, and its page reads that folder's README (#411)
Settings → Plugins adds a plugin by npm package name or by a path, and the
path had to be typed in full: the market's field was the only way in, with
nothing to pick the folder from. And a plugin added from a folder had no page
of its own — its README comes from plugin.Readme, which answers only for an
npm package name ("%q isn't an npm package name"), so opening one said "npm
didn't answer" and offered an npm link for a folder that npm has never heard
of.

The field now has the system's folder picker beside it, and a folder plugin's
page shows the README that folder carries.

- POST /api/plugins/choose asks Windows.ChooseFolder, which the app answers
  with the system's panel — the Library's "Choose a project" already used it,
  and a page served by another magpie forwards it to the magpie it is served
  by. pluginsState carries "picker" beside "bun", false for `magpie web`: a
  browser has no such panel (webHost.ChooseFolder refuses it), and the page is
  then left with the field to type into, as before.
- plugin.Readme takes a path: folderReadme reads README.md whatever its case,
  else a markdown or a text one named beside it, and says so when the folder
  has none rather than answering with npm's silence. Updated is that file's
  mtime, and the text is capped at 200 KB as npm's answer is.
- A folder plugin's page drops what only npm can say: no npm link, no version,
  downloads or license, and a failure names the folder rather than npm. Its
  row and its title name the folder it was added from (opencode-mine, not
  /Users/me/plugins/opencode-mine).

TestReadmeFromFolder (internal/plugin) fails without the change: with a
README.txt and an unsuffixed README beside it, the README.md is still the one
read; ReadMe.MD is too, and through file:// and a trailing .; a folder with
none is an error, as is one that isn't there.
TestPluginsPickerWhereItCan (internal/gui) fails without the change: the
picker is offered for the app, not for `magpie web` nor for no host at all.
plugin-local.test.cjs (new; Chromium and WebKit, en and zh) fails without the
change: the picker is beside the field and titled as such, a click asks for it
and what it answers is what the field holds and takes the focus, what the
field holds is what Install posts, the Installed row names the folder rather
than its path, its page renders that folder's README with no npm link, and a
market that offers no picker draws no button (english and Chinese).

Not only the faked page: a sandbox `magpie web` in a HOME of its own, with a
plugin added by path, read that folder's README.md — its mtime as Updated —
and listed it by the folder's name.

go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows
go build, go test -tags nogui ./internal/gui/ pass; plugin-market,
plugin-signin and plugin-local pass in Chromium and WebKit, en and zh.
TestPluginProxy (internal/plugin) and the hermes tests (internal/agent,
internal/profile) fail on main on this machine too — its own proxy and a
hermes install it finds — and are unrelated.

Not done: the panel itself hasn't been clicked in the app. It is the system's
own, and the call under it is the one the Library's folder chooser already
uses, but the screenshots here are the page with a faked backend underneath
(the app's own picker is offered, `magpie web` has none), plus the real
`magpie web` for the README.
2026-10-01 18:46:56 +08:00

170 lines
5.0 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)
}
if len(x.Providers) == 0 || x.Icon == "" {
t.Errorf("%s: needs providers and an icon", x.Package)
}
}
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)
}
}