tenacious_eagle_96548 asked, after v0.1.3281's typed balance, to type it
in RMB and to have it refill (e.g. $10 a month, on the 1st).
- A balance may be typed in yuan ("¥100", "100元", or a bare number while
Settings shows costs in CNY). What magpie counts spent stays in USD and
is turned into yuan at fx.Known(): the rate internal/fx last fetched
(cached 12h, fetched again in the background, never on a request).
fx.Known never returns fx.Fallback, so with no rate fetched and
something spent, what is left is unknown (left null; the card says
why), not a guess.
- An optional refill: monthly on day 1-31 (past a month's end, its last
day) or weekly on a weekday, at midnight in the user's local time.
Nothing is stored on a refill: the spend counts from the last boundary
after the balance was typed. Typing the amount again keeps the refill.
- GUI: the pill's menu gains Refills each month… (a day, in place),
Refills each week… (the app's menu of weekdays) and Never refills;
provider/keybalancerefill; keyownbalance takes currency cny. CLI:
magpie provider key-balance <id> <key> [usd|¥cny|off]
[--refill monthly[:day]|weekly[:mon…sun]|off]. Strings in zh, zh-TW,
ja, de. docs/subsystems/providers-accounts.md updated.
Run on mote at this tree (base origin/main 350599a0f):
- go test -tags nogui -count=1 -timeout 45m ./internal/fx ./internal/provider
./internal/cli ./internal/gui ./internal/usage: all ok (suite lock).
- GOOS=linux go vet -tags nogui ./... ok; GOOS=windows go vet ./... ok;
gofmt -l clean.
- key-own-balance.test.cjs 20/20 (chromium + webkit, en zh zh-TW ja de,
900 and 400px): yuan typed posts currency cny, monthly day (40
refused), weekly Friday, never. i18n-keys, i18n-en-no-cjk, old-webkit ok.
- Broken by hand, each red: Known returning Fallback
(TestKnownIsNeverAGuess); refill days taken in UTC
(TestTypedBalanceRefills, TestRefillDays); refill dropped on retype
(TestTypedBalanceRefills).
- Full GUI suite still running at push: 3502 ok, 6 red so far, none in a
file this touches: webkit By weight; webkit de 1100/420 Codex resets
used by themselves; chromium en named caller keys; chromium zh-TW 420
a key's several limits; webkit en gateway key held to its models.
They are run on origin/main next and filed if red there.
Not tried: a live rate fetch (tests stay offline), the real app window,
WebKitGTK.
Reported-by: tenacious_eagle_96548 (Discord)
84 lines
3.2 KiB
Go
84 lines
3.2 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)
|
|
}
|
|
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")
|
|
}
|
|
}
|