Files
magpie/internal/access/move_test.go
T
yetone 333e7f573b gateway: gateway keys are dragged into the order you want, kept with the keys (alextjy on Discord)
alextjy asked on Discord whether gateway keys can be reordered. They
could not: the Gateway page listed them in the order caller-keys.json
held them, and nothing changed that order.

- access: a "move-key" action puts one key at a place in the list,
  under the same file lock as every other key change. Nothing else
  about the key changes, and the sharing key's mirror in settings is
  left alone (its pointer would otherwise land on another key).
- GUI: the Gateway page's key rows are dragged the way search APIs'
  rows are (shared dragRows, a grip shown on hover only, Alt+Up/Down,
  the existing "Drag to reorder" title). A refused move puts the list
  back from the server. New strings in zh, zh-TW, ja and de.
- CLI: `magpie gateway-key move <id> <place>` (from 1); list already
  prints the stored order.
- Usage > Requests: the key filter lists keys in the user's order,
  removed keys after. The Overview stays a ranking by use.

Run (worktree at 14b451c11, then rebased on origin/main):
- gofmt -l clean; go vet (darwin) on access, cli, usageapi, gui;
  GOOS=linux -tags nogui and GOOS=windows go vet on access, cli,
  usageapi (mote).
- go test access, cli, usageapi: ok (mote). internal/gui: every test
  ok except the real-window AppKit tests #4125 lists
  (TestCoveredWindowStaysUp, TestPanelCloseHidesIt and 7 more), killed
  at their deadlines with pboard not running on mote.
- Broken by hand: with move-key removed, TestMoveKeyKeepsOrder,
  TestGatewayKeyCLIMove and TestLedgerKeyFilterInKeyOrder go red; with
  the mirror exclusion removed and the filter left unsorted,
  TestMoveKeyKeepsOrder ("the sharing key's mirror moved") and
  TestLedgerKeyFilterInKeyOrder go red.
- GUI (WSL): gateway-key-arrange 20/20 (chromium and webkit; en, zh,
  zh-TW, ja, de; 1100 and 420px). Red by hand: no arrange (20 red),
  grip always shown (chromium 10/10 red), drop commits nothing (webkit
  10/10 red).
- GUI (WSL), the 68 files the change can reach (every caller-keys
  fixture user, gateway-*, search-*, usage-ledger*, i18n-keys, gui-ja,
  gui-de, gui-zh-tw, key-page, phone-rows, api-key-*): 900/923. The
  reds were run again at origin/main 43966a53f and fail the same there
  (api-key-theme webkit, gateway-key-limit webkit, gateway-key-models
  webkit, search-first, usage-ledger-open-settled, usage-ledger-ttft,
  usage-ledger), each filed: #3307, #3523, #4081, #3795, #4262,
  #3104/#1876, and #4489 (usage-ledger-ttft, new). phone-rows,
  search-keys and connect-snippet-shell reds of the first run passed
  when run again at the head. usage-ledger's "zh: request routing
  links" went red once at the head (Usage view position on return) and
  passed in the first run.

At the rebased head (on 2ac423f4f): TestCoreScriptBudget and the
other Budget/I18n/Asset tests in internal/gui ok (mote);
gateway-key-arrange, i18n-keys and gateway-caller-keys 27/28 in WSL.
The red is gateway-caller-keys webkit en "wider than 560 right after
the resize" (#3797, red alone on main). A probe of that file, printing
the page width and any element past it, read 560/560 with nothing
wider in both engines, in two runs beside the arrange file.

Not tried: dragging in the real app (WebKitGTK, WKWebView, WebView2),
the full Go suite (started after the push).
2026-10-31 03:48:27 +08:00

133 lines
3.8 KiB
Go

