Files
magpie/internal/plugin/download_proxy_test.go
T
yetone 4bfac12683 plugins: downloads take a proxy of their own, apart from the vendors' (JonahFu on X)
JonahFu on X: GitHub is unreachable without a proxy, while API requests
may go direct, so plugin downloads' proxy should be set apart from the
one model requests take.

Before: plugin downloads already followed the global proxy (fetches
through http.DefaultClient and netproxy.Func, bun add and git through
netproxy.Env), so nothing ignored it; there was only no way to give
them a different one.

Now Settings › Network has a Plugin downloads row under Proxy: Same as
above (the default, unset: downloads follow the global proxy as
before), Direct, or Custom with its login, remembered as
pluginProxyCustom as Proxy's Custom is. The CLI takes it as
`magpie settings set pluginProxy <url|direct|"">`. It covers the plugins
list and Discover's lists (fetchJSONFrom), a user's own npm registry
(fetchRegistry), Bun's download (getURLFrom), bun add/update/remove and
the git commands of GitHub-installed plugins (dlEnv). The plugin host
(bun run) keeps the global proxy: its requests are the vendors'. Both
fields belong to this computer (KeepOwn) and are left out of
auto-backups, since a proxy URL may hold a password. Settings' Proxy
row and the new one are drawn by one helper (proxyRow); the core's
script budget is met by cutting the proxy rows' comments, no raise.

