Files
magpie/internal/update/proxy_test.go
T
yetone 8718e0427d settings: app updates get their own proxy choice (moonlityu on X)
Settings > Network has a new "App updates" row under the plugin proxy row:
Same as above (default, today's behaviour), Direct, Custom or a saved proxy.
It is Settings.UpdateProxy / UpdateProxyCustom, kept per computer
(KeepOwn), left out of auto-backups with the other proxies, and listed in
the saved proxies' "in use" check, so a saved proxy used by updates can't be
deleted. update.proxied() wraps its transport: a request that names no
proxy of its own (magpie update --proxy does) takes the setting; unset, it
follows the global proxy as before. A value a user already set is never
changed.

Per-model proxy inside one provider is not added: a proxy belongs to a
provider, an account or a key, so a provider can't split by model without a
second provider on the same URL (noted in providers-accounts.md).

app.js stays within TestCoreScriptBudget by cutting five long comments, no
raise.

Run (Linux via wsl-run, rebased on origin/main 961427f12): go test of
internal/update, settings, settingsapi, backup and internal/gui (-tags
nogui) incl. TestCoreScriptBudget and the settings-keep guards; new
settings-update-proxy.test.cjs in chromium and webkit, 5 languages, 1100
and 420px; gofmt; go vet for darwin, windows and linux (nogui) on the
touched packages.
Broken by hand: removing the updateProxy wrapper turns
TestUpdateTakesItsOwnProxy red.
GUI reds named: settings-proxy-auth "optional proxy credentials" webkit
(expected 'name', actual ''; red on origin/main too, #5562, seen again);
saved-proxies "webkit de 420px" stopped passing with the third row because
the add form's Save button sat 1px below a 760px window and WebKit's
scrollTop stuck at 214 (#6130); that test's window is now 900px high.
Not tried: a real update behind a real proxy, the Windows/macOS apps,
WebKitGTK.
2026-11-11 17:23:14 +08:00

135 lines
4.6 KiB
Go

package update
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"slices"
"sync"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The update feed and a release's download go through the proxy set in
// Settings, as magpie's other requests do (#294): a network that reaches
// usemagpie.ai and GitHub only through it gets its updates too.
func TestUpdateGoesThroughTheProxy(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
t.Setenv(k, "")
}
body := []byte("new magpie")
sum := sha256.Sum256(body)
var mu sync.Mutex
var seen []string
// the proxy: a plain http:// request comes to it whole, by its URL
px := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
seen = append(seen, r.URL.String())
mu.Unlock()
if r.URL.Path == "/asset" {
w.Write(body)
return
}
json.NewEncoder(w).Encode(Release{Version: "9.9.9", Assets: map[string]Asset{
BinaryAsset(): {URL: "http://releases.magpie.invalid/asset", SHA256: hex.EncodeToString(sum[:])},
}})
}))
defer px.Close()
if err := settings.Save(settings.Settings{Proxy: px.URL}); err != nil {
t.Fatal(err)
}
// hosts that resolve nowhere: only the proxy can answer for them
t.Setenv("MAGPIE_UPDATE_FEED", "http://feed.magpie.invalid/api/latest")
rel, err := Latest(context.Background())
if err != nil {
t.Fatal(err)
}
if rel.Version != "9.9.9" {
t.Fatalf("version %q", rel.Version)
}
path := filepath.Join(t.TempDir(), "magpie.new")
if err := download(context.Background(), rel.Assets[BinaryAsset()], path); err != nil {
t.Fatal(err)
}
mu.Lock()
defer mu.Unlock()
if want := []string{"http://feed.magpie.invalid/api/latest", "http://releases.magpie.invalid/asset"}; !slices.Equal(seen, want) {
t.Fatalf("through the proxy: %v, want %v", seen, want)
}
}
// Updates take a proxy of their own (moonlityu on X: models direct, updates
// through a proxy): UpdateProxy is used while the global proxy is direct,
// a proxy given to the run comes first, and unset follows the global one.
func TestUpdateTakesItsOwnProxy(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
t.Setenv(k, "")
}
var mu sync.Mutex
hits := map[string]int{}
serve := func(name string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
hits[name]++
mu.Unlock()
json.NewEncoder(w).Encode(Release{Version: "9.9.9"})
}))
}
own, global, run := serve("own"), serve("global"), serve("run")
defer own.Close()
defer global.Close()
defer run.Close()
t.Setenv("MAGPIE_UPDATE_FEED", "http://feed.magpie.invalid/api/latest")
ask := func(ctx context.Context, s settings.Settings) {
t.Helper()
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if _, err := Latest(ctx); err != nil {
t.Fatal(err)
}
}
want := func(own_, global_, run_ int) {
t.Helper()
mu.Lock()
defer mu.Unlock()
if hits["own"] != own_ || hits["global"] != global_ || hits["run"] != run_ {
t.Fatalf("hits own/global/run = %d/%d/%d, want %d/%d/%d", hits["own"], hits["global"], hits["run"], own_, global_, run_)
}
}
// global direct, updates through their own proxy
ask(context.Background(), settings.Settings{Proxy: "direct", UpdateProxy: own.URL})
want(1, 0, 0)
// the global proxy elsewhere: the update's own still wins
ask(context.Background(), settings.Settings{Proxy: global.URL, UpdateProxy: own.URL})
want(2, 0, 0)
// a proxy given to this run (magpie update --proxy) comes first
ask(WithProxy(context.Background(), run.URL), settings.Settings{Proxy: global.URL, UpdateProxy: own.URL})
want(2, 0, 1)
// unset: the global proxy, as before
ask(context.Background(), settings.Settings{Proxy: global.URL})
want(2, 1, 1)
// direct for updates while the global proxy is set: neither proxy is asked
// (the feed host resolves nowhere, so the request fails)
if err := settings.Save(settings.Settings{Proxy: global.URL, UpdateProxy: "direct"}); err != nil {
t.Fatal(err)
}
if _, err := Latest(context.Background()); err == nil {
t.Fatal("a direct update reached a host that resolves nowhere")
}
want(2, 1, 1)
}