Files
magpie/internal/gateway/pace_expiry_test.go
T
yetone 95d0c97bf9 routing: "Use before expiry" counts credits that expire before the reset (lcyksp)
lcyksp on Discord: the 「重置前用完」 strategy (pace) ranked a WorkBuddy
account by its monthly 500 credits ("积分 0 / 500 · 已用 0% · 28 天后重置").
WorkBuddy spends check-in credits first because they expire sooner
("70.85 积分 将于 2026/11/3 09:28:52 过期"), and the account was ranked as
if nothing would be lost for 28 days.

What changed:
- provider.Allowance.Pace takes a budget window's soonest expiry into
  account (Limit.RunsOut, from QuotaWindow.RunsOutAt, which the
  WorkBuddy plugin and others set on the credits window). When that
  expiry comes before the window's reset, the window's pace is the
  higher of:
  - what is left per hour until the reset;
  - the share that expires (QuotaWindow.RunsOut, now carried as
    Limit.RunsOutLeft) per hour until it expires. When the amount isn't
    told, all that is left counts. A share is capped at 100%, and the
    hours at no less than one.
  The account whose credits are lost soonest per hour goes first.
  Expiry at or after the reset, or already past, changes nothing.
  Zero-config: nothing new to set.
- The trace says DueBy "expiry" when the expiry set the deadline. The
  Routing page's reason then reads "Use before expiry: {who} has the most
  credits expiring per hour until they expire — {n} of its allowance
  expires in {d}."
- Label: "Weekly pace" / 重置前用完 / 週ペース / Wochentempo becomes
  "Use before expiry" / 到期前用完 (zh, zh-TW) / 期限前に使い切る /
  Vor Ablauf aufbrauchen. Product call: the strategy's goal was always
  "lose nothing at the deadline". A reset and an expiry are both such a
  deadline, and "到期" covers both, where 「重置」 names only one. The id
  stays pace, so saved groups are unchanged.
- The descriptions (Routing, Settings, group editor), docs/reference.md,
  docs/subsystems/gateway-routing.md and the site's start page (en, zh,
  ja) say the same.

