Files
magpie/internal/provider/allowance_fullest_test.go
T
yetone 446bffcac7 gateway, gui: a Routing row's share used renews with its own window, not the biggest one (#1778)
Vigilans' Factory account showed "32% used · renews in 27 days": the
week's 32% beside the month's reset. Allowance.For gives the fullest
window's share and the renewals biggest first, and routing.js paired the
share with renews[0], so for any account of more than one window the row
mixed two windows.

- provider.Allowance.Fullest(model, now): the reset of the window For's
  share is of (the later of two as full; a window whose reset has passed
  counts as empty, as For does).
- The trace's Weighed gains usedRenews (from weighRouted's left.fullest);
  routing.js's usedRenews(w) uses it in both the story rows and the
  account rows, and falls back to renews[0] only for an account of one
  window (a route recorded before this commit).
- Smart's order is unchanged on purpose: it goes by the biggest window,
  and Factory's month holds less than its weeks add up to, so what a week
  leaves isn't lost at the week's reset. docs/reference.md says so, and
  gateway-routing.md names Fullest/usedRenews.

Run:
- go test ./internal/provider ./internal/gateway -run
  'Fullest|UsedRenews|Weigh|Routing|Smart' and the packages whole.
- TestUsedRenewsWithItsOwnWindow fails with left.fullest set to the
  biggest window's reset (broken by hand, then restored).
- routing-key-windows.test.cjs: 44/44 in Chromium and WebKit, all
  languages and widths; it fails on HEAD's routing.js.
- Every routing-*.test.cjs plus panel-routing in both engines: 594/601.
  The 7 WebKit failures are clicks waiting for an element to be "stable"
  under load average ~44, in routing-group-fold (#1612, #1672),
  routing-sessions (#1307) and routing-days-fit. routing-days-fit passes
  run alone. The other two fail the same alone with HEAD's routing.js.

Not tried against a real Factory account: the windows are those the
Factory plugin's limitWindows reports (5h/7d/30d with resetsAt).
2026-10-12 19:38:13 +08:00

42 lines
1.6 KiB
Go

package provider
import (
"testing"
"time"
)
// Fullest is when the window For's share is of renews (#1778): Factory's
// week, 32% used, not its month, 16% and the biggest. Of two as full, the
// later; one whose reset has passed is empty again; a window of other
// models doesn't count.
func TestFullestRenewsWithTheShare(t *testing.T) {
now := time.Now()
five, week, month := now.Add(4*time.Hour), now.Add(3*24*time.Hour), now.Add(27*24*time.Hour)
a := Allowance{
{Used: 0, Resets: five, Span: 5 * time.Hour},
{Used: 32, Resets: week, Span: 7 * 24 * time.Hour},
{Used: 16, Resets: month, Span: 30 * 24 * time.Hour},
{Used: 90, Resets: five, Span: 7 * 24 * time.Hour, Model: "glm"},
}
if used, renews := a.For("claude-opus-5-5", now); used != 32 || !renews[0].Equal(month) {
t.Fatalf("For: %v %v", used, renews)
}
if got := a.Fullest("claude-opus-5-5", now); !got.Equal(week) {
t.Fatalf("Fullest %v, want the week's %v", got, week)
}
if got := a.Fullest("glm-5.2", now); !got.Equal(five) {
t.Fatalf("a model with a window of its own: %v, want %v", got, five)
}
tie := Allowance{{Used: 100, Resets: five, Span: 5 * time.Hour}, {Used: 100, Resets: week, Span: 7 * 24 * time.Hour}}
if got := tie.Fullest("m", now); !got.Equal(week) {
t.Fatalf("two used up: %v, want the later %v", got, week)
}
// the week renewed: it is empty, the month the fullest
if got := a.Fullest("claude-opus-5-5", week.Add(time.Minute)); !got.Equal(month) {
t.Fatalf("after the week renewed: %v, want the month's %v", got, month)
}
if got := (Allowance{{Used: 40, Span: 7 * 24 * time.Hour}}).Fullest("m", now); !got.IsZero() {
t.Fatalf("a reset not known: %v", got)
}
}