Files
magpie/internal/provider/plugin_checkin_test.go
T
yetone 45fa29973c checkin: an early "already checked in" is asked again, not kept for the day (#3326)
connoryang331 (v0.1.1328, Windows): at 00:00:28 Beijing 14 of 29 plugin
check-ins (AutoClaw CN x5, WorkBuddy x2, CodeArts, Trae x3, MiniMax,
Raccoon x2) answered "already" (done). magpie kept that as the day's
result and never asked again, and Check in now answered from the record
without asking. A real claim at 10:54 paid +200: the vendors' days turn
later than Beijing midnight, so 00:00's "already" was yesterday's.

The class, in the checkiner every check-in shares (built-in and plugin):
- A done, inactive or ineligible answered before noon Beijing
  (checkinSureHour) for an account magpie hasn't seen claim that Beijing
  day is provisional. The loop asks it again 30 minutes (wbCheckinEvery)
  after the answer, until a claim or an answer from noon on: at most 24
  more asks a day per such account, none once magpie claims.
- A press (soon) asks again at once for done, inactive and ineligible,
  whatever the hour. own-app (Trae 9074) and captcha still stand.
- A claim stamps Claimed, carried over while later answers that day say
  done, so a done after magpie's own claim is settled (Qoder's claim then
  done case).
- The record goes out with again: true (worked out when shown, never
  kept). The card says "{name} says already checked in; its day may not
  have turned yet, so magpie asks again later" and keeps Check in now;
  Settings, TUI and CLI say the same. en, zh, zh-TW, ja, de.
- Qoder's recheck (#2696, pluginRechecks) is unchanged: its tests pass
  as they were.

The plugins repo does not cache "already": autoclaw reads the task list
or calls dailySignin each time, codearts, minimax, raccoon and trae ask
the server each call (read at magpie-community/plugins origin/main
652f06d). No plugin change, publish or mover min.

