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

241 lines
8.5 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
// fakeTraeCheckin is Trae CN's check-in as one account sees it, behind
// the plugin's fetch: status, and a claim answered with claimCode (0
// claims).
type fakeTraeCheckin struct {
mu sync.Mutex
enable bool
checked bool
claimCode int
claimMsg string // the claim's message with claimCode
expired bool // the plugin marks the sign-in expired (code 1001)
old bool // a plugin before 0.1.4: chat completions only
paths []string
}
func (f *fakeTraeCheckin) serve(t *testing.T) *httptest.Server {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
if f.old {
http.Error(w, "only chat completions are served", http.StatusBadRequest)
return
}
f.paths = append(f.paths, r.Method+" "+strings.TrimPrefix(r.URL.Path, "/trae/api/v2/ug/checkin_credits"))
if f.expired {
w.Header().Set("X-Magpie-Sign-In", "expired")
json.NewEncoder(w).Encode(map[string]any{"code": 1001, "message": "We're sorry, but we are not able to authenticate you"})
return
}
w.Header().Set("X-Magpie-Sign-In", "kept")
switch r.URL.Path {
case "/trae/api/v2/ug/checkin_credits/status":
json.NewEncoder(w).Encode(map[string]any{"code": 0, "message": "success", "enable": f.enable, "checked_in": f.checked, "credits": 100})
case "/trae/api/v2/ug/checkin_credits/claim":
if f.claimCode != 0 {
json.NewEncoder(w).Encode(map[string]any{"code": f.claimCode, "message": orStr(f.claimMsg, "already checked in on this device")})
return
}
f.checked = true
json.NewEncoder(w).Encode(map[string]any{"code": 0, "credits": 100, "extra_credits": 0})
default:
w.WriteHeader(404)
}
}))
t.Cleanup(srv.Close)
old := traeCheckinURL
traeCheckinURL = srv.URL + "/trae/api/v2/ug/checkin_credits"
t.Cleanup(func() { traeCheckinURL = old })
return srv
}
func (f *fakeTraeCheckin) asked() []string {
f.mu.Lock()
defer f.mu.Unlock()
out := f.paths
f.paths = nil
return out
}
// traeCheckinFixture is a checkiner for one Trae CN account, its clock at
// *now, its file in a temp folder.
func traeCheckinFixture(t *testing.T, now *time.Time) checkiner {
c := newTraeCheckiner(func() []traeAccount {
return []traeAccount{{User: "hu", On: true, uid: "7300", card: TraeCNID, via: http.DefaultClient.Do}}
})
c.path = filepath.Join(t.TempDir(), "trae-checkin.json")
c.now = func() time.Time { return *now }
return c
}
// One check-in a Beijing day (#694): the status first, a claim while it
// is on and today's isn't in; asked again the same day, nothing is sent,
// and the next day finds it in already when it was pressed in Trae.
func TestTraeCheckinOncePerDay(t *testing.T) {
f := &fakeTraeCheckin{enable: true}
f.serve(t)
now := time.Date(2026, 10, 4, 9, 0, 0, 0, beijing)
c := traeCheckinFixture(t, &now)
ctx := context.Background()
rs := c.checkinNow(ctx, false)
if len(rs) != 1 || rs[0].Outcome != CheckinClaimed || rs[0].Credit != 100 || rs[0].User != "hu" || rs[0].By != "trae" || !rs[0].Asked {
t.Fatalf("first: %+v", rs)
}
if got := strings.Join(f.asked(), ", "); got != "POST /status, POST /claim" {
t.Fatalf("asked %s", got)
}
now = now.Add(3 * time.Hour)
rs = c.checkinNow(ctx, true)
if len(rs) != 1 || rs[0].Outcome != CheckinClaimed || rs[0].Asked {
t.Fatalf("same day: %+v", rs)
}
if got := f.asked(); len(got) != 0 {
t.Fatalf("same day asked %v", got)
}
now = now.Add(24 * time.Hour)
rs = c.checkinNow(ctx, false)
if rs[0].Outcome != CheckinDone || rs[0].Credit != 100 {
t.Fatalf("next day, pressed in Trae: %+v", rs)
}
if got := strings.Join(f.asked(), ", "); got != "POST /status" {
t.Fatalf("next day asked %s", got)
}
}
// Trae's answers for the day hold for it; a failure is tried again.
func TestTraeCheckinAnswers(t *testing.T) {
ctx := context.Background()
now := time.Date(2026, 10, 4, 9, 0, 0, 0, beijing)
off := &fakeTraeCheckin{}
off.serve(t)
if rs := traeCheckinFixture(t, &now).checkinNow(ctx, false); rs[0].Outcome != CheckinInactive {
t.Fatalf("not on: %+v", rs)
}
if got := off.asked(); len(got) != 1 {
t.Fatalf("claimed while off: %v", got)
}
// 9095: this device is in for today, an answer; no second device id
dev := &fakeTraeCheckin{enable: true, claimCode: traeDeviceChecked}
dev.serve(t)
c := traeCheckinFixture(t, &now)
if rs := c.checkinNow(ctx, false); rs[0].Outcome != CheckinIneligible || rs[0].Msg == "" {
t.Fatalf("9095: %+v", rs)
}
dev.asked()
// a press asks again, on the same device: a 9095 from before the
// vendor's day turned may be yesterday's (#3326)
if rs := c.checkinNow(ctx, true); rs[0].Outcome != CheckinIneligible || strings.Join(dev.asked(), ", ") != "POST /status, POST /claim" {
t.Fatalf("9095 pressed: %+v", rs)
}
// a failure that got no answer is tried again the same day
down := &fakeTraeCheckin{enable: true, claimCode: 5000, claimMsg: "internal error"}
down.serve(t)
c = traeCheckinFixture(t, &now)
if rs := c.checkinNow(ctx, false); rs[0].Outcome != CheckinFailed {
t.Fatalf("5000: %+v", rs)
}
down.asked()
down.claimCode = 0
if rs := c.checkinNow(ctx, true); rs[0].Outcome != CheckinClaimed {
t.Fatalf("5000, again: %+v", rs)
}
// a refused token, marked by the plugin
exp := &fakeTraeCheckin{expired: true}
exp.serve(t)
rs := traeCheckinFixture(t, &now).checkinNow(ctx, false)
if rs[0].Outcome != CheckinFailed || !strings.Contains(rs[0].Msg, "sign in again") {
t.Fatalf("expired: %+v", rs)
}
if got := exp.asked(); len(got) != 1 {
t.Fatalf("claimed when signed out: %v", got)
}
// a plugin too old to send Trae's own pages
old := &fakeTraeCheckin{old: true}
old.serve(t)
rs = traeCheckinFixture(t, &now).checkinNow(ctx, false)
if rs[0].Outcome != CheckinFailed || !strings.Contains(rs[0].Msg, "0.1.4") {
t.Fatalf("old plugin: %+v", rs)
}
}
// #808: with the check-in on and today's not in, a claim answered 9074
// 「当前参与用户太多,请稍后再试」 (the reporter's bytes) is Trae refusing
// the claim: the day's answer, so neither the loop nor a press claims
// again that day; the account is told Trae CN refused it and to check in
// in Trae's app (#1920).
// The next day it is asked as any other.
func TestTraeCheckin9074IsOwnAppOnly(t *testing.T) {
ctx := context.Background()
now := time.Date(2026, 10, 4, 9, 0, 0, 0, beijing)
f := &fakeTraeCheckin{enable: true, claimCode: 9074, claimMsg: "当前参与用户太多,请稍后再试"}
f.serve(t)
c := traeCheckinFixture(t, &now)
first := c.checkinNow(ctx, false)
if got := strings.Join(f.asked(), ", "); got != "POST /status, POST /claim" {
t.Fatalf("asked %s", got)
}
// the loop past wbCheckinRetry, and a press (soon), the same day
for _, soon := range []bool{false, true} {
now = now.Add(wbCheckinRetry + time.Minute)
rs := c.checkinNow(ctx, soon)
if got := f.asked(); len(got) != 0 {
t.Fatalf("same day, soon=%v, asked again after 9074: %v", soon, got)
}
if rs[0].Outcome != CheckinOwnApp || rs[0].Asked {
t.Fatalf("same day, soon=%v: %+v", soon, rs)
}
}
if len(first) != 1 || first[0].Outcome != CheckinOwnApp || first[0].Msg != "当前参与用户太多,请稍后再试" || !first[0].Asked {
t.Fatalf("9074: %+v", first)
}
now = time.Date(2026, 10, 5, 9, 0, 0, 0, beijing)
f.claimCode = 0
if rs := c.checkinNow(ctx, false); rs[0].Outcome != CheckinClaimed {
t.Fatalf("next day: %+v", rs)
}
// 9074 on the status, before any claim, is still a failure tried again
if r := traeRefused(9074, "当前参与用户太多,请稍后再试"); r.Outcome != CheckinFailed {
t.Fatalf("status 9074: %+v", r)
}
}
// A Trae CN account's usage card is told its check-in, and whose it is,
// so the card's switch is Trae's; another plugin's card isn't.
func TestTraeCheckinOnTheCard(t *testing.T) {
st := map[string]WorkBuddyCheckin{TraeCNID + "|7300": {Day: "2026-10-04", Outcome: CheckinClaimed, Credit: 100}}
accts := []traeAccount{{User: "hu", On: true, uid: "7300", card: TraeCNID}, {User: "nouid", On: true, card: TraeCNID}}
qs := []SubscriptionQuota{{Provider: TraeCNID, User: "hu"}, {Provider: TraeCNID, User: "nouid"}, {Provider: "kiro", User: "hu"}}
out := markCheckins(qs, traeCards(accts), st, "trae")
if !out[0].Checkins || out[0].CheckinBy != "trae" || out[0].Checkin == nil || out[0].Checkin.Credit != 100 || out[0].Checkin.By != "trae" {
t.Fatalf("trae card: %+v", out[0])
}
if out[1].Checkins || out[2].Checkins {
t.Fatalf("marked: %+v, %+v", out[1], out[2])
}
if qs[0].Checkins {
t.Fatal("qs changed")
}
}