Files
magpie/internal/provider/plugin_checkin_test.go
T
yetoneandLoKi 21f7f9df23 checkin: the Qoder plugins' check-in is asked again within the day, as the built-in's was (#2696)
The native Qoder and Qoder CN plugin check-in (pluginCheckinProvider) built
its checkiner without recheck, while the built-in qoderCheckiner set it.
So an inactive or done result was kept for the whole Beijing day, and a
press of "Check in now" was answered from that record too. Qoder CN's
campaign runs 10:00 to 09:59 the next day (mufanmu), so an early read
describes yesterday's campaign and said nothing about today's.

What changed:
- provider: pluginRechecks(id) is true for "qoder" and "qoder-cn" only,
  and sets recheck on their checkiner. A press asks at once. The loop asks
  again after wbCheckinEvery (30m) for inactive and done. Every other
  plugin stays settled for the day.
- GUI (checkinRow): a Qoder account (built-in or plugin) whose check-in
  found no campaign today keeps "Check in now". A press reports "Check-in
  status refreshed" instead of "Checked in", since it may still find
  none. The unused "Checked in" key is replaced in all four languages.
- docs/subsystems/provider-plugins.md names the exception and the tests.

Taken from LoKi's (lokic7123-star) patch on the issue: the recheck on the
Qoder plugin ids, the Check in now button for an inactive Qoder card, and
the neutral press status. Their GUI test was adapted into
qoder-checkin-recheck.test.cjs.
Left out, as the earlier review asked: the blanket 30-minute message, the
UTC timestamps and the client-computed next check. Also left out: the
"Already checked in today" wording and the "· credit" display. That is
not this bug, and the TUI and CLI would need the same change.

Run:
- Red on the old code:
  - All 8 subtests of TestPluginCheckinQoderRechecksCampaigns
    ({qoder,qoder-cn} x {inactive,done} x {loop,press}) fail without
    the fix.
  - Forcing recheck on for every plugin turns
    TestPluginCheckinOthersSettledForTheDay red.
  - qoder-checkin-recheck.test.cjs is red on the origin/main assets (no
    .ci-now on the card).
- At 91bb01e65 (base 9572a3576):
  - Full suite: set -o pipefail; go test -timeout 45m ./... |
    tee. EXIT=0, 68 packages ok, run alone on the machine.
  - qoder-checkin-recheck.test.cjs passes 20/20: chromium and webkit,
    en/zh/zh-TW/ja/de, 900 and 440px.
  - 76 GUI files reachable from the change were run: check-in files,
    gui-*, i18n-keys and tray-usages. Reds:
    - login-import, chromium en/zh: red on main too (#1307).
    - quota-reads webkit de 360: red on main too (#2150, commented
      with its c.toFixed page error).
    - usage-checkin webkit en/zh, Qoder tab: red on main too (#2549).
    - update-wait chromium en and usage-alert webkit en: red only
      under load, pass alone (#3275).
- At this tree, rebased on 87d703c5c (b91bcf260, library only, came
  after). The 8 new main commits since
  9572a3576 bring app.js, i18n.js and provider plugin changes; none of
  them touches check-in code.
  - gofmt -l: clean.
  - go vet ./...: clean on darwin, linux (-tags nogui) and windows.
  - go test ./internal/provider/ ./internal/gui/ ./internal/plugin/
    ./internal/cli/: all ok. internal/gui includes TestCoreScriptBudget.
  - GUI, both engines: qoder-checkin-recheck, i18n-keys,
    plugin-checkin, checkin-account, account-models and
    plugin-keyless-signin pass 119/119.

Not tried:
- No real Qoder or Qoder CN account, and no campaign that opens
  mid-day.
- The full GUI suite: another session's full GUI run held the machine,
  so 76 files were run instead.
- Linux and Windows at runtime.

Reported by mufanmu and LoKi; patch by LoKi.

Co-authored-by: LoKi <317300729+lokic7123-star@users.noreply.github.com>
2026-10-22 21:18:13 +08:00

565 lines
20 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 only, 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-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)+2 {
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: neither
// the loop after 30 minutes nor a press asks again (#2696 changes Qoder's
// alone).
func TestPluginCheckinOthersSettledForTheDay(t *testing.T) {
for _, first := range []string{CheckinInactive, CheckinDone, CheckinClaimed} {
t.Run(first, func(t *testing.T) {
claudeHome(t)
now := time.Date(2026, 10, 18, 1, 0, 0, 0, time.UTC)
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)
for _, press := range []bool{false, true} {
got := p.checkiner(func() time.Time { return now }).checkinNow(context.Background(), press)
if len(got) != 1 || got[0].Outcome != first || calls != 1 {
t.Fatalf("press=%v at %s: got %+v after %d asks; want %s kept after 1", press, now, got, calls, first)
}
}
}
})
}
}