Files
magpie/internal/gui/profile_rename_test.go
T
yetoneanderlinerd 1cb4d43013 profiles: a saved profile renames, in its details, the TUI and magpie rename (#2721, erlinerd)
erlinerd's patch on #2721 (profile.Rename, /api/state profile/rename,
a Rename button in the details), reviewed against main and adapted:

- profile.Rename trims both names, refuses an empty, taken or missing
  name and leaves profiles.json as it was, renames by case only
  (home -> Home), and keeps what the profile holds.
- GUI: Rename turns the details' name into a field. Enter renames
  through profileAction, so the chip is renamed at once, pending, and
  rolled back with the error if magpie refuses; the details stay open
  under the new name, with In use kept. Escape or a click away puts
  the name back. The field lives in profilerename.js, imported on
  press, so the core script stays under its budget (no raise; 50 bytes
  under after comments in app.js were cut). While the field is open
  In use, Apply and Rename hide so a 360px panel's field has room.
- TUI: ctrl+e on a profile renames it on the name line.
- CLI: `magpie rename <old> <new>`; a flag is never taken for a name.
- WebDAV: a rename syncs as the old name gone and the new one in
  (davsync test).

Run on mote at 47672f11c (on 70b96126e), and again after rebasing on
52195b6b0 (backup), as 3b279fdee:
- go test ./internal/profile ./internal/cli ./internal/gui ./internal/tui
  ./internal/davsync: ok except TestEveryRouteHasACommandOrIsNamed
  (#4826) and TestMainWindowCorners (#3949), both red on origin/main
  07f4d46c5 too. gofmt clean; GOOS=linux (-tags nogui) and
  GOOS=windows go vet ok.
- GUI, chromium and webkit: profile-rename (en, zh, zh-TW, ja, de;
  900x560 window and 360x420 panel), panel-profiles, profile-confirm,
  profile-in-use, profile-details, hermes-profiles,
  view-wheel-after-click, i18n-keys, gui-de, gui-ja, gui-zh-tw,
  editor-roomy: 219 pass, 0 fail. After the rebase: profile, cli,
  gui, tui and vet again (same two reds only, core budget ok), and the
  GUI files above with auto-backup in place of editor-roomy: 161 pass.
  Then rebased over ea51526b6 (gateway, plugin, provider only; no
  file of this change, no GUI asset) without another run.

Broken by hand, each red, then restored:
- Rename comparing names with EqualFold: TestRename red (case-only).
- Dropping the CLI's "-" check: TestRenameCommand red.
- Dropping `profileOpen = to`: profile-rename 22 of 22 red, both engines.

Not tried: the real app on Linux (WebKitGTK) or Windows; a rename
synced over a real WebDAV server.

Co-authored-by: erlinerd <erlinerd@outlook.com>
2026-11-02 18:37:25 +08:00

89 lines
2.4 KiB
Go

package gui
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/profile"
"github.com/yetone/magpie/internal/provider"
)
// Renaming a profile through the page's own call moves its key and keeps the
// setup it held, and a name already taken is rejected without dropping either
// profile (#2721).
func TestStateProfileRename(t *testing.T) {
home := sandboxHome(t)
t.Setenv("CODEX_HOME", home+"/.codex")
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
h := Handler(nil, nil)
call := func(path, body string) (stateJSON, int) {
t.Helper()
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
var s stateJSON
if rec.Code == 200 {
if err := json.Unmarshal(rec.Body.Bytes(), &s); err != nil {
t.Fatal(err)
}
}
return s, rec.Code
}
names := func(s stateJSON) []string {
var ns []string
for _, p := range s.Profiles {
ns = append(ns, p.Name)
}
return ns
}
has := func(ns []string, want string) bool {
for _, n := range ns {
if n == want {
return true
}
}
return false
}
if _, code := call("/api/profile/save", `{"name":"work"}`); code != 200 {
t.Fatalf("save code %d", code)
}
if err := profile.Save("taken", profile.Profile{Fields: map[string]string{"codex.model": "relay/m1"}}); err != nil {
t.Fatal(err)
}
// rename success: old name gone, new name present
s, code := call("/api/profile/rename", `{"name":"work","to":"home"}`)
if code != 200 {
t.Fatalf("rename code %d", code)
}
if has(names(s), "work") {
t.Errorf("old name still present: %v", names(s))
}
if !has(names(s), "home") {
t.Errorf("new name missing: %v", names(s))
}
// content kept: the renamed profile keeps what work held
ps, err := profile.Load()
if err != nil {
t.Fatal(err)
}
if _, ok := ps["home"]; !ok {
t.Errorf("renamed profile not written: %v", ps)
}
// rename onto a taken name is rejected, both names survive
s, code = call("/api/profile/rename", `{"name":"home","to":"taken"}`)
if code == 200 {
t.Fatalf("rename onto taken should fail: %v", names(s))
}
ps, _ = profile.Load()
if _, ok := ps["home"]; !ok {
t.Errorf("rejected rename dropped the source: %v", ps)
}
if _, ok := ps["taken"]; !ok {
t.Errorf("rejected rename dropped the taken one: %v", ps)
}
}