Go, run on mote at base f95bacdfb:
- gofmt -l internal plugins: clean.
- go test -count=1 -timeout 45m on ./internal/provider,
  ./internal/gateway/..., ./internal/routes, ./plugins/..., ./internal/gui,
  ./internal/tui and ./internal/cli: all ok except three reds that are
  filed and don't touch routing or pace:
  - TestCodexPoolSaysTheClientsVersion (#4807)
  - TestMainWindowCorners (#3949)
  - TestEveryRouteHasACommandOrIsNamed (#4826)
- GOOS=linux and GOOS=windows go vet on the same packages: ok.
- GUI: the touched files (routing-pace-expiry, routing-save,
  routing-group-strategy, routing-reset-deadline,
  routing-wallet-balance, builtins), chromium and webkit: 90 pass, 0
  fail.
- GUI on WSL magpie-desktop (Linux), after the rebase onto 9c58c87e8:
  the 387 files that name routing, i18n, strategy or reason text,
  chromium and webkit: 6211 tests, 6012 pass, 199 fail in 54 files.
  Another session's full suite was running beside it.
- Those 54 files again on mote, with this change and at 9c58c87e8
  without it: 100 and 99 reds. They are the same tests, differing only in
  which language went red. Filed as #4932. routing-group-strategy, whose
  one WSL red was a 9px webkit position, passed both times.
- After the last rebase, onto d29f02d65 (it changed gateway/routing.go,
  i18n.js and app.js), on mote: gofmt clean.
  - go test ./internal/provider ./internal/routes ./plugins/rotation/...:
    ok.
  - go test -run "Pace|Reset|Deadline|Expir|Routing|Weekly|Strategy|Order|Allowance"
    ./internal/gateway: ok.
  - linux and windows vet: ok.
  - GUI routing-pace-expiry, routing-save, routing-group-strategy,
    routing-reset-deadline, routing-wallet-balance, builtins and
    i18n-keys: 94 pass, 0 fail.
- c47fc516a (Tencent Cloud's Token Plan) landed after that run and was
  not run again with this change.

Broken by hand, each red:
- The expiry branch in Pace taken out: TestPaceCreditsExpiring and
  TestPaceGoesByCreditsExpiring.
- dueExpiry left unset: TestPaceGoesByCreditsExpiring's trace check.
- RunsOutLeft not carried: TestAllowanceCarriesRunsOutLeft.
- client.js's expiry reason line taken out: routing-pace-expiry, 0 of 10
  pass in chromium.

Not tried:
- A live WorkBuddy account with check-in credits about to expire. The
  fixtures use lcyksp's numbers (500 monthly, 70.85 expiring).
- The site pages are not deployed by this commit.
2026-11-02 21:52:57 +08:00

95 lines
3.7 KiB
Go

package gateway
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// Use before expiry (Pace) takes credits that expire before their window
// renews for that window's deadline: what expires, as a share of the
// window, per hour until it does. lcyksp on Discord: a WorkBuddy month of
// 500 credits renewing in 28 days, 70.85 check-in credits in it expiring
// tomorrow morning, was billed after an account whose month had more to
// lose per hour, so the check-in credits lapsed unspent. Credits that
// expire after the window renews change nothing, nor do a few credits
// expiring when the month has more to lose; when the amount isn't told,
// all that is left is taken as lost then, as Smart's Renewal takes it.
// The trace says the deadline is the expiry.
func TestPaceGoesByCreditsExpiring(t *testing.T) {
old := allowances
defer func() { allowances = old }()
now := time.Now()
share := map[string]provider.Allowance{}
allowances = func(string) map[string]provider.Allowance { return share }
const model = "deepseek-v4.1-flash"
p := provider.Provider{ID: "workbuddy", Routing: provider.Pace, Account: &provider.Account{Agent: "workbuddy", User: "a@x.com"}}
acct := func(user string) candidate {
return candidate{p: provider.Provider{ID: "workbuddy", Account: &provider.Account{Agent: "workbuddy", User: user}}, model: model, rest: "workbuddy#" + user}
}
month := func(used float64, renews time.Duration) provider.Allowance {
return provider.Allowance{{Used: used, Amount: 500 * used / 100, Of: 500, Unit: "credits", Resets: now.Add(renews), Name: "Credits"}}
}
order := func() (string, weighing, []candidate) {
got, wg := weigh(p, []candidate{acct("b@x.com"), acct("a@x.com")}, model, provider.Chat)
var s []string
for _, c := range got {
s = append(s, c.p.Account.User[:1])
}
return strings.Join(s, ""), wg, got
}
// B: half its month left, renewing in ten days: 50/240h ≈ 0.21 an
// hour, ahead of A's 100/672h ≈ 0.15 when nothing of A's expires
share["b@x.com"] = month(50, 10*24*time.Hour)
share["a@x.com"] = month(0, 28*24*time.Hour)
if got, _, _ := order(); got != "ba" {
t.Fatalf("nothing expiring: %s", got)
}
// the report: 70.85 of A's credits expire in 14 hours, 14% of its
// 500 lost then, 1% an hour
share["a@x.com"][0].RunsOut, share["a@x.com"][0].RunsOutLeft = now.Add(14*time.Hour), 70.85
got, wg, cs := order()
if got != "ab" {
t.Fatalf("credits expiring tomorrow go first: %s", got)
}
w := weighed(cs[0], p, wg, false, provider.Chat)
if w.DueBy != "expiry" || w.Due == nil || !w.Due.Equal(now.Add(14*time.Hour)) {
t.Fatalf("trace: due %v by %q", w.Due, w.DueBy)
}
if b := weighed(cs[1], p, wg, false, provider.Chat); b.DueBy != "" {
t.Fatalf("trace: B by %q", b.DueBy)
}
// the amount not told: all A has left is taken as lost then
share["a@x.com"][0].RunsOutLeft = 0
if got, _, _ := order(); got != "ab" {
t.Fatalf("amount not told: %s", got)
}
// one credit expiring: 0.2% over 14h is less than A's month per hour,
// so its month decides, and B goes first
share["a@x.com"][0].RunsOutLeft = 1
if got, wg, cs = order(); got != "ba" {
t.Fatalf("a credit expiring: %s", got)
}
if w := weighed(cs[1], p, wg, false, provider.Chat); w.DueBy != "" {
t.Fatalf("trace, month decides: by %q", w.DueBy)
}
// expiring after the month renews: lost to the reset first, nothing
// changes
share["a@x.com"][0].RunsOut, share["a@x.com"][0].RunsOutLeft = now.Add(30*24*time.Hour), 70.85
if got, _, _ := order(); got != "ba" {
t.Fatalf("expiring after the renewal: %s", got)
}
// expired already: nothing changes
share["a@x.com"][0].RunsOut = now.Add(-time.Hour)
if got, _, _ := order(); got != "ba" {
t.Fatalf("expired already: %s", got)
}
}