Files
magpie/internal/gui/plugins_mirror_test.go
yetone 860ed6618d plugins: a 「国内镜像」 switch on the Plugins page downloads the list, plugins and Bun from mirrors in China first (Discord, akic404: GitHub/npm are slow from the mainland; asked for ghproxy) — Plugins › Mirrors in China, off by default, kept in settings (chinaMirror). On, source asks a copy of the very file before its official address: npm's registry (packages, versions, search) at registry.npmmirror.com, Bun's release zips at registry.npmmirror.com/-/binary/bun, and the community registry.json at cdn.jsdelivr.net/gh/magpie-community/plugins@<branch>; bun add gets BUN_CONFIG_REGISTRY=npmmirror unless the environment names a registry. No proxy: gh-proxy (MAGPIE_MIRRORS' GitHub fallback) handles nothing the switch sends, since a proxy could hand back other code. A mirror asked first that can't answer (404/403/5xx, e.g. a version it hasn't synced) falls through to the official address; npm's weekly downloads stay api.npmjs.org's (npmmirror counts its own). Checked by hand: npmmirror's bun-v1.4.2 darwin-aarch64 zip has the SHA-256 pinned in bun.go, jsDelivr's registry.json is byte-identical to raw.githubusercontent's, and bun 1.4.2 add through npmmirror locks the same sha512 integrity npmjs gives. TestChinaFirst, TestChinaComesFirst, TestChinaMirrorAnswers, TestChinaMirrorsMissIsAskedOfficially, TestDoFaithful, TestBunInstallsFromChinaMirror and TestPluginsMirrorSwitch cover it. A live run with the switch on fetched the list from jsDelivr, npm info from npmmirror and Bun from npmmirror (checksum passed); a sandbox magpie web showed the switch in the Plugins head, saved it, kept it after a reload, and a click didn't move the page. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried from inside mainland China.
2026-10-03 15:14:45 +08:00

51 lines
1.5 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/source"
)
// The Plugins page's 「国内镜像」 switch is kept in settings, which is
// where source asks whether it is on, and /api/plugins says how it is.
func TestPluginsMirrorSwitch(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("MAGPIE_PLUGIN_MARKET", "off")
mux := http.NewServeMux()
pluginRoutes(mux, nil)
do := func(method, path, body string) map[string]any {
t.Helper()
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
if rec.Code != 200 {
t.Fatalf("%s %s: %d %s", method, path, rec.Code, rec.Body)
}
var v map[string]any
if err := json.Unmarshal(rec.Body.Bytes(), &v); err != nil {
t.Fatal(err)
}
return v
}
if v := do("GET", "/api/plugins", ""); v["mirror"] != false {
t.Fatalf("at first: mirror %v", v["mirror"])
}
for _, on := range []bool{true, false} {
b, _ := json.Marshal(map[string]bool{"on": on})
if v := do("POST", "/api/plugins/mirror", string(b)); v["mirror"] != on {
t.Fatalf("turned %v: answered %v", on, v)
}
if settings.Load().ChinaMirror != on || source.China() != on {
t.Fatalf("turned %v: settings say %v, source %v", on, settings.Load().ChinaMirror, source.China())
}
if v := do("GET", "/api/plugins", ""); v["mirror"] != on {
t.Fatalf("turned %v: /api/plugins says %v", on, v["mirror"])
}
}
}