onkashiko on X: DeepSeek, Kimi (China), Zhipu and other domestic vendors
price in yuan, the rest in dollars, and one currency for every cost made
the Usage page read wrong for half of them.
Settings > Usage > Currency gains a third choice, By provider. With it,
a figure of one provider's calls (a model's, an account's, a key's row,
a request, a routing try, a quota card's whole) shows in the currency
that provider sells in, and every sum across providers (the total,
agents, sessions, charts, the tray, Bark's daily, the day page) in the
currency a "Totals in" row picks, a row shown only while By provider is
chosen. GUI, TUI and CLI agree. Costs stay stored in dollars; only the
display converts, at the live rate (internal/fx).
Which providers sell in yuan (provider.PricesInYuan): a .cn host; a
preset at its own site that is a yuan preset (deepseek, zhipu,
volcengine, tencent-cloud, bailian, siliconflow…), ends in -cn, or is in
its China region; a plugin id ending in -cn; a provider of one's own
with a yuan models.dev catalog served at that catalog's site. A relay
made from DeepSeek's preset, Z.ai, Kimi's and MiniMax's international
endpoints stay in dollars: they share a catalog with their Chinese
twins, so the catalog alone can't tell.
Known limit: models.dev has only dollar prices (converted from the
vendors' yuan at ~6.7-7.0), so a yuan figure is that dollar price at
today's rate and can differ a few percent from the vendor's own list.
A second ask from onkashiko: set the currency of the menu bar and of
the totals apart, sums converted by themselves.
So Settings > Usage also gains an Advanced fold (closed by default, open while
set) with one row, Menu bar currency: Same as totals (the default), $ or
¥ (settings.TrayCurrency, TrayCostIn). It sets the menu bar's totals
cards and the tray tooltip's cost; the Usage page, the panel, the TUI,
the CLI and Bark's daily message stay in the totals' currency
(totalsCurrency). Like the other menu bar settings it stays on this
computer (KeepOwn). Every sum is still made in dollars and converted
once, at the live rate, so a mixed-currency total is never added up
from yuan and dollars.
Settings' currency rows (Currency, Totals in, Menu bar currency and the
rate in the tooltip) moved out of app.js into currencyprefs.js, imported
when Settings draws its usage prefs, and costInYuan is packed: the core
is 3 bytes under coreScriptBudget, with no raise.
Run:
- go test -count=1 at this tree on mote: internal/settings,
settingsapi, tui, cli, catalog, provider, fx and gui all ok
(TestCoreScriptBudget included). The full go test ./... and the whole
GUI suite are queued behind other sessions' suites on suite.lock
(mote and mica, load 35 and 60) and had not started at push.
- GUI files at this tree (mica): currency-provider, currency, i18n-keys,
i18n-en-no-cjk, tray-cost, tray-totals, every settings-*, old-webkit:
362 pass, 28 red. Every red is red the same at origin/main ac0b5c287
on mote, and filed: settings-search (#6133, #6353), settings-hints
(#6107, #6353), settings-lines-wrap de 440px (#6353), old-webkit
appearance.js (#6467) and :has() (#5919). The one red of this change,
settings-groups' list of Usage's parts (now with the Advanced fold),
is fixed here: 14 of 14.
- currency-provider.test.cjs (new): en, zh, zh-TW, ja, de at 900 and
420px, both engines; By provider, Totals in ¥, then Advanced: $ for
the menu bar with totals in ¥, totals stay ¥21.60, the fold opens
itself after a reload. Plus currency.test.cjs (3 choices now),
i18n-keys, i18n-en-no-cjk, tray-cost: green in both engines.
- TestTrayCostIn (values, refused eur/provider, KeepOwn),
TestFXByProvider (trayCurrency vs totalsCurrency), TestCoreScriptBudget.
- gofmt -l clean; GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., GOOS=linux go build -tags nogui,
GOOS=windows go build on mote at this tree: ok.
- Broken by hand, each red: costInYuan ignoring the provider (GUI
test: deepseek showed $1.00 for ¥7.20), PricesInYuan without the
preset's site check (a relay made from DeepSeek's preset read as
yuan), CostIn without its By provider branch (settings test),
TrayCostIn ignoring TrayCurrency (TestTrayCostIn, TestFXByProvider),
the fold not opening while set (webkit), the row saving into
totalCurrency (chromium).
- Real app in a sandbox HOME on mote (dev build, magpie web and magpie
usage) with DeepSeek, OpenAI and Z.ai presets and one call each: the
window and the CLI both show deepseek-v4-pro ≈¥15.44, gpt-4.1 ≈$1.00,
glm-4.6 ≈$0.290 and the total ≈¥24.07 at 6.685; /api/settings' fx
carries yuan ["deepseek"].
Not tried: Windows and Linux windows and trays, the real menu bar with
Menu bar currency changed (drawn natively; covered through trayCurrency
in Go), a real Kimi CN or Zhipu account.
301 lines
12 KiB
Go
301 lines
12 KiB
Go
package tui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/fx"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// The Usage page lists the accounts' windows and balances as the app's
|
|
// cards do, a window reading as used or left, and the vendor's own count
|
|
// beside the percentage, and when each window starts again.
|
|
func TestQuotaLines(t *testing.T) {
|
|
now := time.Date(2026, 9, 26, 12, 0, 0, 0, time.UTC)
|
|
reset, soon := now.Add(3*time.Hour), now.Add(40*time.Minute)
|
|
qs := []provider.SubscriptionQuota{
|
|
{Name: "Codex", Plan: "Plus", User: "me@example.com", Windows: []provider.QuotaWindow{
|
|
{Name: "5 hours", Used: 64, ResetsAt: &soon},
|
|
{Name: "7 days", Used: 99, ResetsAt: &reset},
|
|
}},
|
|
{Name: "ZCode", Windows: []provider.QuotaWindow{{Name: "Month", Used: 1.2, Display: "345 / 28000"}}},
|
|
{Name: "DeepSeek", Balance: "¥12.30"},
|
|
{Name: "Gemini", Error: "sign-in has expired"},
|
|
}
|
|
plain := func(ls []string) string { return strings.Join(ls, "\n") } // no terminal here, so no colour
|
|
|
|
used := plain(quotaLines(qs, true, false, 200, now))
|
|
for _, want := range []string{"Codex · Plus · me@example.com", "64% used ↻ " + provider.ResetClock(soon, now) + " (40m)", "99% used ↻ " + provider.ResetClock(reset, now) + " (3h)", "345 / 28000 · 1% used", "¥12.30 left", "sign-in has expired"} {
|
|
if !strings.Contains(used, want) {
|
|
t.Errorf("used: missing %q in\n%s", want, used)
|
|
}
|
|
}
|
|
left := plain(quotaLines(qs, true, true, 200, now))
|
|
for _, want := range []string{"36% left", "1% left", "345 / 28000 · 99% left", "% is how much is left"} {
|
|
if !strings.Contains(left, want) {
|
|
t.Errorf("left: missing %q in\n%s", want, left)
|
|
}
|
|
}
|
|
// a narrow terminal puts the windows that don't fit on lines of their own
|
|
if n := len(quotaLines(qs[:1], true, false, 60, now)); n != 3 {
|
|
t.Errorf("narrow: %d lines, want 3 (head, and a window a line)", n)
|
|
}
|
|
// a Codex account's rate-limit resets follow its windows, until when
|
|
// only when they run out
|
|
expires := now.Add(7 * 24 * time.Hour)
|
|
held := []provider.SubscriptionQuota{qs[0], qs[0]}
|
|
held[0].Resets = &provider.ResetCredits{Count: 2, Until: &expires}
|
|
held[1].Resets = &provider.ResetCredits{Count: 1}
|
|
got := quotaLines(held, true, false, 200, now)
|
|
if !strings.HasSuffix(got[1], "↺ 2 resets until "+provider.ResetClock(expires, now)) || !strings.HasSuffix(got[2], "↺ 1 reset") {
|
|
t.Errorf("resets:\n%s", plain(got))
|
|
}
|
|
// a ChatGPT account's credits are told beside its windows, not in
|
|
// their place
|
|
credits := []provider.SubscriptionQuota{qs[0]}
|
|
credits[0].Balance = "1.2K credits"
|
|
if got := plain(quotaLines(credits, true, false, 200, now)); !strings.Contains(got, "64% used") || !strings.Contains(got, "99% used") || !strings.Contains(got, "1.2K credits left") {
|
|
t.Errorf("credits:\n%s", got)
|
|
}
|
|
if quotaLines(nil, false, false, 80, now) != nil {
|
|
t.Error("not asked yet: want nothing")
|
|
}
|
|
if got := plain(quotaLines(nil, true, false, 80, now)); !strings.Contains(got, "asking the vendors") {
|
|
t.Errorf("asking: %q", got)
|
|
}
|
|
}
|
|
|
|
// fmtCost shows a computed cost at Settings' currency choice — $ (its own,
|
|
// unconverted) by default, ¥ at the cached exchange rate once cny is chosen
|
|
// — never touching a vendor's own balance string (that's quotaLines above).
|
|
func TestFmtCostShowsChosenCurrency(t *testing.T) {
|
|
home(t)
|
|
// a currency read in the last second (an earlier run's ¥, -count) is
|
|
// kept; read this home's
|
|
costAt = time.Time{}
|
|
|
|
if got := fmtCost(usage.Totals{Cost: 1.23}); got != "≈$1.23" {
|
|
t.Fatalf("default currency: %q", got)
|
|
}
|
|
|
|
if err := settings.Save(settings.Settings{Currency: "cny"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// pre-seed the fx cache so fmtCost's rate lookup hits no network
|
|
if err := os.MkdirAll(filepath.Dir(fx.CachePath()), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := json.Marshal(fx.Rate{CNYPerUSD: 7.2, At: time.Now()})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(fx.CachePath(), b, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fx.Reset()
|
|
// costCurrency() only rereads settings once a second; force it stale
|
|
costAt = time.Time{}
|
|
|
|
if got := fmtCost(usage.Totals{Cost: 1.23}); got != "≈¥8.86" {
|
|
t.Fatalf("cny: %q", got)
|
|
}
|
|
if got := fmtCost(usage.Totals{Cost: 0, Unpriced: 1}); got != "no price" {
|
|
t.Fatalf("unpriced: %q", got)
|
|
}
|
|
|
|
// By provider (onkashiko on X): DeepSeek's row in yuan, OpenAI's in
|
|
// dollars, a total in the currency picked for totals
|
|
for _, p := range []provider.Provider{
|
|
{ID: "deepseek", Name: "DeepSeek", Preset: "deepseek", Key: "k", Chat: "https://api.deepseek.com/v1"},
|
|
{ID: "openai", Name: "OpenAI", Preset: "openai", Key: "k", Chat: "https://api.openai.com/v1"},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := settings.Save(settings.Settings{Currency: settings.CurrencyByProvider, TotalCurrency: "usd"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
costAt = time.Time{}
|
|
for _, c := range []struct{ id, want string }{{"deepseek", "≈¥8.86"}, {"openai", "≈$1.23"}, {"", "≈$1.23"}, {usage.UnknownProvider, "≈$1.23"}} {
|
|
if got := fmtCostOf(usage.Totals{Cost: 1.23}, c.id); got != c.want {
|
|
t.Errorf("by provider, %q: %q, want %q", c.id, got, c.want)
|
|
}
|
|
}
|
|
if err := settings.Save(settings.Settings{Currency: settings.CurrencyByProvider, TotalCurrency: "cny"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
costAt = time.Time{}
|
|
if got := fmtCost(usage.Totals{Cost: 1.23}); got != "≈¥8.86" {
|
|
t.Errorf("by provider, the total in cny: %q", got)
|
|
}
|
|
if got := fmtCostOf(usage.Totals{Cost: 1.23}, "openai"); got != "≈$1.23" {
|
|
t.Errorf("by provider, openai with totals in cny: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestUnlimitedQuotaCell(t *testing.T) {
|
|
for _, left := range []bool{false, true} {
|
|
got := quotaCell(provider.QuotaWindow{Name: "Chat", Unlimited: true}, left, time.Now())
|
|
if !strings.Contains(got, "Unlimited") || strings.Contains(got, "%") || strings.Contains(got, "░") {
|
|
t.Fatalf("unlimited: %q", got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A plugin's row of text alone (AutoClaw's 积分余额) reads as its text: no
|
|
// meter and no share after it, where its used 0 read "100% left".
|
|
func TestTextQuotaCell(t *testing.T) {
|
|
for _, left := range []bool{false, true} {
|
|
got := quotaCell(provider.QuotaWindow{Name: "积分余额", Display: "1,234 积分", Text: true}, left, time.Now())
|
|
if !strings.Contains(got, "1,234 积分") || strings.Contains(got, "%") || strings.Contains(got, "░") {
|
|
t.Fatalf("text: %q", got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The Usage page shows, under the gateway's calls, those the agents made
|
|
// on their own, read from their session files, as the app's Usage page
|
|
// counts them (Kumo31 on Discord: Codex used outside magpie wasn't in the
|
|
// TUI).
|
|
func TestUsagePageShowsCallsNotThroughMagpie(t *testing.T) {
|
|
home(t)
|
|
// the usage clock held at noon, so that the calls a minute or two
|
|
// before it are today's whenever the test runs
|
|
now := time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local)
|
|
oldClock := usage.Clock
|
|
usage.Clock = func() time.Time { return now }
|
|
t.Cleanup(func() { usage.Clock = oldClock })
|
|
usage.Append(usage.Record{Time: now.Add(-time.Minute), Agent: "claude", Provider: "relay", Model: "m", Input: 5, Status: 200})
|
|
old := usage.LogCalls
|
|
usage.LogCalls = func(time.Time) []sessions.Call {
|
|
return []sessions.Call{{Time: now.Add(-2 * time.Minute), Agent: "codex", Session: "c1", Model: "gpt-6-luna", Tokens: sessions.Tokens{Input: 1200, Output: 300}}}
|
|
}
|
|
t.Cleanup(func() { usage.LogCalls = old })
|
|
m := press(t, model{w: 160, h: 60, page: pageUsage}, "t")
|
|
v := m.viewUsage()
|
|
gw, own, ok := strings.Cut(v, "not through magpie")
|
|
if !ok || !strings.Contains(gw, "relay/m") || strings.Contains(gw, "gpt-6-luna") || !strings.Contains(own, "gpt-6-luna") {
|
|
t.Fatal(v)
|
|
}
|
|
if strings.Contains(own, usage.UnknownProvider) {
|
|
t.Fatalf("the unknown provider's id is shown:\n%s", own)
|
|
}
|
|
}
|
|
|
|
// The Usage page asked for as midnight falls shows the calls through magpie
|
|
// and those not through it of one day, the day it was asked on, when a
|
|
// period is picked and when the page is read again.
|
|
func TestUsagePageAtMidnight(t *testing.T) {
|
|
home(t)
|
|
midnight := time.Date(2026, 10, 1, 0, 0, 0, 0, time.Local)
|
|
usage.Append(usage.Record{Time: midnight.Add(-time.Minute), Agent: "claude", Provider: "relay", Model: "m", Input: 5, Status: 200})
|
|
old := usage.LogCalls
|
|
usage.LogCalls = func(time.Time) []sessions.Call {
|
|
return []sessions.Call{{Time: midnight.Add(-2 * time.Minute), Agent: "codex", Session: "c1", Model: "gpt-6-luna", Tokens: sessions.Tokens{Input: 1200, Output: 300}}}
|
|
}
|
|
t.Cleanup(func() { usage.LogCalls = old })
|
|
// the usage clock reads a tenth of a second before midnight the first
|
|
// time and a tenth of a second after it from then on, as when midnight
|
|
// falls while the page is worked out
|
|
var read atomic.Bool
|
|
oldClock := usage.Clock
|
|
usage.Clock = func() time.Time {
|
|
if read.Swap(true) {
|
|
return midnight.Add(100 * time.Millisecond)
|
|
}
|
|
return midnight.Add(-100 * time.Millisecond)
|
|
}
|
|
t.Cleanup(func() { usage.Clock = oldClock })
|
|
both := func(how string, m model) {
|
|
t.Helper()
|
|
v := m.viewUsage()
|
|
gw, own, ok := strings.Cut(v, "not through magpie")
|
|
if !ok || !strings.Contains(gw, "relay/m") || !strings.Contains(own, "gpt-6-luna") {
|
|
t.Errorf("%s:\n%s", how, v)
|
|
}
|
|
}
|
|
m := press(t, model{w: 160, h: 60, page: pageUsage}, "t")
|
|
both("today picked", m)
|
|
read.Store(false)
|
|
m.reload()
|
|
both("read again", m)
|
|
}
|
|
|
|
// A Codex account set not to spend its credits says so beside them.
|
|
func TestQuotaLinesCreditsNotSpent(t *testing.T) {
|
|
home(t)
|
|
now := time.Date(2026, 9, 26, 12, 0, 0, 0, time.UTC)
|
|
qs := []provider.SubscriptionQuota{{Provider: "codex", Name: "Codex", User: "me@example.com", Balance: "1.2K credits",
|
|
Windows: []provider.QuotaWindow{{Name: "5 hours", Used: 100}}}}
|
|
if got := strings.Join(quotaLines(qs, true, false, 200, now), "\n"); strings.Contains(got, "not spent") {
|
|
t.Errorf("spends them by default:\n%s", got)
|
|
}
|
|
if err := provider.SetCodexCredits("Me@example.com", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := strings.Join(quotaLines(qs, true, false, 200, now), "\n"); !strings.Contains(got, "1.2K credits left · not spent") {
|
|
t.Errorf("set not to:\n%s", got)
|
|
}
|
|
}
|
|
|
|
// The Usage page's headline says how many calls its cost leaves out for
|
|
// having no known price (#1856), not only a "+" after the sum.
|
|
func TestUsagePageSaysCallsUnpriced(t *testing.T) {
|
|
home(t)
|
|
now := time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local)
|
|
oldClock := usage.Clock
|
|
usage.Clock = func() time.Time { return now }
|
|
t.Cleanup(func() { usage.Clock = oldClock })
|
|
old := usage.LogCalls
|
|
usage.LogCalls = func(time.Time) []sessions.Call { return nil }
|
|
t.Cleanup(func() { usage.LogCalls = old })
|
|
for i := range 3 {
|
|
usage.Append(usage.Record{Time: now.Add(-time.Duration(i+1) * time.Minute), Agent: "claude", Provider: "relay", Model: "kimi-k3-auto", Input: 500, Output: 50, Status: 200})
|
|
}
|
|
m := press(t, model{w: 160, h: 60, page: pageUsage}, "t")
|
|
if v := m.viewUsage(); !strings.Contains(v, "(3 calls unpriced)") {
|
|
t.Fatal(v)
|
|
}
|
|
}
|
|
|
|
// u on the Usage page shows a window by how much of it is left rather than
|
|
// used, as the app's toggle does, and that choice is Settings' QuotaLeft
|
|
// where the app keeps it: the app and the tray panel change it there, so the
|
|
// TUI starts from it (#122).
|
|
func TestQleftSeededFromSettings(t *testing.T) {
|
|
home(t)
|
|
if err := settings.Save(settings.Settings{QuotaLeft: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m := newModel(); !m.qleft {
|
|
t.Fatal("Settings say left, and the TUI starts showing used")
|
|
}
|
|
}
|
|
|
|
// u writes what it picked to Settings, so the pick survives the next start
|
|
// and the app's own toggle and the menu bar follow it.
|
|
func TestQleftWritesBack(t *testing.T) {
|
|
home(t)
|
|
if settings.Load().QuotaLeft {
|
|
t.Fatal("the sandbox's settings start with left chosen")
|
|
}
|
|
m := press(t, model{w: 120, h: 40, page: pageUsage}, "u")
|
|
if !m.qleft || !settings.Load().QuotaLeft {
|
|
t.Fatalf("after u: qleft %v, Settings %v", m.qleft, settings.Load().QuotaLeft)
|
|
}
|
|
m = press(t, m, "u")
|
|
if m.qleft || settings.Load().QuotaLeft {
|
|
t.Fatalf("after the second u: qleft %v, Settings %v", m.qleft, settings.Load().QuotaLeft)
|
|
}
|
|
}
|