Tests:
- TestPluginCheckinEarlyDoneAskedAgain (the reporter's 00:00:28: loop at
  +29m no ask, +30m asks then claims; a press at 10:54 claims; done all
  day is 25 asks and settles at noon; done after magpie's claim settled)
  and TestWorkBuddyCheckinEarlyDoneAskedAgain.
- TestPluginCheckinOthersSettledForTheDay now answers at 12:00 Beijing
  and checks a press asks again (claimed still settled);
  TestPluginCheckinRunsAsTheBuiltinsDo, TestTraeCheckinAnswers and the
  WorkBuddy nothing-to-claim/refused tests follow the new rule.
- TestCheckinAgainNotShownAsToday (TUI words and cell).
- checkin-early-done.test.cjs: again card's wording, data-state wait,
  Check in now asks plugin-checkin {provider: autoclaw-cn}; a kept done
  is "Checked in today +50" with no press. Chromium and WebKit, 5
  languages, 900 and 440px, 20/20.

Red on the old code: with the non-test change reverse-applied (fields
kept so it compiles), TestPluginCheckinRunsAsTheBuiltinsDo,
TestPluginCheckinOthersSettledForTheDay (inactive, done, ineligible) and
all 4 subtests of TestPluginCheckinEarlyDoneAskedAgain fail; with
origin/main's app.js, checkin-early-done fails ("Checked in today").

Run at this tree:
- gofmt -l clean; go vet ./... (darwin), GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./... clean.
- go test -tags nogui ./internal/provider/ -run 'Checkin|CheckIn',
  ./internal/tui/ -run 'Checkin|OwnApp': ok.
- GUI: checkin-account, checkin-early-done, i18n-keys, plugin-checkin,
  qoder-checkin-recheck, usage-card-refresh, usage-checkin: 177/179. The
  2 reds are usage-checkin's "webkit en/zh: Settings has a Qoder check-in
  tab", red the same alone on origin/main 0815c9186 (filed #2549, #3091).
- Full suite: set -o pipefail; go test -tags nogui -timeout 45m ./...
  at c1a72f2b4 (this tree on origin/main 0815c9186), sandbox HOME: 69
  packages ok, exit 0.

Not tried: real vendor accounts (no early-morning "already" to replay),
Linux and Windows at run time. A vendor whose day turns after noon
Beijing (a US day) is not covered.
2026-10-23 19:03:58 +08:00

661 lines
25 KiB
Go

package provider
import (
"context"
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/testenv"
)
// pluginCheckinSandbox is the fake plugin with its own check-in, signed in
// with each key; it says the file each press is written to.
func pluginCheckinSandbox(t *testing.T, keys ...string) string {
t.Helper()
bun := testenv.Bun(t)
home := claudeHome(t)
t.Setenv("MAGPIE_BUN", bun)
presses := filepath.Join(home, "presses")
t.Setenv("FAKE_CHECKIN", presses)
t.Cleanup(plugin.Settle)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
if _, err := plugin.Add(ctx, abs); err != nil {
t.Fatal(err)
}
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
for _, k := range keys {
if _, err := PluginAPIKey(ctx, "fakeco", 0, nil, k); err != nil {
t.Fatal(err)
}
}
return presses
}
// pressed is the keys the plugin's check-in was pressed for, in order.
func pressed(t *testing.T, file string) []string {
t.Helper()
b, err := os.ReadFile(file)
if os.IsNotExist(err) {
return nil
}
if err != nil {
t.Fatal(err)
}
return strings.Fields(string(b))
}
// A plugin's own check-in (auth.checkin; Lemon on Discord) runs as magpie's
// own do: each account once a Beijing day while it is on, a failure asked
// again after wbCheckinRetry, a captcha left for the user (never retried
// that day), and what came of each kept, on its card and in Settings.
func TestPluginCheckinRunsAsTheBuiltinsDo(t *testing.T) {
keys := []string{"ck-claim", "ck-done", "ck-captcha", "ck-throw", "ck-odd"}
presses := pluginCheckinSandbox(t, keys...)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
ps := pluginCheckinProviders()
if len(ps) != 1 || ps[0].ID != "fakeco" || ps[0].Name != "FakeCo" || len(ps[0].accounts) != len(keys) {
t.Fatalf("the plugins that check in: %+v", ps)
}
if !HasPluginCheckin() {
t.Fatal("HasPluginCheckin is false")
}
now := time.Date(2026, 10, 6, 10, 0, 0, 0, beijing)
loop := pluginCheckiner{now: func() time.Time { return now }, only: func(id string) bool { return PluginCheckinOn(settings.Load(), id) }}
// off (as it is by default): nothing pressed
if rs := loop.checkinNow(ctx, false); len(rs) != 0 || len(pressed(t, presses)) != 0 {
t.Fatalf("while off: %+v, pressed %q", rs, pressed(t, presses))
}
s := settings.Load()
s.PluginCheckins = map[string]bool{"fakeco": true}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
rs := loop.checkinNow(ctx, false)
if len(rs) != len(keys) {
t.Fatalf("on: %d answers, want %d: %+v", len(rs), len(keys), rs)
}
// by what each answered: one account per key
got := map[string]WorkBuddyCheckin{}
for _, r := range rs {
if r.By != "plugin:fakeco" || r.Vendor != "FakeCo" || !r.Asked || r.Day != "2026-10-06" {
t.Fatalf("an answer: %+v", r)
}
k := r.Outcome
if k == CheckinFailed {
k += ":" + r.Msg
}
got[k] = r
}
if r := got[CheckinClaimed]; r.Credit != 50 || r.Streak != 3 {
t.Fatalf("ck-claim: %+v (%+v)", r, rs)
}
if r := got[CheckinDone]; r.Credit != 50 {
t.Fatalf("ck-done: %+v (%+v)", r, rs)
}
if r := got[CheckinCaptcha]; r.Msg != "slide the puzzle" {
t.Fatalf("ck-captcha: %+v (%+v)", r, rs)
}
failed := 0
for k := range got {
if strings.Contains(k, "check-in is down") || strings.Contains(k, "no outcome") {
failed++
}
}
if failed != 2 {
t.Fatalf("the failures (ck-throw, ck-odd): %+v", rs)
}
if p := pressed(t, presses); len(p) != len(keys) {
t.Fatalf("pressed %q", p)
}
// a minute on: nothing asked again, each answered as kept
now = now.Add(time.Minute)
if rs := loop.checkinNow(ctx, false); len(rs) != len(keys) || len(pressed(t, presses)) != len(keys) {
t.Fatalf("a minute on: %+v, pressed %q", rs, pressed(t, presses))
}
// past wbCheckinRetry: the failures, and the done given before noon
// (#3326), not the captcha
now = now.Add(wbCheckinRetry)
loop.checkinNow(ctx, false)
if p := pressed(t, presses)[len(keys):]; strings.Join(slices.Sorted(slices.Values(p)), ",") != "ck-done,ck-odd,ck-throw" {
t.Fatalf("past the retry, pressed again %q", p)
}
// the next Beijing day: each again
now = time.Date(2026, 10, 7, 0, 5, 0, 0, beijing)
loop.checkinNow(ctx, false)
if p := pressed(t, presses); len(p) != 2*len(keys)+3 {
t.Fatalf("the next day, pressed %q", p)
}
// kept, for Settings
pcs := PluginCheckins()
if len(pcs) != 1 || pcs[0].ID != "fakeco" || !pcs[0].On || len(pcs[0].Checkins) != len(keys) {
t.Fatalf("PluginCheckins = %+v", pcs)
}
// and on each account's card
var qs []SubscriptionQuota
for _, a := range ps[0].accounts {
qs = append(qs, SubscriptionQuota{Provider: PluginID("fakeco"), User: a.User})
}
marked := WithCheckins(qs)
for i, q := range marked {
if !q.Checkins || q.CheckinBy != "plugin:fakeco" || q.Checkin == nil || q.Checkin.Day != "2026-10-07" {
t.Fatalf("card %d: %+v %+v", i, q, q.Checkin)
}
}
if qs[0].Checkins {
t.Fatal("WithCheckins changed the cards it was given")
}
// a press now (Check in now, the CLI) asks the failure again at once,
// whatever the switch; another provider's press asks none of these
before := len(pressed(t, presses))
if rs := CheckInPlugins(ctx, "someone-else"); len(rs) != 0 || len(pressed(t, presses)) != before {
t.Fatalf("another provider's press: %+v", rs)
}
}
// A plugin that checks in itself takes over from magpie's own check-in
// through its fetch, so an account isn't checked in twice; and its switch
// follows the vendor's, which it took over, until set on its own.
func TestPluginCheckinTakesOverTheVendors(t *testing.T) {
bun := testenv.Bun(t)
claudeHome(t)
t.Setenv("MAGPIE_BUN", bun)
t.Setenv("FAKE_ID", "qoder")
t.Cleanup(plugin.Settle)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
if _, err := plugin.Add(ctx, abs); err != nil {
t.Fatal(err)
}
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
if _, err := plugin.APIKey(ctx, "qoder", 0, nil, "ck-claim", plugin.NewAccount); err != nil {
t.Fatal(err)
}
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
// a plugin without auth.checkin: magpie claims Qoder's credits itself
if n := len(qoderCheckinAccounts()); n != 1 || HasPluginCheckin() {
t.Fatalf("without the hook: %d Qoder accounts, plugins that check in %v", n, HasPluginCheckin())
}
// with it, the plugin does, and magpie's own lists none
t.Setenv("FAKE_CHECKIN", filepath.Join(t.TempDir(), "presses"))
plugin.Restart()
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
if n := len(qoderCheckinAccounts()); n != 0 || !HasPluginCheckin() {
t.Fatalf("with the hook: %d Qoder accounts, plugins that check in %v", n, HasPluginCheckin())
}
for _, c := range []struct {
s settings.Settings
id string
want bool
}{
{settings.Settings{QoderCheckin: true}, "qoder", true},
{settings.Settings{QoderCheckin: true}, QoderCNID, true},
{settings.Settings{QoderCheckin: true, PluginCheckins: map[string]bool{"qoder": false}}, "qoder", false},
{settings.Settings{WorkBuddyCheckin: true}, "workbuddy", true},
{settings.Settings{TraeCheckin: true}, TraeCNID, true},
{settings.Settings{MiniMaxCheckin: true}, MiniMaxCodeGlobalID, true},
{settings.Settings{QoderCheckin: true}, "fakeco", false},
{settings.Settings{PluginCheckins: map[string]bool{"fakeco": true}}, "fakeco", true},
} {
if got := PluginCheckinOn(c.s, c.id); got != c.want {
t.Errorf("PluginCheckinOn(%+v, %s) = %v, want %v", c.s, c.id, got, c.want)
}
}
}
// pluginCheckinTeams is the fake plugin with its own check-in, signed in
// through its browser sign-in as each team (name/uid): the reporter's case
// in plugins#254 is two AutoClaw display names with two sign-ins each,
// told apart only by their uid. It says the file each press is written to,
// as name@fake/uid, and each team's sign-in key.
func pluginCheckinTeams(t *testing.T, teams ...string) (string, map[string]string) {
t.Helper()
presses := pluginCheckinSandbox(t)
for _, team := range teams {
st, err := StartPluginSignIn("fakeco", 1, map[string]string{"where": "work", "team": team})
if err != nil {
t.Fatal(err)
}
if err := SubmitSignInCallback(st.ID, "good"); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if _, err := plugin.Providers(ctx); err != nil {
t.Fatal(err)
}
keys := map[string]string{} // name@fake/uid → its sign-in's key
for k, a := range plugin.Auths("fakeco") {
id, _ := a["accountId"].(string)
uid, _ := a["uid"].(string)
keys[id+"/"+uid] = k
}
if len(keys) != len(teams) {
t.Fatalf("signed in as %v, want %d sign-ins", keys, len(teams))
}
return presses, keys
}
// pluginCheckinOn turns fakeco's daily check-in on.
func pluginCheckinOn(t *testing.T) {
t.Helper()
s := settings.Load()
s.PluginCheckins = map[string]bool{"fakeco": true}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
}
// pluginCards are a card for each of p's accounts, as the Usage page has.
func pluginCards(p pluginCheckinProvider) []SubscriptionQuota {
var qs []SubscriptionQuota
for _, a := range p.accounts {
qs = append(qs, SubscriptionQuota{Provider: PluginID(p.ID), User: a.User})
}
return qs
}
// Two sign-ins of one plugin that share a display name are two accounts
// to check in (plugins#254: four AutoClaw sign-ins under two default
// names, which AutoClaw doesn't let the user change, and only the first of
// each name was ever checked in, by the loop or Check in now). Each is
// checked in once a day, kept, carded and switched off on its own.
func TestPluginCheckinTellsSignInsOfOneNameApart(t *testing.T) {
presses, keys := pluginCheckinTeams(t, "A/u1", "A/u2", "B/u3", "B/u4")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
pluginCheckinOn(t)
ps := pluginCheckinProviders()
if len(ps) != 1 || len(ps[0].accounts) != 4 {
t.Fatalf("the plugins that check in: %+v", ps)
}
now := time.Date(2026, 10, 6, 10, 0, 0, 0, beijing)
loop := pluginCheckiner{now: func() time.Time { return now }, only: func(id string) bool { return PluginCheckinOn(settings.Load(), id) }}
all := "A@fake/u1,A@fake/u2,B@fake/u3,B@fake/u4"
sorted := func(p []string) string { return strings.Join(slices.Sorted(slices.Values(p)), ",") }
if rs := loop.checkinNow(ctx, false); len(rs) != 4 || sorted(pressed(t, presses)) != all {
t.Fatalf("the loop: %+v, pressed %q", rs, pressed(t, presses))
}
// kept for each: nothing asked again that day
now = now.Add(time.Minute)
if rs := loop.checkinNow(ctx, false); len(rs) != 4 || len(pressed(t, presses)) != 4 {
t.Fatalf("a minute on: %+v, pressed %q", rs, pressed(t, presses))
}
st := shownCheckins(pluginCheckinPath())
for who, k := range keys {
if r, ok := st[pluginCheckinKey("fakeco", k)]; !ok || r.Day != "2026-10-06" || r.Outcome != CheckinInactive {
t.Errorf("%s (%s) kept as %+v, ok %v; file %+v", who, k, r, ok, st)
}
}
if len(st) != 4 {
t.Errorf("plugin-checkin.json holds %d entries, want 4: %+v", len(st), st)
}
// Settings lists each, and each card says today's
if pcs := PluginCheckins(); len(pcs) != 1 || len(pcs[0].Checkins) != 4 {
t.Fatalf("PluginCheckins = %+v", pcs)
}
for i, q := range WithCheckins(pluginCards(ps[0])) {
if !q.Checkins || q.Checkin == nil || q.Checkin.Day != "2026-10-06" {
t.Fatalf("card %d (%s): %+v", i, q.User, q.Checkin)
}
}
// each its own row in Settings, and one switched off is left out
var rows []string
for _, a := range CheckinAccounts() {
if a.By == "plugin:fakeco" {
rows = append(rows, a.Key)
}
}
if len(rows) != 4 {
t.Fatalf("Settings' rows: %q", rows)
}
s := settings.Load()
s.CheckinOff = []string{pluginCheckinKey("fakeco", keys["A@fake/u2"])}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
// Check in now, the next day: each but the one switched off
now = time.Date(2026, 10, 7, 9, 0, 0, 0, beijing)
press := pluginCheckiner{now: func() time.Time { return now }, only: func(id string) bool { return id == "fakeco" }}
press.checkinNow(ctx, true)
if p := sorted(pressed(t, presses)[4:]); p != "A@fake/u1,B@fake/u3,B@fake/u4" {
t.Fatalf("Check in now with A@fake/u2 off pressed %q", p)
}
for _, q := range WithCheckins(pluginCards(ps[0])) {
if q.CheckinOff != (q.User == userOf(ps[0], keys["A@fake/u2"])) {
t.Fatalf("card %s: off %v", q.User, q.CheckinOff)
}
}
}
// userOf is the name the account signed in under key is shown by.
func userOf(p pluginCheckinProvider, key string) string {
for _, a := range p.accounts {
if a.key == pluginCheckinKey(p.ID, key) {
return a.User
}
}
return ""
}
// An upgrade from a plugin-checkin.json kept by display name (before
// plugins#254) checks no account in twice and keeps the user's own Off: a
// name checked in today is carried to the one sign-in the old key asked,
// the first in use, so only its namesake is asked; a name switched off
// stays off for each sign-in of it; another vendor's Off is left alone.
func TestPluginCheckinCarriesOverDisplayNameEntries(t *testing.T) {
presses, keys := pluginCheckinTeams(t, "A/u1", "A/u2", "B/u3", "B/u4")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
now := time.Date(2026, 10, 6, 10, 0, 0, 0, beijing)
legacy := map[string]WorkBuddyCheckin{
"fakeco|A@fake": {Day: "2026-10-06", At: now.Add(-time.Hour), Outcome: CheckinClaimed, Credit: 400},
"fakeco|B@fake": {Day: "2026-10-06", At: now.Add(-time.Hour), Outcome: CheckinClaimed, Credit: 400},
// another plugin's, whose accounts aren't signed in: left as kept
"other|C": {Day: "2026-10-05", At: now.Add(-25 * time.Hour), Outcome: CheckinDone},
}
b, _ := json.Marshal(legacy)
if err := os.WriteFile(pluginCheckinPath(), b, 0o600); err != nil {
t.Fatal(err)
}
s := settings.Load()
s.PluginCheckins = map[string]bool{"fakeco": true}
s.CheckinOff = []string{"workbuddy|123", "fakeco|B@fake"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
ps := pluginCheckinProviders()
if len(ps) != 1 || len(ps[0].accounts) != 4 {
t.Fatalf("the plugins that check in: %+v", ps)
}
// the sign-in of A the old key asked: the first of A's in use
var heir, namesake string
for _, a := range ps[0].accounts {
if a.legacy == "fakeco|A@fake" && a.On {
if heir == "" {
heir = a.key
} else if namesake == "" {
namesake = a.key
}
}
}
if heir == "" || namesake == "" {
t.Fatalf("A's sign-ins: %+v", ps[0].accounts)
}
st := shownCheckins(pluginCheckinPath())
if r, ok := st[heir]; !ok || r.Outcome != CheckinClaimed || r.Credit != 400 || r.Day != "2026-10-06" {
t.Fatalf("A's check-in went to %s as %+v (ok %v): %+v", heir, r, ok, st)
}
if _, ok := st[namesake]; ok {
t.Fatalf("A's check-in was carried to both its sign-ins: %+v", st)
}
if _, ok := st["fakeco|A@fake"]; ok {
t.Fatalf("A's old entry was kept: %+v", st)
}
if _, ok := st["other|C"]; !ok {
t.Fatalf("another plugin's entry went: %+v", st)
}
off := settings.Load().CheckinOff
want := []string{"workbuddy|123", pluginCheckinKey("fakeco", keys["B@fake/u3"]), pluginCheckinKey("fakeco", keys["B@fake/u4"])}
if !slices.Equal(slices.Sorted(slices.Values(off)), slices.Sorted(slices.Values(want))) {
t.Fatalf("CheckinOff = %q, want %q", off, want)
}
// the loop asks only A's namesake: the heir is in, B's are off
loop := pluginCheckiner{now: func() time.Time { return now }, only: func(id string) bool { return PluginCheckinOn(settings.Load(), id) }}
loop.checkinNow(ctx, false)
var who string
for w, k := range keys {
if pluginCheckinKey("fakeco", k) == namesake {
who = w
}
}
if p := pressed(t, presses); len(p) != 1 || p[0] != who {
t.Fatalf("after the upgrade pressed %q, want only %s", p, who)
}
// and nothing is carried again, or asked again, on the next look
pluginCheckinProviders()
loop.checkinNow(ctx, false)
if p := pressed(t, presses); len(p) != 1 {
t.Fatalf("a second look pressed %q", p)
}
for _, a := range CheckinAccounts() {
if a.By == "plugin:fakeco" && a.On == strings.HasPrefix(a.User, "B@fake") {
t.Fatalf("Settings' row %+v: B's should be off, A's on", a)
}
}
}
// A plugin-checkin.json that doesn't parse is unknown: nothing is carried
// over or written over, and it is carried once it reads.
func TestPluginCheckinCarriesNothingOverAnUnreadableFile(t *testing.T) {
path := filepath.Join(claudeHome(t), "plugin-checkin.json")
order, heirs := []string{"fakeco|A"}, map[string][]string{"fakeco|A": {"fakeco|fakeco", "fakeco|fakeco#x1"}}
bad := []byte(`{"fakeco|A": {"day": "2026-10-06",`)
if err := os.WriteFile(path, bad, 0o600); err != nil {
t.Fatal(err)
}
if adoptLegacyCheckinFile(path, order, heirs) {
t.Fatal("a file that doesn't parse was taken as carried over")
}
if b, _ := os.ReadFile(path); string(b) != string(bad) {
t.Fatalf("the file was written over: %s", b)
}
if err := os.WriteFile(path, []byte(`{"fakeco|A": {"day": "2026-10-06", "outcome": "claimed"}}`), 0o600); err != nil {
t.Fatal(err)
}
if !adoptLegacyCheckinFile(path, order, heirs) {
t.Fatal("not carried once the file reads")
}
st := shownCheckins(path)
if _, ok := st["fakeco|fakeco"]; !ok || len(st) != 1 {
t.Fatalf("carried as %+v", st)
}
}
// pluginCheckinCounter is a plugin provider with one account whose
// check-in answers what *outcome holds and counts the asks in *calls.
func pluginCheckinCounter(id string, outcome *string, calls *int) pluginCheckinProvider {
return pluginCheckinProvider{ID: id, accounts: []pluginCheckinAcct{{User: "ann", On: true, key: id + "|ann", card: id, do: func(context.Context) WorkBuddyCheckin {
*calls++
return WorkBuddyCheckin{Outcome: *outcome}
}}}}
}
// #2696 (LoKi, mufanmu): the Qoder plugins' check-in is rechecked as
// magpie's own Qoder check-in is. A campaign can open mid-day, so a done or
// inactive read before it was kept for the whole Beijing day, even on a
// press. Now the loop asks again 30 minutes after the last answer, not
// before, and a press asks at once.
func TestPluginCheckinQoderRechecksCampaigns(t *testing.T) {
for _, id := range []string{"qoder", QoderCNID} {
for _, first := range []string{CheckinInactive, CheckinDone} {
// each: wait this long, press or not, then the outcome shown
// and the asks so far
type step struct {
wait time.Duration
press bool
want string
wantCalls int
}
for name, steps := range map[string][]step{
"loop": {
{0, false, first, 1},
{29 * time.Minute, false, first, 1},
{time.Minute, false, CheckinClaimed, 2},
{29 * time.Minute, false, CheckinClaimed, 2},
{time.Minute, false, CheckinClaimed, 3}, // a claim is rechecked too
},
"press": {
{0, false, first, 1},
{10 * time.Minute, true, CheckinClaimed, 2},
{0, false, CheckinClaimed, 2}, // the loop keeps what the press read
{0, true, CheckinClaimed, 3},
},
} {
t.Run(id+"/"+first+"/"+name, func(t *testing.T) {
claudeHome(t)
now := time.Date(2026, 10, 18, 1, 0, 0, 0, time.UTC) // 09:00 Beijing
outcome, calls := first, 0
p := pluginCheckinCounter(id, &outcome, &calls)
for i, s := range steps {
now = now.Add(s.wait)
got := p.checkiner(func() time.Time { return now }).checkinNow(context.Background(), s.press)
if len(got) != 1 || got[0].Outcome != s.want || calls != s.wantCalls {
t.Fatalf("step %d (press=%v at %s): got %+v after %d asks; want %s after %d", i, s.press, now.Format(time.TimeOnly), got, calls, s.want, s.wantCalls)
}
outcome = CheckinClaimed
}
})
}
}
}
}
// Every other plugin's check-in is settled for the Beijing day by a claim,
// or by a done, inactive or ineligible answered from noon Beijing on: the
// loop doesn't ask again after 30 minutes (#2696 changes Qoder's alone).
// A press asks again for any of those but a claim: the record is not the
// vendor's answer now (#3326).
func TestPluginCheckinOthersSettledForTheDay(t *testing.T) {
for _, first := range []string{CheckinInactive, CheckinDone, CheckinIneligible, CheckinClaimed} {
t.Run(first, func(t *testing.T) {
claudeHome(t)
now := time.Date(2026, 10, 18, 4, 0, 0, 0, time.UTC) // 12:00 Beijing
outcome, calls := first, 0
p := pluginCheckinCounter("fakeco", &outcome, &calls)
p.checkiner(func() time.Time { return now }).checkinNow(context.Background(), false)
outcome = CheckinClaimed
for _, step := range []time.Duration{31 * time.Minute, 5 * time.Hour} {
now = now.Add(step)
got := p.checkiner(func() time.Time { return now }).checkinNow(context.Background(), false)
if len(got) != 1 || got[0].Outcome != first || got[0].Again || calls != 1 {
t.Fatalf("loop at %s: got %+v after %d asks; want %s kept after 1", now, got, calls, first)
}
}
got := p.checkiner(func() time.Time { return now }).checkinNow(context.Background(), true)
want, wantCalls := CheckinClaimed, 2
if first == CheckinClaimed {
wantCalls = 1
}
if len(got) != 1 || got[0].Outcome != want || calls != wantCalls {
t.Fatalf("press at %s: got %+v after %d asks; want %s after %d", now, got, calls, want, wantCalls)
}
})
}
}
// #3326 (connoryang331): at 00:00:28 Beijing AutoClaw CN, WorkBuddy,
// CodeArts, Trae CN, MiniMax and Raccoon answered "already in" (done) for
// accounts whose day's credits were still to claim; magpie kept it as the
// day's, the loop never asked again, and a press answered from the record.
// An answer magpie saw no claim for, given before noon Beijing, is asked
// again every 30 minutes; a press asks at once.
func TestPluginCheckinEarlyDoneAskedAgain(t *testing.T) {
early := time.Date(2026, 10, 23, 0, 0, 28, 0, beijing)
run := func(p pluginCheckinProvider, now time.Time, press bool) WorkBuddyCheckin {
t.Helper()
got := p.checkiner(func() time.Time { return now }).checkinNow(context.Background(), press)
if len(got) != 1 {
t.Fatalf("at %s: %+v", now, got)
}
return got[0]
}
t.Run("loop", func(t *testing.T) {
claudeHome(t)
outcome, calls := CheckinDone, 0
p := pluginCheckinCounter("autoclaw-cn", &outcome, &calls)
now := early
if r := run(p, now, false); r.Outcome != CheckinDone || !r.Again || calls != 1 {
t.Fatalf("at midnight: %+v after %d asks", r, calls)
}
// shown so on the card too, and in Settings
if r := shownCheckins(pluginCheckinPath())["autoclaw-cn|ann"]; !r.Again {
t.Fatalf("kept: %+v", r)
}
if r := run(p, now.Add(29*time.Minute), false); r.Outcome != CheckinDone || !r.Again || calls != 1 {
t.Fatalf("29 minutes on: %+v after %d asks", r, calls)
}
now = now.Add(wbCheckinEvery)
if r := run(p, now, false); r.Outcome != CheckinDone || !r.Again || calls != 2 {
t.Fatalf("30 minutes on: %+v after %d asks", r, calls)
}
// the vendor's day turns
outcome = CheckinClaimed
now = now.Add(wbCheckinEvery)
if r := run(p, now, false); r.Outcome != CheckinClaimed || r.Again || calls != 3 {
t.Fatalf("after the vendor's turn: %+v after %d asks", r, calls)
}
// a claim settles the day, for the loop and a press
for _, step := range []time.Duration{wbCheckinEvery, 5 * time.Hour} {
now = now.Add(step)
for _, press := range []bool{false, true} {
if r := run(p, now, press); r.Outcome != CheckinClaimed || calls != 3 {
t.Fatalf("press=%v at %s: %+v after %d asks", press, now, r, calls)
}
}
}
})
t.Run("press", func(t *testing.T) {
claudeHome(t)
outcome, calls := CheckinDone, 0
p := pluginCheckinCounter("autoclaw-cn", &outcome, &calls)
run(p, early, false)
outcome = CheckinClaimed
if r := run(p, time.Date(2026, 10, 23, 10, 54, 0, 0, beijing), true); r.Outcome != CheckinClaimed || !r.Asked || calls != 2 {
t.Fatalf("pressed at 10:54: %+v after %d asks", r, calls)
}
})
t.Run("done all day", func(t *testing.T) {
claudeHome(t)
outcome, calls := CheckinDone, 0
p := pluginCheckinCounter("autoclaw-cn", &outcome, &calls)
var last WorkBuddyCheckin
for now := early; wbCheckinDay(now) == "2026-10-23"; now = now.Add(wbCheckinEvery) {
last = run(p, now, false)
}
// 00:00:28 to 11:30:28 provisional, 12:00:28 the day's
if calls != 25 || last.Again || last.Outcome != CheckinDone {
t.Fatalf("%d asks, last %+v", calls, last)
}
})
t.Run("done after magpie's claim", func(t *testing.T) {
claudeHome(t)
outcome, calls := CheckinClaimed, 0
// Qoder's is rechecked after a claim, and then answers done
p := pluginCheckinCounter("qoder", &outcome, &calls)
run(p, early, false)
outcome = CheckinDone
if r := run(p, early.Add(wbCheckinEvery), false); r.Outcome != CheckinDone || r.Again || r.Claimed.IsZero() || calls != 2 {
t.Fatalf("done after the claim: %+v after %d asks", r, calls)
}
})
}