Files
magpie/internal/usage/daystart_test.go
T
yetone 5241308212 settings: a day can start at 0 to 6 o'clock (ssharpcc on X)
Settings > Usage > "Day starts at" (dayStart, 0 to 6, default 0; also
magpie settings set dayStart 4). With 4, a call at 01:30 counts toward the
day before: the Usage page (summary, charts, heatmap, ledger day filter,
hour labels of Today) and the daily and monthly key caps (access.Window,
provider.SpendCap.Window).

internal/dayzone makes a time.Location whose midnight is the real day's
hour:00 (TZif built from the zone's own offsets, same DST instants), so
every place that cuts days by a Zone's midnight follows without edits;
usage.Zone returns it and the "now" reads use it too.

Run (mote): go test usage, settings, settingsapi, access, provider, gui,
dayzone, also with TZ=UTC and TZ=Pacific/Kiritimati; gofmt; go vet darwin,
linux nogui and windows; day-start, currency, number-units and i18n-keys
GUI files in chromium and webkit, en zh zh-TW ja de; TestCoreScriptBudget.
Broken by hand: TestDayStartsAfterMidnight red with 9 failures when the
hour is ignored. New tests: dayzone over 5 zones x hours 1/4/6 x two years
of days, usage, limit window, spend-cap window, day-start GUI.
Not tried: a real log history in a real window.
Not shifted, filed as #6314: the Sessions page, TUI sessions filter,
davsync shared per-day files, gateway_sessions day keys, credits_daily.
Rows are never stored in the shifted zone.
2026-11-12 18:31:50 +08:00

109 lines
3.4 KiB
Go

package usage
import (
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
func startAt(t *testing.T, hour int) {
t.Helper()
s := settings.Load()
s.DayStart = hour
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
}
// With a day that starts at 4 (ssharpcc on X, coding past 1am), a call at
// 01:30 is the day before's: not in Today, on the day before in the week's
// chart, the heatmap and a picked day, while one at 04:30 is today's, at
// hour 04. Without it, both are today's. Every clock is held, in a zone
// whatever this computer keeps; run under TZ=UTC and TZ=Pacific/Kiritimati.
func TestDayStartsAfterMidnight(t *testing.T) {
pageHome(t)
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
t.Skip(err)
}
late := time.Date(2026, 9, 30, 1, 30, 0, 0, loc)
early := time.Date(2026, 9, 30, 4, 30, 0, 0, loc)
now := time.Date(2026, 9, 30, 5, 0, 0, 0, loc)
holdClock(t, late)
Append(Record{Agent: "claude", Provider: "openai", Model: "m", Input: 10, Status: 200})
holdClock(t, early)
Append(Record{Agent: "claude", Provider: "openai", Model: "m", Input: 10, Status: 200})
holdClock(t, now)
run := func(hour int) {
startAt(t, hour)
v := Zone("Asia/Shanghai")
f := Filter{Zone: v}
wantToday := 2
if hour == 4 {
wantToday = 1
}
if got := SummarizeTopIn(Today, 10, v).Calls; got != wantToday {
t.Errorf("start %d: Summary Today has %d calls, want %d", hour, got, wantToday)
}
if got := QueryPage(Today, f, 0, 10).Total; got != wantToday {
t.Errorf("start %d: Requests Today has %d, want %d", hour, got, wantToday)
}
if got := len(LedgerOf(Today, f).Rows); got != wantToday {
t.Errorf("start %d: ledger Today has %d rows, want %d", hour, got, wantToday)
}
days := map[string]int{}
for _, d := range HeatmapOf(f).Days {
days[d.Date] = d.Calls
}
wantDays := map[string]int{"2026-09-30": 2}
if hour == 4 {
wantDays = map[string]int{"2026-09-29": 1, "2026-09-30": 1}
}
if len(days) != len(wantDays) || days["2026-09-29"] != wantDays["2026-09-29"] || days["2026-09-30"] != wantDays["2026-09-30"] {
t.Errorf("start %d: heatmap days %v, want %v", hour, days, wantDays)
}
for date, want := range wantDays {
if got := QueryPage(All, Filter{Zone: v, Day: date}, 0, 10).Total; got != want {
t.Errorf("start %d: day %s has %d calls, want %d", hour, date, got, want)
}
}
w := SummarizeTopIn(Week, 10, v)
byLabel := map[string]int{}
for _, p := range w.Series {
if p.Calls > 0 {
byLabel[p.Label] = p.Calls
}
}
wantWeek := map[string]int{"Sep 30": 2}
if hour == 4 {
wantWeek = map[string]int{"Sep 29": 1, "Sep 30": 1}
}
if len(byLabel) != len(wantWeek) || byLabel["Sep 29"] != wantWeek["Sep 29"] || byLabel["Sep 30"] != wantWeek["Sep 30"] {
t.Errorf("start %d: the week's chart has %v, want %v", hour, byLabel, wantWeek)
}
// Today's chart is a point an hour, labelled by the real hour of the day
var hours []string
for _, p := range w.Series {
_ = p
}
for _, p := range SummarizeTopIn(Today, 10, v).Series {
if p.Calls > 0 {
hours = append(hours, p.Label)
}
}
if hour == 4 && (len(hours) != 1 || hours[0] != "04") {
t.Errorf("start 4: Today's chart hours %v, want [04]", hours)
}
if hour == 4 {
if first := SummarizeTopIn(Today, 10, v).Series[0].Label; first != "04" {
t.Errorf("start 4: Today's chart begins at %q, want 04", first)
}
}
}
run(0)
run(4)
run(0)
}