Run (mote, through mote-run): gofmt -l clean; go vet of the touched
packages (darwin), GOOS=linux -tags nogui ./..., GOOS=windows of the
touched packages; go test -count=1 internal/settings, settingsapi,
backup, netproxy, plugin, gui, cli all ok (gui after the comment cut;
its first run had TestCoreScriptBudget 1,352 bytes over). GUI, chromium
and webkit: settings-plugin-proxy (new, 5 languages x 2 widths),
settings-proxy-remember, settings-proxy-auth, proxy-test,
settings-sections, proxy-login-fields and the i18n files: 102 pass, 0
fail. plugin-proxy-page, proxy-own-wording, account-proxy,
provider-proxy, routing-proxy, provider-many-keys-proxy and
signin-proxy: 104 pass, 2 fail, both signin-proxy's "a subscription
with no proxy offers the global one" at 380px in Chromium, red the same
way on origin/main b596da773 (filed #5866).

Again at this commit, rebased on b596da773: gofmt, the three go vets,
the seven packages' go test (all ok, TestCoreScriptBudget included) and
settings-plugin-proxy, -remember and -auth (50 pass, 0 fail).

Broken by hand: fetchJSONFrom without netproxy.Downloads ->
TestPluginDownloadsTakeTheirOwnProxy red (the list went direct and hit
testenv's offline guard); bunCommand always taking env() -> red (bun
add's HTTPS_PROXY ""); the GUI row saving key "proxy" ->
settings-plugin-proxy 10 of 10 red in chromium.

Not tried: a real install behind a real proxy with GitHub blocked; the
Library's (skills, MCP) downloads and plugin icons (loaded by the
webview through usemagpie.ai) still follow the global proxy; Windows.
2026-11-10 11:44:17 +08:00

128 lines
4.9 KiB
Go

package plugin
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/netproxy"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/source"
)
// JonahFu on X: GitHub is reached only through a proxy, the vendors
// without one. Plugin downloads (the list, npm's registry, GitHub, the
// dist branch, Bun, bun add and git) go through the proxy Settings sets for
// them, while the host — the plugins' requests to their vendors — keeps
// magpie's own. Left unset, downloads follow the global proxy as before.
func TestPluginDownloadsTakeTheirOwnProxy(t *testing.T) {
dir := t.TempDir()
t.Setenv("HOME", dir)
t.Setenv("USERPROFILE", dir)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(dir, "cache"))
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
t.Setenv(k, "")
}
// a proxy: it answers every request sent through it, and keeps the
// address each asked for
var mu sync.Mutex
var asked []string
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
asked = append(asked, r.URL.String())
mu.Unlock()
fmt.Fprint(w, `{"ok":true}`)
}))
t.Cleanup(proxy.Close)
seen := func() []string {
mu.Lock()
defer mu.Unlock()
return slices.Clone(asked)
}
// magpie's own transport, as the app installs it, put back after
tr := http.DefaultTransport.(*http.Transport)
wasProxy, wasClient := tr.Proxy, http.DefaultClient.Transport
t.Cleanup(func() { tr.Proxy, http.DefaultClient.Transport = wasProxy, wasClient })
netproxy.Install()
// API requests direct, plugin downloads through the proxy
if err := settings.Save(settings.Settings{Proxy: "direct", PluginProxy: proxy.URL}); err != nil {
t.Fatal(err)
}
ctx := context.Background()
const list = "http://plugins.example.invalid/registry.json"
if b, err := fetchJSONFrom(ctx, list, 1<<10, source.DoOfficial); err != nil || !strings.Contains(string(b), "ok") {
t.Fatalf("the plugins list through plugin downloads' proxy: %q %v", b, err)
}
const reg = "http://npm.example.invalid/@me%2fx"
if b, err := fetchRegistry(ctx, reg, "tok", 1<<10); err != nil || !strings.Contains(string(b), "ok") {
t.Fatalf("a registry of the user's through plugin downloads' proxy: %q %v", b, err)
}
const bunZip = "http://github.example.invalid/bun.zip"
if b, err := getURLOfficial(ctx, bunZip, 1<<10); err != nil || !strings.Contains(string(b), "ok") {
t.Fatalf("Bun's download through plugin downloads' proxy: %q %v", b, err)
}
if got := seen(); !slices.Contains(got, list) || !slices.Contains(got, reg) || !slices.Contains(got, bunZip) {
t.Fatalf("the proxy was asked %v", got)
}
// bun add and git get the proxy; the host (bun run) doesn't
proxyOf := func(env []string) string {
for _, e := range env {
if v, ok := strings.CutPrefix(e, "HTTPS_PROXY="); ok {
return v
}
}
return ""
}
if p := proxyOf(bunCommand(ctx, "bun", dir, "add", "--ignore-scripts", "x").Env); p != proxy.URL {
t.Fatalf("bun add's HTTPS_PROXY %q, want %q", p, proxy.URL)
}
if p := proxyOf(dlEnv()); p != proxy.URL {
t.Fatalf("git's HTTPS_PROXY %q, want %q", p, proxy.URL)
}
if p := proxyOf(bunCommand(ctx, "bun", dir, "run", "host.js").Env); p != "" {
t.Fatalf("the host, whose requests are API requests, got plugin downloads' proxy %q", p)
}
// plugin downloads direct, API requests through the proxy: the host
// keeps the proxy, and a download goes around it
if err := settings.Save(settings.Settings{Proxy: proxy.URL, PluginProxy: "direct"}); err != nil {
t.Fatal(err)
}
if p := proxyOf(bunCommand(ctx, "bun", dir, "run", "host.js").Env); p != proxy.URL {
t.Fatalf("the host's HTTPS_PROXY %q, want magpie's %q", p, proxy.URL)
}
if p := proxyOf(bunCommand(ctx, "bun", dir, "add", "x").Env); p != "" {
t.Fatalf("bun add went through %q with plugin downloads set direct", p)
}
n := len(seen())
if _, err := fetchJSONFrom(ctx, "http://other.example.invalid/registry.json", 1<<10, source.DoOfficial); err == nil {
t.Fatal("a download set direct was answered")
}
if len(seen()) != n {
t.Fatalf("a download set direct went through the proxy: %v", seen())
}
// unset, downloads follow the global proxy, as they always did
if err := settings.Save(settings.Settings{Proxy: proxy.URL}); err != nil {
t.Fatal(err)
}
const later = "http://plugins.example.invalid/later.json"
if _, err := fetchJSONFrom(ctx, later, 1<<10, source.DoOfficial); err != nil || !slices.Contains(seen(), later) {
t.Fatalf("an unset plugin downloads' proxy didn't follow the global one: %v %v", err, seen())
}
if p := proxyOf(bunCommand(ctx, "bun", dir, "add", "x").Env); p != proxy.URL {
t.Fatalf("bun add's HTTPS_PROXY %q, want the global %q", p, proxy.URL)
}
}