Files
magpie/internal/cli/update_auto_cli_test.go
T
yetone ff670797a1 gui: an update found waits for Download update when Settings' Download updates automatically is off (#2296, meifu2027)
On a metered network or a proxy billed by traffic, a newer version was
downloaded as soon as a check found it, by the clock or by Settings'
Check. Settings → About now has "Download updates automatically", on by
default (as before). Off (settings.json noAutoDownload):

- a check, by the clock or by a click, only reports the version: the
  version row says "{v} is out · {size} MB" with its notes, and the
  header's Update pill shows; the updater's state is "found";
- "Download update" on that row, or a click on the pill, posts
  update/install, which downloads it (updater.download) as a retry does;
- a version downloaded on a click stays ready: a newer one found later
  isn't fetched over it;
- a settings save that doesn't send the field keeps it (api.go), so an
  older page can't switch it back on.

CLI: `magpie update auto notify|download` sets it, and the status says
which. The proxy-change retry in api.go goes through the same run, so it
respects the setting too.

hold() (the click guard that keeps what was clicked in place) took a
zero-height element as its anchor: the empty .val beside Settings' Add
to PATH. "Moved by less than half its height" never held for it, so
once the new row made About 27px taller than a 900x760 window, the click
slid the view 11px off its top (cli-path.test.cjs, red on this branch,
green on origin/main where About couldn't scroll). hold now prefers an
anchor with a height, and falls back to the old choice.

Run:
- go test -tags nogui ./internal/gui ./internal/cli ./internal/settings
  (sandbox HOME): ok (gui 547s, cli 678s; settings ok 253s). A first run
  with the default 10m timeout lost cli to it at load ~100
- TestUpdateDownloadWaitsForAClick broken by hand three ways, red each
  time: no `case hold` (Check gave ready, 1 download); no api.go keep
  ("a save without noAutoDownload turned it off"); no `|| hold` ("a
  newer one, downloads held: found 0.1.12").
- gofmt -l clean; go vet -tags nogui for darwin, linux and windows on
  gui, cli and settings: ok.
- GUI: update-download.test.cjs (en, zh, zh-TW, ja, de at 1100 and 420)
  passes in Chromium and WebKit; breaking the found case times it out.
  cli-path, click-scroll and update-download pass in both engines after
  the hold fix. Every GUI test that checks scrolling (327
  files): Chromium 2261 pass, WebKit 2263 pass,
  6-wide at load ~100. Red: refusal-retries-setting ja 420 (#2258; base
  95e7c6bcc red 2 of 3 alone too), usage-agents-hide (#2276) and
  usage-fold (#2163), red alone on base as well; fonts (chromium) and
  sessions-manage-paging (webkit zh) red only in the loaded run, green
  alone twice on both. gui-ja, gui-de, gui-zh-tw, i18n-keys and
  update-stuck: 32/32 in each engine.
- Not tried: a real metered connection or a real release download.

Full suite: SUITE_LINE
2026-10-15 23:08:03 +08:00

125 lines
4.0 KiB
Go

package cli
import (
"bytes"
"os"
"testing"
"github.com/yetone/magpie/internal/settings"
)
func TestUpdateAutoKeepsCorruptSettings(t *testing.T) {
for _, tc := range []struct{ name, body string }{
{"truncated", `{"theme":"dark","githubToken":"SYNTHETIC_PRIVATE_TOKEN"`},
{"theme type", `{"theme":7,"githubToken":"SYNTHETIC_PRIVATE_TOKEN"}`},
} {
t.Run(tc.name, func(t *testing.T) {
groupsHome(t)
if err := os.MkdirAll(settings.Dir(), 0o755); err != nil {
t.Fatal(err)
}
original := []byte(tc.body)
if err := os.WriteFile(settings.Path(), original, 0o600); err != nil {
t.Fatal(err)
}
if err := updateCmd([]string{"update", "auto", "off"}); err == nil {
t.Error("the command reported success with corrupt settings")
}
if after, err := os.ReadFile(settings.Path()); err != nil || !bytes.Equal(after, original) {
t.Errorf("the command overwrote the corrupt settings: %v", err)
}
})
}
}
func TestUpdateAutoRecoversEmptySettings(t *testing.T) {
for _, body := range []string{"", " \t\r\n", "\ufeff \t\r\n"} {
t.Run(body, func(t *testing.T) {
groupsHome(t)
if err := os.MkdirAll(settings.Dir(), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(settings.Path(), []byte(body), 0o600); err != nil {
t.Fatal(err)
}
if err := updateCmd([]string{"update", "auto", "off"}); err != nil {
t.Fatal(err)
}
if !settings.Load().NoAutoUpdate {
t.Fatal("the update preference was not saved")
}
})
}
}
func TestUpdateAutoWithBOMSettings(t *testing.T) {
groupsHome(t)
if err := os.MkdirAll(settings.Dir(), 0o755); err != nil {
t.Fatal(err)
}
const token = "SYNTHETIC_PRIVATE_TOKEN"
body := "\ufeff" + `{"theme":"dark","proxy":"direct","lang":"zh","githubToken":"` + token + `"}`
if err := os.WriteFile(settings.Path(), []byte(body), 0o600); err != nil {
t.Fatal(err)
}
if err := updateCmd([]string{"update", "auto", "off"}); err != nil {
t.Fatal(err)
}
got := settings.Load()
if !got.NoAutoUpdate || got.Theme != "dark" || got.Proxy != "direct" || got.Lang != "zh" || got.GitHubToken != token {
t.Fatal("the update command lost fields from the BOM file")
}
}
// magpie update auto turns the app's own update checks off and on and sets
// how often they run, from the presets only (#472).
func TestUpdateAutoCmd(t *testing.T) {
groupsHome(t)
if err := updateCmd([]string{"update", "auto"}); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.NoAutoUpdate || s.UpdateEvery != 360 {
t.Fatalf("default: off %v, every %d", s.NoAutoUpdate, s.UpdateEvery)
}
if err := updateCmd([]string{"update", "auto", "off", "30m"}); err != nil {
t.Fatal(err)
}
if s := settings.Load(); !s.NoAutoUpdate || s.UpdateEvery != 30 {
t.Fatalf("off 30m: off %v, every %d", s.NoAutoUpdate, s.UpdateEvery)
}
if err := updateCmd([]string{"update", "auto", "on"}); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.NoAutoUpdate || s.UpdateEvery != 30 {
t.Fatalf("on: off %v, every %d", s.NoAutoUpdate, s.UpdateEvery)
}
for _, a := range []string{"1h", "24h", "6h"} {
if err := updateCmd([]string{"update", "auto", a}); err != nil {
t.Fatal(err)
}
if got := settings.Load().UpdateEvery; got != updateEveries[a] {
t.Fatalf("%s: every %d", a, got)
}
}
// notify: a version found waits for a click to be downloaded (#2296),
// the other choices left as they are; download puts it back
if err := updateCmd([]string{"update", "auto", "notify"}); err != nil {
t.Fatal(err)
}
if s := settings.Load(); !s.NoAutoDownload || s.NoAutoUpdate || s.UpdateEvery != 360 {
t.Fatalf("notify: download held %v, off %v, every %d", s.NoAutoDownload, s.NoAutoUpdate, s.UpdateEvery)
}
if err := updateCmd([]string{"update", "auto", "download"}); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.NoAutoDownload {
t.Fatal("download: still held")
}
if err := updateCmd([]string{"update", "auto", "2h"}); err == nil {
t.Fatal("2h taken")
}
if got := settings.Load().UpdateEvery; got != 360 {
t.Fatalf("2h refused, yet every %d", got)
}
}