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

233 lines
7.7 KiB
Go

package provider
import (
"context"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
"github.com/yetone/magpie/internal/settings"
)
// 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, err := exec.LookPath("bun")
if err != nil {
t.Skip("no bun on PATH")
}
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, err := exec.LookPath("bun")
if err != nil {
t.Skip("no bun on PATH")
}
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)
}
}
}