Files
magpie/internal/settings/dock_test.go
T
yetone 8e5567161e settings: an older settings.json with no Dock choice keeps Hide
v0.1.2060 (1407c9a3b) made With window the default for a settings file
with no Dock choice. Before it, Hide was the default and was stored as
nothing, so a user who picked Hide can't be told from one who never
chose, and v0.1.2060 put both in the Dock and Command-Tab. That broke
"never let magpie's default override a value the user set".

What changes:
- A new field, dockSettled, marks a file whose Dock choice is read with
  With window as the default.
- A file without it is read the way an older magpie read it
  (settleDock): no Dock, dockWindow or dockNever means Hide (dockNever),
  and the next save writes it settled. This happens once.
- A missing file (a new install) is settled from the start, so it gets
  With window.
- A file nobody can read is read like an older one, Hide.
- The Settings page never sends dockSettled; the API save keeps the
  file's. Without that, a new install's first save from the page would
  turn into Hide.
- KeepOwn keeps it machine-local, like the choice itself.
- An older magpie writing the file drops the marker, and the file then
  reads as old (Hide unless it chose), which is what that magpie meant.
- v0.1.2060 users who picked a choice keep it. Those who picked nothing
  are back to Hide and can choose With window under Settings › Show in
  Dock.

Tests:
- TestOlderSettingsKeepHide: {} and {"theme":"dark"} read and save as
  Hide; dockWindow, dock and dockNever are kept; a second read is
  unchanged.
- TestNewInstallFollowsWindow: no file means With window, settled, and
  the first save reads back the same.
- TestSettingsSaveKeepsDockReading (gui, !nogui): a POST /api/settings
  from the page keeps a new install in the Dock while open, and keeps an
  old file's Hide.
- TestLoadChecked's unreadable-file default now expects the settled Hide.

Broken by hand (each red, then restored):
- settleDock not setting dockNever: TestOlderSettingsKeepHide red,
  "{"theme":"dark"} read as ... never false".
- The missing-file branch not settling: TestNewInstallFollowsWindow red,
  "settled false".
- api.go not carrying DockSettled: TestSettingsSaveKeepsDockReading red,
  "a new install: after a save from the page ... in the Dock ... false".

Run at this commit's tree (9bdb831b7 before this body, on origin/main
66b5e4a84), on mote via mote-run:
- gofmt -l: clean.
- go vet ./... (darwin), GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./...: all ok.
- go test -count=1 -timeout 45m ./internal/settings ./internal/gui:
  settings ok. In gui the only red is TestMainWindowCorners/macOS_26_SDK,
  which is red on origin/main on mote too (#3949).
- node --test dock-choice, settings-general and i18n-keys: 34/34 (5
  languages, chromium and webkit, 1000px and 440px).

Real app on mote: a -tags dev build, bundled as com.yetone.magpie.sbtest,
in a sandbox HOME on 127.0.0.1:3591/3592, launched with its window open,
type read with lsappinfo:
- An old {"theme":"dark"} file: UIElement (not in Command-Tab); the file
  now has dockNever and dockSettled.
- No file: Foreground; dockSettled is set, dockNever is not.
- An old {"dockWindow":true} file: Foreground.

Not tried:
- Closing the window by hand on mote, since ssh has no Accessibility.
- A downgrade to v0.1.2060 and back again with a real file (reasoned
  above).
2026-10-29 20:53:36 +08:00

111 lines
3.1 KiB
Go

package settings
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/lastgood"
)
// Show in Dock's Hide (DockNever) is this computer's own, as Dock and
// DockWindow are: a sync or restored backup from a Mac that left it as it
// came never takes a Hide chosen here away, nor brings one.
func TestDockNeverIsMachineLocal(t *testing.T) {
from := Settings{}
from.KeepOwn(Settings{DockNever: true})
if !from.DockNever {
t.Fatal("a sync cleared this Mac's Hide")
}
from = Settings{DockNever: true}
from.KeepOwn(Settings{})
if from.DockNever {
t.Fatal("a sync brought another Mac's Hide")
}
from = Settings{}
from.KeepOwn(Settings{DockSettled: true})
if !from.DockSettled {
t.Fatal("a sync took how this Mac's Dock choice is read")
}
}
func dockHome(t *testing.T) {
t.Helper()
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
lastgood.Reset()
t.Cleanup(lastgood.Reset)
if err := os.MkdirAll(Dir(), 0o755); err != nil {
t.Fatal(err)
}
}
// A settings file written before With window became the default (no
// dockSettled) stored Hide as nothing, so one with no Dock choice is read,
// and written back, as Hide: the new default never moves a user who chose
// Hide into the Dock. A choice the file has is kept as it is.
func TestOlderSettingsKeepHide(t *testing.T) {
for _, c := range []struct {
file string
dock, wind bool
never bool
}{
{`{"theme":"dark"}`, false, false, true},
{`{}`, false, false, true},
{`{"dockWindow":true}`, false, true, false},
{`{"dock":true}`, true, false, false},
{`{"dockNever":true}`, false, false, true},
} {
dockHome(t)
if err := os.WriteFile(Path(), []byte(c.file), 0o600); err != nil {
t.Fatal(err)
}
s, err := LoadChecked()
if err != nil {
t.Fatal(err)
}
if s.Dock != c.dock || s.DockWindow != c.wind || s.DockNever != c.never || !s.DockSettled {
t.Fatalf("%s read as dock %v window %v never %v settled %v", c.file, s.Dock, s.DockWindow, s.DockNever, s.DockSettled)
}
if err := Save(s); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(Path())
var w Settings
if err := json.Unmarshal(b, &w); err != nil {
t.Fatal(err)
}
if w.DockNever != c.never || !w.DockSettled {
t.Fatalf("%s written back as %s", c.file, b)
}
// read again, it stays: the migration runs once
if s, _ = LoadChecked(); s.DockNever != c.never {
t.Fatalf("%s changed on a second read: never %v", c.file, s.DockNever)
}
}
}
// A new install (no settings file) has With window: no Hide, and what it
// saves says so, so the next read doesn't take it for an older file.
func TestNewInstallFollowsWindow(t *testing.T) {
dockHome(t)
os.Remove(Path())
s, err := LoadChecked()
if err != nil {
t.Fatal(err)
}
if s.Dock || s.DockNever || !s.DockSettled {
t.Fatalf("a new install: dock %v never %v settled %v", s.Dock, s.DockNever, s.DockSettled)
}
s.Theme = "dark"
if err := Save(s); err != nil {
t.Fatal(err)
}
if s, _ = LoadChecked(); s.DockNever || !s.DockSettled {
t.Fatalf("a new install's first save read back as never %v settled %v", s.DockNever, s.DockSettled)
}
}