Files
magpie/internal/cli/settings_cli_test.go
T
yetone 5dfbca6274 cli: magpie settings list|get|set|reset, through the window's own save (#4745)
Stage 1 of #4745 (StringKe): every setting POST /api/settings saves can
be read and set from the terminal.

- internal/settingsapi holds the save the handler made: Merge (the
  carry of keys a page leaves out, moved verbatim from api.go, plus
  upstream's RouterNote line), the vision/imageGen/searcher checks,
  settings.Save's own checks, catalog.Touched. POST /api/settings and
  `magpie settings` both call settingsapi.Apply, so a value is checked
  and saved one way. It sits outside internal/gui so the nogui build
  has it.
- `magpie settings` (list, the default, with value, default and
  choices; get; set; reset to settings.Defaults()), --json on list and
  get. Body sends the whole file with one key changed, so a key the
  save keeps when absent (noBird, refusalRetries, ...) is set even to
  off. A flag where a key or value goes, an unknown key (with near
  matches named) or a refused value writes nothing and exits 1;
  --help prints usage.
- Parity guards: TestKeysAreWhatTheSaveWrites (each Settings field sent
  changed on its own; what the save takes must equal Keys),
  TestSettingsCLISetsWhatTheWindowSaves (every key the page's prefsKeep
  and savePrefs send is a key), TestEveryRouteHasACommandOrIsNamed
  (each /api route has a command or a reason in notYet, which later
  stages shrink; 36 have one now).
- Item 3 of the stage (per-provider CLI-only fields in the window) is
  left for stage 2.
- The open app's in-process follow-ups (fonts, Dock, tray menu, alerts,
  language, update re-check) run only from the window's save: #4752.

Run (mote, at the pushed head 1c3591b5d + this):
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; GOOS=linux go build -tags nogui;
  GOOS=windows go build: clean (before rebase, the vets again after).
