nb5p has two ChatGPT accounts in magpie, B first. The Codex app showed A, and once A ran out Send was grey for every model, magpie's and another provider's too. Cause: magpie's switch back to the first account (or to the account Codex is kept signed in to) leaves the Codex app's app-server running unless "Restart the Codex app to follow" is on. The app-server keeps the account it started on (CodexAppStale), so the app stayed on A while auth.json said B. magpie read only B, which had room, so nothing moved, magpie never took over, and the app-server was never restarted: the app greyed Send on A's allowed false for good. Fix: on each full look of KeepOnAnAccountWithRoom, restartCodexAppLeftOnSpent reads the account the app is left on, and once that is held (codexHeld) marks a spent switch (codexAppSwitched true), which codexAppStep restarts past the RestartApp tick, as a switch off a used-up account already does. Once per app-server left. Run (mote, at this tree): - go test ./internal/provider -count=1: ok - TestCodexAppLeftOnASpentAccountIsRestarted: user switches to A, magpie switches back to B and leaves the app running, A still has room (nothing ended), A runs out with B's room (app-server 7 ended), next look (not ended again). Broken by hand: - codexAppSwitched(true, false) commented out: red, "ended [], want the app-server left on the used-up account". - the once-per-app-server check off: red, "restarted again: [7]". Not tried: the real Codex app on a real account running out; the call in KeepOnAnAccountWithRoom's loop is covered by reading, not by a test.
124 lines
3.7 KiB
Go
124 lines
3.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// magpie switched Codex back to B, its first account, and left the Codex
|
|
// app running (Restart the Codex app to follow is off), so the app's
|
|
// app-server stayed on A, the account it started on. Once A ran out the
|
|
// app greyed Send for every model while B, the one Codex is signed in to,
|
|
// had room: the app-server is restarted then, once, as a switch off a
|
|
// used-up account restarts it (nb5p on Discord).
|
|
func TestCodexAppLeftOnASpentAccountIsRestarted(t *testing.T) {
|
|
home := signIn(t) // me@example.com, acct-1: A
|
|
rememberLogins(true)
|
|
codexSignIn(t, home, "b@example.com", "r-b")
|
|
rememberLogins(true)
|
|
for _, u := range []string{"me@example.com", "b@example.com"} {
|
|
if err := SetLoginOn("codex", u, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var mu sync.Mutex
|
|
held := map[string]bool{}
|
|
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
out := held[r.Header.Get("chatgpt-account-id")]
|
|
mu.Unlock()
|
|
used := 10
|
|
if out {
|
|
used = 100
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"plan_type": "plus", "rate_limit": map[string]any{
|
|
"allowed": !out, "limit_reached": out,
|
|
"primary_window": map[string]any{"used_percent": used, "limit_window_seconds": 18000}}})
|
|
}))
|
|
defer fake.Close()
|
|
old := CodexBase
|
|
CodexBase = fake.URL + "/backend-api/codex"
|
|
t.Cleanup(func() { CodexBase = old })
|
|
fresh := func() {
|
|
loginUsageCache.Lock()
|
|
loginUsageCache.m = nil
|
|
loginUsageCache.Unlock()
|
|
}
|
|
|
|
s := stubCodexApp(t)
|
|
codexRestartsApp = func() bool { return false } // as it ships
|
|
s.running = []int{7}
|
|
now := time.Now()
|
|
idle := func() (int, time.Time) { return 0, now.Add(-time.Minute) }
|
|
step := func() []int { return codexAppStep(context.Background(), now, idle) }
|
|
t.Cleanup(func() {
|
|
DismissCodexApp()
|
|
codexDaemonMu.Lock()
|
|
codexAppLeftRestarted = leftDaemon{}
|
|
codexDaemonMu.Unlock()
|
|
})
|
|
// the user signs Codex in to A, and the app restarts on it
|
|
if err := SwitchLogin("codex", "me@example.com"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
step()
|
|
s.ended, s.running = nil, []int{7}
|
|
codexApp.Lock()
|
|
codexApp.stale, codexApp.ended = nil, nil
|
|
codexApp.Unlock()
|
|
|
|
// magpie signs Codex back in to B with A not spent: the app is left
|
|
// running, on A
|
|
if err := switchLoginBy("codex", "b@example.com", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := step(); got != nil {
|
|
t.Fatalf("magpie's own switch back ended %v with RestartApp off", got)
|
|
}
|
|
codexDaemonMu.Lock()
|
|
codexAppLeft = leftDaemon{pid: 7, user: "me@example.com"}
|
|
codexDaemonChecked = time.Now().Add(time.Hour) // what the listing would find
|
|
codexDaemonMu.Unlock()
|
|
if was := CodexAppStale(); was != "me@example.com" {
|
|
t.Fatalf("the app is said to be on %q", was)
|
|
}
|
|
|
|
// A still has room: the app is left as it is
|
|
fresh()
|
|
restartCodexAppLeftOnSpent(context.Background())
|
|
if got := step(); got != nil {
|
|
t.Fatalf("ended %v with the app's account not spent", got)
|
|
}
|
|
|
|
// A runs out: B, the one Codex is signed in to, still has room
|
|
mu.Lock()
|
|
held["acct-1"] = true
|
|
mu.Unlock()
|
|
fresh()
|
|
if CodexUsedUp(context.Background()) {
|
|
t.Fatal("B taken for used up")
|
|
}
|
|
restartCodexAppLeftOnSpent(context.Background())
|
|
if got := step(); !slices.Equal(got, []int{7}) {
|
|
t.Fatalf("ended %v, want the app-server left on the used-up account", got)
|
|
}
|
|
|
|
// the same app-server is asked to restart once, not on every look
|
|
s.ended = nil
|
|
s.running = []int{7}
|
|
codexApp.Lock()
|
|
codexApp.stale, codexApp.ended = nil, nil
|
|
codexApp.Unlock()
|
|
fresh()
|
|
restartCodexAppLeftOnSpent(context.Background())
|
|
if got := step(); got != nil {
|
|
t.Fatalf("restarted again: %v", got)
|
|
}
|
|
}
|