Files
magpie/internal/gui/update_mirror_test.go
T
yetone 78a0dbdd29 gui: a download the network broke off says so, not the signed address
AIMetaPhD on X: Settings › About read "0.1.1618 下载失败 · https://github.com/
…/magpie-windows-amd64.exe: Get \"https://release-assets.githubusercontent.com/
…?sig=…&jwt=…\": EOF". The text was hundreds of characters of signature and
token, and it said nothing about what to do.

/api/update now carries network: true when update.Network(err) is true
(the previous commit's NetError, after its own tries). The Version row
then says "{v}'s download broke off, a network problem. Click Check to
try again, or download it from the release page", translated into zh,
zh-TW, ja and de. The raw error stays in the tooltip. The release page
button the row already had stays. A mirror's failure keeps its own text
(#893). begin() clears the flag along with the mirror.

Run: GUI tests (Playwright 1.62.0, Chromium and WebKit):
update-*.test.cjs (16 files, the new update-network among them) and
i18n-keys, 266 of 266 pass. go test ./internal/gui (ok, 224s); go
test ./internal/update ./internal/cli (ok, previous commit); gofmt -l
clean; go vet; GOOS=windows go vet; GOOS=linux go vet -tags nogui
(./internal/gui ./internal/update).

After rebasing both commits onto origin/main b5d17b26e (its i18n.js
changed), these were run again at this tree: go test ./internal/update
(ok), go test ./internal/gui -run 'Budget|I18n|Update|Asset' (ok), and
i18n-keys, update-network and update-mirror in both engines (36 of 36).

Broken by hand: with the row's new line turned off, update-network
is red, 11 of 11 in Chromium. TestUpdateMirrorSetting now also checks
that a mirror's 404 isn't flagged as a network failure.

Not tried: the real app on Windows, or a real dropped connection, and
the gui-*.test.cjs language suites beyond i18n-keys.
2026-10-27 01:37:04 +08:00

86 lines
3.5 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+"/" || j.Network || !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())
}
}