Files
magpie/internal/plugin/catalog_test.go
T
yetone 14be7f69df plugins: Discover's list comes from a file CI builds, not GitHub from each desktop (River_WLan on X)
Plugins -> Discover read GitHub's topic search plus a package.json, an npm
answer and an entry file for ~400 repositories on every desktop. Where GitHub
is unreachable or rate-limited the page said "Couldn't ask GitHub".

Now the community repo's `tagged` workflow (plugins repo, scripts/tagged.mjs,
every half hour) builds tagged.json on its dist branch, usemagpie.ai serves
it at /api/plugins/tagged (worker, edge-cached 10 min, jsDelivr and the last
whole copy as fallbacks), and magpie asks: usemagpie.ai, the dist branch
(jsDelivr with the China-mirror switch), then GitHub as before. parseCatalog
keeps only entries whose spec is github:<repo> or the package named from it,
and drops a file older than 48h. Degrades: a tagged repo shows within the
hour, not ten minutes; stars/https icons are as of the last build; owner
avatars and https icon URLs still come from GitHub hosts (data: icons work).

Run (wsl-run, mote's disk is full): go vet + go test ./internal/plugin,
node --test site/worker.test.mjs (21 pass), scripts/tagged.mjs against the
real GitHub (367 plugins, 253 with icons, 0 unread; workflow run green and
tagged.json on dist), usemagpie.ai/api/plugins/tagged after wrangler deploy
(200, 4.8MB). Broken by hand: skipping askCatalog turns
TestTaggedReposFromTheBuiltList red. Not tried: a real desktop with GitHub
blocked; GUI suites (no GUI file changed); Windows/darwin vet.
2026-11-11 15:07:54 +08:00

106 lines
3.2 KiB
Go

package plugin
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync/atomic"
"testing"
"time"
)
// The Discover list is asked of the built file first, so a desktop that
// can't reach GitHub still has it (River_WLan on X): with GitHub's API
// refused, the list still comes, in stars order, with what the file kept
// and nothing it made up.
func TestTaggedReposFromTheBuiltList(t *testing.T) {
file := func(at time.Time, ps ...Tagged) []byte {
b, _ := json.Marshal(catalogFile{Version: 1, Generated: at.UTC().Format(time.RFC3339), Plugins: ps})
return b
}
good := []Tagged{
{Spec: "github:a/few", Repo: "a/few", Package: "few", Stars: 2, Icon: "data:image/svg+xml;base64,AAAA"},
{Spec: "github:b/many", Repo: "b/many", Package: "many", Stars: 90, Kind: "page"},
{Spec: "mine", Repo: "c/mine", Package: "mine", Stars: 5},
{Spec: "https://evil.example/x.tgz", Repo: "d/evil", Package: "evil", Stars: 999},
{Spec: "github:e/odd", Repo: "e/odd", Package: "odd", Kind: "shell", Stars: 999},
{Spec: "github:f/bad", Repo: "f/bad", Package: "Not A Name", Stars: 999},
}
body := file(time.Now(), good...)
var asked atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asked.Add(1)
w.Write(body)
}))
defer srv.Close()
var gh atomic.Int32
hub := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gh.Add(1)
http.Error(w, "unreachable", 503)
}))
defer hub.Close()
api, raw, urls := githubAPI, githubRaw, catalogURLs
githubAPI, githubRaw, catalogURLs = hub.URL, hub.URL+"/raw", []string{srv.URL}
t.Cleanup(func() {
githubAPI, githubRaw, catalogURLs = api, raw, urls
taggedMu.Lock()
taggedL = nil
taggedMu.Unlock()
_ = os.Remove(taggedCache())
})
forget := func() {
taggedMu.Lock()
taggedL = nil
taggedMu.Unlock()
_ = os.Remove(taggedCache())
}
forget()
l, err := TaggedRepos(context.Background())
if err != nil {
t.Fatal(err)
}
var got []string
for _, x := range l {
got = append(got, x.Repo)
}
if strings.Join(got, " ") != "b/many c/mine a/few" {
t.Fatalf("listed %v", got)
}
if l[2].Icon == "" {
t.Error("the icon the file kept was dropped")
}
if gh.Load() != 0 {
t.Fatalf("GitHub was asked %d times with the list at hand", gh.Load())
}
// the file stopped being built: GitHub is asked after all (and its
// refusal is what the page says)
forget()
body = file(time.Now().Add(-catalogMaxAge-time.Hour), good...)
if _, err := TaggedRepos(context.Background()); err == nil || gh.Load() == 0 {
t.Fatalf("a list from days ago stood: err %v, GitHub asked %d", err, gh.Load())
}
// the address down: the next one answers
forget()
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write(file(time.Now(), good...)) }))
defer srv2.Close()
catalogURLs = []string{"http://127.0.0.1:1", srv2.URL}
if l, err := TaggedRepos(context.Background()); err != nil || len(l) != 3 {
t.Fatalf("second address: %d, %v", len(l), err)
}
// off keeps to GitHub
forget()
t.Setenv("MAGPIE_PLUGIN_CATALOG", "off")
before := gh.Load()
if _, err := TaggedRepos(context.Background()); err == nil || gh.Load() == before {
t.Fatalf("off still read the list: err %v", err)
}
}