Files
magpie/internal/gui/instance_name_test.go
T
yetone ead91a9a50 gui: a browser tab's title names this magpie: MAGPIE_NAME or Settings › Network and sharing › Advanced › Name (starballl on X)
starballl on X (tweet 2122376116220633444) runs several magpie web pages
and every tab is titled "magpie", so they can't be told apart. A magpie
now has a name, and a browser tab's title is "<name> · magpie" (plain
"magpie" with none).

- settings.Name, set in Settings › Network and sharing › Advanced › Name
  (instancename.js, imported when Settings is drawn, so nothing is added
  to app.js's core). Advanced is closed until a name is set.
- MAGPIE_NAME (Docker, magpie web) names it while the field is empty and
  is the field's placeholder. It is read, never written, so a name typed
  in Settings always wins and clearing the field goes back to it.
- One line, at most 60 characters (settings.CleanName).
- This computer's own (KeepOwn): sync and a restored backup never bring
  another magpie's name. A Settings save that leaves `name` out keeps it
  (settingsapi.Merge).
- boot.js titles a browser tab itself for the first paint, so app.js's
  core gains only the line in applyPrefs, which sets the title again from
  every settings read (TestCoreScriptBudget was 74 bytes over with a
  title function in app.js). Only a browser page (web) is retitled: the
  app's windows and tray keep "magpie".
- Docker docs (en, zh, ja) list MAGPIE_NAME; docs/reference.md and
  docs/subsystems/gui-shell.md say how it works.

Tests:
- TestInstanceName (internal/gui): the variable alone names it at boot;
  a saved name wins over it, trimmed; a save without `name` keeps it;
  KeepOwn keeps it; cleared goes back to the variable; no name, none at
  boot. Broken by hand, each red: KeepOwn without Name ("KeepOwn took
  \"other\""), Merge without the keep ("a save without the name changed
  it to \"\""), boot.js with s.Name in place of InstanceName ("named by
  the variable alone: boot <nil>"), boot.js not setting the title ("a
  browser tab's boot.js sets no title"). The app window's boot.js sets
  none.
- TestSettingsSaveKeepsWhatItDoesNotSend now sets Name: the page's other
  saves leave it out, and it is kept.
- instance-name.test.cjs: title from the first paint, the field under a
  closed Advanced, save and clear retitle the tab, Advanced open after a
  reload, 360px, the app's window keeps "magpie"; en, zh, zh-TW, ja, de,
  Chromium and WebKit, 16/16. 14 of 16 red with applyPrefs not setting
  the title.

Run, at this commit on dbbb2f26b plus 9d5d8baf2 (the site index):
- go test ./... -count=1 -timeout 45m on Linux (WSL magpie-desktop, Go
  1.26.8), at the tree before the boot.js move: 99 packages ok,
  internal/gateway included (959s); ., internal/cli and internal/gui
  don't build there without GTK headers. On mica (macOS): . and
  internal/cli ok; internal/gui/... ok at this commit, after the two
  reds that run found (the core budget, the keep test) were fixed.
- go vet and GOOS=linux -tags nogui / GOOS=windows go vet of
  internal/gui and internal/settings, earlier the whole tree on all
  three: ok. gofmt -l clean.
- site: node --test *.test.mjs, 132/132.
- GUI: the 135 files the change reaches (settings, network, LAN, i18n,
  old-webkit, core budget) on mica: 16 files had reds. Run again on this
  tree and on its base, the same tests are red on both (allow-ips
  #4357, settings-hints #6107/#6353, lan-join #6353, i18n-keys #6877,
  old-webkit #6030/#5919 and the rest of the 16) but two:
  search-mcp-library (WebKit, #4756) and gateway-pause (WebKit de 420px,
  #6684), red only beside other files: alone, 0 of 10 and 0 of 3 red on
  both trees.
2026-11-17 03:11:31 +08:00

91 lines
3.4 KiB
Go

package gui
import (
"encoding/json"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// A magpie is named for its browser tab's title (starballl on X: several
// deployed, every tab titled "magpie"): MAGPIE_NAME names it until a name
// is set in Settings, which then wins and is never written over by the
// variable; boot.js hands the page the name in use and the variable's,
// and titles a browser tab from the first paint; a
// save of the rest of the page keeps it; sync never carries it.
func TestInstanceName(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv(settings.NameEnv, " nas \n home ")
srv := Handler(nil, nil)
post := func(body string) {
t.Helper()
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("POST", "/api/settings", strings.NewReader(body)))
if rec.Code != 200 {
t.Fatalf("settings returned %d: %s", rec.Code, rec.Body)
}
}
boot := func() map[string]any {
t.Helper()
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("GET", "/boot.js", nil))
var got map[string]any
if err := json.Unmarshal([]byte(strings.TrimSuffix(strings.TrimPrefix(rec.Body.String(), "window.bootPrefs = "), ";\n")), &got); err != nil {
t.Fatal(err)
}
return got
}
if b := boot(); b["name"] != "nas home" || b["nameEnv"] != "nas home" {
t.Fatalf("named by the variable alone: boot %v %v", b["name"], b["nameEnv"])
}
if st := settingsState(); st.NameEnv != "nas home" || st.Name != "" {
t.Fatalf("state: env %q, name %q", st.NameEnv, st.Name)
}
post(`{"theme":"dark","lang":"en","name":" Tokyo box "}`)
if got := settings.Load().Name; got != "Tokyo box" {
t.Fatalf("saved %q", got)
}
if b := boot(); b["name"] != "Tokyo box" || b["nameEnv"] != "nas home" {
t.Fatalf("the name set wins over the variable: boot %v %v", b["name"], b["nameEnv"])
}
// a browser tab is titled from the first paint; the app's window isn't
tab := httptest.NewRecorder()
Handler(webTab{}, nil).ServeHTTP(tab, httptest.NewRequest("GET", "/boot.js", nil))
if !strings.Contains(tab.Body.String(), "\ndocument.title = \"Tokyo box · magpie\";\n") {
t.Fatalf("a browser tab's boot.js sets no title: %.300s", tab.Body)
}
app := httptest.NewRecorder()
srv.ServeHTTP(app, httptest.NewRequest("GET", "/boot.js", nil))
if strings.Contains(app.Body.String(), "document.title") {
t.Fatalf("the app window's boot.js sets a title: %.300s", app.Body)
}
post(`{"theme":"light","lang":"en"}`)
if got := settings.Load().Name; got != "Tokyo box" {
t.Fatalf("a save without the name changed it to %q", got)
}
if long := strings.Repeat("名", 80); settings.CleanName(long) != strings.Repeat("名", settings.NameMost) {
t.Fatalf("a long name kept whole")
}
// another computer's name never comes with a sync or a backup
from := settings.Settings{Name: "other"}
from.KeepOwn(settings.Load())
if from.Name != "Tokyo box" {
t.Fatalf("KeepOwn took %q", from.Name)
}
post(`{"theme":"light","lang":"en","name":""}`)
if b := boot(); settings.Load().Name != "" || b["name"] != "nas home" {
t.Fatalf("cleared: saved %q, boot %v", settings.Load().Name, b["name"])
}
t.Setenv(settings.NameEnv, "")
if _, ok := boot()["name"]; ok {
t.Fatal("no name at all, still one at boot")
}
}