@emilochen on X asked to batch-export one provider's keys, to sync them between a local and a cloud magpie. Each key row could copy only its own key (#1480), and nothing printed them all. - A provider with two or more keys gets a "Copy all N keys" row under Add another key. It asks provider/key with allKeys (provider.AllKeys: the main key and every listed key, deduplicated, turned-off ones too) and copies them one a line, for the other magpie's Paste several (keys/import). The secret goes only to the clipboard on the click, through the page's copy(); nothing logs it, and the route sits behind the GUI API's existing local/origin guards. A signed-in account has no keys and is refused. - The row is drawn by keyrates.js (copyAllKeysRow), already imported when a provider with two or more keys opens; keyRateField now takes which of its parts to draw. It has its own class, keys-copy-all, not .add, since ~21 GUI tests take .acc.add as the one adding row. TestCoreScriptBudget's core budget goes up by the 72 bytes left in app.js (the placing line and the helper's new argument); app.js stays under its own budget. - CLI: `magpie provider keys <id>` prints the keys one a line; `magpie provider keys <id> add [key… | -]` adds them from words or stdin (SplitKeys + AddKeys), so `magpie provider keys relay | ssh cloud magpie provider keys relay add` works. A word starting with "-" is refused, never taken as a key. - Strings in en, zh, zh-TW, ja, de. Related: #5387 (sharing saved providers through magpie:// links), not closed by this. Run on mote (mote-run) at this tree on11ddf076b(debc80271, its new parent, changes LESSONS.md only): - gofmt -l internal/: clean. go vet (darwin) ./internal/provider ./internal/gui ./internal/cli; GOOS=linux go vet -tags nogui ./...; GOOS=windows go vet ./...; GOOS=linux go build -tags nogui and GOOS=windows go build: ok. - go test -count=1 ./internal/provider ./internal/cli: ok; ./internal/gui: ok (TestCoreScriptBudget included, after the budget change). - GUI, Chromium + WebKit: key-copy-all (en/zh/zh-TW/ja/de × 900/420px), api-key-usage, i18n-keys, key-price-rate, key-copy, key-weight, click-scroll: 104/104. key-copy-all again in WebKit twice: 15/15 each. - GUI, 43 key/provider/i18n files at the change vs the same files at untouched11ddf076b: the only new reds were api-key-usage's key-row count (it counted .acc:not(.add); now leaves keys-copy-all out). The other 67 reds are the same on the base and filed: #5835 key-switch, #6414 key-balance-card de, #6424 key-box-once, #5866 signin-proxy 380, #6444 api-key-theme, #4126 key-replace 420, #4673 provider-typed, #6416 key-import Paste several, and #6628 (filed now) login-export's Export row count. Broken by hand: key-copy-all.test.cjs at untouched11ddf076bassets fails 10 of 15 in WebKit (the 5 that pass are "one key has no Copy all"). The Go tests can't build on the old code (AllKeys, keysCmd). Not tried: the real app and the system clipboard, the Windows and Linux boxes, a real cloud/headless magpie over ssh, and the full Go suite (mote and mica were both over their load limits; the three touched packages and cross-OS vet ran instead).
91 lines
3.2 KiB
Go
91 lines
3.2 KiB
Go
package provider
|
||
|
||
import (
|
||
"path/filepath"
|
||
"reflect"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// Many keys pasted at once (361 on Discord: a provider with many keys was
|
||
// tedious to fill one by one): split one a line, or by commas or spaces,
|
||
// trimmed of quotes, each once.
|
||
func TestSplitKeys(t *testing.T) {
|
||
got := SplitKeys("sk-a\nsk-b, sk-c;\t\"sk-d\"\r\n\nsk-a sk-e,sk-f 'sk-g'")
|
||
want := []string{"sk-a", "sk-b", "sk-c", "sk-d", "sk-e", "sk-f", "sk-g"}
|
||
if !reflect.DeepEqual(got, want) {
|
||
t.Fatalf("got %q", got)
|
||
}
|
||
if len(SplitKeys(" \n , ")) != 0 {
|
||
t.Fatal("blank text has keys")
|
||
}
|
||
}
|
||
|
||
// AddKeys saves the new ones after the provider's own, on, with the
|
||
// protocol given, in one save, and passes over the keys it has.
|
||
func TestAddKeys(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"))
|
||
if err := Save(Provider{ID: "bulk", Name: "Bulk", Chat: "https://example.invalid/v1", Key: "k1"}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
// the key it has is passed over, the others go after it
|
||
added, had, err := AddKeys("bulk", []string{"k1", "k2", "k2", "k3"}, "")
|
||
if err != nil || added != 2 || had != 2 {
|
||
t.Fatalf("added %d had %d: %v", added, had, err)
|
||
}
|
||
p, _ := Find("bulk")
|
||
if p.Key != "k1" || len(p.Keys) != 2 || p.Keys[0].Key != "k2" || p.Keys[1].Key != "k3" || p.Keys[0].Off {
|
||
t.Fatalf("saved %q %+v", p.Key, p.Keys)
|
||
}
|
||
added, had, err = AddKeys("bulk", []string{"k3", "k4"}, Anthropic)
|
||
if err != nil || added != 1 || had != 1 {
|
||
t.Fatalf("added %d had %d: %v", added, had, err)
|
||
}
|
||
p, _ = Find("bulk")
|
||
if l := p.Keys[len(p.Keys)-1]; l.Key != "k4" || l.Protocol != Anthropic {
|
||
t.Fatalf("last %+v", l)
|
||
}
|
||
if _, _, err := AddKeys("bulk", []string{"k1", "k4"}, ""); err == nil {
|
||
t.Fatal("only keys it had: no error")
|
||
}
|
||
if _, _, err := AddKeys("bulk", nil, ""); err == nil {
|
||
t.Fatal("no keys: no error")
|
||
}
|
||
// the rest key the gateway gives each: the id alone with one key on
|
||
if k := (Provider{ID: "x", Key: "a"}).KeyList()[0]; (Provider{ID: "x", Key: "a"}).RestKey(k) != "x" {
|
||
t.Fatal("one key rests as the provider")
|
||
}
|
||
if k := p.KeyList()[1]; p.RestKey(k) != "bulk#"+KeyID("k2") {
|
||
t.Fatalf("rest key %q", p.RestKey(k))
|
||
}
|
||
}
|
||
|
||
// AllKeys gives every key a provider holds, on or off, in its list's
|
||
// order, each once, for another magpie to take in with SplitKeys
|
||
// (@emilochen on X); a signed-in account has none.
|
||
func TestAllKeys(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"))
|
||
if err := Save(Provider{ID: "bulk", Name: "Bulk", Chat: "https://example.invalid/v1", Key: "k1", KeyName: "Team",
|
||
Keys: []KeyAccount{{Key: "k2", Off: true}, {Name: "Spare", Key: "k3", Protocol: Anthropic}, {Key: "k1"}}}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
got, err := AllKeys("bulk")
|
||
if want := []string{"k1", "k2", "k3"}; err != nil || !reflect.DeepEqual(got, want) {
|
||
t.Fatalf("got %q, %v", got, err)
|
||
}
|
||
if !reflect.DeepEqual(SplitKeys(strings.Join(got, "\n")), got) {
|
||
t.Fatal("what it gives is not what Paste several reads")
|
||
}
|
||
if _, err := AllKeys("nope"); err == nil {
|
||
t.Fatal("no provider: no error")
|
||
}
|
||
}
|