* tui: show a ChatGPT account's credits beside its windows The Usage page printed only the balance for an account that had one, so a ChatGPT account holding credits lost its 5-hour and weekly windows there. The balance now follows the windows, as on the app's cards and in magpie quota. * accounts: tell a ChatGPT account's credits magpie accounts left out the credits an account holds, which magpie quota and the Usage page show. Its rows now carry them, in the line and as balance in --json. * codex: a switch for whether an account spends its credits A ChatGPT account holding credits keeps answering once a usage window is used up: the vendor spends the credits, and a task goes on. That stays the default. Each Codex account now has a say in it (Usage card and menu bar panel: Use credits; magpie quota credits <account> on|off; settings' codexNoCredits). Turned off, the gateway holds the account at 100% of a window, as a usage cap holds it at its share, so the other accounts, groups and fallbacks take the request and no credits are spent. With none left the request gets a 429 naming what holds each account and how to go on, unless the account spends its resets by itself and its week is used up: then a reset is spent first, whether every candidate was held or the last one tried was refused. A cap set on the account takes precedence. A reset made now forgets the account's cached allowance, so neither a cap nor this hold outlives the windows it started again. The switch is routing's only: which account Codex is signed in to is unchanged. The Routing page tells why such an account was left out, and magpie quota, magpie accounts and the TUI mark credits not spent. * usage: an auto-reset switch keeps by its first line The auto-reset row centred its switch on its words, so the switch moved when they rewrapped on a click, and it sat lower than the Use credits switch beneath it. Both now keep the switch by the first line. * gui tests: quota-curve expects the Use credits row to close a Codex account's section The credits switch adds a row after the Auto-use one, so the account after a Codex account follows the curve, the Auto-use row and then the Use credits row.
161 lines
6.5 KiB
Go
161 lines
6.5 KiB
Go
package tui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"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)
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
now := time.Now()
|
|
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)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|