tongzhenbao on X: a Codex Pro account with "when its week runs out" on,
its week at 0% left, one reset held until 11-06 (after the week's own
reset on 11-03) and Use credits on. Overnight ~10k credits went and the
reset stayed; the card said "not auto-used" beside the switch on.
Cause: the week's auto-use ran only on a vendor refusal. autoReset in
routing needs a 429 usage_limit_reached on the last candidate, and the
relay's autoResetSignedIn the same on its 429. An account let spend its
credits is never refused for its week: ChatGPT answers on the credits,
and routing never benches it (full() and onCredits). So the week's reset
could only ever be spent with credits off.
Decision (product call delegated): a reset is free, credits cost money,
so a reset the user set to go when the week runs out goes before any
credits. resetBeforeCredits runs before a try is sent to a Codex account
on its credits whose last reading has its week used up, with the week's
switch on; AutoUseCodexReset reads the week again and keeps one a week
at most, so a second time in the same week goes on the credits. Hooked
in the routing loop and in codexUpstreamOn (the relay, signed-in or an
account). With no reset held, or the week's switch off, nothing changes.
The card's summary: with only the week's switch on it now says "auto-used
when the week runs out" (zh 周额度用完时自动使用), not "not auto-used",
with its title in all five languages. CLI help and auto-reset messages
say "or before its credits are spent". gateway-routing.md names it.
Siblings: the expiring switch already spends regardless of credits
(usedUp ignores them); the credits-off path (planned.held) is unchanged.
Run (mote, through mote-run, at this tree):
- go test ./internal/gateway -run 'TestCodexResetBeforeCredits|TestCodexAutoReset|TestCodexCredits|TestCodexNoCredits': ok
- broken by hand: gateway.go and codex_backend.go at origin/main:
TestCodexResetBeforeCredits/relayed and /routed red (200, spent ""),
the two negative cases green
- node --test codex-auto-reset, i18n-keys, codex-auto-reset-settings,
codex-resets-used, panel-options-fold, quota-curve: 96/96 (chromium +
webkit, en/zh/zh-TW/ja/de); codex-auto-reset red with origin/main's
app.js ("· not auto-used")
- full suite, before the rebase onto 300b7eee0: wsl-run go test -timeout
45m ./... ok for internal/gateway, provider, settings, tui and the rest;
root, cli and gui don't build in WSL (no gtk4/webkitgtk-6.0), and
internal/agent's fake-binary tests (TestInstalledVersion and four more)
fail there with "Invalid argument"; on mote those packages are ok
(., cli 49s, agent 179s)
- at the pushed head 7b6a0675f (on 300b7eee0), on mote: internal/gui,
provider and cli; provider and cli ok, gui ok except TestMainWindowCorners,
red the same on a clean 300b7eee0 copy on mote (macOS 27), #3949;
TestCoreScriptBudget ok with no raise (app.js 1,311,588 of 1,311,735,
after shortening resetsWords' change); the focused gateway tests and the
six GUI files above again, ok and 96/96
Not tried: a real account with credits and a held reset at 0% week; the
owner's accounts have no reset at 0% to spend.
100 lines
4.0 KiB
Go
100 lines
4.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// tongzhenbao on X: a Codex account set to spend a reset by itself when
|
|
// its week runs out (that switch alone on), its week used up, holding a
|
|
// reset that runs out after the week starts again, and let spend its
|
|
// credits. ChatGPT answers it on its credits, so nothing refuses it as out
|
|
// of its allowance, and the reset was never spent while ~10k credits were.
|
|
// The reset is spent before the turn goes on the credits, once a week;
|
|
// with the week's switch off, the turn goes on the credits as before.
|
|
func TestCodexResetBeforeCredits(t *testing.T) {
|
|
reporter := func(t *testing.T, b *resetBackend, user, acct string) {
|
|
t.Helper()
|
|
// answers all the same, on its credits; the reset held runs out
|
|
// 20 days on, past the week's own reset 3 days on
|
|
b.credits[acct] = true
|
|
if err := provider.SetCodexAutoResetCase(user, provider.ResetCaseWeek, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w, e := provider.CodexAutoResetWhen(user); !w || e {
|
|
t.Fatalf("set week only: week %v expiring %v", w, e)
|
|
}
|
|
}
|
|
t.Run("relayed", func(t *testing.T) {
|
|
codexSignedIn(t)
|
|
b := newResetBackend(t, "acct-1")
|
|
creditsUsage(t, map[string]float64{"me@example.com": 100}, "me@example.com")
|
|
reporter(t, b, "Me@example.com", "acct-1")
|
|
srv := New()
|
|
code, body := resetPost(t, srv)
|
|
if tried, spent := b.seen(); code != 200 || !strings.Contains(body, "pong") || tried != "acct-1" || spent != "acct-1" {
|
|
t.Fatalf("%d %s tried %q spent %q", code, body, tried, spent)
|
|
}
|
|
if got := resetsTraced(srv); len(got) != 1 || got[0] != "me@example.com: 2 windows started again" {
|
|
t.Fatalf("traced %v", got)
|
|
}
|
|
if calls := srv.Recent(); len(calls) == 0 || !strings.Contains(calls[0].Fallback, "before its credits") {
|
|
t.Fatalf("request log %+v", calls)
|
|
}
|
|
// used up again in the same week: the credits go on, no second reset
|
|
b.mu.Lock()
|
|
b.out["acct-1"], b.week["acct-1"], b.held["acct-1"] = true, 100, 1
|
|
b.mu.Unlock()
|
|
if code, _ := resetPost(t, srv); code != 200 {
|
|
t.Fatalf("second time in the week: %d", code)
|
|
}
|
|
if _, spent := b.seen(); spent != "acct-1" {
|
|
t.Fatalf("second time in the week spent %q", spent)
|
|
}
|
|
})
|
|
t.Run("routed", func(t *testing.T) {
|
|
codexSignedIn(t, "spare@example.com")
|
|
b := newResetBackend(t, "acct-1", "acct-2")
|
|
b.held["acct-2"] = 0
|
|
// spare used up with no credits: benched, never asked
|
|
creditsUsage(t, map[string]float64{"me@example.com": 100, "spare@example.com": 100}, "me@example.com")
|
|
reporter(t, b, "me@example.com", "acct-1")
|
|
srv := New()
|
|
code, body := resetPost(t, srv)
|
|
if tried, spent := b.seen(); code != 200 || !strings.Contains(body, "pong") || tried != "acct-1" || spent != "acct-1" {
|
|
t.Fatalf("%d %s tried %q spent %q", code, body, tried, spent)
|
|
}
|
|
if got := resetsTraced(srv); len(got) != 1 || got[0] != "me@example.com: 2 windows started again" {
|
|
t.Fatalf("traced %v", got)
|
|
}
|
|
})
|
|
t.Run("week's switch off", func(t *testing.T) {
|
|
codexSignedIn(t)
|
|
b := newResetBackend(t, "acct-1")
|
|
b.credits["acct-1"] = true
|
|
creditsUsage(t, map[string]float64{"me@example.com": 100}, "me@example.com")
|
|
if err := provider.SetCodexAutoResetCase("me@example.com", provider.ResetCaseExpiring, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
code, _ := resetPost(t, New())
|
|
if tried, spent := b.seen(); code != 200 || tried != "acct-1" || spent != "" {
|
|
t.Fatalf("%d tried %q spent %q", code, tried, spent)
|
|
}
|
|
})
|
|
t.Run("week not used up", func(t *testing.T) {
|
|
codexSignedIn(t)
|
|
b := newResetBackend(t)
|
|
b.week["acct-1"], b.held["acct-1"], b.credits["acct-1"] = 60, 1, true
|
|
creditsUsage(t, map[string]float64{"me@example.com": 60}, "me@example.com")
|
|
if err := provider.SetCodexAutoResetCase("me@example.com", provider.ResetCaseWeek, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
code, _ := resetPost(t, New())
|
|
if tried, spent := b.seen(); code != 200 || tried != "acct-1" || spent != "" {
|
|
t.Fatalf("%d tried %q spent %q", code, tried, spent)
|
|
}
|
|
})
|
|
}
|