Files
magpie/internal/gateway/cap_codex_fallback_test.go
yetone b942a872ba caps: a lone capped Codex account says its turns go to the fallbacks (not_4O4 on X)
not_4O4 asked for a way to switch to non-official models once a set share
of the ChatGPT quota is used. magpie already does this: a usage cap on the
Codex account (or a cap on the 5-hour / weekly window alone) plus the
provider's Fallback. When Codex is connected to magpie beside its ChatGPT
sign-in, or fails over through it, codexAccounts sends a capped lone
account's own-model turn through serve, so the fallbacks answer it.

The GUI said otherwise. The account row of a lone Codex account said
"Codex still uses it" past the cap even when Codex was connected, and the
cap note said requests are refused when the provider has fallbacks. Now:
- the direct-use note shows for Codex only when it isn't connected or
  failing over (an agent row with wired or failover);
- the held note says the requests go to this provider's fallbacks when it
  has some and Stop at the caps is off, in en, zh, zh-TW, ja, de.
- TestCoreScriptBudget: +236 bytes for this, packed to its shortest form
  with no page to move it to (the account rows are core).
The 11-04 grey Send default and the takeover are untouched: a cap never
takes Codex off ChatGPT.

New tests:
- internal/gateway TestCappedLoneCodexAccountGoesToItsFallback: a lone
  account capped at 80% with usage at 85 is not tried and the fallback
  answers (stream and not); at 50 the account is tried.
- account-cap.test.cjs: Codex connected, the lone capped row's note ends
  with the fallback line and has no direct note; not connected, the
  direct note stays. Chromium and WebKit, every language.

Run (mica, mote was over its load), at the rebased head
(origin/main c2a1d80fc):
- GUI before the rebase: account-cap, i18n-keys, i18n-en-no-cjk,
  window-cap, window-cap-pool, key-usage-cap, account-row-line,
  codex-app-menu-cap, routing-steps-cap: 235 of 236 ok. At the rebased
  head: account-cap, i18n-keys, i18n-en-no-cjk, window-cap,
  account-row-line again: 165 of 166 ok, the same #6422 red; go test -run Budget ./internal/gui ok. The red is i18n-keys "a plugin's
  words are its own" ("No folder" in memory_mcp.go, from 72facf72e),
  filed as #6422; this change touches neither file.
- go test -run 'Cap|Codex.*Fallback|Fallback' ./internal/gateway: ok.
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows for
  internal/gateway and internal/gui: clean.

Broken by hand:
- `if false &&` before the HoldCaps check in codexAccounts: the Go test
  went red (acct-1 tried, fallback 0).
- the old `direct` and the fallback flag forced false: the 10 connected GUI
  cases went red in both engines; the not-connected ones stayed green.

Not tried: a real ChatGPT account reaching its cap with the real Codex
CLI or app; the full suite runs after the tag.
2026-11-13 13:02:05 +08:00

57 lines
2.1 KiB
Go

package gateway
import (
"fmt"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// not_4O4 on X: the Codex app, signed in to its one ChatGPT account,
// should stop spending the subscription at a share of its windows and go
// to another vendor's model. That is the account's usage cap with a
// fallback set on the Codex subscription: past the cap, Codex's own model
// (asked bare, on Codex's sign-in) is served by the fallback, and nothing
// reaches ChatGPT; under it, the account answers as ever.
func TestCappedLoneCodexAccountGoesToItsFallback(t *testing.T) {
for _, stream := range []bool{true, false} {
t.Run(fmt.Sprint(stream), func(t *testing.T) {
codexSignedIn(t)
b := newResetBackend(t)
if err := provider.SetAccountCap("codex", "me@example.com", 80); err != nil {
t.Fatal(err)
}
a := &scripted{replies: []reply{{200, "", chatSSEReply("OK")}, {200, "", chatSSEReply("OK")}}}
scriptedOn(t, "a", provider.Chat, a)
p, _ := provider.Find("codex")
p.Fallback = []string{"a/m"}
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
post := func() *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(fmt.Sprintf(`{"model":"gpt-5.5","stream":%v,"input":"hi"}`, stream)))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("chatgpt-account-id", "acct-1")
New().Handler().ServeHTTP(rec, req)
return rec
}
capUsage(t, map[string]float64{"me@example.com": 85})
rec := post()
if tried, _ := b.seen(); rec.Code != 200 || tried != "" || a.n != 1 {
t.Fatalf("past the cap: %d %s (codex %q, fallback %d)", rec.Code, rec.Body.String(), tried, a.n)
}
capUsage(t, map[string]float64{"me@example.com": 50})
sticks.Lock()
sticks.m = map[string]stick{}
sticks.Unlock()
rec = post()
if tried, _ := b.seen(); rec.Code != 200 || tried != "acct-1" || a.n != 1 {
t.Fatalf("under the cap: %d %s (codex %q, fallback %d)", rec.Code, rec.Body.String(), tried, a.n)
}
})
}
}