Files
magpie/internal/cli/quota_reset_cli_test.go
T
yetone 323159f1cc usage: a Codex reset's confirmation picks which of the account's resets is used (01huadalang)
01huadalang on Discord: resets were used only automatically, or by hand
the one that runs out first. Which account was already the user's to pick
(each account's card has its own Use); which of its resets was not.

- Usage card's "Use a reset" and the menu bar panel's "Use one...": when
  the account holds two or more resets listed with their ids, the
  confirmation has a "Reset to use" pop-up (the app's menu, not <select>),
  soonest first and picked unless another is. Once another is picked it
  names the account and that reset ("The one used is me@'s reset that
  runs out ..."), and says the sooner one is kept and lost if unused.
  Escape closes the open menu first, not the confirmation.
- POST /api/usage/codex-reset takes an optional credit. The server reads
  the account's resets again and spends that credit only if it is still
  held; a gone one is refused and no other is spent in its place. With no
  credit it is as before (the soonest). Auto-use is unchanged.
- ResetCard carries the credit id; `magpie quota reset --card n` is the
  CLI's sibling, its prompt naming the reset.
- askReset's dialog moved out to resetask.js, imported when Use is
  pressed (like signsheets.js), so the core's scripts are 1,530,733 bytes,
  under the 1,533,105 budget with no raise (the pick alone took it 2,068
  over). Five keys in zh, zh-TW, ja, de.

Run on mote (mote-run), at the base 2597c9636:
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... ok.
- go test -tags nogui ./internal/provider ./internal/cli ok;
  ./internal/gui: only TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestColdTurnsTaken (#4510) red, both filed and the same on main;
  TestCoreScriptBudget ok after the move.
- GUI, Chromium and WebKit, 78/78 each: reset-pick-card (new: en, zh,
  zh-TW, ja, de; Usage at 900 and 390px, panel at 440px), codex-resets-used,
  account-every-reset, codex-reset-lead, reset-each-title, reset-providers,
  reset-reminder, panel-usage, old-webkit, i18n-keys, codex-auto-reset,
  codex-auto-reset-settings, warm-reset-asleep, quota-credits,
  usage-accounts.
- Red on the old code: reset-pick-card against origin/main's assets fails
  at "an account with three resets offers the pick"; TestUseCodexResetCard
  and TestCodexResetsCounted fail without the change.
- Broken by hand, each red then restored: the chosen credit ignored
  (spent [soon]); the held check skipped (a used credit spent); ids dropped
  from each; the credit left out of the POST; the Escape guard removed.

Again at the pushed head (rebased on 88372a39d): go vet ./... ok;
provider, cli ok; gui only #4514 red (TestColdTurnsTaken passed this
time); reset-pick-card, old-webkit, i18n-keys in Chromium 15/15.

Not run: the full go test ./... and the whole GUI suite. mote had two to
three other full Go suites and several whole GUI suites running the whole
time (load 120 to 250), and the WSL box too; one suite per machine.
Not tried: a real account's reset spent (none was, by design: fakes only),
so that Codex's consume takes a credit_id other than the soonest is
inferred from it being the same field the soonest is sent in.
2026-11-01 04:51:55 +08:00

112 lines
3.7 KiB
Go

package cli
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// #2008: `magpie provider quota-rest <id> until 00:00+08:00` sets the time
// of day the allowance comes back, a number of minutes after it takes its
// place, and default lets both go.
func TestQuotaRestUntil(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "xk", Name: "xk", Chat: "https://example.invalid/v1", Key: "k"}); err != nil {
t.Fatal(err)
}
find := func() provider.Provider {
p, err := provider.Find("xk")
if err != nil {
t.Fatal(err)
}
return *p
}
if err := quotaRestCmd([]string{"xk", "until", "00:00+08:00"}); err != nil {
t.Fatal(err)
}
if p := find(); p.QuotaResetAt != "00:00+08:00" {
t.Fatalf("until: %q", p.QuotaResetAt)
}
if err := quotaRestCmd([]string{"xk", "until", "25:00"}); err == nil {
t.Fatal("25:00 taken")
}
if err := quotaRestCmd([]string{"xk", "6h"}); err != nil {
t.Fatal(err)
}
if p := find(); p.QuotaResetAt != "" || p.QuotaRest != 360 {
t.Fatalf("6h: %q %d", p.QuotaResetAt, p.QuotaRest)
}
if err := quotaRestCmd([]string{"xk", "at", "8:00"}); err != nil {
t.Fatal(err)
}
if err := quotaRestCmd([]string{"xk", "default"}); err != nil {
t.Fatal(err)
}
if p := find(); p.QuotaResetAt != "" || p.QuotaRest != 0 {
t.Fatalf("default: %q %d", p.QuotaResetAt, p.QuotaRest)
}
}
// #2008: `magpie provider rest-cap <id> 4h` sets where its keys' rests stop
// doubling, and default gives it back to the gateway's own.
func TestRestCapCmd(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "xk", Name: "xk", Chat: "https://example.invalid/v1", Key: "k"}); err != nil {
t.Fatal(err)
}
cap := func() int {
p, err := provider.Find("xk")
if err != nil {
t.Fatal(err)
}
return p.RestCap
}
if err := restCapCmd([]string{"xk", "4h"}); err != nil || cap() != 240 {
t.Fatalf("4h: %v %d", err, cap())
}
if err := restCapCmd([]string{"xk", "9d"}); err == nil || cap() != 240 {
t.Fatalf("9d taken: %d", cap())
}
if err := restCapCmd([]string{"xk", "default"}); err != nil || cap() != 0 {
t.Fatalf("default: %v %d", err, cap())
}
// Kismet Alan on Discord: a relay whose unknown errors pass is set
// never to rest after one, saved and read back so; a minute is the
// shortest cap, and under it is turned away, not read as default
if err := restCapCmd([]string{"xk", "off"}); err != nil || cap() != provider.RestCapOff {
t.Fatalf("off: %v %d", err, cap())
}
if p, _ := provider.Find("xk"); !p.NoFailRest() {
t.Fatal("off not read back as no rest")
}
if err := restCapCmd([]string{"xk", "1m"}); err != nil || cap() != 1 {
t.Fatalf("1m: %v %d", err, cap())
}
if err := restCapCmd([]string{"xk", "30s"}); err == nil || cap() != 1 {
t.Fatalf("30s taken: %v %d", err, cap())
}
}
// `magpie quota reset --card <n>` picks which of the account's resets is
// spent (01huadalang on Discord); a card that isn't a place among them, or
// a flag it doesn't know, is turned away before any account is read, never
// taken for an account's name.
func TestQuotaResetCardArgs(t *testing.T) {
for _, args := range [][]string{{"--card"}, {"--card", "0"}, {"--card", "two"}, {"me@example.com", "--card", "-1"}, {"--cards", "2"}} {
if err := quotaResetCmd(args); err == nil {
t.Fatalf("%q taken", args)
}
}
for n, want := range map[int]string{1: "1st", 2: "2nd", 3: "3rd", 4: "4th", 11: "11th", 12: "12th", 13: "13th", 21: "21st", 22: "22nd", 101: "101st", 111: "111th"} {
if got := ordinal(n); got != want {
t.Fatalf("ordinal(%d) = %q, want %q", n, got, want)
}
}
}