Files
magpie/internal/cli/key_rate_cli_test.go
T
yetone a95a7a1cd8 providers: each API key can bill at a price rate of its own (tnt12231 on X)
A relay sells keys in groups that bill at different rates against the
official price (0.5x on one, 1.2x on another) under one endpoint, and
magpie had only the provider's price rate. Now a key can have its own:

- provider.KeyPriceRates, keyed by key id like KeySpendCaps: cloned,
  normalized on save, carried when a key is replaced. A key without one
  bills at the provider's PriceRate. A price the user set for a model
  stands as set, as it does under the provider's rate.
- Usage prices each call at its key's rate (usage.Pricer memoizes per
  key id, BaseKeyPriceIn), so the Usage page, a key's spend cap and its
  typed balance follow it. PriceAt, used to route by price, takes the
  rate of the key the provider holds.
- GUI: the provider editor's Advanced has "Price rate per key" under
  Price rate, shown only with two or more keys, folded past 8 keys like
  the per-key proxies. An empty line shows the provider's rate as its
  placeholder. Save sends keyPriceRates ({} clears, left out keeps).
  The lines, the check and the placeholder live in keyrates.js,
  imported when a provider with two or more keys opens. The core's
  budget goes up 397 for what is left in app.js (the import, the
  draft's copy, Advanced opening for it, the save's line, proxyLines
  drawing another kind of line); app.js stays under its own.
- CLI: magpie provider key-rate <id> [key [rate|default]].
- docs/subsystems/providers-accounts.md: a Key price rates row.

Run, at this head (rebased on 287a1ffa2) through mote-run:
- go test ./internal/usage ./internal/cli ./internal/gui: ok.
  ./internal/provider: ok but TestHostBuildsNoList, red the same on
  origin/main 51b98b1a8 (3 of 3), filed as #6479 (likely 16b8c300f).
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build: ok.
- GUI: key-price-rate.test.cjs (chromium and webkit x en, zh, zh-TW,
  ja, de: own rates saved, provider rate as placeholder, bad values
  refused, comma decimals, {} on clear, a folded key keeps its rate,
  one key shows no lines and posts none), i18n-keys and
  provider-many-keys-proxy (it shares proxyLines): ok. old-webkit:
  keyrates.js added to its on-demand list; its two reds (compat.js
  first, Safari 15.0 styles) fail the same on origin/main 51b98b1a8
  (#5919 and the on-demand list's #6467 family).
- Earlier, at 92bce5c72: the GUI files that reach the provider editor
  (134 tests). The 6 provider-many-keys reds fail the same on that base
  (#6209); proxy-login-fields' chromium zh timeout passes 12/12 alone on
  base and branch (#6374).

Broken by hand: atKeyRate leaving PriceRate as the provider's made
TestKeyPriceRatePrices and TestPricerKeyPriceRates red; the save not
sending keyPriceRates made 20 of 30 key-price-rate GUI tests red, and
keyrates.js dropping each parsed rate made them red again after the
move.

Not tried: a real relay with several keys at different rates; the real
Linux WebKitGTK app. The budget estimate and the Sessions page's costs
still use the provider's rate.
2026-11-13 16:31:24 +08:00

65 lines
2.2 KiB
Go

package cli
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// magpie provider key-rate gives a key a price rate of its own (by its
// name or id, @tnt12231 on X), and default has it bill at the provider's
// again. With no rate it lists the keys' rates. A key the provider
// hasn't, a rate that isn't one and --help as a value are refused, and
// nothing is written for them.
func TestKeyRateCmd(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("DSH_HOME", "")
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k1", KeyName: "Team", Chat: "http://127.0.0.1:1/v1", PriceRate: 0.8,
Keys: []provider.KeyAccount{{Name: "Spare", Key: "k2"}}}); err != nil {
t.Fatal(err)
}
rateOf := func(key string) (float64, bool) {
t.Helper()
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
return p.KeyPriceRate(provider.KeyID(key))
}
if err := providerCmd([]string{"provider", "key-rate", "relay", "Team", "0.5"}); err != nil {
t.Fatal(err)
}
if r, own := rateOf("k1"); !own || r != 0.5 {
t.Fatalf("Team: %v %v", r, own)
}
if err := providerCmd([]string{"provider", "key-rate", "relay", provider.KeyID("k2"), "1.2x"}); err != nil {
t.Fatal(err)
}
if r, own := rateOf("k2"); !own || r != 1.2 {
t.Fatalf("Spare: %v %v", r, own)
}
for _, args := range [][]string{{"relay"}, {"relay", "Team"}} {
if err := providerCmd(append([]string{"provider", "key-rate"}, args...)); err != nil {
t.Fatal(args, err)
}
}
for _, bad := range [][]string{{"relay", "Team", "--help"}, {"relay", "Team", "lots"}, {"relay", "Team", "0"}, {"relay", "nokey", "1"}, {"relay", "--help"}, {"relay", "Team", "1", "extra"}, {}} {
if err := providerCmd(append([]string{"provider", "key-rate"}, bad...)); err == nil {
t.Fatalf("%v taken", bad)
}
}
if r, _ := rateOf("k1"); r != 0.5 {
t.Fatalf("a refused value changed the rate: %v", r)
}
if err := providerCmd([]string{"provider", "key-rate", "relay", "Team", "default"}); err != nil {
t.Fatal(err)
}
if r, own := rateOf("k1"); own || r != 0.8 {
t.Fatalf("default, the provider's: %v %v", r, own)
}
}