Files
magpie/internal/provider/checkin_unreadable_test.go
T
yetone a8ecf4020e provider: a check-in file that can't be read or parsed stops the check-in
readCheckins, the reader every daily check-in shares (WorkBuddy, Trae CN,
MiniMax Code, Qoder and the plugins' own), took a file it couldn't read
or parse as empty. The next check-in then asked the vendor again for
every account and wrote its answers over the file: an account could be
checked in twice in one day, and the entries it couldn't read were lost.

readCheckins now returns an error for a file that is there but can't be
read, or doesn't parse into entries (cut short, zeroed, empty, `[]`,
`null`). A file not there is still empty. On the error:
- checkiner.checkinNow asks nothing and writes nothing. Each account it
  would look at is `failed` with the reason (the path and why), so the
  loop tries again after 30 minutes and a press (GUI, TUI, `magpie
  accounts checkin`) says why. The error is logged once while it stays
  the same.
- The readers that only show results (WithCheckins, *Checkins(),
  PluginCheckins) go through shownCheckins, which gives what last read
  from the file in this process, so a card keeps its last good data.
- adoptLegacyPluginCheckins' own strict copy of the read now uses
  readCheckins too.
Docs: providers-accounts.md no longer lists check-in marks among the
caches written over; provider-plugins.md says the plugin file's case.

Test: TestCheckinUnreadableFileLeftAlone writes a cut-short, zeroed,
empty, `[]` and `null` workbuddy-checkin.json, runs the loop and a press
against a fake WorkBuddy, and checks no status or claim request went out,
the file is byte for byte unchanged, and the result says the path; a
folder in place of plugin-checkin.json does the same through a plugin
checkiner. TestCheckinMissingFileIsEmpty and
TestCheckinUnreadableKeepsLastGood cover the other two sides.
Broken by hand (checkinNow taking the error as an empty map again): the
test went red, every case claimed (Asked true), and green once restored.

Run in a worktree at origin/main 62a1df801, sandbox HOME (testenv):
- gofmt -l . : clean
- go vet ./... (darwin), GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... : all exit 0
- set -o pipefail; go test -timeout 45m ./... : exit 0, 68 packages ok,
  none failed (internal/gateway 613s, internal/agent 529s,
  internal/provider 262s)
No red seen, so no issue filed.
2026-10-21 07:23:23 +08:00

119 lines
4.0 KiB
Go

package provider
import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
)
// A check-in file that is there but can't be read or parsed is unknown,
// never empty: neither the loop nor a press asks the vendor anything, the
// file is left byte for byte as it was, and each account says why. Read
// as empty, the next check-in asked again (a second check-in in a day) and
// wrote over the entries it couldn't read.
func TestCheckinUnreadableFileLeftAlone(t *testing.T) {
bad := map[string][]byte{
"cut short": []byte(`{"workbuddy|u1": {"day": "2026-10-21", "outc`),
"zeroed": make([]byte, 512),
"empty": {},
"a list": []byte(`[]`),
"null": []byte(`null`),
}
for name, b := range bad {
t.Run(name, func(t *testing.T) {
f := &fakeWBCheckin{active: true}
srv := f.serve(t)
now := time.Date(2026, 10, 21, 3, 0, 0, 0, time.UTC)
c := wbCheckinFixture(t, srv, &now)
if err := os.WriteFile(c.path, b, 0o600); err != nil {
t.Fatal(err)
}
for _, soon := range []bool{false, true} {
rs := c.checkinNow(context.Background(), soon)
if len(rs) != 1 || rs[0].Outcome != CheckinFailed || rs[0].Asked || rs[0].User != "旅行者" ||
!strings.Contains(rs[0].Msg, c.path) {
t.Fatalf("soon %v: %+v", soon, rs)
}
}
if s, cl := f.counts(); s != 0 || cl != 0 {
t.Fatalf("asked WorkBuddy with its file unread: status %d, claims %d", s, cl)
}
if got, err := os.ReadFile(c.path); err != nil || !bytes.Equal(got, b) {
t.Fatalf("the file became %q (%v), was %q", got, err, b)
}
})
}
// one that can't be read at all (a folder in its place)
t.Run("unreadable", func(t *testing.T) {
var asked atomic.Int32
path := filepath.Join(t.TempDir(), "plugin-checkin.json")
if err := os.Mkdir(path, 0o700); err != nil {
t.Fatal(err)
}
c := checkiner{path: path, now: time.Now, mu: &pluginCheckinMu, by: "plugin:x", label: "x", recheck: true,
accounts: func() []checkinAcct {
return []checkinAcct{{User: "a", On: true, key: "x|a", do: func(context.Context) WorkBuddyCheckin {
asked.Add(1)
return WorkBuddyCheckin{Outcome: CheckinClaimed}
}}}
}}
rs := c.checkinNow(context.Background(), true)
if len(rs) != 1 || rs[0].Outcome != CheckinFailed || rs[0].By != "plugin:x" || asked.Load() != 0 {
t.Fatalf("checked in %d times: %+v", asked.Load(), rs)
}
if fi, err := os.Stat(path); err != nil || !fi.IsDir() {
t.Fatalf("its place was written over: %v", err)
}
})
}
// A file not there is still empty: the account is checked in.
func TestCheckinMissingFileIsEmpty(t *testing.T) {
f := &fakeWBCheckin{active: true}
srv := f.serve(t)
now := time.Date(2026, 10, 21, 3, 0, 0, 0, time.UTC)
c := wbCheckinFixture(t, srv, &now)
rs := c.checkinNow(context.Background(), false)
if len(rs) != 1 || rs[0].Outcome != CheckinClaimed || !rs[0].Asked {
t.Fatalf("%+v", rs)
}
}
// While a file doesn't parse, its cards keep what last read from it, and
// once it parses again the check-in goes on from what it holds.
func TestCheckinUnreadableKeepsLastGood(t *testing.T) {
f := &fakeWBCheckin{active: true}
srv := f.serve(t)
now := time.Date(2026, 10, 21, 3, 0, 0, 0, time.UTC)
c := wbCheckinFixture(t, srv, &now)
ctx := context.Background()
if rs := c.checkinNow(ctx, false); len(rs) != 1 || rs[0].Outcome != CheckinClaimed {
t.Fatalf("first: %+v", rs)
}
good, err := os.ReadFile(c.path)
if err != nil {
t.Fatal(err)
}
os.WriteFile(c.path, good[:len(good)/2], 0o600)
if st := shownCheckins(c.path); st["workbuddy|u1"].Outcome != CheckinClaimed {
t.Fatalf("shown while unread: %+v", st)
}
if _, err := readCheckins(c.path); err == nil {
t.Fatal("a file cut short read without an error")
}
// the file mended: today's claim read back, nothing asked again
os.WriteFile(c.path, good, 0o600)
if rs := c.checkinNow(ctx, false); len(rs) != 1 || rs[0].Outcome != CheckinClaimed || rs[0].Asked {
t.Fatalf("mended: %+v", rs)
}
if s, cl := f.counts(); s != 1 || cl != 1 {
t.Fatalf("status %d, claims %d", s, cl)
}
}