tenacious_eagle_96548 (Discord, #general): some vendors split a balance
into money that stays and credit good until a date. A key's balance typed
by hand can now carry that part (`expiring`: {amount, until, at}, in the
balance's currency, until = the last day it counts).
- Spending order: the expiring part is spent first, as DeepSeek spends
its granted balance before money topped up. It takes the spend from its
`at` up to its end (midnight after `until`, local time) or now; only
what it took after `From` comes off the rest, so a refill or typing the
balance again doesn't count it twice.
- Left is the whole balance. From its end the remainder counts no more,
and unspent credit is gone.
- Typing the balance again keeps the part in the same currency and drops
it in the other, where its amount would mean another sum.
- A balance saved before has no part and reads exactly as it did
(TestTypedBalanceSavedBeforeExpiring).
- Set from the pill's menu: "Credit that expires…" asks the amount, then
the last day (a day gone by, or the other currency's sign, refused);
"No credit that expires" takes it off. GUI route provider/keyexpiring;
CLI `magpie provider key-balance <id> <key> --expiring amount@yyyy-mm-dd|off`.
- Shown: the pill "≈$100.00 left · $30.00 expires in 30 days", the Usage
page and panel line "Expires in 30 days ≈$30.00", amber from 7 days;
TUI and `magpie quota` "($30.00 until Dec 7)". New strings in zh,
zh-TW, ja and de.
Run (mote):
- go test of internal/provider, internal/gui, internal/cli (with
TestCoreScriptBudget: the days' lines in app.js are paid for by five
balance comments cut there, no raise; core 1,533,559 of 1,533,802); the new tests
again with TZ=UTC and TZ=Pacific/Kiritimati (every clock held);
gofmt -l clean; GOOS=linux and GOOS=windows go vet of those packages.
- GUI: key-balance-expiring.test.cjs 20/20 (chromium and webkit, 400px,
en/zh/zh-TW/ja/de), with every balance-*, key-*, i18n-* file: 768
pass, 6 red, each red the same alone on origin/main 691a10446:
i18n-keys "a plugin's words are its own" (#5327) and key-replace's
five webkit 420px cases (#4126, numbers added there).
- A real sandbox (own HOME, -tags nogui binary, ports 3591/3592): set
$70 + $30 until +30d and ¥40 + ¥5 for +3d through the CLI, then
through the pill's menu in the real page (¥8 for +6d, then removed);
a past date shows the part no longer counting; retyping in USD dropped
the CNY part; `magpie quota` shows the split. No page errors.
Broken by hand, each red:
- Go (TestTypedBalanceExpires): part not spent first; end taken in UTC
(red under Kiritimati); part kept after its end; part kept across a
currency change.
- GUI (key-balance-expiring): no soon class; soon only from 2 days; a
past day accepted; the other currency's sign accepted.
Not tried: a real vendor's spend priced against the part; the GUI on
Windows or Linux. The full Go and GUI suites were queued on mote's
suite lock behind other sessions' and had not run when this was pushed.
Reported-by: tenacious_eagle_96548
110 lines
4.6 KiB
Go
110 lines
4.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// magpie provider key-balance sets, shows and takes off a key's balance
|
|
// typed by hand (tenacious_eagle on Discord), and a flag or an amount
|
|
// that isn't one is refused without writing anything.
|
|
func TestKeyBalanceCmd(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"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
typed := func() (provider.OwnBalance, bool) {
|
|
t.Helper()
|
|
p, err := provider.Find("relay")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p.KeyOwnBalance(provider.KeyID("k1"))
|
|
}
|
|
if err := providerCmd([]string{"provider", "key-balance", "relay", "Team", "$1,250.5"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, ok := typed(); !ok || b.USD != 1250.5 || b.At.IsZero() {
|
|
t.Fatalf("typed: %+v %v", b, ok)
|
|
}
|
|
for _, args := range [][]string{{"relay"}, {"relay", "Team"}} {
|
|
if err := providerCmd(append([]string{"provider", "key-balance"}, args...)); err != nil {
|
|
t.Fatal(args, err)
|
|
}
|
|
}
|
|
for _, bad := range [][]string{{"relay", "Team", "--help"}, {"relay", "--help"}, {"relay", "Team", "lots"}, {"relay", "Team", "-3"}, {"relay", "nokey", "5"}, {"relay", "Team", "5", "extra"}} {
|
|
if err := providerCmd(append([]string{"provider", "key-balance"}, bad...)); err == nil {
|
|
t.Fatalf("%v taken", bad)
|
|
}
|
|
}
|
|
if b, _ := typed(); b.USD != 1250.5 {
|
|
t.Fatalf("a refused value changed the balance: %+v", b)
|
|
}
|
|
// in yuan, refilled on the 1st of each month; then weekly, the
|
|
// amount kept; then refilling no more
|
|
if err := providerCmd([]string{"provider", "key-balance", "relay", "Team", "¥700", "--refill", "monthly:1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, _ := typed(); b.CNY == nil || *b.CNY != 700 || b.USD != 0 || b.Refill == nil || *b.Refill != (provider.Refill{Every: provider.RefillMonth, Day: 1}) {
|
|
t.Fatalf("¥700 monthly:1: %+v", b)
|
|
}
|
|
if err := providerCmd([]string{"provider", "key-balance", "relay", "Team", "--refill=weekly:fri"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, _ := typed(); b.CNY == nil || *b.CNY != 700 || b.Refill == nil || *b.Refill != (provider.Refill{Every: provider.RefillWeek, Day: 5}) {
|
|
t.Fatalf("weekly:fri: %+v", b)
|
|
}
|
|
for _, bad := range [][]string{{"relay", "Team", "--refill", "yearly"}, {"relay", "--refill", "monthly"}, {"relay", "Team", "off", "--refill", "monthly"}, {"relay", "Team", "--refill"}} {
|
|
if err := providerCmd(append([]string{"provider", "key-balance"}, bad...)); err == nil {
|
|
t.Fatalf("%v taken", bad)
|
|
}
|
|
}
|
|
if b, _ := typed(); b.Refill == nil || b.Refill.Every != provider.RefillWeek {
|
|
t.Fatalf("a refused refill changed it: %+v", b)
|
|
}
|
|
if err := providerCmd([]string{"provider", "key-balance", "relay", "Team", "--refill", "off"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, _ := typed(); b.Refill != nil || b.CNY == nil {
|
|
t.Fatalf("refill off: %+v", b)
|
|
}
|
|
// credit that expires (tenacious_eagle_96548): set beside the rest,
|
|
// kept when the balance is typed again in its currency, taken off
|
|
if err := providerCmd([]string{"provider", "key-balance", "relay", "Team", "--expiring", "200@2099-12-31"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, _ := typed(); b.Expiring == nil || b.Expiring.Amount != 200 || b.Expiring.Until != "2099-12-31" || b.Expiring.At.IsZero() || *b.CNY != 700 {
|
|
t.Fatalf("--expiring 200@2099-12-31: %+v", b)
|
|
}
|
|
for _, bad := range [][]string{{"relay", "Team", "--expiring", "200"}, {"relay", "Team", "--expiring", "200@soon"}, {"relay", "Team", "--expiring", "¥200@2099-12-31"},
|
|
{"relay", "Team", "--expiring", "0@2099-12-31"}, {"relay", "--expiring", "1@2099-12-31"}, {"relay", "Team", "off", "--expiring", "1@2099-12-31"}} {
|
|
if err := providerCmd(append([]string{"provider", "key-balance"}, bad...)); err == nil {
|
|
t.Fatalf("%v taken", bad)
|
|
}
|
|
}
|
|
if err := providerCmd([]string{"provider", "key-balance", "relay", "Team", "¥650"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, _ := typed(); b.Expiring == nil || b.Expiring.Amount != 200 {
|
|
t.Fatalf("typed again in yuan, the part that expires went: %+v", b)
|
|
}
|
|
if err := providerCmd([]string{"provider", "key-balance", "relay", "Team", "--expiring=off"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if b, _ := typed(); b.Expiring != nil || *b.CNY != 650 {
|
|
t.Fatalf("--expiring off: %+v", b)
|
|
}
|
|
if err := providerCmd([]string{"provider", "key-balance", "relay", "Team", "off"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := typed(); ok {
|
|
t.Fatal("off left a balance")
|
|
}
|
|
}
|