Files
magpie/internal/provider/copy_test.go
T
yetone ad164b6311 providers: a provider can be duplicated, the signed-in account paused, and a Balance URL names each key with {key} (#268, #263, #266)
- #268: the editor's Duplicate opens Add filled in from the provider ("X copy", a new id, its URLs, models, headers, balance and proxy); AddCopy takes the source's keys, routing, balance token and Zhipu team unless a new key is pasted, the same-key refusal skipped for it. Signed-in accounts can't be copied.
- #263: unticking the account Codex or Claude Code is signed in to pauses it while another account is on (logins.json `paused`): the gateway passes over it and the agent stays signed in, so its remote control keeps working. With no other account on it is used again; switching accounts clears it. The row reads Paused, dimmed; ticking resumes it.
- #266: {key}, {apiKey} or {api_key} in the Balance URL or a header is each key's own (URL-escaped in the URL), so every key shows its own balance; a network error naming the key shows it masked.

Tested with TestAddCopy, TestCodexPausedOwnAccountPassedOver, TestBalanceURLNamesTheKey (each fails without its change) and duplicate-pause.test.cjs in Chromium and WebKit, English and Chinese (6 of 8 fail on the old app.js); balance-fix, add-sheet, click-scroll pass. go vet, go build -tags nogui (darwin, GOOS=linux, GOOS=windows), go test -tags nogui ./... pass. Not tried with real accounts.
2026-09-30 11:49:37 +08:00

61 lines
2.1 KiB
Go

package provider
import (
"path/filepath"
"slices"
"testing"
)
// A copy of a provider (#268) is added beside it with its keys, balance
// token and routing, the same key on the same host being what was asked
// for; a key pasted in the form is the copy's own, and a signed-in
// account has no copy.
func TestAddCopy(t *testing.T) {
isolate(t)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
src := Provider{ID: "relay", Name: "Relay", Chat: "https://api.relay.example/v1", Key: "sk-1", KeyName: "main",
Keys: []KeyAccount{{Name: "spare", Key: "sk-2"}}, Routing: "rotate", BalanceToken: "tok",
Headers: map[string]string{"X-Org": "acme"}, Fallback: []string{"other/m"},
BalanceURL: "https://api.relay.example/q?key={key}", BalancePath: "balance"}
if err := Save(src); err != nil {
t.Fatal(err)
}
// the form: what it shows of the provider, the key left blank
form := Provider{Name: "Relay copy", Chat: src.Chat, Headers: src.Headers, BalanceURL: src.BalanceURL, BalancePath: src.BalancePath, Models: []string{"m"}}
if _, err := Add(form); err == nil {
t.Fatal("a plain Add without a key")
}
form.Key = "sk-1"
if _, err := Add(form); err == nil {
t.Fatal("a plain Add took the same key on the same host")
}
form.Key = ""
id, err := AddCopy(form, "relay")
if err != nil {
t.Fatal(err)
}
c, err := Find(id)
if err != nil {
t.Fatal(err)
}
if id != "relay-copy" || c.Name != "Relay copy" || c.Key != "sk-1" || c.KeyName != "main" || len(c.Keys) != 1 || c.Keys[0].Key != "sk-2" ||
c.Routing != "rotate" || c.BalanceToken != "tok" || !slices.Equal(c.Fallback, src.Fallback) || c.BalanceURL != src.BalanceURL {
t.Fatalf("copy: %+v", c)
}
// again, under the next free id and name, with a key of its own
form.Key = "sk-9"
id, err = AddCopy(form, "relay")
if err != nil {
t.Fatal(err)
}
if c, _ := Find(id); id != "relay-copy-2" || c.Name != "Relay copy 2" || c.Key != "sk-9" || len(c.Keys) != 0 {
t.Fatalf("second copy %s: %+v", id, c)
}
if _, err := AddCopy(form, "nope"); err == nil {
t.Fatal("a copy of nothing")
}
}