Files
magpie/internal/plugin/market_test.go
T
yetone 9ec237d4e6 gui: a Plugins tab, a market to find and install provider plugins in (在产品里要有插件市场功能,整个插件市场要美观易用), and a way to it from the providers list (供应商列表里有个精致优雅的按钮) — Discover lists the magpie community's plugins, then OpenCode's community's by weekly downloads, each card with its logo, publisher, summary, downloads, version and one button that takes it all the way: Install (Bun fetched first if need be), Sign in, Signed in; a package not on npm yet says Coming soon. Typing searches the list and, after a pause, npm, with the field keeping its focus. A card opens the plugin's page: version, downloads, licence, updated, what it signs in to, npm/source/homepage links, and its README (no images, HTML or scripts; only http(s) links, opened outside). Installed lists each plugin with its providers' sign-ins, a newer version to update to, Update all, switch off and remove; Settings' Plugins section moves here. The list comes from registry.json in magpie-community/plugins, kept for 6 hours, falling back to the copy fetched last and then the one built in (MAGPIE_PLUGIN_MARKET overrides, "off" never asks). Under Add provider a "More subscriptions in Plugins" pill opens it; the add sheet ends in a Plugins tile, and a search that finds nothing offers to look for a plugin. The header, seven tabs now, draws them in at the narrowest window. plugin-market.test.cjs (Chromium, WebKit, en/zh) and market_test.go fail without the change; add-button, plugin-signin, usage-ledger, text-size, old-webkit, click-scroll pass. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.
2026-09-30 17:57:46 +08:00

75 lines
2.0 KiB
Go

package plugin
import (
"context"
"net/http"
"net/http/httptest"
"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 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"},{"package":"not a name"}]}`))
}))
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()
}