Files
magpie/internal/provider/codex_reset_lead_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

151 lines
5.4 KiB
Go

package provider
import (
"net/http"
"os"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// setResetLead saves the user's lead, in minutes, in a sandbox HOME.
func setResetLead(t *testing.T, minutes int) {
t.Helper()
s := settings.Load()
s.CodexResetLead = minutes
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
}
// The spend and routing go by the lead the user set (the owner, 10-30:
// 把提前用卡的时间做成可配置的), the same one, so they can't drift apart
// (#717, #718): three hours set, a reset running out in four is spent in
// one, and routing takes the windows to start again then.
func TestResetLeadFollowsSetting(t *testing.T) {
signIn(t)
now := time.Now().Round(0)
until := now.Add(4 * time.Hour)
if got := expiringResetSpent(until, now, false, time.Time{}); !got.Equal(until.Add(-30 * time.Minute)) {
t.Fatalf("unset: spent at %v, want half an hour before", until.Sub(got))
}
setResetLead(t, 180)
want := until.Add(-3 * time.Hour)
if got := expiringResetSpent(until, now, false, time.Time{}); !got.Equal(want) {
t.Fatalf("spent %v before it runs out, want 3h", until.Sub(got))
}
a := Allowance{{Used: 20, Resets: now.Add(120 * time.Hour), Span: 7 * 24 * time.Hour}}.restartedBy(until)
if got := a.Restarts(now); !got.Equal(want) {
t.Fatalf("routing: restarts %v before it runs out, want 3h", until.Sub(got))
}
// within the lead already: at once, for both
now2 := until.Add(-2 * time.Hour)
if got := expiringResetSpent(until, now2, false, time.Time{}); !got.Equal(now2) {
t.Fatalf("within the lead: %v", got)
}
if got := a.Restarts(now2); !got.Equal(now2) {
t.Fatalf("routing within the lead: %v", got)
}
// and the check spends it then, not before
e := expiringResets{next: map[string]time.Time{}}
used := []QuotaWindow{{Span: 7 * 24 * time.Hour, Used: 40}}
if out, _ := e.check("me@example.com", want.Add(-time.Minute), func() ([]QuotaWindow, *ResetCredits, error) {
return used, &ResetCredits{Count: 1, Until: &until}, nil
}, func() (ResetOutcome, error) { return ResetOutcome{Code: "reset"}, nil }); out.Code != "" {
t.Fatalf("spent a minute before the lead: %+v", out)
}
e.next = map[string]time.Time{}
if out, _ := e.check("me@example.com", want, func() ([]QuotaWindow, *ResetCredits, error) {
return used, &ResetCredits{Count: 1, Until: &until}, nil
}, func() (ResetOutcome, error) { return ResetOutcome{Code: "reset"}, nil }); out.Code != "reset" {
t.Fatalf("not spent at the lead: %+v", out)
}
}
// A long lead still has several looks inside it: looks tighten to every
// resetExpiryClose half an hour before the lead starts, so a spend that
// fails when it is due is tried again five minutes later, not half an
// hour, and one missed look doesn't push the spend toward the end.
func TestResetLeadLooksInsideLongLead(t *testing.T) {
signIn(t)
setResetLead(t, 6*60)
start := time.Date(2026, 10, 30, 0, 0, 0, 0, time.Local)
until := start.Add(12 * time.Hour)
due := until.Add(-6 * time.Hour)
used := []QuotaWindow{{Span: 7 * 24 * time.Hour, Used: 40}}
e := expiringResets{next: map[string]time.Time{}}
var spent time.Time
var looks []time.Time
tries := 0
for now := start; now.Before(until); now = now.Add(resetExpiryClose) {
e.check("me@example.com", now, func() ([]QuotaWindow, *ResetCredits, error) {
looks = append(looks, now)
if !spent.IsZero() {
return used, nil, nil
}
return used, &ResetCredits{Count: 1, Until: &until}, nil
}, func() (ResetOutcome, error) {
// the first try fails (ChatGPT not answering)
if tries++; tries == 1 {
return ResetOutcome{}, http.ErrHandlerTimeout
}
spent = now
return ResetOutcome{Code: "reset"}, nil
})
}
if want := due.Add(resetExpiryClose); !spent.Equal(want) {
t.Fatalf("spent %v before it runs out, want %v (looked at %v)", until.Sub(spent), until.Sub(want), looks)
}
// looked at every five minutes from half an hour before the lead
n := 0
for _, l := range looks {
if !l.Before(due.Add(-30*time.Minute)) && !l.After(due) {
n++
}
}
if n != 7 {
t.Fatalf("%d looks in the half hour before the lead, want 7: %v", n, looks)
}
for _, c := range []struct{ lead, near time.Duration }{
{15 * time.Minute, time.Hour},
{30 * time.Minute, time.Hour},
{2 * time.Hour, 150 * time.Minute},
{24 * time.Hour, 24*time.Hour + 30*time.Minute},
} {
if got := resetExpiryNear(c.lead); got != c.near {
t.Errorf("lead %v: near %v, want %v", c.lead, got, c.near)
}
}
}
// A lead out of range, written by hand, or a settings file that can't be
// read, is the default half hour: never none, which would spend a reset
// only as it runs out.
func TestResetLeadUnreadIsDefault(t *testing.T) {
signIn(t)
for _, m := range []int{-5, 1, 14, 24*60 + 1, 1 << 30} {
setResetLead(t, m)
if got := resetLead(); got != 30*time.Minute {
t.Errorf("%d minutes: lead %v", m, got)
}
}
setResetLead(t, 15)
if got := resetLead(); got != 15*time.Minute {
t.Fatalf("15 minutes: lead %v", got)
}
if err := os.WriteFile(settings.Path(), []byte(`{"codexResetLead": 15,`), 0o600); err != nil {
t.Fatal(err)
}
if got := resetLead(); got != 30*time.Minute && got != 15*time.Minute {
// a half-written file is read as the last good one or the default
t.Fatalf("unreadable: lead %v", got)
}
if err := os.WriteFile(settings.Path(), []byte(`{"codexResetLead": "an hour"}`), 0o600); err != nil {
t.Fatal(err)
}
if got := resetLead(); got != 30*time.Minute && got != 15*time.Minute {
t.Fatalf("not a number: lead %v", got)
}
}