Files
magpie/internal/settings/warmats_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

61 lines
2.1 KiB
Go

package settings
import (
"encoding/json"
"os"
"slices"
"testing"
)
// #1260 (Sinnhu): a daily warm-up has several times. A settings file from
// before them, with the one codexWarmAt, has that one; the list is kept
// as 06:00, earliest first, once each, and its first written as
// codexWarmAt for an older magpie.
func TestWarmAtsMigrate(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := os.MkdirAll(Dir(), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(Path(), []byte(`{"codexWarmAt":"6:00","claudeWarmAt":"21:30"}`), 0o644); err != nil {
t.Fatal(err)
}
s := Load()
if !slices.Equal(s.CodexWarmAts, []string{"06:00"}) || s.CodexWarmAt != "06:00" || !slices.Equal(s.ClaudeWarmAts, []string{"21:30"}) {
t.Fatalf("old file: %q %q %q", s.CodexWarmAts, s.CodexWarmAt, s.ClaudeWarmAts)
}
if err := Save(s); err != nil {
t.Fatal(err)
}
if s = Load(); !slices.Equal(s.CodexWarmAts, []string{"06:00"}) || s.ClaudeWarmAt != "21:30" {
t.Fatalf("saved as read: %q %q", s.CodexWarmAts, s.ClaudeWarmAt)
}
// several, however given; the page still sends the first it was drawn with
s.CodexWarmAts = []string{"19:10", " 9:00", "15:05:00", "09:00", ""}
if err := Save(s); err != nil {
t.Fatal(err)
}
if s = Load(); !slices.Equal(s.CodexWarmAts, []string{"09:00", "15:05", "19:10"}) || s.CodexWarmAt != "09:00" {
t.Fatalf("several: %q %q", s.CodexWarmAts, s.CodexWarmAt)
}
// what an older magpie reads of the file: the day's first
b, _ := os.ReadFile(Path())
var old struct {
CodexWarmAt string `json:"codexWarmAt"`
}
if json.Unmarshal(b, &old) != nil || old.CodexWarmAt != "09:00" {
t.Fatalf("first not kept for an older magpie:\n%s", b)
}
// one that isn't a time is refused, wherever it is in the list
if Save(Settings{CodexWarmAts: []string{"09:00", "25:00"}}) == nil {
t.Fatal("bad time in the list accepted")
}
// all removed, though the page still sends the first it was drawn with
s.CodexWarmAts, s.CodexWarmAt = []string{}, "09:00"
if err := Save(s); err != nil {
t.Fatal(err)
}
if s = Load(); len(s.CodexWarmAts) != 0 || s.CodexWarmAt != "" {
t.Fatalf("off: %q %q", s.CodexWarmAts, s.CodexWarmAt)
}
}