Files
magpie/internal/gui/update_mirror_test.go
T
yetone dab887db57 update, gui: Settings › About › Download source sets the update mirror, and a download that failed through it offers GitHub (#893, M3chD09) — the mirror updates could come through was only magpie update mirror in a terminal, and the GUI never said where updates came from or that a failed download was the mirror's. About now has a Download source row: GitHub, or the mirror set (the same UpdateMirror setting the CLI writes); Use a mirror… takes a full https:// address (http, a bare host or a malformed one refused, with the reason in the row and the draft kept), Change edits it, Use GitHub takes it away in one click; Enter saves, Esc cancels, no click scrolls. POST /api/settings/update-mirror sets it; the Settings page's own save keeps it. A download through the mirror that doesn't answer, answers other than 200, or sends a file whose SHA-256 isn't usemagpie.ai's feed's is an update.MirrorError (never installed, the file removed, as before); /api/update's error carries the mirror, and the Version row says the download failed through that mirror and offers Download from GitHub, which takes the mirror away and downloads again. magpie names no mirror or CDN of its own. en/zh/ja/de. TestMirrorFailuresAreTheMirrors (tampered, 404 and closed mirrors each the mirror's, no file kept; a direct failure no mirror's; CheckMirrorURL) and TestUpdateMirrorSetting (set trimmed, http/bare/ftp/empty-host refused, Settings save keeps it, off, an updater failing through a 404 mirror reports it with retry, a new check clears it) are new; update-mirror.test.cjs (WebKit, en and zh: GitHub shown, http refused with the reason, https saved with Enter, Use GitHub posts "", a failed download through the mirror offers GitHub, which posts "" then update/install; no select, no left border, no scroll) fails without the change; update-auto, cli-path, cli-behind, settings-sections and click-scroll pass in WebKit. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 18:49:04 +08:00

86 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/update"
)
// Settings › About › Download source (#893): the mirror is set and taken
// away on its own, the same setting `magpie update mirror` writes, a full
// https:// address only, and the Settings page's save keeps it. A download
// that fails through it says so (the page's mirror), which a check clears.
func TestUpdateMirrorSetting(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"} {
t.Setenv(k, "")
}
call := func(method, path, body string) (int, string) {
t.Helper()
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
return rec.Code, rec.Body.String()
}
const m = "https://mirror.example/gh/"
code, body := call("POST", "/api/settings/update-mirror", `{"mirror":" `+m+` "}`)
if code != http.StatusOK || settings.Load().UpdateMirror != m || !strings.Contains(body, `"updateMirror":"`+m+`"`) {
t.Fatalf("set: %d %s, saved %q", code, body, settings.Load().UpdateMirror)
}
for _, bad := range []string{"http://mirror.example/", "mirror.example", "ftp://mirror.example/", "https://"} {
if code, body = call("POST", "/api/settings/update-mirror", `{"mirror":"`+bad+`"}`); code == http.StatusOK || settings.Load().UpdateMirror != m || !strings.Contains(body, "full https://") {
t.Errorf("%s: %d %s, saved %q", bad, code, body, settings.Load().UpdateMirror)
}
}
// the Settings page's save, which doesn't send it, keeps it
if code, body = call("POST", "/api/settings", `{"theme":"dark","updateMirror":""}`); code != http.StatusOK || settings.Load().UpdateMirror != m {
t.Fatalf("save: %d %s, mirror %q", code, body, settings.Load().UpdateMirror)
}
// GitHub again
if code, body = call("POST", "/api/settings/update-mirror", `{"mirror":""}`); code != http.StatusOK || settings.Load().UpdateMirror != "" || strings.Contains(body, `"updateMirror"`) {
t.Fatalf("off: %d %s", code, body)
}
// a download through a mirror that answers 404: the error is the mirror's
mirror := httptest.NewServer(http.NotFoundHandler())
defer mirror.Close()
feed := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(update.Release{Version: "0.1.11", Assets: map[string]update.Asset{
update.BinaryAsset(): {URL: "https://github.com/yetone/magpie-releases/releases/download/v0.1.11/x", SHA256: strings.Repeat("0", 64)},
}})
}))
defer feed.Close()
t.Setenv("MAGPIE_UPDATE_FEED", feed.URL)
if err := settings.Save(settings.Settings{UpdateMirror: mirror.URL + "/"}); err != nil {
t.Fatal(err)
}
old := Version
Version = "0.1.10"
defer func() { Version = old }()
exe, err := update.Executable()
if err != nil {
t.Fatal(err)
}
u := &updater{exe: exe}
u.self, _ = os.Stat(exe)
defer os.Remove(exe + ".new")
u.check()
j := u.json()
if j.State != "error" || !j.Retry || j.Mirror != mirror.URL+"/" || !strings.Contains(j.Error, mirror.URL) {
t.Fatalf("through the mirror: %+v", j)
}
// the next check starts afresh: no mirror said while it runs
if !u.begin() || u.json().Mirror != "" {
t.Fatalf("begin kept the mirror: %+v", u.json())
}
}