- go test internal/settingsapi, internal/settings, internal/cli: ok.
  internal/gui -run Settings|Prefs|Keep after the rebase: ok. The full
  internal/gui run before the rebase had three reds:
  TestSettingsSaveKeepsWhatItDoesNotSend (BusyLoad, fixed on main in
  b646a9127), TestColdTurnsTaken (fixed on main in 647323409) and
  TestMainWindowCorners/macOS_26_SDK (#3949, mote is macOS 27).
- Real run in a sandbox HOME on mote with a magpie built from this:
  settings --help / help, list, get, set theme dark, noBird on,
  redactWords "acme, globex", usageAlert 85; refused theme neon, `them`
  (names theme), usageAlert lots, trayUsageEvery 4, vision
  nobody/nothing, each exit 1 with settings.json unchanged; set
  usageAlert --help printed usage and wrote nothing. Then `magpie web`
  with MAGPIE_WEB_KEY: GET /api/settings read back each value the CLI
  set; POST /api/settings lang ja, then `settings get lang` said ja;
  reset theme gave system.

Broken by hand, each red, then put back: noBird dropped from Keys
(TestKeys, TestBodySetsOneKeyOnly, the CLI set test and the GUI parity
test red); Body dropping false values (TestBodySetsOneKeyOnly and the
CLI "noBird off" check red); a route taken out of notYet and another
renamed (TestEveryRouteHasACommandOrIsNamed red, 3 errors). On the old
code the new tests don't compile (no settingsapi, no command).

Not tried: a real run on Windows or Linux; the live app reacting to a
CLI write while open (#4752); the GUI node suite (no page file changed).
2026-11-01 15:57:43 +08:00

150 lines
4.9 KiB
Go

package cli
import (
"encoding/json"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/settingsapi"
)
// settingsHome is a home of its own whose settings.json has the user's
// own choices in it, and gives that file's bytes.
func settingsHome(t *testing.T) []byte {
t.Helper()
helpHome(t)
s := settings.Defaults()
s.Theme, s.NoBird, s.Currency, s.Proxy = "dark", true, "cny", "http://127.0.0.1:7890"
s.RedactWords = []string{"acme"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(settings.Path())
if err != nil {
t.Fatal(err)
}
return b
}
func TestSettingsSetGetReset(t *testing.T) {
settingsHome(t)
if _, err := runOut(t, "settings", "set", "lang", "ja"); err != nil {
t.Fatal(err)
}
if out, err := runOut(t, "settings", "get", "lang"); err != nil || strings.TrimSpace(out) != "ja" {
t.Fatalf("get lang = %q, %v", out, err)
}
s := settings.Load()
if s.Lang != "ja" || s.Theme != "dark" || !s.NoBird || s.Currency != "cny" || s.Proxy != "http://127.0.0.1:7890" || len(s.RedactWords) != 1 {
t.Fatalf("setting lang changed what the user set: %+v", s)
}
// a switch to off is sent, not left out as a zero value
if _, err := runOut(t, "settings", "set", "noBird", "off"); err != nil {
t.Fatal(err)
}
if settings.Load().NoBird {
t.Error("noBird off stayed on")
}
// numbers, lists, and a value the save puts in its own form
for _, c := range []struct{ key, value, want string }{
{"usageAlert", "80", "80"},
{"trayUsageEvery", "10", "10"},
{"redactWords", "acme, globex", "acme,globex"},
{"redactWords", `["a","b"]`, "a,b"},
{"redactWords", "", "[]"},
{"codexWarmup", "off", `""`},
{"refusalRetries", "2", "2"},
{"otel", `{"enabled":true,"endpoint":"http://127.0.0.1:4318"}`, ""},
} {
if _, err := runOut(t, "settings", "set", c.key, c.value); err != nil {
t.Fatalf("set %s %q: %v", c.key, c.value, err)
}
out, _ := runOut(t, "settings", "get", c.key)
if c.want != "" && strings.TrimSpace(out) != c.want {
t.Errorf("set %s %q, then get = %q, want %q", c.key, c.value, out, c.want)
}
}
if _, err := runOut(t, "settings", "reset", "theme"); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.Theme != "system" || s.Currency != "cny" {
t.Errorf("reset theme: theme %q currency %q", s.Theme, s.Currency)
}
if _, err := runOut(t, "settings", "reset", "refusalRetries"); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.RefusalRetries != nil {
t.Errorf("reset refusalRetries left %d", *s.RefusalRetries)
}
}
// A key or value that isn't one, and a flag where a value goes, write
// nothing and say why.
func TestSettingsRefusesAndWritesNothing(t *testing.T) {
before := settingsHome(t)
for _, args := range [][]string{
{"settings", "set", "nope", "1"},
{"settings", "set", "port", "9999"}, // a route of its own
{"settings", "set", "theme", "neon"}, // not one of its choices
{"settings", "set", "noBird", "maybe"},
{"settings", "set", "usageAlert", "lots"},
{"settings", "set", "usageAlert", "200"}, // the save's own range
{"settings", "set", "trayUsageEvery", "4"},
{"settings", "set", "vision", "nobody/nothing"},
{"settings", "set", "otel", `{"bogus":1}`},
{"settings", "set", "theme", "--help-me"},
{"settings", "set", "theme"},
{"settings", "reset", "nope"},
{"settings", "get", "nope"},
{"settings", "frob"},
} {
out, err := runOut(t, args...)
if err == nil {
t.Errorf("magpie %s: no error (%q)", strings.Join(args, " "), out)
}
after, _ := os.ReadFile(settings.Path())
if string(after) != string(before) {
t.Fatalf("magpie %s changed settings.json", strings.Join(args, " "))
}
}
_, err := runOut(t, "settings", "set", "them", "dark")
if err == nil || !strings.Contains(err.Error(), "theme") {
t.Errorf("a near miss doesn't name the key: %v", err)
}
// a negative number is a value, not a flag
_, err = runOut(t, "settings", "set", "usageAlert", "-1")
if err == nil || strings.Contains(err.Error(), "flag") {
t.Errorf("-1 read as a flag or saved: %v", err)
}
}
func TestSettingsListAndJSON(t *testing.T) {
settingsHome(t)
out, err := runOut(t, "settings", "list", "--json")
if err != nil {
t.Fatal(err)
}
var rows []settingRow
if err := json.Unmarshal([]byte(out), &rows); err != nil {
t.Fatalf("list --json: %v\n%s", err, out)
}
if len(rows) != len(settingsapi.Keys) {
t.Fatalf("list --json has %d rows, Keys %d", len(rows), len(settingsapi.Keys))
}
for _, r := range rows {
if r.Key == "theme" && (r.Value != "dark" || r.Default != "system") {
t.Errorf("theme row: %+v", r)
}
}
out, err = runOut(t, "settings")
if err != nil || !strings.Contains(out, "theme") || !strings.Contains(out, "default system") {
t.Errorf("settings list: %v\n%s", err, out)
}
out, err = runOut(t, "settings", "get", "redactWords", "--json")
if err != nil || strings.TrimSpace(strings.Join(strings.Fields(out), "")) != `["acme"]` {
t.Errorf("get redactWords --json = %q, %v", out, err)
}
}