Files
magpie/internal/gateway/reset_deadline_test.go
T
yetone 995f84a0f9 codex: how early an expiring reset is used is a setting
A Codex reset card that would run out unused was spent 30 minutes before
it expired, a fixed resetExpiryLead. Now the lead is codexResetLead in
settings.json (minutes, 15 to 1440). Unset, out of range or unreadable
reads as 30m, never 0, and magpie never writes it unless the user does.

- settings: CodexResetLead and Settings.ResetLead(); the menu's values
  are 15m, 30m (default), 1h, 2h, 3h, 6h, 12h, 24h.
- provider: the spend (expiringResetSpent) and routing
  (Allowance.Restarts, through nextResetLook) read the same resetLead(),
  so they still agree (#717, #718). The "near" look cadence follows it:
  lead + 30m, at least 1h, so a look lands inside a long lead.
- GUI: Settings > Usage, under the Codex auto-use rows, a row "Codex:
  how early before one expires" with an app menu (no <select>), and the
  sub text says spending earlier loses what the windows have left. The
  four "before it expires" strings say the chosen lead. POST
  /api/settings/codex-reset-lead; the general save keeps the field.
  en, zh, zh-TW, ja, de.
- CLI: magpie quota auto-reset lead [30m|3h|<minutes>|default].
- app.js budget: the auto-use rows moved out to lazy autoresetown.js
  (painted in the same draw once loaded), so app.js and the core script
  stay under their budgets with no raise. The button sizes to its widest
  label (a grid of every label), so picking one never rewraps the row.

Run (all on mote through mote-run):
- go vet: darwin, GOOS=linux -tags nogui, GOOS=windows; nogui build.
- go test -tags nogui -timeout 45m ./... at 3955adccf (this change
  before the paint and sizer fixes, which touch only GUI assets): 81 ok.
  go test ./internal/gui at this head: ok.
- GUI, Chromium: codex-reset-lead, codex-auto-reset,
  codex-auto-reset-settings, segs-press-redraw, i18n-keys: 62/62.
  WebKit: every case of those files failed at browserType.launch
  (180s timeout) before a page loaded, both runs; mote's browser
  launches hang under #4125 (load ~26), so WebKit is not run.
- Rebased onto f7f2d75f2 (old-webkit's lazy list now has traywindows.js
  too): go build ./..., go vet -tags nogui ./internal/..., go test
  internal/gui (budgets included), cli, settings, gateway: ok. provider:
  ok but TestQuotaSpanCarriesEveryWindowField, red on main since
  7d4a63148 (#4457, fixed in ccbefe2a2). GUI old-webkit, i18n-keys,
  codex-reset-lead in Chromium: pass.
- Rebased again onto 62a1dcdd7 (clean): go test internal/gui (budgets)
  and internal/provider: ok; i18n-keys and Chromium codex-reset-lead
  12/12 pass; old-webkit 9/10, its Safari 15.0 case a WebKit launch
  timeout (#4125).

Broken by hand, each went red:
- resetLead() pinned to 30m: all 3 provider tests.
- resetExpiryNear pinned to 1h: the long-lead look test.
- ResetLead's range check turned into m == 0: TestResetLeadUnreadIsDefault.
- GUI resetLeadMinutes returning 30: codex-reset-lead in every language.
- The button without the sizer: de at 420px, "no click moved the page".

Not tried: a real Codex account spending a card at a non-default lead;
Windows and Linux runs (vet only). The whole GUI suite in Chromium on
mote has ~45 tests in 31 files crash with "browser has been closed" on
untouched origin/main c84c3e1c0 too (reducedMotion files, #4458).
2026-10-31 00:41:29 +08:00

106 lines
4.2 KiB
Go

package gateway
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A Codex account that spends a reset about to run out by itself (auto-use
// on, its windows used) loses what its week has left when that reset is
// spent, resetLead (half an hour unless set) before it runs out: Weekly pace and Smart take
// that for its deadline when it is sooner than the week renews (#717,
// #718, thedavidweng: A with 80% left renewing in five days and a reset
// running out that afternoon went behind B with 50% left renewing in two).
// Holding a reset that runs out after the week renews, or none, changes
// nothing; the 90% and 98% tiers are kept; the trace says when the reset
// set it.
func TestResetSpentIsTheDeadline(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 }
acct := func(user string) candidate {
return candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: user}}, model: "gpt-5.6-sol", rest: "codex#" + user}
}
// runsOut zero: no reset the account spends by itself — none held,
// or auto-use off (provider.resetRunsOut leaves it out then)
week := func(used float64, renews, runsOut time.Duration) provider.Allowance {
a := provider.Allowance{
{Used: 10, Resets: now.Add(3 * time.Hour), Span: 5 * time.Hour},
{Used: used, Resets: now.Add(renews), Span: 7 * 24 * time.Hour},
}
if runsOut != 0 {
for i := range a {
a[i].ResetRunsOut = now.Add(runsOut)
}
}
return a
}
for _, routing := range []string{provider.Pace, ""} {
p := provider.Provider{ID: "codex", Routing: routing, Account: &provider.Account{Agent: "codex", User: "a@x.com"}}
order := func() (string, weighing, []candidate) {
got, wg := weigh(p, []candidate{acct("b@x.com"), acct("a@x.com")}, "gpt-5.6-sol", provider.Chat)
var s []string
for _, c := range got {
s = append(s, c.p.Account.User[:1])
}
return strings.Join(s, ""), wg, got
}
name := map[string]string{provider.Pace: "pace", "": "smart"}[routing]
share["b@x.com"] = week(50, 48*time.Hour, 0)
// no reset: B, renewing sooner, as before
share["a@x.com"] = week(20, 120*time.Hour, 0)
if got, _, _ := order(); got != "ba" {
t.Fatalf("%s, no reset: %s", name, got)
}
// the issue's case: A's reset runs out in an hour and a half, spent
// in one, its 80% lost
share["a@x.com"] = week(20, 120*time.Hour, 90*time.Minute)
got, wg, cs := order()
if got != "ab" {
t.Fatalf("%s, reset spent before the week renews: %s", name, got)
}
w := weighed(cs[0], p, wg, false, provider.Chat)
if routing == provider.Pace && (w.DueBy != "reset" || w.Due == nil || !w.Due.Equal(now.Add(time.Hour))) {
t.Fatalf("pace trace: due %v by %q", w.Due, w.DueBy)
}
if w.Restarts == nil {
t.Fatalf("%s trace: restarts not told", name)
}
if b := weighed(cs[1], p, wg, false, provider.Chat); b.DueBy != "" || b.Restarts != nil {
t.Fatalf("%s trace: B by %q, restarts %v", name, b.DueBy, b.Restarts)
}
// the reset runs out after the week renews: the week decides
share["a@x.com"] = week(20, 120*time.Hour, 6*24*time.Hour)
if got, wg, cs = order(); got != "ba" {
t.Fatalf("%s, week sooner than the reset: %s", name, got)
}
if w := weighed(cs[1], p, wg, false, provider.Chat); w.DueBy != "" || w.Restarts != nil {
t.Fatalf("%s trace, week sooner: by %q, restarts %v", name, w.DueBy, w.Restarts)
}
// A sooner than the week renews, but at 90% of its five hours:
// it waits behind B all the same
share["a@x.com"] = week(20, 120*time.Hour, 90*time.Minute)
share["a@x.com"][0].Used = 92
if got, _, _ := order(); got != "ba" {
t.Fatalf("%s, low tier kept: %s", name, got)
}
// all but used up: last, whatever its reset
share["a@x.com"] = week(99, 120*time.Hour, 90*time.Minute)
if got, _, _ := order(); got != "ba" {
t.Fatalf("%s, spent tier kept: %s", name, got)
}
// both spend one: the sooner first
share["a@x.com"] = week(20, 120*time.Hour, 30*time.Hour)
share["b@x.com"] = week(50, 120*time.Hour, 10*time.Hour)
if got, _, _ := order(); got != "ba" {
t.Fatalf("%s, both spend one, B sooner: %s", name, got)
}
}
}