Files
magpie/internal/provider/checkin_until_test.go
T
yetone cd77c725d2 check-in: a plugin's failed with a later until waits for it (plugins#503, plugins#793)
A plugin's check-in can answer failed with an until hours away: ZCode
rate limits a claim read, and its plugin (plugins#793) says when to ask
again. magpie asked any failed again after 30 minutes whatever the until
said, the half hour that kept the limit on (#503: "gift plans to claim:
Too Many Requests" 180 times over four days). A review probe on
origin/main 19c45d5d4 saw a failed with until 6h out asked at 31 minutes
and at 2 hours.

Now heldUntil holds a failed whose until is later than its own retry
until that time, at most 24 hours after the failure (checkinHoldMax), so
a wrong far-off until can't stop check-ins for good. Every path that
schedules a check-in follows it:
- settled: the loop doesn't ask before it, across the Beijing day's turn
  too; a press (soon) still asks at once.
- the recheck branch (ZCode, Qoder) counts a held failure as waiting,
  where it was otherwise asked again every 30 minutes.
- the loop's wake time (wbCheckinLoop.again) is the until, not 30
  minutes on.
A failed with no until keeps the 30-minute retry (offline failures their
minute ladder), and one with an until sooner than the retry is asked at
the until, as before. The GUI and TUI show no time for the next ask
("magpie tries again later"), so nothing there changed.
docs/subsystems/provider-plugins.md says it.

Run (mote, through mote-run):
- go test -count=1 -timeout 30m ./internal/provider/ ./internal/tui/
  ./internal/cli/: ok (193.7s, 17.1s, 76.6s).
- go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet on
  ./internal/provider/: ok. gofmt -l: nothing.
- The full suite runs after the push, through the suite lock.

Broken by hand (new tests TestPluginCheckinFailedWaitsForUntil,
TestPluginCheckinFailedUntilBounded):
- the old settled/checkin.go/tick: red, "loop at +31m0s: asked again
  (2 asks), until ..." for zcode and a plain plugin, and the far until
  case asked at +31m.
- the recheck branch without the held failure: zcode red at +31m.
- the old wake time: "the loop wakes at 14:30, want the until 20:00".
- no 24h cap: "at +24h0m0s: not asked again".
Restored: ok.

Not tried: a real ZCode account rate limited with until; the plugin's
own backoff (plugins#793) still holds off for older magpie.

Reported-by: kongbai01203 (plugins#503)
2026-11-06 19:48:21 +08:00

120 lines
4.5 KiB
Go

package provider
import (
"context"
"slices"
"testing"
"time"
)
// pluginCheckinFails is a plugin provider whose one account's check-in
// answers failed, with until (zero for none) after the ask, counting asks.
func pluginCheckinFails(id string, until func(time.Time) time.Time, now *time.Time, calls *int) pluginCheckinProvider {
return pluginCheckinProvider{ID: id, accounts: []pluginCheckinAcct{{User: "ann", On: true, key: id + "|ann", card: id, do: func(context.Context) WorkBuddyCheckin {
*calls++
r := WorkBuddyCheckin{Outcome: CheckinFailed, Msg: "gift plans to claim: Too Many Requests"}
if until != nil {
r.ValidUntil = until(*now)
}
return r
}}}}
}
// plugins#503 (kongbai01203), plugins#793: ZCode rate limits a claim read,
// and the plugin answers failed with an until hours away. magpie asked it
// again 30 minutes on all the same, which kept the limit on. A failed with
// an until later than its retry is not asked by the loop before the until,
// across the Beijing day's turn too; at most 24 hours after the failure; a
// press still asks; one with no until is asked again in 30 minutes.
func TestPluginCheckinFailedWaitsForUntil(t *testing.T) {
for _, id := range []string{"zcode", "fakeco"} { // rechecked or not
t.Run(id, func(t *testing.T) {
claudeHome(t)
// 22:00 Beijing: a 6h until is past the day's turn
start := time.Date(2026, 11, 5, 14, 0, 0, 0, time.UTC)
now, calls := start, 0
sixHours := func(at time.Time) time.Time { return at.Add(6 * time.Hour) }
p := pluginCheckinFails(id, sixHours, &now, &calls)
c := p.checkiner(func() time.Time { return now })
ask := func(press bool) WorkBuddyCheckin {
t.Helper()
got := c.checkinNow(context.Background(), press)
if len(got) != 1 || got[0].Outcome != CheckinFailed {
t.Fatalf("at %s: %+v", now, got)
}
return got[0]
}
ask(false)
for _, at := range []time.Duration{31 * time.Minute, 2 * time.Hour, 3 * time.Hour, 6*time.Hour - time.Minute} {
now = start.Add(at)
if r := ask(false); r.Asked || calls != 1 {
t.Fatalf("loop at +%v: asked again (%d asks), until %v", at, calls, r.ValidUntil)
}
}
now = start.Add(6 * time.Hour)
if r := ask(false); !r.Asked || calls != 2 {
t.Fatalf("loop at the until: not asked (%d asks)", calls)
}
// a press asks while held
now = start.Add(6*time.Hour + 31*time.Minute)
if r := ask(true); !r.Asked || calls != 3 {
t.Fatalf("press while held: not asked (%d asks)", calls)
}
// the loop's wake time is the until, not 30 minutes on
var l wbCheckinLoop
now = start.AddDate(0, 0, 2)
calls = 0
var asked []time.Duration
for m := time.Duration(0); m <= 7*time.Hour; m += time.Minute {
now = start.AddDate(0, 0, 2).Add(m)
for _, r := range l.tick(context.Background(), c) {
if r.Asked {
asked = append(asked, m)
}
}
if m == 0 && !l.again.Equal(sixHours(now)) {
t.Fatalf("the loop wakes at %v, want the until %v", l.again, sixHours(now))
}
}
if want := []time.Duration{0, 6 * time.Hour}; !slices.Equal(asked, want) {
t.Fatalf("the loop asked at %v, want %v", asked, want)
}
})
}
}
// An until a day or more away holds a failure 24 hours, no longer; a
// failure with no until, or one sooner than its retry, is asked as before.
func TestPluginCheckinFailedUntilBounded(t *testing.T) {
for _, tc := range []struct {
name string
until func(time.Time) time.Time
quiet []time.Duration // not asked again then
again time.Duration // asked again then
}{
{"a far until", func(at time.Time) time.Time { return at.AddDate(1, 0, 0) }, []time.Duration{31 * time.Minute, 23*time.Hour + 59*time.Minute}, 24 * time.Hour},
{"no until", nil, []time.Duration{29 * time.Minute}, 30 * time.Minute},
{"a sooner until", func(at time.Time) time.Time { return at.Add(2 * time.Minute) }, []time.Duration{time.Minute}, 2 * time.Minute},
} {
t.Run(tc.name, func(t *testing.T) {
claudeHome(t)
start := time.Date(2026, 11, 5, 6, 0, 0, 0, time.UTC) // 14:00 Beijing
now, calls := start, 0
c := pluginCheckinFails("zcode", tc.until, &now, &calls).checkiner(func() time.Time { return now })
c.checkinNow(context.Background(), false)
for _, at := range tc.quiet {
now = start.Add(at)
if c.checkinNow(context.Background(), false); calls != 1 {
t.Fatalf("at +%v: asked again", at)
}
}
now = start.Add(tc.again)
if got := c.checkinNow(context.Background(), false); calls != 2 || len(got) != 1 || !got[0].Asked {
t.Fatalf("at +%v: not asked again (%d asks)", tc.again, calls)
}
})
}
}