Files
magpie/internal/provider/keepalive_test.go
T
yetone e698e7dddd claude: adding a Claude account is Claude Code's own claude auth login, and magpie never refreshes a Claude sign-in nor asks Anthropic anything with one (the user: 把 magpie 自己的 Claude 登录改成直接用 claude /login,只要体验上没什么差别就改) — magpie traded the sign-in page's code for tokens with Claude Code's OAuth client, asked Anthropic's profile and model list with them, refreshed saved accounts itself (daily keepalive included) and handed a saved account to Claude Code as CLAUDE_CODE_OAUTH_TOKEN. Now Add account runs claude auth login --claudeai in a config directory of magpie's own: the page Claude Code would open comes to magpie's window (Claude Code is given magpie as its BROWSER, which writes the page down and exits), the browser comes back to Claude Code's own listener, and when the browser can't reach this machine the address it ended on, or the code Claude's page shows, is pasted as before and handed to Claude Code. Who signed in is what Claude Code kept (.claude.json, auth status). A saved account in use beside the signed-in one runs Claude Code with CLAUDE_CONFIG_DIR set to its own directory (claude-accounts/ beside magpie's config), its sign-in put there once from logins.json and read back as Claude Code refreshes it; switching to it moves it into Claude Code's own store and removes the directory, as forgetting it does. Claude's models are the catalog's (models.dev), not asked of Anthropic; an imported Claude login is kept as the file has it, and Claude Code refreshes it on first use. Claude Code is required to add a Claude account. TestClaudeSignInByPaste, TestClaudeSignInByPastedAddress, TestClaudeSavedAccountDir (a fake claude CLI, and a server standing in for Anthropic that fails the test if asked) and the reworked sign-in, import and gateway tests fail without the change. With the real Claude Code 2.1.286 in a sandbox directory, its sign-in page (localhost callback) reached magpie's binary and the sign-in waited for the browser; nothing was left behind once it was canceled. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; claude-risk, plugin-signin, signin-callback, signin-link, signin-paste-codex, routing-signin, own-signin-remove, click-scroll, old-webkit pass.
2026-10-01 16:37:07 +08:00

139 lines
4.7 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
)
func TestRenewLogins(t *testing.T) {
home := signIn(t)
rememberLogins(true) // me@ saved…
codexSignIn(t, home, "work@example.com", "r-work")
rememberLogins(true) // …and codex is on work@ now
var got []string
status := http.StatusOK
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
got = append(got, body["refresh_token"])
if status != http.StatusOK {
w.WriteHeader(status)
w.Write([]byte(`{"error":"invalid_grant"}`))
return
}
json.NewEncoder(w).Encode(map[string]any{"access_token": fakeJWT(map[string]any{"exp": float64(time.Now().Add(time.Hour).Unix())}),
"refresh_token": body["refresh_token"] + "-2"})
}))
defer fake.Close()
old := codexTokenURL
codexTokenURL = fake.URL
t.Cleanup(func() { codexTokenURL = old })
ctx := context.Background()
saved := func() savedLogin {
for _, l := range readLogins() {
if l.User == "me@example.com" {
return l
}
}
t.Fatal("me@ isn't saved")
return savedLogin{}
}
// seen in codex's hands a moment ago: not due
if rs := RenewLogins(ctx, keepAliveEvery); len(rs) != 1 || rs[0].User != "me@example.com" || rs[0].Renewed || len(got) != 0 {
t.Fatalf("renewed what wasn't due: %+v %v", rs, got)
}
// every account, now: me@ is refreshed in logins.json, codex's own isn't touched
if rs := RenewLogins(ctx, 0); len(rs) != 1 || !rs[0].Renewed || rs[0].Err != "" {
t.Fatalf("renew: %+v", rs)
}
if strings.Join(got, ",") != "r" {
t.Fatalf("refreshed with %v", got)
}
l := saved()
if !strings.Contains(string(l.Auth), `"r-2"`) || time.Since(l.Renewed) > time.Minute || !renewalDue(l, keepAliveEvery, time.Now().Add(25*time.Hour)) {
t.Fatalf("saved after renewal: %+v", l)
}
var live codexAuth
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
if live.Tokens.RefreshToken != "r-work" {
t.Fatalf("the agent's own sign-in changed: %+v", live.Tokens)
}
// no answer worth the name: it may yet renew, so nothing is marked
status = http.StatusServiceUnavailable
if rs := RenewLogins(ctx, 0); rs[0].Err == "" || rs[0].Lapsed || saved().Lapsed != "" {
t.Fatalf("a 503 lapsed it: %+v", rs)
}
// refused: the sign-in is gone, and says so until it's made again
status = http.StatusBadRequest
rs := RenewLogins(ctx, 0)
if !rs[0].Lapsed || !strings.Contains(rs[0].Err, "sign-in has expired") {
t.Fatalf("refused: %+v", rs)
}
for _, l := range Logins("codex") {
if (l.User == "me@example.com") != (l.Lapsed != "") {
t.Fatalf("lapsed: %+v", l)
}
}
// a 403 may be a proxy's: marked or not, the next refresh is tried
status = http.StatusForbidden
if rs := RenewLogins(ctx, 0); rs[0].Lapsed || saved().Lapsed == "" || len(got) != 4 {
t.Fatalf("403: %+v %v", rs, got)
}
// signed in again: no longer lapsed, once it's saved again
status = http.StatusOK
writeFile(t, filepath.Join(home, ".codex", "auth.json"), map[string]any{
"auth_mode": "chatgpt",
"tokens": map[string]any{
"id_token": fakeJWT(map[string]any{"email": "me@example.com", "https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "pro", "chatgpt_account_id": "acct-1"}}),
"access_token": fakeJWT(map[string]any{"exp": float64(time.Now().Add(time.Hour).Unix())}),
"refresh_token": "r-new", "account_id": "acct-1",
},
})
rememberLogins(true)
if err := SwitchLogin("codex", "work@example.com"); err != nil {
t.Fatal(err)
}
ls := Logins("codex")
for _, l := range ls {
if l.Lapsed != "" {
t.Fatalf("still lapsed: %+v", l)
}
}
if len(ls) != 2 {
t.Fatalf("%d accounts: %+v", len(ls), ls)
}
}
func TestRenewalDue(t *testing.T) {
now := time.Date(2026, 9, 25, 12, 0, 0, 0, time.UTC)
codex := func(last time.Time) []byte {
b, _ := json.Marshal(map[string]any{"tokens": map[string]any{"access_token": "a"}, "last_refresh": last.Format(time.RFC3339Nano)})
return b
}
for _, c := range []struct {
name string
l savedLogin
due bool
}{
{"never renewed, seen long ago", savedLogin{Agent: "codex", Seen: now.Add(-48 * time.Hour)}, true},
{"seen in the agent's hands today", savedLogin{Agent: "codex", Seen: now.Add(-time.Hour)}, false},
{"renewed today", savedLogin{Agent: "codex", Seen: now.Add(-72 * time.Hour), Renewed: now.Add(-time.Hour)}, false},
{"codex refreshed itself lately", savedLogin{Agent: "codex", Seen: now.Add(-72 * time.Hour), Auth: codex(now.Add(-2 * time.Hour))}, false},
{"codex a day since", savedLogin{Agent: "codex", Seen: now.Add(-72 * time.Hour), Auth: codex(now.Add(-25 * time.Hour))}, true},
} {
if got := renewalDue(c.l, keepAliveEvery, now); got != c.due {
t.Errorf("%s: due %v", c.name, got)
}
}
}