Files
magpie/internal/provider/left_alone_test.go
T
yetone 75cbc7e1e5 provider: a subscription switched off or removed makes no background requests, no claude auth status every minute (Riko on Discord)
Riko switched Claude off in Providers, and magpie still ran
`claude auth status --json` about every minute. Off only kept the
gateway from routing to it. Every page and request lists accounts,
which re-asked the CLI, and the background loops went on reading
allowances, renewing, switching, warming, checking in and fetching lists.
The same held for each sibling with a CLI or account check: Cursor
(`cursor-agent about`), Devin (`devin auth status`) and the rest.

What changed:
- left_alone.go: leftAlone(id), true for a provider that is Off or
  Hidden (account removed).
- account.go claudeIdentity and cli_identity.go get (Cursor, Devin): the
  CLI isn't asked; what it said last (memory or cli-identity.json) is
  served, so the account stays listed to switch on again.
- subscription_usage.go: claude, cursor and grok are checked as hidden
  before their account is looked up (the look-up ran the CLI).
- login_usage.go loginUsageAt, keepalive.go RenewLogins,
  codex_switch.go NextLogin, codex_warmup.go keepWarm: leftAlone where
  only accountRemoved (or nothing) was checked.
- models.go newDue: a provider that is off has no list fetched.
- checkin.go: each check-in account knows its card's provider; the loop
  skips one that is left alone, a press still checks it in (minimax,
  trae, qoder, workbuddy, plugin check-ins set it).
- docs/subsystems/providers-accounts.md says so.
Not covered: KeepPluginsListed lists every plugin at once in the host. A
Cursor switched off that was never asked who is signed in isn't listed
until switched on.

What was run:
- TestLeftAloneRunsNoCLI: fake claude, cursor-agent and devin on PATH log
  each run; with claude and cursor off and devin removed, All, Hidden,
  fetchSubscriptionUsage, LoginUsage, RenewLogins, SwitchWhenSpent and
  FetchNew run three times over stale caches and no CLI is run; switched
  back on, all three are asked again.
- Break check: with the source changes reverted it fails with
  "switched off or removed, the CLIs were run: [claude_auth_status_--json
  cursor-agent_about_--format_json devin_auth_status ...]"; with only the
  claude gate removed only claude is logged, with only cliIdentity's only
  cursor and devin.
- go test -tags nogui -timeout 45m ./internal/provider ./internal/gateway:
  ok (399s, 1271s).
- gofmt -l: clean. go vet (darwin), GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: clean, under set -o pipefail.
2026-10-14 08:28:08 +08:00

121 lines
3.7 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, cursor-agent 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", "cursor-agent", "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()
forgetCursorStatus()
forgetDevinStatus()
t.Cleanup(func() { forgetClaudeStatus(); forgetCursorStatus(); forgetDevinStatus() })
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 cliIdentities() {
c.Lock()
if !c.at.IsZero() {
c.at = time.Now().Add(-time.Hour)
}
c.Unlock()
}
loginsMu.Lock()
loginsSeenAt = time.Time{}
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", "cursor", "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": "cursor", "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
save(false, false)
loop()
got := strings.Join(runs(), " ")
for _, want := range []string{"claude_auth_status_--json", "cursor-agent_", "devin_auth_status"} {
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 cliIdentities() {
if pending, done := c.asking(); pending {
<-done
}
}
for {
claudeStatusMu.Lock()
busy := claudeStatusBusy
claudeStatusMu.Unlock()
if !busy {
break
}
time.Sleep(10 * time.Millisecond)
}
}