Files
magpie/internal/cli/update_auto_cli_test.go
T
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00

111 lines
3.4 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)
}
}
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)
}
}