providers: a notification when an account in use has to be signed in again, and before a Claude sign-in ends
steven_4422 asked on Discord whether magpie warns when a Claude Code login is close to expiring or has expired. It didn't, outside the app: a sign-in that Anthropic, OpenAI, GitHub or a plugin's vendor refused showed only on the account's row (Sign-in required, Sign in again) and on the provider row's Signed out pill, and nothing warned before a Claude sign-in ended. The system notifications covered only usage, balance, reset, masking and task-done alerts. What Claude's tokens carry (read from Claude Code 2.1.316): the access token refreshes itself, but claudeAiOauth.refreshTokenExpiresAt ends the sign-in. Claude Code sets it from refresh_token_expires_in, or to 30 days from the sign-in, keeps it over a refresh that sends none, says "Your login expires in N days · run /login to renew" from 3 days before, and treats a sign-in past it as none. It skips the warning when the access token outlasts that end by more than the 3 days. - provider.WatchSignIns looks a minute after start and then every two minutes. It reads only magpie's own files and asks no vendor. It covers every account in use of every subscription that is on. - An account is told of when its row says Sign in again, by the Providers page's own test (needsSignIn mirrors renderAccounts): any mark on a Claude, Copilot or plugin account, and on another built-in (Codex revoked, a refresh that failed, ChatGPT API) a mark that signInGone matches. - A Claude account is also told of 3 days before refreshTokenExpiresAt, by Claude Code's rule (claudeSignInEnds). - Each alert goes out once: a lapse until the account is usable again, an end until it moves. The marks are kept in signin-alerts.json, so a restart doesn't repeat them, and an unreadable file tells nothing. - The GUI sends the alert as "<Name> · <user>" with "Signed out — click to sign in again" or "Sign-in ends in N days — click to sign in again", in en, zh, zh-TW, ja and de. A click opens that provider's editor (?view=providers&edit=<id>), where the account's Sign in again is. A Linux dismissal still opens nothing (#2513). Run on mote in a sandbox HOME, on this change over04ed05733. It was rebased onto437580535after the run, which changes only a gateway test: - gofmt -l: clean. - go vet ./...: ok. - GOOS=linux go vet -tags nogui ./...: ok. - GOOS=windows go vet ./...: ok. - go build .: ok. - go test -count=1 -timeout 45m ./internal/provider ./internal/gui: ok internal/provider 163.7s, ok internal/gui 162.2s. Broken by hand, each red and then restored: - Gone left false, which is the old code where nothing was told: TestSignInAlertClaudeSignedOut is red ("alerts …: []"). - The refreshTokenExpiresAt read turned off: TestSignInAlertClaudeEnding is red. - The once-only mark dropped: TestSignInAlertClaudeSignedOut and TestDueSignIns are red ("told again"). - A click on a sign-in alert opening Usage: TestSignInAlertOpensItsProvider is red on darwin, windows and linux. Not tried: - A real notification on any OS. - A real Claude account near its refreshTokenExpiresAt. - A real revoked Codex, Copilot or plugin sign-in end to end. - The GUI page is unchanged, so no Playwright run. - Full suite: not run at push. Reported-by: steven_4422 (Discord)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -140,7 +140,7 @@ func notice(goos, id, title, body string) notifications.NotificationOptions {
|
||||
|
||||
// clickView is the page a response to a notification opens, "" for none.
|
||||
// A click on an alert opens Usage; on a value masked, the 脱敏 page at the
|
||||
// request that masked it. On a turn's end, where the user goes is their
|
||||
// request that masked it; on a sign-in alert, its provider's accounts. On a turn's end, where the user goes is their
|
||||
// agent, not magpie. On Linux only a click (linuxClick) is one: an alert
|
||||
// dismissed there opened the main window, maximised if it was left so,
|
||||
// over the desktop with the focus (#2513).
|
||||
@@ -155,6 +155,9 @@ func clickView(goos string, r notifications.NotificationResponse) string {
|
||||
}
|
||||
return "masking&key=" + key
|
||||
}
|
||||
if rest, ok := strings.CutPrefix(r.ID, signInNotice); ok {
|
||||
return signInView(rest)
|
||||
}
|
||||
return "usage"
|
||||
}
|
||||
|
||||
@@ -190,6 +193,13 @@ func (h *host) watchAlerts() {
|
||||
title, body := turnDoneText(trayLang(settings.Load().Lang, systemLang), agentName(t.Agent), t)
|
||||
n.sendAs(turnNotice, title, body)
|
||||
}
|
||||
// an account in use signed out, or a Claude sign-in about to end
|
||||
// (steven_4422 on Discord): always on, as the account's Sign in
|
||||
// again is
|
||||
go provider.WatchSignIns(backendCtx, func(a provider.SignInAlert) {
|
||||
title, body := signInText(trayLang(settings.Load().Lang, systemLang), a, time.Now())
|
||||
n.sendAs(signInKind(a.Provider), title, body)
|
||||
})
|
||||
// until this one has handed the gateway to the new version,
|
||||
// which alerts from then on (#4938)
|
||||
provider.WatchQuotas(backendCtx, wake, func(a provider.QuotaAlert) {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// signInNotice is the id a sign-in alert is notified under, followed by
|
||||
// its provider's id and "|", so a click opens that provider's accounts.
|
||||
const signInNotice = "magpie-signin-"
|
||||
|
||||
// signInKind is the start of the id a sign-in alert for provider id is
|
||||
// notified under (sendAs puts the time after it).
|
||||
func signInKind(id string) string { return signInNotice + url.QueryEscape(id) + "|" }
|
||||
|
||||
// signInView is the page a click on a sign-in alert opens, rest being its
|
||||
// id after signInNotice: the provider's editor, where the account's Sign
|
||||
// in again is.
|
||||
func signInView(rest string) string {
|
||||
p, _, _ := strings.Cut(rest, "|")
|
||||
if p, err := url.QueryUnescape(p); err == nil && p != "" {
|
||||
return "providers&edit=" + url.QueryEscape(p)
|
||||
}
|
||||
return "providers"
|
||||
}
|
||||
|
||||
// signInDays is how many days are left until at, rounded up, as Claude
|
||||
// Code counts them in its own warning.
|
||||
func signInDays(at, now time.Time) int {
|
||||
d := int((at.Sub(now) + 24*time.Hour - 1) / (24 * time.Hour))
|
||||
return max(d, 1)
|
||||
}
|
||||
|
||||
// signInText is the notification of a sign-in alert in the language (en,
|
||||
// zh, zh-TW, ja or de): the subscription and account, and that it has to
|
||||
// be signed in again, or in how many days its sign-in ends.
|
||||
func signInText(lang string, a provider.SignInAlert, now time.Time) (title, body string) {
|
||||
title = a.Name
|
||||
if a.User != "" {
|
||||
title += " · " + a.User
|
||||
}
|
||||
n := 0
|
||||
if a.Kind == "ending" && a.EndsAt != nil {
|
||||
n = signInDays(*a.EndsAt, now)
|
||||
}
|
||||
switch lang {
|
||||
case "zh":
|
||||
if n > 0 {
|
||||
return title, fmt.Sprintf("登录将在 %d 天后失效,点此重新登录", n)
|
||||
}
|
||||
return title, "登录已失效,点此重新登录"
|
||||
case "zh-TW":
|
||||
if n > 0 {
|
||||
return title, fmt.Sprintf("登入將在 %d 天後失效,點此重新登入", n)
|
||||
}
|
||||
return title, "登入已失效,點此重新登入"
|
||||
case "ja":
|
||||
if n > 0 {
|
||||
return title, fmt.Sprintf("サインインはあと %d 日で切れます。クリックしてサインインし直してください", n)
|
||||
}
|
||||
return title, "サインインが切れました。クリックしてサインインし直してください"
|
||||
case "de":
|
||||
if n == 1 {
|
||||
return title, "Die Anmeldung endet in 1 Tag – zum erneuten Anmelden klicken"
|
||||
}
|
||||
if n > 0 {
|
||||
return title, fmt.Sprintf("Die Anmeldung endet in %d Tagen – zum erneuten Anmelden klicken", n)
|
||||
}
|
||||
return title, "Abgemeldet – zum erneuten Anmelden klicken"
|
||||
}
|
||||
if n == 1 {
|
||||
return title, "Sign-in ends in 1 day — click to sign in again"
|
||||
}
|
||||
if n > 0 {
|
||||
return title, fmt.Sprintf("Sign-in ends in %d days — click to sign in again", n)
|
||||
}
|
||||
return title, "Signed out — click to sign in again"
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//go:build !nogui
|
||||
|
||||
package gui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/services/notifications"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A click on a sign-in alert opens its provider's editor, where the
|
||||
// account's Sign in again is (steven_4422 on Discord); a Linux dismissal
|
||||
// still opens nothing.
|
||||
func TestSignInAlertOpensItsProvider(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
goos, provider, action, want string
|
||||
}{
|
||||
{"darwin", "claude", notifications.DefaultActionIdentifier, "providers&edit=claude"},
|
||||
{"windows", "codex", notifications.DefaultActionIdentifier, "providers&edit=codex"},
|
||||
{"linux", "copilot", linuxClick, "providers&edit=copilot"},
|
||||
{"linux", "copilot", notifications.DefaultActionIdentifier, ""},
|
||||
{"darwin", "a|b&c", notifications.DefaultActionIdentifier, "providers&edit=a%7Cb%26c"},
|
||||
} {
|
||||
id := fmt.Sprintf("%s%d", signInKind(c.provider), time.Now().UnixNano())
|
||||
r := notifications.NotificationResponse{ID: id, ActionIdentifier: c.action}
|
||||
if got := clickView(c.goos, r); got != c.want {
|
||||
t.Errorf("clickView(%s, %s %s) = %q, want %q", c.goos, id, c.action, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The notification names the subscription and account, and says to sign
|
||||
// in again, or in how many days the sign-in ends, in every language.
|
||||
func TestSignInText(t *testing.T) {
|
||||
now := time.Date(2026, 11, 8, 12, 0, 0, 0, time.UTC)
|
||||
gone := provider.SignInAlert{Provider: "claude", Name: "Claude", User: "a@example.com", Kind: "lapsed"}
|
||||
in := func(d time.Duration) provider.SignInAlert {
|
||||
at := now.Add(d)
|
||||
return provider.SignInAlert{Provider: "claude", Name: "Claude", User: "a@example.com", Kind: "ending", EndsAt: &at}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
lang string
|
||||
a provider.SignInAlert
|
||||
want string
|
||||
}{
|
||||
{"en", gone, "Signed out — click to sign in again"},
|
||||
{"en", in(2*24*time.Hour + time.Hour), "Sign-in ends in 3 days — click to sign in again"},
|
||||
{"en", in(5 * time.Hour), "Sign-in ends in 1 day — click to sign in again"},
|
||||
{"zh", gone, "登录已失效,点此重新登录"},
|
||||
{"zh", in(48 * time.Hour), "登录将在 2 天后失效,点此重新登录"},
|
||||
{"zh-TW", gone, "登入已失效,點此重新登入"},
|
||||
{"ja", in(24 * time.Hour), "サインインはあと 1 日で切れます。クリックしてサインインし直してください"},
|
||||
{"de", in(5 * time.Hour), "Die Anmeldung endet in 1 Tag – zum erneuten Anmelden klicken"},
|
||||
{"de", gone, "Abgemeldet – zum erneuten Anmelden klicken"},
|
||||
} {
|
||||
title, body := signInText(c.lang, c.a, now)
|
||||
if title != "Claude · a@example.com" || body != c.want {
|
||||
t.Errorf("%s %s: %q / %q, want %q", c.lang, c.a.Kind, title, body, c.want)
|
||||
}
|
||||
}
|
||||
if title, _ := signInText("en", provider.SignInAlert{Name: "Cursor", Kind: "lapsed"}, now); strings.Contains(title, "·") {
|
||||
t.Errorf("an account with no name titled %q", title)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package provider
|
||||
|
||||
// Sign-in alerts (steven_4422 on Discord): a notification when an account
|
||||
// in use has to be signed in again, and, for Claude, before its sign-in
|
||||
// ends. Until now a sign-in its vendor refused or revoked showed only on
|
||||
// the account's row (Sign-in required, Sign in again) and the row's
|
||||
// Signed out pill, seen only by opening magpie. The test of an account that
|
||||
// has to be signed in again is the one the Providers page uses
|
||||
// (renderAccounts in app.js), so a notification names exactly the rows
|
||||
// that say so. Each is said once, until the account is usable again; what
|
||||
// was said is kept in a file, so a restart doesn't say it again.
|
||||
//
|
||||
// Claude Code keeps when a sign-in's refresh token ends
|
||||
// (claudeAiOauth.refreshTokenExpiresAt, from the token reply's
|
||||
// refresh_token_expires_in, else 30 days from the sign-in) and says "Your
|
||||
// login expires in N days · run /login to renew" from three days before;
|
||||
// past it, it takes the sign-in for none. A refresh doesn't move it unless
|
||||
// Anthropic sends a new one. magpie tells the same, from the same field.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SignInAlert is a sign-in to tell the user of. Kind is "lapsed" for an
|
||||
// account that has to be signed in again, "ending" for one whose sign-in
|
||||
// ends at EndsAt.
|
||||
type SignInAlert struct {
|
||||
Provider string // the provider's id: the row a click opens
|
||||
Name string
|
||||
User string
|
||||
Kind string
|
||||
EndsAt *time.Time
|
||||
}
|
||||
|
||||
// needsSignIn says an account of agent marked lapsed has to be signed in
|
||||
// again, as the Providers page decides it (renderAccounts' unusable and
|
||||
// signedOut): any mark on a Claude, Copilot or plugin account, and on
|
||||
// another built-in one a mark that says so (signInGone, app.js's
|
||||
// SIGN_IN_GONE). A plugin's usage read that says so marks the account
|
||||
// too (pluginLoginQuota), so its mark covers the page's other case.
|
||||
func needsSignIn(agent string, isPlugin bool, lapsed string) bool {
|
||||
if lapsed == "" {
|
||||
return false
|
||||
}
|
||||
return agent == "claude" || agent == "copilot" || isPlugin || signInGone.MatchString(lapsed)
|
||||
}
|
||||
|
||||
// claudeEndsWithin is how long before a Claude sign-in ends it is told of:
|
||||
// Claude Code's own warning's three days.
|
||||
const claudeEndsWithin = 3 * 24 * time.Hour
|
||||
|
||||
// claudeSignInEnds is when the Claude sign-in auth ends, as Claude Code
|
||||
// reads it: its refresh token's end, unless its access token outlasts that
|
||||
// by more than the warning (a sign-in whose end is not the refresh
|
||||
// token's). ok is false when it says none.
|
||||
func claudeSignInEnds(auth []byte) (at time.Time, ok bool) {
|
||||
c, parsed := parseClaudeCredentials(auth)
|
||||
if !parsed || c.OAuth.RefreshToken == "" || c.OAuth.RefreshExpiresAt <= 0 {
|
||||
return time.Time{}, false
|
||||
}
|
||||
if c.OAuth.ExpiresAt > c.OAuth.RefreshExpiresAt+claudeEndsWithin.Milliseconds() {
|
||||
return time.Time{}, false
|
||||
}
|
||||
return time.UnixMilli(c.OAuth.RefreshExpiresAt), true
|
||||
}
|
||||
|
||||
// signInState is one account as the alerts see it.
|
||||
type signInState struct {
|
||||
Provider, Name, User string
|
||||
Gone bool
|
||||
EndsAt *time.Time
|
||||
}
|
||||
|
||||
// signInStates is every account in use of every subscription switched on:
|
||||
// whether it has to be signed in again, and when a Claude one's sign-in
|
||||
// ends.
|
||||
func signInStates() []signInState {
|
||||
ends := map[string]time.Time{}
|
||||
var out []signInState
|
||||
for _, p := range Accounts() {
|
||||
a := p.Account
|
||||
if a == nil || p.Off {
|
||||
continue
|
||||
}
|
||||
agent, isPlugin := a.Agent, false
|
||||
if _, ok := PluginOf(p.ID); ok && p.IsPlugin() {
|
||||
agent, isPlugin = p.ID, true // its accounts go by the provider's id
|
||||
}
|
||||
ls := Logins(agent)
|
||||
if agent == "claude" && len(ls) > 0 {
|
||||
for _, l := range sharedLogins() {
|
||||
if l.Agent == "claude" {
|
||||
if at, ok := claudeSignInEnds(l.Auth); ok {
|
||||
ends[strings.ToLower(l.User)] = at
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, l := range ls {
|
||||
if !l.On && !l.Active {
|
||||
continue // not in use: nothing is lost while it is signed out
|
||||
}
|
||||
s := signInState{Provider: p.ID, Name: p.Name, User: l.User, Gone: needsSignIn(a.Agent, isPlugin, l.Lapsed)}
|
||||
if at, ok := ends[strings.ToLower(l.User)]; ok && agent == "claude" && !s.Gone {
|
||||
s.EndsAt = &at
|
||||
}
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// dueSignIns is what the accounts ss call for, the marks in marks brought
|
||||
// up to date: an account that has to be signed in again, once until it is
|
||||
// usable again; a sign-in ending within claudeEndsWithin, once for that
|
||||
// end. An account not listed now (its plugin not started yet) keeps its
|
||||
// marks a while, so it isn't told of again when it is.
|
||||
func dueSignIns(ss []signInState, marks map[string]alertMark, now time.Time) []SignInAlert {
|
||||
var out []SignInAlert
|
||||
seen := map[string]bool{}
|
||||
for _, s := range ss {
|
||||
pre := s.Provider + "|" + strings.ToLower(s.User) + "|"
|
||||
gone, ends := pre+"gone", pre+"ends"
|
||||
seen[gone], seen[ends] = true, true
|
||||
if !s.Gone {
|
||||
delete(marks, gone)
|
||||
} else if _, ok := marks[gone]; !ok {
|
||||
marks[gone] = alertMark{At: now}
|
||||
out = append(out, SignInAlert{Provider: s.Provider, Name: s.Name, User: s.User, Kind: "lapsed"})
|
||||
}
|
||||
if s.EndsAt == nil || !s.EndsAt.After(now) || s.EndsAt.Sub(now) > claudeEndsWithin {
|
||||
if s.EndsAt == nil || s.EndsAt.Sub(now) > claudeEndsWithin {
|
||||
delete(marks, ends) // signed in anew, or not ending yet
|
||||
}
|
||||
continue
|
||||
}
|
||||
if m, ok := marks[ends]; ok && sameRun(m.Until, s.EndsAt) {
|
||||
continue
|
||||
}
|
||||
at := *s.EndsAt
|
||||
marks[ends] = alertMark{At: now, Until: &at}
|
||||
out = append(out, SignInAlert{Provider: s.Provider, Name: s.Name, User: s.User, Kind: "ending", EndsAt: &at})
|
||||
}
|
||||
for k, m := range marks {
|
||||
if !seen[k] && now.Sub(m.At) > alertKeep {
|
||||
delete(marks, k)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func signInAlertPath() string { return filepath.Join(filepath.Dir(Path()), "signin-alerts.json") }
|
||||
|
||||
// signInAlertMu keeps the file to one writer in this magpie.
|
||||
var signInAlertMu sync.Mutex
|
||||
|
||||
// checkSignIns says what the accounts ss call for, keeping the marks in
|
||||
// path.
|
||||
func checkSignIns(path string, ss []signInState, now time.Time) []SignInAlert {
|
||||
signInAlertMu.Lock()
|
||||
defer signInAlertMu.Unlock()
|
||||
marks := map[string]alertMark{}
|
||||
if b, err := os.ReadFile(path); err == nil {
|
||||
if json.Unmarshal(b, &marks) != nil {
|
||||
// unknown, not empty: a mark lost would say every lapse again
|
||||
return nil
|
||||
}
|
||||
} else if !os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
before, _ := json.Marshal(marks)
|
||||
out := dueSignIns(ss, marks, now)
|
||||
if after, _ := json.Marshal(marks); string(after) != string(before) {
|
||||
os.MkdirAll(filepath.Dir(path), 0o755)
|
||||
writePrivate(path, append(after, '\n'))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// signInAlertEvery is how often the accounts are looked at: a mark is set
|
||||
// as a request is refused, so this reads magpie's own files, asking no
|
||||
// vendor.
|
||||
const signInAlertEvery = 2 * time.Minute
|
||||
|
||||
// WatchSignIns hands send each sign-in alert due, a minute after it starts
|
||||
// and every signInAlertEvery after, until ctx ends.
|
||||
func WatchSignIns(ctx context.Context, send func(SignInAlert)) {
|
||||
t := time.NewTimer(time.Minute)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
}
|
||||
for _, a := range checkSignIns(signInAlertPath(), signInStates(), time.Now()) {
|
||||
log.Printf("sign-in alert: %s %s %s", a.Kind, a.Name, a.User)
|
||||
send(a)
|
||||
}
|
||||
t.Reset(signInAlertEvery)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// signInAlertsOf is what a look at the accounts calls for now, the marks
|
||||
// kept in path.
|
||||
func signInAlertsOf(t *testing.T, path string, now time.Time) []SignInAlert {
|
||||
t.Helper()
|
||||
forgetAccountCaches()
|
||||
return checkSignIns(path, signInStates(), now)
|
||||
}
|
||||
|
||||
// setSideClaude writes the saved Claude account side back as it is now.
|
||||
func setSideClaude(t *testing.T, side savedLogin) {
|
||||
t.Helper()
|
||||
writeFile(t, loginsPath(), []savedLogin{side})
|
||||
forgetAccountCaches()
|
||||
}
|
||||
|
||||
// A Claude account in use whose sign-in Claude Code couldn't use is told
|
||||
// of once, with the provider a click opens, and again only after it was
|
||||
// usable in between (steven_4422 on Discord: nothing said so outside the
|
||||
// account's row).
|
||||
func TestSignInAlertClaudeSignedOut(t *testing.T) {
|
||||
_, side := claudeAuthFixture(t, claudeAuthLapse)
|
||||
path := filepath.Join(t.TempDir(), "signin-alerts.json")
|
||||
now := time.Now()
|
||||
got := signInAlertsOf(t, path, now)
|
||||
if len(got) != 1 || got[0].Kind != "lapsed" || got[0].Provider != "claude" || got[0].User != side.User || got[0].Name == "" {
|
||||
t.Fatalf("alerts for a signed-out Claude account: %+v", got)
|
||||
}
|
||||
if got := signInAlertsOf(t, path, now.Add(5*time.Minute)); len(got) != 0 {
|
||||
t.Fatalf("told again: %+v", got)
|
||||
}
|
||||
side.Lapsed = ""
|
||||
setSideClaude(t, side)
|
||||
if got := signInAlertsOf(t, path, now.Add(10*time.Minute)); len(got) != 0 {
|
||||
t.Fatalf("a usable account told of: %+v", got)
|
||||
}
|
||||
side.Lapsed = claudeAuthLapse
|
||||
setSideClaude(t, side)
|
||||
if got := signInAlertsOf(t, path, now.Add(15*time.Minute)); len(got) != 1 || got[0].Kind != "lapsed" {
|
||||
t.Fatalf("signed out again, not told: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// An account not in use is told of nothing: no request goes to it.
|
||||
func TestSignInAlertSkipsAccountsNotInUse(t *testing.T) {
|
||||
_, side := claudeAuthFixture(t, claudeAuthLapse)
|
||||
side.On = false
|
||||
setSideClaude(t, side)
|
||||
if got := signInAlertsOf(t, filepath.Join(t.TempDir(), "a.json"), time.Now()); len(got) != 0 {
|
||||
t.Fatalf("an account not in use told of: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A Claude sign-in whose refresh token ends within three days is told of
|
||||
// once for that end, as Claude Code's own "Your login expires in N days"
|
||||
// is; one further off, or signed in anew, is not.
|
||||
func TestSignInAlertClaudeEnding(t *testing.T) {
|
||||
_, side := claudeAuthFixture(t, "")
|
||||
now := time.Now()
|
||||
creds := func(ends time.Time) {
|
||||
side.Auth = mustJSONRaw(t, map[string]any{"claudeAiOauth": map[string]any{
|
||||
"accessToken": "old-access", "refreshToken": "old-refresh",
|
||||
"expiresAt": now.Add(time.Hour).UnixMilli(), "refreshTokenExpiresAt": ends.UnixMilli(), "subscriptionType": "max",
|
||||
}})
|
||||
setSideClaude(t, side)
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "signin-alerts.json")
|
||||
creds(now.Add(20 * 24 * time.Hour))
|
||||
if got := signInAlertsOf(t, path, now); len(got) != 0 {
|
||||
t.Fatalf("told of a sign-in 20 days from its end: %+v", got)
|
||||
}
|
||||
ends := now.Add(2*24*time.Hour + time.Hour)
|
||||
creds(ends)
|
||||
got := signInAlertsOf(t, path, now)
|
||||
if len(got) != 1 || got[0].Kind != "ending" || got[0].User != side.User || got[0].EndsAt == nil || !got[0].EndsAt.Equal(time.UnixMilli(ends.UnixMilli())) {
|
||||
t.Fatalf("alerts for a sign-in ending in 2 days: %+v", got)
|
||||
}
|
||||
if got := signInAlertsOf(t, path, now.Add(time.Hour)); len(got) != 0 {
|
||||
t.Fatalf("told again of the same end: %+v", got)
|
||||
}
|
||||
creds(now.Add(30 * 24 * time.Hour)) // signed in anew
|
||||
if got := signInAlertsOf(t, path, now.Add(2*time.Hour)); len(got) != 0 {
|
||||
t.Fatalf("told of a new sign-in: %+v", got)
|
||||
}
|
||||
creds(now.Add(24 * time.Hour))
|
||||
if got := signInAlertsOf(t, path, now.Add(3*time.Hour)); len(got) != 1 || got[0].Kind != "ending" {
|
||||
t.Fatalf("the new sign-in's end not told: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Code's own rule: no end said, or an access token that outlasts
|
||||
// the refresh token's end by more than the warning, is no end to tell.
|
||||
func TestClaudeSignInEnds(t *testing.T) {
|
||||
now := time.Now()
|
||||
blob := func(o map[string]any) []byte {
|
||||
o["accessToken"], o["refreshToken"] = "a", "r"
|
||||
return mustJSONRaw(t, map[string]any{"claudeAiOauth": o})
|
||||
}
|
||||
if at, ok := claudeSignInEnds(blob(map[string]any{"expiresAt": now.Add(time.Hour).UnixMilli(), "refreshTokenExpiresAt": now.Add(48 * time.Hour).UnixMilli()})); !ok || at.UnixMilli() != now.Add(48*time.Hour).UnixMilli() {
|
||||
t.Fatalf("end not read: %v %v", at, ok)
|
||||
}
|
||||
if _, ok := claudeSignInEnds(blob(map[string]any{"expiresAt": now.Add(time.Hour).UnixMilli()})); ok {
|
||||
t.Fatal("an end read where none is said")
|
||||
}
|
||||
if _, ok := claudeSignInEnds(blob(map[string]any{"expiresAt": now.Add(10 * 24 * time.Hour).UnixMilli(), "refreshTokenExpiresAt": now.Add(time.Hour).UnixMilli()})); ok {
|
||||
t.Fatal("an end read under an access token that outlasts it")
|
||||
}
|
||||
}
|
||||
|
||||
// Every subscription whose row says Sign in again is told of, as the
|
||||
// Providers page decides it: a Claude, Copilot or plugin account's mark,
|
||||
// and a built-in's that says its sign-in is gone. A failure of the moment
|
||||
// is not.
|
||||
func TestSignInGoneAsThePageSaysIt(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
agent string
|
||||
plugin bool
|
||||
lapsed string
|
||||
want bool
|
||||
}{
|
||||
{"claude", false, claudeAuthLapse, true},
|
||||
{"copilot", false, copilotLapsedText, true},
|
||||
{"cursor", true, "Cursor refused this sign-in", true},
|
||||
{"codex", false, revokedText("a@example.com"), true},
|
||||
{"codex", false, "a@example.com's sign-in has expired — add the account again to use it", true},
|
||||
{"chatgpt-api", false, "a@example.com's ChatGPT sign-in has expired — sign in again", true},
|
||||
{"codex", false, "usage read failed: HTTP 502", false},
|
||||
{"codex", false, "", false},
|
||||
{"claude", false, "", false},
|
||||
} {
|
||||
if got := needsSignIn(c.agent, c.plugin, c.lapsed); got != c.want {
|
||||
t.Errorf("needsSignIn(%q, %v, %q) = %v", c.agent, c.plugin, c.lapsed, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The marks: one alert per account and kind, cleared when the account is
|
||||
// usable again; an account not listed keeps its marks a while, so a plugin
|
||||
// not started yet isn't told of again when it is.
|
||||
func TestDueSignIns(t *testing.T) {
|
||||
now := time.Now()
|
||||
marks := map[string]alertMark{}
|
||||
gone := []signInState{{Provider: "cursor", Name: "Cursor", User: "U@x", Gone: true}, {Provider: "codex", Name: "Codex", User: "b"}}
|
||||
if got := dueSignIns(gone, marks, now); len(got) != 1 || got[0].Provider != "cursor" {
|
||||
t.Fatalf("first look: %+v", got)
|
||||
}
|
||||
if got := dueSignIns(nil, marks, now.Add(time.Hour)); len(got) != 0 || len(marks) != 1 {
|
||||
t.Fatalf("not listed: %+v, marks %v", got, marks)
|
||||
}
|
||||
if got := dueSignIns(gone, marks, now.Add(2*time.Hour)); len(got) != 0 {
|
||||
t.Fatalf("told again after a look it wasn't listed in: %+v", got)
|
||||
}
|
||||
if dueSignIns(nil, marks, now.Add(alertKeep+3*time.Hour)); len(marks) != 0 {
|
||||
t.Fatalf("a mark kept past alertKeep: %v", marks)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user