Files
magpie/internal/gui/keys_copy_test.go
T
yetone 167f133166 gui: each of a provider's several keys has a Copy button on its row (#1480, IamMiao)
With one key, the provider editor has an API key field whose Show
reveals the key to copy. With more than one the field is gone and the
key rows had no way to copy a key, as IamMiao reported.

Each key's row now has the same Copy button the app uses elsewhere
(copy icon, tick on success, "<key name> copied"). It asks
provider/key for that row's key: the route takes the key's id in
"account" (as accountmodels already does) and answers through the new
provider.KeyOf, the first key included; without it, the provider's
first key as before; an id it has not is an error, never another key.
The route stays where Show's was, so nothing new is reachable.

Tests:
- gui TestEachKeyCopiesItsOwn: three keys, each row's id answers its
  own key, none answers the first, an unknown id fails.
- key-copy.test.cjs: every row has Copy; team-b's and the off key's
  copy their own key (asked with that id, /api/copy gets it), say so,
  open no rename and move nothing; rows fit 420px. Red on origin/main
  (no button) in all four; passes in Chromium and WebKit, en and zh,
  420 and 900px. No new strings ("Copy", "{what} copied" exist in all
  languages).

Run: go test -tags nogui ./internal/provider/ ./internal/gui/ (ok),
gofmt -l clean.
2026-10-10 20:00:38 +08:00

54 lines
1.7 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Each of a provider's several keys copies its own (#1480, IamMiao): the
// editor asks provider/key with the row's key id and gets that key, the
// first's included; with no id, the first as before; an id it hasn't is
// an error, never another key.
func TestEachKeyCopiesItsOwn(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(path, body string) (int, string) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", path, strings.NewReader(body)))
return w.Code, w.Body.String()
}
if code, body := post("/api/provider/save", `{"id":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1","key":"sk-a, sk-b, sk-c","new":true}`); code != 200 {
t.Fatalf("save: %d %s", code, body)
}
keyOf := func(ref string) (int, string) {
t.Helper()
in, _ := json.Marshal(map[string]string{"id": "relay", "account": ref})
code, body := post("/api/provider/key", string(in))
var out struct{ Key string }
json.Unmarshal([]byte(body), &out)
return code, out.Key
}
for _, k := range []string{"sk-a", "sk-b", "sk-c"} {
if code, got := keyOf(provider.KeyID(k)); code != 200 || got != k {
t.Errorf("row %s copies %q (%d)", k, got, code)
}
}
if code, got := keyOf(""); code != 200 || got != "sk-a" {
t.Errorf("no row: %q (%d), want the first", got, code)
}
if code, got := keyOf(provider.KeyID("sk-gone")); code == 200 || got != "" {
t.Errorf("a key it hasn't: %q (%d)", got, code)
}
}