package access
import (
"encoding/json"
"os"
"slices"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The keys are kept in the order the user drags them into (alextjy on
// Discord): move-key puts one at a place, the store on disk keeps it, so a
// restart lists them the same way, and nothing else about any key changes,
// the sharing key's mirror in the settings included.
func TestMoveKeyKeepsOrder(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
var ids []string
for _, name := range []string{"Laptop", "Phone", "Desk display", "CI"} {
if _, err := Update("add-key", Change{Name: name, ReadOnly: name == "Desk display"}); err != nil {
t.Fatal(err)
}
}
keys, _ := List()
for _, k := range keys {
ids = append(ids, k.ID)
}
// the first is the sharing key, with a limit and models of its own
s := settings.Load()
s.LAN, s.LANKeyID = true, ids[0]
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if _, err := Update("models-key", Change{Key: ids[0], Models: []string{"openai/*"}}); err != nil {
t.Fatal(err)
}
if _, err := Update("off-key", Change{Key: ids[3]}); err != nil {
t.Fatal(err)
}
before, _ := Export()
if !before[0].LAN || before[0].Limit != nil {
t.Fatal("the sharing key as set up:", before[0])
}
lan := settings.Load()
move := func(id string, to int) {
t.Helper()
if _, err := Update("move-key", Change{Key: id, Index: &to}); err != nil {
t.Fatal(err)
}
}
order := func() []string {
t.Helper()
// read from the file itself, as a magpie started again would
b, err := os.ReadFile(Path())
if err != nil {
t.Fatal(err)
}
var ks []Key
if err := json.Unmarshal(b, &ks); err != nil {
t.Fatal(err)
}
var out []string
for _, k := range ks {
out = append(out, k.Name)
}
return out
}
move(ids[2], 0) // Desk display to the top
if got := order(); !slices.Equal(got, []string{"Desk display", "Laptop", "Phone", "CI"}) {
t.Fatal("to the top:", got)
}
move(ids[0], 3) // the sharing key to the foot
if got := order(); !slices.Equal(got, []string{"Desk display", "Phone", "CI", "Laptop"}) {
t.Fatal("to the foot:", got)
}
move(ids[3], 2) // where it is: nothing moves
if got := order(); !slices.Equal(got, []string{"Desk display", "Phone", "CI", "Laptop"}) {
t.Fatal("in place:", got)
}
keys, _ = List()
if got := []string{keys[0].Name, keys[1].Name, keys[2].Name, keys[3].Name}; !slices.Equal(got, order()) {
t.Fatal("List:", got)
}
// each key is as it was, but for its place
after, _ := Export()
byID := func(ks []Key) map[string]string {
m := map[string]string{}
for _, k := range ks {
b, _ := json.Marshal(k)
m[k.ID] = string(b)
}
return m
}
a, b := byID(before), byID(after)
for id := range a {
if a[id] != b[id] {
t.Fatalf("key %s changed:\n%s\n%s", id, a[id], b[id])
}
}
if len(a) != len(b) {
t.Fatal("keys:", len(a), len(b))
}
if now := settings.Load(); now.LANKeyID != lan.LANKeyID || now.LANKey != lan.LANKey {
t.Fatalf("the sharing key's mirror moved: %q %q, was %q %q", now.LANKeyID, now.LANKey, lan.LANKeyID, lan.LANKey)
}
if who, ok := Authenticate(lanSecretOf(t, after, ids[0])); !ok || who.KeyID != ids[0] {
t.Fatal("the sharing key after moving:", who, ok)
}
for _, bad := range []int{-1, 4} {
if _, err := Update("move-key", Change{Key: ids[1], Index: &bad}); err == nil {
t.Fatal("a place out of the list:", bad)
}
}
if _, err := Update("move-key", Change{Key: ids[1]}); err == nil {
t.Fatal("no place")
}
if got := order(); !slices.Equal(got, []string{"Desk display", "Phone", "CI", "Laptop"}) {
t.Fatal("after refused moves:", got)
}
}
func lanSecretOf(t *testing.T, ks []Key, id string) string {
t.Helper()
i := slices.IndexFunc(ks, func(k Key) bool { return k.ID == id })
if i < 0 {
t.Fatal("no key", id)
}
return ks[i].Secret
}