Files
magpie/internal/usage/period_range_test.go
T
yetone 6d3bc9183d usage: the Usage and Requests pages count any days picked, from a Custom opt after All that opens a month to pick them on (#1492, nianlee-official)
The period bar had only Today, 7 days, 30 days and All. A Custom opt now
follows them. It opens the app's own menu (no native select): Yesterday,
This month, Last month and 90 days a click away, and a month of days to
pick the first day on, then the last. Days after today can't be picked;
arrows walk the days and Escape closes. Once picked, the opt names the
days ("Oct 1 – Oct 7"), and narrow windows show only its icon until then.

The backend takes the days as period "2026-10-01..2026-10-07", both
included, in the local time zone (usage.RangeOf, Period.Known/Until).
The summary, the Requests page (index and ledger fallback), Direct and the
CSV name count only calls from the first day's midnight up to the
midnight after the last. The chart is an hour each for one day, a day each
up to 60 days, and a week each from a Monday past that. A picked range is
summarised afresh each time, so ranges never pile up in the summary cache.
The credits-per-day card follows the days picked. New strings in zh,
zh-TW, ja and de.

Run:
- go test -tags nogui ./internal/usage ./internal/gui (sandbox HOME): ok.
- TestPickedDaysArePeriod with Until returning zero (no range end):
  FAIL, Summarize 5 calls / QueryPage 5 rows / LedgerOf 5 rows, want 3.
- usage-range.test.cjs, Chromium and WebKit, en and zh, at 390px: 4/4.
- Every GUI test touching period or the Usage view (87 files): Chromium
  388/388; WebKit 386/388, the two purpose-filter's Routing focus check
  (scrollTop off by 1px after a wheel), which then passed 52 of 56 lone
  runs here and 25 of 25 on origin/main; nothing in this change runs on
  the Routing page. gui-de, gui-ja, gui-zh-tw, i18n-keys: pass.
- gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui on both pkgs.
2026-10-10 19:15:12 +08:00

64 lines
2.6 KiB
Go

package usage
import (
"testing"
"time"
)
// Days the reader picked (#1492, nianlee-official) are a period of their own:
// the Usage page, the Requests page, the ledger and Direct count the calls of
// those days and nothing after the last, under a chart of those days, an hour
// each for one day and a week each past 60 days.
func TestPickedDaysArePeriod(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 10, 10, 12, 0, 0, 0, time.Local))
day := func(m time.Month, d, h int) time.Time { return time.Date(2026, m, d, h, 0, 0, 0, time.Local) }
for _, at := range []time.Time{
day(9, 30, 23), // the day before
day(10, 1, 0), // the first day's first hour
day(10, 2, 9),
day(10, 3, 23), // the last day's last hour
day(10, 4, 0), // the day after
day(10, 9, 12),
} {
Append(Record{Time: at, Agent: "claude", Provider: "openai", Model: "m", Input: 10, Status: 200})
}
p, ok := RangeOf("2026-10-03", "2026-10-01") // either order
if !ok || p != "2026-10-01..2026-10-03" || !p.Known() {
t.Fatalf("RangeOf: %q %v", p, ok)
}
if !p.Since(now).Equal(day(10, 1, 0)) || !p.Until(now).Equal(day(10, 4, 0)) {
t.Errorf("%s: from %v until %v", p, p.Since(now), p.Until(now))
}
s := Summarize(p)
if s.Calls != 3 || s.Period != p || s.Bucket != "day" || len(s.Series) != 3 || !s.Series[0].Time.Equal(day(10, 1, 0)) {
t.Errorf("Summarize(%s): %d calls, period %q, %d %s points; want 3 calls in 3 days", p, s.Calls, s.Period, len(s.Series), s.Bucket)
}
if pg := QueryPage(p, Filter{}, 0, 10); pg.Total != 3 {
t.Errorf("QueryPage(%s): %d rows, want 3", p, pg.Total)
}
if l := LedgerOf(p, Filter{}); len(l.Rows) != 3 {
t.Errorf("LedgerOf(%s): %d rows, want 3", p, len(l.Rows))
}
if d := Direct(p); d.Period != p || !d.Since.Equal(day(10, 1, 0)) {
t.Errorf("Direct(%s): period %q since %v", p, d.Period, d.Since)
}
// one day: its hours
one, _ := RangeOf("2026-10-09", "2026-10-09")
if s := Summarize(one); s.Calls != 1 || s.Bucket != "hour" || len(s.Series) != 24 {
t.Errorf("Summarize(%s): %d calls, %d %s points; want 1 in 24 hours", one, s.Calls, len(s.Series), s.Bucket)
}
// past 60 days: weeks from a Monday, and still none after the last day
long, _ := RangeOf("2026-07-01", "2026-10-01")
if s := Summarize(long); s.Calls != 2 || s.Bucket != "week" || s.Series[0].Time.Weekday() != time.Monday {
t.Errorf("Summarize(%s): %d calls in %s points from %v; want 2 in weeks from a Monday", long, s.Calls, s.Bucket, s.Since)
}
for _, bad := range []Period{"2026-10-03..2026-10-01", "2026-13-01..2026-13-02", "x..y", "90d"} {
if bad.Known() {
t.Errorf("%q is known", bad)
}
}
}