tui: the Usage page reads again while a read is under way, and on each opening (sean_80289)
sean_80289 on Discord: in the TUI the remaining-usage panel didn't show
new numbers; only refreshing each account on its own page did. The page
read the quotas once per TUI start, unasked, so it showed what was kept
and never what a read under way brought.
It now does what the GUI's Usage page does (#959):
- Opening the page more than 30s after the last ask (quotasOpenGap, the
GUI's QUOTAS_OPEN_GAP) is an asked read (provider.Asked,
AskClaudeUsage); within 30s it reads what is kept.
- While provider.SubscriptionUsageReading() says a read is under way, it
reads again every 1.5s, at most 20 times, as the GUI does on
X-Magpie-Reading; it stops when the page is left.
- "r" asks again. An answer to an earlier load is dropped (qgen).
Run on mote at a8d6a861a, one commit (internal/proc only) before the
pushed head: go test ./internal/tui ok; go vet
internal/tui internal/cli for darwin, linux (-tags nogui) and windows;
gofmt -l clean. go test ./internal/cli: TestEveryRouteHasACommandOrIsNamed
is red on origin/main too (POST /api/usage/checkin-account, #4826), not
this change.
At the pushed head after the rebase: go build ./... ok, go test
./internal/tui ./internal/proc ok.
Broken by hand: with the re-read in gotQuotas taken out and goTo back
to reading only once, TestUsagePageAsksAgainWhileReading,
TestUsagePageReadsAgainOnReturn and TestUsagePageAsksAgainAtMostTries
went red.
Not tried: a real TUI session against signed-in accounts; the tests
drive the model with a fake read.
This commit is contained in:
+67
-6
@@ -837,16 +837,77 @@ var periods = []usage.Period{usage.Today, usage.Week, usage.Month, usage.All}
|
||||
var periodNames = map[usage.Period]string{usage.Today: "today", usage.Week: "7 days", usage.Month: "30 days", usage.All: "all time"}
|
||||
|
||||
// quotaMsg is what is left of every subscription, plan bought with a key
|
||||
// and key balance, as the app's Usage page shows them.
|
||||
type quotaMsg []provider.SubscriptionQuota
|
||||
// and key balance, as the app's Usage page shows them. reading is that a
|
||||
// read of the accounts' usage was still under way, so qs may be the stale
|
||||
// copy it replaces; gen is the load it answers (model.qgen).
|
||||
type quotaMsg struct {
|
||||
qs []provider.SubscriptionQuota
|
||||
reading bool
|
||||
gen int
|
||||
}
|
||||
|
||||
func quotasCmd() tea.Msg {
|
||||
provider.AskClaudeUsage() // the page opened, or r pressed
|
||||
ctx, cancel := context.WithTimeout(provider.Asked(context.Background()), 12*time.Second)
|
||||
// The Usage page reads as the app's does (loadQuotas in app.js): opening
|
||||
// it is asking (an asked read, Claude Code's /usage run) unless the last
|
||||
// ask was within quotasOpenGap, and an answer given while a read is under
|
||||
// way is asked for again every quotasAgain, quotasTries times at most,
|
||||
// until the new one has landed (#959). The terminal asked only once a
|
||||
// session, so the page kept what it first read till r was pressed
|
||||
// (sean_80289 on Discord).
|
||||
var (
|
||||
quotasOpenGap = 30 * time.Second
|
||||
quotasAgain = 1500 * time.Millisecond
|
||||
)
|
||||
|
||||
const quotasTries = 20
|
||||
|
||||
// readQuotas is every card, and whether a read is still under way; a var
|
||||
// so tests can stand in for the vendors.
|
||||
var readQuotas = func(asked bool) ([]provider.SubscriptionQuota, bool) {
|
||||
ctx := context.Background()
|
||||
if asked {
|
||||
provider.AskClaudeUsage() // the page opened, or r pressed
|
||||
ctx = provider.Asked(ctx)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 12*time.Second)
|
||||
defer cancel()
|
||||
// a WorkBuddy (China) account's line says how its daily check-in went,
|
||||
// as its card in the app does
|
||||
return quotaMsg(provider.InProviderOrder(provider.WithCheckins(provider.Quotas(ctx))))
|
||||
qs := provider.InProviderOrder(provider.WithCheckins(provider.Quotas(ctx)))
|
||||
return qs, provider.SubscriptionUsageReading()
|
||||
}
|
||||
|
||||
func quotasCmd(asked bool, gen int) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
qs, reading := readQuotas(asked)
|
||||
return quotaMsg{qs: qs, reading: reading, gen: gen}
|
||||
}
|
||||
}
|
||||
|
||||
// loadQuotas starts a read of the cards, asked or not, which stands in
|
||||
// for any read before it still on its way.
|
||||
func (m *model) loadQuotas(asked bool) tea.Cmd {
|
||||
if asked {
|
||||
m.qaskedAt = time.Now()
|
||||
}
|
||||
m.qasked = true
|
||||
m.qgen++
|
||||
m.qtries = 0
|
||||
return quotasCmd(asked, m.qgen)
|
||||
}
|
||||
|
||||
// gotQuotas takes a read's answer, and asks again while the read it was
|
||||
// given under is still on its way and the page is up.
|
||||
func (m *model) gotQuotas(msg quotaMsg) tea.Cmd {
|
||||
if msg.gen != m.qgen {
|
||||
return nil // a load since stands in for it
|
||||
}
|
||||
m.quotas = msg.qs
|
||||
if !msg.reading || m.page != pageUsage || m.qtries >= quotasTries {
|
||||
return nil
|
||||
}
|
||||
m.qtries++
|
||||
gen := m.qgen
|
||||
return tea.Tick(quotasAgain, func(time.Time) tea.Msg { return quotasCmd(false, gen)() })
|
||||
}
|
||||
|
||||
// checkinMsg is what came of c, WorkBuddy's daily check-in pressed now.
|
||||
|
||||
+10
-7
@@ -127,6 +127,9 @@ type model struct {
|
||||
direct usage.Summary // the agents' own calls, read from their session files
|
||||
quotas []provider.SubscriptionQuota // nil while the vendors are asked
|
||||
qasked bool // quotas were asked for
|
||||
qaskedAt time.Time // when they were last asked for (an asked read)
|
||||
qgen int // the quotas load the page waits for
|
||||
qtries int // that load's asks again while a read was under way
|
||||
qleft bool // windows read as what is left, not what is used
|
||||
sstats sessions.Stats // the sessions' usage in the range picked
|
||||
srange int // in sessRanges
|
||||
@@ -230,9 +233,10 @@ func (m *model) goTo(p page) tea.Cmd {
|
||||
m.asked = true
|
||||
return balancesCmd(m.provs)
|
||||
}
|
||||
if p == pageUsage && !m.qasked {
|
||||
m.qasked = true
|
||||
return quotasCmd
|
||||
if p == pageUsage {
|
||||
// opened: an asked read, or what is kept within quotasOpenGap of
|
||||
// the last ask (going to another page and back)
|
||||
return m.loadQuotas(!m.qasked || time.Since(m.qaskedAt) >= quotasOpenGap)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -304,8 +308,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
return m, nil
|
||||
case quotaMsg:
|
||||
m.quotas = msg
|
||||
return m, nil
|
||||
return m, m.gotQuotas(msg)
|
||||
case checkinMsg:
|
||||
m.flash, m.flashOK = msg.text, msg.ok
|
||||
// the accounts' lines say today's check-in now, with nothing asked
|
||||
@@ -378,8 +381,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
m.reload()
|
||||
m.flash, m.flashOK = "reloaded", true
|
||||
if m.page == pageUsage {
|
||||
m.quotas, m.qasked = nil, true
|
||||
return m, quotasCmd
|
||||
m.quotas = nil
|
||||
return m, m.loadQuotas(true)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// fakeQuotas stands in for the vendors: each read answers the next of
|
||||
// answers (the last one again once they run out), and says whether it was
|
||||
// asked.
|
||||
type fakeQuotas struct {
|
||||
answers []fakeAnswer
|
||||
asked []bool
|
||||
}
|
||||
|
||||
type fakeAnswer struct {
|
||||
used float64
|
||||
reading bool
|
||||
}
|
||||
|
||||
func (f *fakeQuotas) install(t *testing.T) {
|
||||
t.Helper()
|
||||
readQ, gap, again := readQuotas, quotasOpenGap, quotasAgain
|
||||
t.Cleanup(func() { readQuotas, quotasOpenGap, quotasAgain = readQ, gap, again })
|
||||
quotasAgain = time.Millisecond
|
||||
readQuotas = func(asked bool) ([]provider.SubscriptionQuota, bool) {
|
||||
f.asked = append(f.asked, asked)
|
||||
a := f.answers[min(len(f.asked), len(f.answers))-1]
|
||||
return []provider.SubscriptionQuota{{Provider: "claude", User: "a@b.c", Windows: []provider.QuotaWindow{{Name: "5h", Used: a.used}}}}, a.reading
|
||||
}
|
||||
}
|
||||
|
||||
// run feeds cmd's messages back into m till nothing more is asked, as the
|
||||
// program does (a tick waits quotasAgain).
|
||||
func run(t *testing.T, m model, cmd tea.Cmd) model {
|
||||
t.Helper()
|
||||
for i := 0; cmd != nil; i++ {
|
||||
if i > 100 {
|
||||
t.Fatal("the page never stops asking")
|
||||
}
|
||||
next, c := m.Update(cmd())
|
||||
m, cmd = next.(model), c
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func used(m model) float64 {
|
||||
if len(m.quotas) == 0 || len(m.quotas[0].Windows) == 0 {
|
||||
return -1
|
||||
}
|
||||
return m.quotas[0].Windows[0].Used
|
||||
}
|
||||
|
||||
// TestUsagePageAsksAgainWhileReading: an answer given while the accounts'
|
||||
// usage is still being read is the stale copy that read replaces. The page
|
||||
// asks again until the new one has landed, as the app's does (#959), not
|
||||
// leave the stale one up till r is pressed (sean_80289).
|
||||
func TestUsagePageAsksAgainWhileReading(t *testing.T) {
|
||||
f := &fakeQuotas{answers: []fakeAnswer{{10, true}, {10, true}, {64, false}}}
|
||||
f.install(t)
|
||||
m := model{w: 160, h: 60}
|
||||
cmd := m.goTo(pageUsage)
|
||||
m = run(t, m, cmd)
|
||||
if got := used(m); got != 64 {
|
||||
t.Fatalf("the page shows %v%% used, want the new reading's 64%%", got)
|
||||
}
|
||||
if want := []bool{true, false, false}; len(f.asked) != len(want) || f.asked[0] != true || f.asked[1] || f.asked[2] {
|
||||
t.Fatalf("reads asked %v, want %v: the page opened asks, its asks again take what is read", f.asked, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUsagePageAsksAgainAtMostTries: a read that never lands is asked for
|
||||
// quotasTries times, not forever.
|
||||
func TestUsagePageAsksAgainAtMostTries(t *testing.T) {
|
||||
f := &fakeQuotas{answers: []fakeAnswer{{10, true}}}
|
||||
f.install(t)
|
||||
m := model{w: 160, h: 60}
|
||||
m = run(t, m, m.goTo(pageUsage))
|
||||
if len(f.asked) != 1+quotasTries {
|
||||
t.Fatalf("%d reads, want %d", len(f.asked), 1+quotasTries)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUsagePageStopsAskingOffThePage: the asks again stop once the page is
|
||||
// left; coming back reads anew.
|
||||
func TestUsagePageStopsAskingOffThePage(t *testing.T) {
|
||||
f := &fakeQuotas{answers: []fakeAnswer{{10, true}}}
|
||||
f.install(t)
|
||||
m := model{w: 160, h: 60}
|
||||
cmd := m.goTo(pageUsage)
|
||||
m.page = pageSessions
|
||||
m = run(t, m, cmd)
|
||||
if len(f.asked) != 1 {
|
||||
t.Fatalf("%d reads off the page, want 1", len(f.asked))
|
||||
}
|
||||
}
|
||||
|
||||
// TestUsagePageReadsAgainOnReturn: the page opened again reads anew, as
|
||||
// the app's does (quotasOpened), where the terminal read once a session:
|
||||
// within quotasOpenGap of the last ask it takes what is kept, after it it
|
||||
// asks.
|
||||
func TestUsagePageReadsAgainOnReturn(t *testing.T) {
|
||||
f := &fakeQuotas{answers: []fakeAnswer{{10, false}, {30, false}, {64, false}}}
|
||||
f.install(t)
|
||||
m := model{w: 160, h: 60}
|
||||
m = run(t, m, m.goTo(pageUsage))
|
||||
m.page = pageSessions
|
||||
m = run(t, m, m.goTo(pageUsage))
|
||||
if got := used(m); got != 30 {
|
||||
t.Fatalf("back on the page within the gap it shows %v%%, want what is kept now, 30%%", got)
|
||||
}
|
||||
m.page = pageSessions
|
||||
m.qaskedAt = time.Now().Add(-quotasOpenGap - time.Second)
|
||||
m = run(t, m, m.goTo(pageUsage))
|
||||
if got := used(m); got != 64 {
|
||||
t.Fatalf("back on the page after the gap it shows %v%%, want 64%%", got)
|
||||
}
|
||||
if want := []bool{true, false, true}; len(f.asked) != 3 || f.asked[0] != want[0] || f.asked[1] != want[1] || f.asked[2] != want[2] {
|
||||
t.Fatalf("reads asked %v, want %v", f.asked, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUsageOldAnswerStandsAside: an answer to a load since replaced (r
|
||||
// pressed while an ask again was on its way) is dropped, so it neither
|
||||
// draws over the new one nor starts asks of its own.
|
||||
func TestUsageOldAnswerStandsAside(t *testing.T) {
|
||||
m := model{w: 160, h: 60, page: pageUsage, qgen: 2}
|
||||
next, cmd := m.Update(quotaMsg{qs: []provider.SubscriptionQuota{{Provider: "claude"}}, reading: true, gen: 1})
|
||||
m = next.(model)
|
||||
if cmd != nil || m.quotas != nil {
|
||||
t.Fatalf("an old load's answer was taken: cmd %v, quotas %v", cmd != nil, m.quotas)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user