Files
magpie/internal/provider/left_alone_test.go
T
yetone c087834d6f provider: rememberLogins checks the time before it takes logins.json's lock
#1665, owner's item 1: rememberLogins took loginsMu first. That made and
locked logins.json.lock even when it then saw that 30 seconds hadn't
passed since its last look and returned. It is asked on every account
list and every page's read: ~370 times in a Windows start's first 25s in
the report, each one a file the antivirus filter driver works on.
loginsSeenAt is now an atomic.Int64, so the time can be read without the
lock. A look that isn't due returns before the lock. A due look checks
again under the lock, in case another ask looked while it waited.
switchSavedLogin, ForgetAccounts and the tests that reset the time now
Store(0).

TestRememberLoginsNotDueTakesNoLock holds loginsMu and asks 100 times
just after a look. It expects the asks to return within 5s, and a due
ask to look. With the check before the lock removed by hand, it goes red:
  --- FAIL: TestRememberLoginsNotDueTakesNoLock (5.67s)
      logins_due_test.go:30: a look not due waited for logins.json's lock

What was run (worktree at 9fd80b212 plus this change and the commits
before it):
  gofmt -l internal/: nothing
  go vet on the touched packages: exit 0
  GOOS=linux go vet -tags nogui ./...: exit 0
  GOOS=windows go vet -tags nogui ./...: exit 0
  go test -timeout 45m -count=1 ./internal/provider: ok 696.341s
2026-10-20 20:23:55 +08:00

123 lines
3.6 KiB
Go

package provider
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/claudecode"
"github.com/yetone/magpie/internal/testenv"
)
// A provider switched off, or whose account was removed from magpie, gets
// no request from magpie in the background: its CLI isn't asked who is
// signed in, its allowance isn't read, its sign-in isn't renewed or
// switched, its list isn't fetched (Riko on Discord: with Claude switched
// off, Surge showed `claude auth status --json` every minute). Fake
// claude and devin CLIs on PATH log every run, and what the
// gateway's loops and the window's pages do is run with each of them
// stale, as minutes passing leave them.
func TestLeftAloneRunsNoCLI(t *testing.T) {
shellFakes(t)
findDevin := DevinExecutable
home := claudeHome(t)
claudeSignIn(t, home, time.Now().Add(24*time.Hour))
writeFile(t, filepath.Join(home, ".claude.json"), map[string]any{"oauthAccount": map[string]any{"emailAddress": "me@example.com"}})
bin, log := t.TempDir(), filepath.Join(t.TempDir(), "runs")
for _, name := range []string{"claude", "devin"} {
testenv.Program(t, filepath.Join(bin, name), "#!/bin/sh\necho \""+name+" $*\" >> "+log+"\n"+
"echo '{\"loggedIn\":true,\"email\":\"me@example.com\",\"subscriptionType\":\"max\"}'\n")
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+"/usr/bin"+string(os.PathListSeparator)+"/bin")
// the real lookups, which find them on PATH
claudeExecutable, DevinExecutable = claudecode.Find, findDevin
forgetClaudeStatus()
t.Cleanup(forgetClaudeStatus)
runs := func() []string {
b, _ := os.ReadFile(log)
return strings.Fields(strings.ReplaceAll(strings.TrimSpace(string(b)), " ", "_"))
}
stale := func() {
claudeStatusMu.Lock()
if !claudeStatusAt.IsZero() {
claudeStatusAt = time.Now().Add(-time.Hour)
}
claudeStatusMu.Unlock()
for _, c := range []*cliIdentity{cursorStatus, devinStatus} {
c.Lock()
if !c.at.IsZero() {
c.at = time.Now().Add(-time.Hour)
}
c.Unlock()
}
loginsMu.Lock()
loginsSeenAt.Store(0)
loginsMu.Unlock()
}
loop := func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for range 3 {
stale()
All()
Hidden()
fetchSubscriptionUsage(ctx)
for _, agent := range []string{"claude", "devin"} {
LoginUsage(ctx, agent)
}
RenewLogins(ctx, 0)
SwitchWhenSpent(ctx, "claude")
FetchNew(time.Second)
}
}
save := func(off, hidden bool) {
writeFile(t, Path(), map[string]any{"providers": []map[string]any{
{"id": "claude", "off": off}, {"id": "devin", "hidden": hidden},
}})
}
save(true, true)
loop()
if got := runs(); len(got) != 0 {
t.Fatalf("switched off or removed, the CLIs were run: %v", got)
}
// the account is still there to switch on again, its CLI never asked
if p, ok := find(All(), "claude"); !ok || !p.Off {
t.Fatalf("claude switched off: %+v %v", p, ok)
}
// switched on, the same loop asks them: the fakes are the ones found.
// Devin's built-in is retired, so nothing here asks its CLI either way.
save(false, false)
loop()
got := strings.Join(runs(), " ")
for _, want := range []string{"claude_auth_status_--json"} {
if !strings.Contains(got, want) {
t.Errorf("switched on, no %s among %s", want, got)
}
}
// what was asked behind the look has answered before the test ends
for _, c := range []*cliIdentity{cursorStatus, devinStatus} {
c.Lock()
asking, done := c.refreshing, c.done
c.Unlock()
if asking {
<-done
}
}
for {
claudeStatusMu.Lock()
busy := claudeStatusBusy
claudeStatusMu.Unlock()
if !busy {
break
}
time.Sleep(10 * time.Millisecond)
}
}