Files
magpie/internal/gui/plugins_own_icon_test.go
T
yetone 0738154520 plugin, gui: an installed plugin's row and page show its own magpie.icon, not its first provider's (#1775)
peterroe's plugin signs in to several providers. Each provider gets its own
picture from the auth hook, and package.json's magpie.icon is the picture
for the plugin itself. Settings → Plugins → Installed only took a plugin
picture from the market listing. A plugin that isn't listed (installed from
a path, git or an unlisted npm package) had none, so its row and its page
showed its first provider's picture.

- plugin.InstalledIcon reads magpie.icon from the installed package.json.
  It goes through the same ownIcon check as a listing's icon (data: or
  https only).
- pluginsState stores the picture through provider.RepoIcons, as listed
  plugins' pictures are, and returns it as `icon` on each plugin entry.
- plugins.js: the installed row and the detail page use the listing's
  icon, then the plugin's own, then its first provider's, as before.
- Docs (en, zh, ja): magpie.icon is the plugin's picture (Discover card
  and Installed row) and the fallback for providers without their own. A
  plugin of several providers gives each provider auth.icon.

Run:
- go test ./internal/plugin ./internal/gui: ok.
  TestPluginsStateGivesThePluginsOwnIcon covers a data: icon (kept), no
  icon and a plain-http icon (both "").
- internal/gui/tests/plugin-own-icon.test.cjs: Chromium and WebKit, en,
  zh, zh-TW, ja, de, at 980 and 420 wide. It fails on the old plugins.js
  (the row shows pa.png).
- The plugins GUI tests: 34/34 in each engine.
- gofmt; go vet darwin, windows, and linux -tags nogui.
- Full go test ./...: the only red is gateway OTel timing under load ~79.
  That's #1794, commented there; alone it passes 3/3.

Not tried with peterroe's real package; the fixture follows the layout
the issue describes.
2026-10-12 19:38:20 +08:00

64 lines
2.1 KiB
Go

package gui
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// #1775 (peterroe): a plugin of several providers gives each its own
// picture in the auth hook and itself one in package.json's magpie.icon.
// Settings → Plugins is told the plugin's own, checked and kept as a
// listed plugin's is, apart from its providers'; one that gives none, or
// a picture magpie doesn't take (plain http), is told none.
func TestPluginsStateGivesThePluginsOwnIcon(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("MAGPIE_BUN", "")
t.Setenv("PATH", "")
const png = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
dir := func(name, icon string) string {
d := filepath.Join(h, "src", name)
if err := os.MkdirAll(d, 0o755); err != nil {
t.Fatal(err)
}
pj := map[string]any{"name": name, "version": "1.0.0", "main": "index.js"}
if icon != "" {
pj["magpie"] = map[string]any{"icon": icon}
}
b, _ := json.Marshal(pj)
if err := os.WriteFile(filepath.Join(d, "package.json"), b, 0o644); err != nil {
t.Fatal(err)
}
return d
}
suite, plain, http := dir("suite", png), dir("plain", ""), dir("http", "http://example.invalid/x.png")
if err := os.MkdirAll(settings.Dir(), 0o755); err != nil {
t.Fatal(err)
}
b, _ := json.Marshal(map[string]any{"plugins": []map[string]any{{"spec": suite}, {"spec": plain}, {"spec": http}}})
if err := os.WriteFile(filepath.Join(settings.Dir(), "plugins.json"), b, 0o644); err != nil {
t.Fatal(err)
}
got := map[string]string{}
for _, p := range pluginsState(context.Background(), nil).Plugins {
got[p.Spec] = p.Icon
}
if len(got) != 3 {
t.Fatalf("plugins: %v", got)
}
if got[suite] == "" || got[suite] == png {
t.Errorf("the suite's own picture: %q, want it kept as an icon", got[suite])
}
if got[plain] != "" || got[http] != "" {
t.Errorf("none of its own: %q, http: %q", got[plain], got[http])
}
}