Files
magpie/internal/provider/warmup_own_test.go
T
yetone 0506ea396f provider, settings, gui: Daily warm-up takes several times a day, each starting the 5-hour windows once (#1260)
Sinnhu asked for more than one daily warm-up time (e.g. 09:00, 15:05,
19:10). Settings has codexWarmAts / claudeWarmAts beside the old single
keys: the list wins when set (an empty one is off), a file from before
it reads its one time as a list of one, and the first time is written
back to codexWarmAt / claudeWarmAt so an older magpie still reads one.
Times are kept as HH:MM, sorted, once each; a bad one is refused.

The scheduler (dayStart, nextDayStart, dailyDue, heldForDay,
dayStartPassed, warmLook) takes the list: the start due is the latest
of any time that has passed, and the day kept per window is now that
start ("2026-09-28 15:05"), so each time sends once a day. A day kept by
an older magpie ("2026-09-28") counts as every start of that day had,
so nothing is sent twice on the day of the update. An account's own
time (codexWarmAtOf) stays one time.

Settings › Allowances › Daily warm-up: each time is a time field with ×
beside it while another is left, + adds one a window after the last,
Off/On as before. The times wrap in a group of their own beside + and
Off/On, so adding or removing one never moves the button pressed. New
strings in zh, en, zh-TW, ja, de.

Verified:
- go test -tags nogui ./internal/provider ./internal/settings
  ./internal/gui (temp HOME, pinned caches, pipefail): ok. go build and
  go vet -tags nogui ./...; GOOS=linux and GOOS=windows go vet -tags
  nogui on the three packages: clean.
- Fails without the change: TestWarmAtSeveralTimes (dayStart looking at
  the first time only, or a day-only key: "sent at 06:00"),
  TestWarmLookAndHoldAtSeveralTimes ("15:05 not looked at"),
  TestWarmAtKeptDayFromBefore and TestDailyDue (an old day-only record
  not honoured: "sent again"), TestWarmAtsMigrate (old key not read:
  "old file: [] ..."; first time not written back: "old file:
  ["06:00"] "" ...").
- GUI: warm-at-times.test.cjs, Chromium and WebKit, en/zh/ja/de, 900
  and 440px: 16/16; with warm-at-own, settings-groups, gui-ja, gui-de,
  gui-zh-tw, i18n-keys: 36/36 in each engine.
  Whole GUI suite (node --test, concurrency 4): 3716/3724; the 8 red
  (agent-models-redraw, azure-endpoint, context-window,
  gateway-key-limit, login-import, member-model-setting, search-api)
  each pass run alone with this change, and none reaches the warm-up
  row.

Not tried with real accounts or a live day of the scheduler.
2026-10-08 16:05:15 +08:00

113 lines
3.8 KiB
Go

package provider
import (
"context"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
// #957 (Evan26Ma): two Plus accounts started at one daily time run out
// together; each can have its own, so one takes over as the other's
// window ends. An account with none follows the time for all, and one set
// off is never started at a time of day.
func TestWarmAtEachAccountsOwn(t *testing.T) {
path := filepath.Join(t.TempDir(), "codex-warmup.json")
f := &fakeWarm{now: at8(28, 6, 0), errs: map[string]error{}}
idleAll := func() {
for _, u := range []string{"a@example.com", "b@example.com", "c@example.com"} {
f.ws[u] = []QuotaWindow{win("5 hours", fiveHours, 0, f.now.Add(fiveHours))}
}
}
f.ws = map[string][]QuotaWindow{}
idleAll()
own := map[string]string{"a@example.com": "09:00", "c@example.com": ""}
run := func(at string) []string {
t.Helper()
f.sent = nil
w := f.warmer(path)
w.ownAt = func() map[string]string { return own }
w.warmNow(context.Background(), "", times(at))
return f.sent
}
// six: b, which follows the time for all; a waits for its nine, c for none
if got := strings.Join(run("06:00"), ","); got != "b@example.com" {
t.Fatalf("at six sent %q", got)
}
// nine: a, as its own name is kept in any case
f.now = at8(28, 9, 0)
idleAll()
own = map[string]string{"a@example.com": "09:00", "c@example.com": ""}
f.ws["A@Example.com"] = f.ws["a@example.com"]
delete(f.ws, "a@example.com")
if got := strings.Join(run("06:00"), ","); got != "A@Example.com" {
t.Fatalf("at nine sent %q", got)
}
// the time for all off: an account's own still starts it, the rest none
f.now = at8(29, 9, 0)
idleAll()
delete(f.ws, "a@example.com")
if got := strings.Join(run(""), ","); got != "A@Example.com" {
t.Fatalf("the next day, the time for all off, sent %q", got)
}
}
// keepWarm looks at the windows when an account's own time comes, even
// with nothing else on, and never with nothing on at all.
func TestWarmLookAtAnAccountsOwnTime(t *testing.T) {
last, now := at8(28, 8, 59), at8(28, 9, 0)
if !warmLook("", times(""), map[string]string{"a@x": "09:00"}, false, last, now) {
t.Error("an account's own nine not looked at")
}
if warmLook("", times(""), map[string]string{"a@x": "10:00"}, false, last, now) {
t.Error("looked at before its time")
}
if warmLook("", times(""), map[string]string{"a@x": ""}, true, time.Time{}, now) {
t.Error("looked at with nothing on")
}
if !warmLook("", times("06:00"), nil, false, time.Time{}, now) || !warmLook("week", times(""), nil, false, now.Add(-codexWarmEvery), now) {
t.Error("the time for all, or the warm-up on reset, not looked at")
}
}
// An account's own time is kept by its name in lower case, as 06:00
// whichever way it came; "" follows the time for all again; one that isn't
// a time is refused.
func TestSetCodexWarmAt(t *testing.T) {
azureHome(t)
if err := SetCodexWarmAt("A@Example.com ", "9:00"); err != nil {
t.Fatal(err)
}
if err := SetCodexWarmAt("b@example.com", "OFF"); err != nil {
t.Fatal(err)
}
if at, ok := CodexWarmAtOf("a@example.com"); !ok || at != "09:00" {
t.Fatalf("a: %q %v", at, ok)
}
got := codexWarmAtOf()
if len(got) != 2 || got["a@example.com"] != "09:00" || got["b@example.com"] != "" {
t.Fatalf("own times %v", got)
}
if err := SetCodexWarmAt("a@example.com", "nine"); err == nil {
t.Fatal("nine taken")
}
if err := SetCodexWarmAt("a@example.com", ""); err != nil {
t.Fatal(err)
}
if _, ok := CodexWarmAtOf("a@example.com"); ok {
t.Fatal("still its own")
}
if err := SetCodexWarmAt("b@example.com", ""); err != nil {
t.Fatal(err)
}
if s := settings.Load(); s.CodexWarmAtOf != nil {
t.Fatalf("kept %v", s.CodexWarmAtOf)
}
if err := SetCodexWarmAt(" ", "06:00"); err == nil {
t.Fatal("no account taken")
}
}