Files
magpie/internal/tui/checkin_test.go
T
yetone 87487c5ef0 plugin, provider: a plugin can press its vendor's daily check-in itself (auth.checkin), and magpie runs it as its own check-ins (Lemon on Discord #feedback: provider 每日签到的方法能放到插件里面去做吗; ARNO earlier asked for Qoder's) — the check-ins magpie had (WorkBuddy, Trae CN, MiniMax Code, Qoder) are each written in magpie against the vendor's endpoint, even for a subscription that moved to its plugin, so a plugin author had no way to give their provider one. A plugin's auth hook may now have checkin(getAuth, provider) beside usage: the host lists the provider with checkin: true (plugin.Provider.Checkin) and answers checkin({provider, account}) (plugin.AccountCheckin), refreshing the account as usage does, with {outcome, credit, streak, message}; outcome is claimed, done, ineligible, inactive, captcha or failed, and a throw or an outcome outside these is failed with the reason. internal/provider/plugin_checkin.go runs these through checkin.go's checkiner, as the built-ins are: each signed-in account in use once a Beijing day (first look 2 minutes after start, then every 30), a failure asked again after wbCheckinRetry, every other outcome settled for the day, results kept in plugin-checkin.json by provider|account; a failure is recorded as failed and resets nothing. captcha is new (CheckinCaptcha): the vendor wants a captcha, which magpie never solves, so it is said as "check in in its own app", not as a failure, and not asked again that day. The results mark the account's Usage card (checkinBy "plugin:<provider>", WithCheckins), so the card's check-in row, its Auto check-in and Check in now work for it (POST /api/settings/plugin-checkin {provider, on}, POST /api/usage/plugin-checkin {provider}); Settings → Usage gains a Plugins tab, shown while such a plugin is signed in, a Daily check-in row per provider with Off/On and how each account went; magpie accounts checkin and c on the TUI's Usage page include them (the TUI's "no account" line names check-in plugins). The switch (settings.PluginCheckins, by provider) is off by default; for WorkBuddy, Trae CN, MiniMax Code and Qoder it follows that vendor's existing switch until set on its own, and magpie's own check-in through the plugin's fetch leaves a provider whose plugin checks in to it (pluginChecksIn), so no account is checked in twice. The Settings page's save carries PluginCheckins over (TestSettingsSaveKeepsWhatItDoesNotSend's fixture names it). No plugin declares the hook yet; Qoder's check-in stays magpie's own here. TestPluginCheckinRunsAsTheBuiltinsDo (the fake plugin with FAKE_CHECKIN, five accounts answering claimed, done, captcha, a throw and an unknown outcome: off presses nothing; on, each outcome kept with its vendor; a minute on nothing pressed again; past the retry only the two failures; the next day all five; PluginCheckins and the cards marked; another provider's press asks none) and TestPluginCheckinTakesOverTheVendors (a qoder plugin without the hook leaves magpie's Qoder check-in its account, with it none; PluginCheckinOn's fallbacks) pass -race -count=10, and the latter fails with pluginChecksIn stubbed false; TestTUIChecksPluginsIn passes -race -count=20. plugin-checkin.test.cjs (the card's row with the captcha said, Auto check-in posting {provider, on}, no page move; the Settings Plugins tab at 440px with the row fitting, no sideways scroll, On posting) passes on Chromium and WebKit in en, zh, ja and de; usage-checkin, i18n-keys, gui-ja, gui-de and settings-* pass. go test -tags nogui ./internal/..., go vet, gofmt -l, linux (nogui) and windows builds pass. Not tried with a real plugin or account: no published plugin has the hook yet. docs/subsystems/provider-plugins.md (the hook's contract) and docs/reference.md say it.
2026-10-06 18:40:45 +08:00

207 lines
8.0 KiB
Go

package tui
import (
"context"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// stubCheckin stands in for the WorkBuddy accounts signed in here: rs is
// what checking them in comes to, none signed in when rs is nil.
func stubCheckin(t *testing.T, rs []provider.WorkBuddyCheckin) *int {
t.Helper()
stubTrae(t, nil)
stubQoder(t, nil)
stubPluginCheckin(t, nil)
oldHere, oldHas := checkinHere, hasWorkBuddy
t.Cleanup(func() { checkinHere, hasWorkBuddy = oldHere, oldHas })
calls := 0
hasWorkBuddy = func() bool { return rs != nil }
checkinHere = func(context.Context) []provider.WorkBuddyCheckin {
calls++
return rs
}
return &calls
}
// stubTrae stands in for the Trae CN accounts, as stubCheckin does
// WorkBuddy's.
func stubTrae(t *testing.T, rs []provider.WorkBuddyCheckin) *int {
t.Helper()
oldHere, oldHas := checkinTrae, hasTrae
t.Cleanup(func() { checkinTrae, hasTrae = oldHere, oldHas })
calls := 0
hasTrae = func() bool { return rs != nil }
checkinTrae = func(context.Context) []provider.WorkBuddyCheckin {
calls++
return rs
}
return &calls
}
func stubMiniMax(t *testing.T, rs []provider.WorkBuddyCheckin) *int {
t.Helper()
oldHere, oldHas := checkinMM, hasMiniMax
t.Cleanup(func() { checkinMM, hasMiniMax = oldHere, oldHas })
calls := 0
hasMiniMax = func() bool { return rs != nil }
checkinMM = func(context.Context) []provider.WorkBuddyCheckin {
calls++
return rs
}
return &calls
}
// stubPluginCheckin stands in for the accounts of plugins that check in
// themselves (auth.checkin), as stubCheckin does WorkBuddy's.
func stubPluginCheckin(t *testing.T, rs []provider.WorkBuddyCheckin) *int {
t.Helper()
oldHere, oldHas := checkinPl, hasPlugin
t.Cleanup(func() { checkinPl, hasPlugin = oldHere, oldHas })
calls := 0
hasPlugin = func() bool { return rs != nil }
checkinPl = func(context.Context) []provider.WorkBuddyCheckin {
calls++
return rs
}
return &calls
}
func stubQoder(t *testing.T, rs []provider.WorkBuddyCheckin) *int {
t.Helper()
oldHere, oldHas := checkinQd, hasQoder
t.Cleanup(func() { checkinQd, hasQoder = oldHere, oldHas })
calls := 0
hasQoder = func() bool { return rs != nil }
checkinQd = func(context.Context) []provider.WorkBuddyCheckin {
calls++
return rs
}
return &calls
}
// c on the Usage page presses WorkBuddy's daily check-in (签到) for every
// WorkBuddy (China) account signed in here at once, as the app's "Check in now" and magpie accounts
// checkin do, and says how each stands: the credits and streak, in already,
// or why not (akic404 on Discord: the TUI had no way to check in).
func TestTUIChecksWorkBuddyIn(t *testing.T) {
home(t)
usagePage := model{w: 200, h: 40, page: pageUsage}
// nothing signed in: said so, nothing asked
calls := stubCheckin(t, nil)
m := press(t, usagePage, "c")
wantFlash(t, m, false, "no WorkBuddy (China), Trae CN, MiniMax Code, Qoder or check-in plugin account is signed in")
if *calls != 0 {
t.Fatal("checked in with no account")
}
calls = stubCheckin(t, []provider.WorkBuddyCheckin{{User: "旅行者", Outcome: provider.CheckinClaimed, Credit: 100, Streak: 4, Asked: true}})
m = press(t, usagePage, "c")
wantFlash(t, m, true, "旅行者 checked in today +100 · a 4-day streak")
if strings.Contains(m.flash, "already") || *calls != 1 {
t.Fatalf("claimed now: %q, %d calls", m.flash, *calls)
}
if !strings.Contains(m.View(), "daily check-in") {
t.Fatalf("the footer doesn't name c:\n%s", m.View())
}
// one in already today, one the vendor refused: the error, in red
stubCheckin(t, []provider.WorkBuddyCheckin{
{User: "旅行者", Outcome: provider.CheckinDone, Credit: 100, Streak: 4},
{User: "second", Outcome: provider.CheckinFailed, Msg: "401: token expired"},
})
m = press(t, usagePage, "c")
wantFlash(t, m, false, "旅行者 checked in today +100 · a 4-day streak · already; second couldn't check in: 401: token expired")
stubCheckin(t, []provider.WorkBuddyCheckin{{User: "a", Outcome: provider.CheckinIneligible}, {User: "b", Outcome: provider.CheckinInactive}})
m = press(t, usagePage, "c")
wantFlash(t, m, true, "a isn't eligible for the check-in; b: no check-in event now")
}
// c checks the Trae CN accounts in too, after WorkBuddy's, each said as
// Trae CN's (#694); with only Trae CN signed in, it is checked in alone.
func TestTUIChecksTraeIn(t *testing.T) {
home(t)
usagePage := model{w: 200, h: 40, page: pageUsage}
wb := stubCheckin(t, []provider.WorkBuddyCheckin{{User: "旅行者", Outcome: provider.CheckinDone, Credit: 100, Streak: 4}})
tr := stubTrae(t, []provider.WorkBuddyCheckin{{User: "hu", By: "trae", Outcome: provider.CheckinClaimed, Credit: 100, Asked: true}})
m := press(t, usagePage, "c")
wantFlash(t, m, true, "旅行者 checked in today +100 · a 4-day streak · already; Trae CN hu checked in today +100")
if *wb != 1 || *tr != 1 {
t.Fatalf("calls: workbuddy %d, trae %d", *wb, *tr)
}
stubCheckin(t, nil)
stubTrae(t, []provider.WorkBuddyCheckin{{User: "hu", By: "trae", Outcome: provider.CheckinIneligible, Msg: "device checked in"}})
m = press(t, usagePage, "c")
wantFlash(t, m, true, "Trae CN hu isn't eligible for the check-in")
// and MiniMax Code's (#811)
stubTrae(t, nil)
mm := stubMiniMax(t, []provider.WorkBuddyCheckin{{User: "hu", By: "minimax", Outcome: provider.CheckinClaimed, Credit: 800, Streak: 2, Asked: true}})
m = press(t, usagePage, "c")
wantFlash(t, m, true, "MiniMax Code hu checked in today +800 · a 2-day streak")
if *mm != 1 {
t.Fatalf("minimax asked %d times", *mm)
}
// and Qoder's daily credits, each said as Qoder's
stubMiniMax(t, nil)
qd := stubQoder(t, []provider.WorkBuddyCheckin{{User: "arno", By: "qoder", Outcome: provider.CheckinClaimed, Credit: 100, Asked: true}})
m = press(t, usagePage, "c")
wantFlash(t, m, true, "Qoder arno checked in today +100")
if *qd != 1 {
t.Fatalf("qoder asked %d times", *qd)
}
}
// A WorkBuddy (China) account's line on the Usage page says how today's
// check-in went, as its card in the app does, and points at c while it
// isn't in.
func TestCheckinOnTheUsageLine(t *testing.T) {
now := time.Now()
today := provider.CheckinDay(now)
win := []provider.QuotaWindow{{Name: "Month", Used: 10}}
qs := []provider.SubscriptionQuota{
{Name: "WorkBuddy", User: "旅行者", Checkins: true, Windows: win, Checkin: &provider.WorkBuddyCheckin{Day: today, Outcome: provider.CheckinClaimed, Credit: 100, Streak: 4}},
{Name: "WorkBuddy", User: "second", Checkins: true, Windows: win, Checkin: &provider.WorkBuddyCheckin{Day: "2020-01-01", Outcome: provider.CheckinClaimed}},
{Name: "WorkBuddy", User: "third", Checkins: true, Error: "sign-in has expired", Checkin: &provider.WorkBuddyCheckin{Day: today, Outcome: provider.CheckinFailed}},
{Name: "Codex", User: "me@example.com", Windows: win},
}
got := strings.Join(quotaLines(qs, true, false, 200, now), "\n")
for _, want := range []string{"签到 ✓ +100 · 4-day streak", "second", "签到 not yet today · c", "sign-in has expired 签到 failed · c tries again"} {
if !strings.Contains(got, want) {
t.Errorf("missing %q in\n%s", want, got)
}
}
for _, l := range strings.Split(got, "\n") {
if strings.Contains(l, "Codex") && strings.Contains(l, "签到") {
t.Errorf("a check-in on a Codex line: %s", l)
}
}
}
// c checks in the accounts of a plugin that checks in itself (auth.checkin;
// Lemon on Discord) with the built-ins', named by the plugin's vendor; a
// captcha is said as such, not as a failure.
func TestTUIChecksPluginsIn(t *testing.T) {
home(t)
usagePage := model{w: 200, h: 40, page: pageUsage}
stubCheckin(t, nil)
calls := stubPluginCheckin(t, []provider.WorkBuddyCheckin{
{User: "a@x", By: "plugin:fakeco", Vendor: "FakeCo", Outcome: provider.CheckinClaimed, Credit: 50, Streak: 3},
{User: "b@x", By: "plugin:fakeco", Vendor: "FakeCo", Outcome: provider.CheckinCaptcha, Msg: "slide the puzzle"},
})
m := press(t, usagePage, "c")
wantFlash(t, m, true, "FakeCo a@x")
wantFlash(t, m, true, "FakeCo b@x asks for a captcha: check in in its own app")
if *calls != 1 {
t.Fatalf("checked in %d times", *calls)
}
}