Files
magpie/internal/source/china_test.go
T
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

175 lines
6.7 KiB
Go

package source
import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync/atomic"
"testing"
)
// the 「国内镜像」 switch is off in every test but those that turn it on,
// whatever the settings of the HOME the tests run in say
func TestMain(m *testing.M) {
China = func() bool { return false }
os.Exit(m.Run())
}
func chinaOn(t *testing.T) {
was := China
China = func() bool { return true }
t.Cleanup(func() { China = was })
}
func TestChinaFirst(t *testing.T) {
for raw, want := range map[string]string{
"https://registry.npmjs.org/@magpie-community%2fopencode-kiro-auth": "https://registry.npmmirror.com/@magpie-community%2fopencode-kiro-auth",
"https://registry.npmjs.org/-/v1/search?text=opencode&size=20": "https://registry.npmmirror.com/-/v1/search?text=opencode&size=20",
"https://github.com/oven-sh/bun/releases/download/bun-v1.4.2/bun-darwin-aarch64.zip": "https://registry.npmmirror.com/-/binary/bun/bun-v1.4.2/bun-darwin-aarch64.zip",
"https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json": "https://cdn.jsdelivr.net/gh/magpie-community/plugins@main/registry.json",
// npm's download counts are npmjs's own: npmmirror counts its own
"https://api.npmjs.org/downloads/point/last-week/pkg": "",
// nothing else of GitHub's: no proxy stands in for it
"https://github.com/oven-sh/bun/releases/download/": "",
"https://github.com/o/r/releases/download/v1/x.zip": "",
"https://api.github.com/repos/oven-sh/bun/releases/latest": "",
"https://raw.githubusercontent.com/someone/else/main/registry.json": "",
"https://raw.githubusercontent.com/magpie-community/plugins/main/x.json?t=1": "",
"http://registry.npmjs.org/pkg": "",
} {
if got := chinaFirst(raw); got != want {
t.Errorf("chinaFirst(%s) = %q, want %q", raw, got, want)
}
}
}
func TestChinaComesFirst(t *testing.T) {
t.Setenv("MAGPIE_MIRRORS", "")
raw := "https://registry.npmjs.org/pkg"
if got := URLs(raw); len(got) != 1 {
t.Fatalf("switch off: URLs = %q", got)
}
chinaOn(t)
if got := strings.Join(URLs(raw), " "); got != "https://registry.npmmirror.com/pkg "+raw {
t.Fatalf("switch on: URLs = %q", got)
}
// with the fallback mirrors too, npmmirror once, first
t.Setenv("MAGPIE_MIRRORS", "on")
t.Setenv("MAGPIE_NPM_REGISTRY", "")
if got := strings.Join(URLs(raw), " "); got != "https://registry.npmmirror.com/pkg "+raw {
t.Fatalf("switch and fallback on: URLs = %q", got)
}
// GitHub's fallback proxy stays after the official address
gh := "https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json"
want := "https://cdn.jsdelivr.net/gh/magpie-community/plugins@main/registry.json " + gh + " https://gh-proxy.com/" + gh
if got := strings.Join(URLs(gh), " "); got != want {
t.Fatalf("plugin list URLs = %q, want %q", got, want)
}
}
// pair is a mirror and an official server, counting their requests
func pair(t *testing.T, mirror, official http.HandlerFunc) (m, o *httptest.Server, mn, on *atomic.Int32) {
mn, on = new(atomic.Int32), new(atomic.Int32)
m = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mn.Add(1); mirror(w, r) }))
o = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { on.Add(1); official(w, r) }))
t.Cleanup(m.Close)
t.Cleanup(o.Close)
return
}
func say(code int, body string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(code); io.WriteString(w, body) }
}
func TestChinaMirrorAnswers(t *testing.T) {
m, o, mn, on := pair(t, say(200, "mirror"), say(200, "official"))
req, _ := http.NewRequest("GET", o.URL+"/pkg", nil)
res, err := do(http.DefaultClient, req, []string{m.URL + "/pkg", o.URL + "/pkg"})
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(res.Body)
res.Body.Close()
if string(b) != "mirror" || mn.Load() != 1 || on.Load() != 0 {
t.Fatalf("got %q, mirror asked %d, official %d", b, mn.Load(), on.Load())
}
}
// what a mirror in China hasn't copied yet (a version just published) is
// asked of the official address, where a fallback mirror's 404 would be
// the answer
func TestChinaMirrorsMissIsAskedOfficially(t *testing.T) {
for _, code := range []int{404, 403, 500} {
m, o, mn, on := pair(t, say(code, "no"), say(200, "official"))
req, _ := http.NewRequest("GET", o.URL+"/pkg", nil)
res, err := do(http.DefaultClient, req, []string{m.URL + "/pkg", o.URL + "/pkg"})
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(res.Body)
res.Body.Close()
if string(b) != "official" || mn.Load() != 1 || on.Load() != 1 {
t.Fatalf("%d: got %q, mirror asked %d, official %d", code, b, mn.Load(), on.Load())
}
}
// the official 404 is the answer, the fallback mirror not asked
m, o, mn, on := pair(t, say(200, "fallback"), say(404, "gone"))
req, _ := http.NewRequest("GET", o.URL+"/pkg", nil)
res, err := do(http.DefaultClient, req, []string{o.URL + "/pkg", m.URL + "/pkg"})
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 404 || mn.Load() != 0 || on.Load() != 1 {
t.Fatalf("official 404: %d, fallback asked %d, official %d", res.StatusCode, mn.Load(), on.Load())
}
}
// the plugin list goes to jsDelivr with the switch, never to a proxy
func TestDoFaithful(t *testing.T) {
t.Setenv("MAGPIE_MIRRORS", "on")
t.Setenv("MAGPIE_GITHUB_MIRROR", "https://gh.invalid")
var asked []string
c := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) {
asked = append(asked, r.URL.String())
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader("{}")), Header: http.Header{}, Request: r}, nil
})}
list := "https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json"
ask := func() {
asked = nil
req, _ := http.NewRequest("GET", list, nil)
res, err := DoFaithful(c, req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
}
ask()
if strings.Join(asked, " ") != list {
t.Fatalf("switch off: asked %q", asked)
}
chinaOn(t)
ask()
if strings.Join(asked, " ") != "https://cdn.jsdelivr.net/gh/magpie-community/plugins@main/registry.json" {
t.Fatalf("switch on: asked %q", asked)
}
// a request with a credential stays official
asked = nil
req, _ := http.NewRequest("GET", "https://registry.npmjs.org/pkg", nil)
req.Header.Set("Authorization", "Bearer x")
res, err := DoFaithful(c, req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if strings.Join(asked, " ") != "https://registry.npmjs.org/pkg" {
t.Fatalf("with a credential: asked %q", asked)
}
}
type roundTrip func(*http.Request) (*http.Response, error)
func (f roundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }