Files
magpie/internal/cli/usage_rolling_cli_test.go
T
yetoneandfchengyan 62378c5f84 cli, usage: magpie usage takes --last <dur> and --days <n>, a window ending now, with or without --csv and --account (#2075, tinysnake, fcy222fcy)
today, 7d and 30d begin at a local midnight, so a caller wanting "the
last few hours" just after midnight had to take 7d's CSV, seven days of
rows for one. `magpie usage [--csv [--account <name>]] --last 24h` (any
Go duration, 90m) or `--days 1.5` now reads a window that began that
long ago; the presets are unchanged.

From fcy222fcy's branch (usage-rolling-window, 60472c661): a rolling
window is a period "last:<dur>" (usage.LastOf / Period.Last) that Since,
Known, shown and the chart's timeline answer for, so the ledger, Direct
and the summary read the same window.

Review fixes on top:
- a summary of a rolling window is worked out each time, like a picked
  range: kept, its start didn't move with the clock while the log
  stayed the same (reachable through the web API's ?period=last:24h);
- the CSV export's file name has no colon from "last:24h0m0s", which
  Windows refuses;
- `--last 24h 7d` is refused rather than silently taking --last;
- --days refuses NaN, Inf and over a century;
- the help line and docs/reference.md name the flags.

Run: new TestRollingWindowIsNotKeptStale fails with the cache fix taken
out ("an hour later: 1 calls, want 0"); TestCSVStamp fails without the
colon fix ("magpie-requests-last:24h0m0s-2026-09-30");
TestUsageRollingWindowRejectsJunk fails without the preset/--days
checks; TestUsageRollingWindow and the new ...WithAccount fail (3 rows,
want 2) with Since moved back. go test -tags nogui ./internal/usage/...
./internal/cli/... ./internal/usageapi/... ./internal/gui/... in a
sandbox HOME: ok. gofmt -l clean; go vet darwin, GOOS=linux -tags nogui,
GOOS=windows; go build darwin, linux nogui, windows.
Real use: the built binary on a sandbox HOME with a generated 50,000-call
7-day usage.jsonl: --csv 7d 47,233 rows / 6.97 MB; today 4,376; --last
24h 7,140 / 1.05 MB; --last 90m 444; --days 1.5 10,712; --account me@x
--last 24h 3,570 (that account's half); oldest row of each at the
window's start.
Not tried: the full go test ./... suite; the process still parses the
whole usage.jsonl into its in-memory index on each run (as every period
does), so the cut is in rows decoded and written, not bytes read.

Co-authored-by: fchengyan <3181088318@qq.com>
2026-10-14 15:26:28 +08:00

89 lines
3.2 KiB
Go

package cli
import (
"encoding/csv"
"strings"
"testing"
"time"
stats "github.com/yetone/magpie/internal/usage"
)
// magpie usage --last and --days read a rolling window ending now (#2075):
// what --csv rows and the summary both cover, where the presets begin at a
// local midnight.
func TestUsageRollingWindow(t *testing.T) {
groupsHome(t)
now := holdUsageClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
stats.Append(stats.Record{Time: now.Add(-time.Hour), Agent: "codex", Provider: "a", Model: "m", Input: 10, Status: 200})
stats.Append(stats.Record{Time: now.Add(-25 * time.Hour), Agent: "claude", Provider: "b", Model: "m", Input: 10, Status: 200})
var b strings.Builder
if err := usageTo(&b, []string{"usage", "--csv", "--last", "24h"}); err != nil {
t.Fatal(err)
}
recs, err := csv.NewReader(strings.NewReader(b.String())).ReadAll()
if err != nil {
t.Fatal(err)
}
if len(recs) != 2 { // the header and the call an hour old, not the one 25 hours old
t.Fatalf("%d rows, want 2:\n%s", len(recs), b.String())
}
b.Reset()
if err := usageTo(&b, []string{"usage", "--csv", "--days", "1.5"}); err != nil {
t.Fatal(err)
}
recs, _ = csv.NewReader(strings.NewReader(b.String())).ReadAll()
if len(recs) != 3 { // 36 hours: both calls
t.Fatalf("--days 1.5: %d rows, want 3", len(recs))
}
b.Reset()
if err := usageTo(&b, []string{"usage", "--last", "24h"}); err != nil {
t.Fatal(err)
}
if s := b.String(); !strings.Contains(s, "last 1 day") || !strings.Contains(s, "1 call") {
t.Errorf("summary says neither its window nor its count:\n%s", s)
}
}
func TestUsageRollingWindowRejectsJunk(t *testing.T) {
groupsHome(t)
for _, args := range [][]string{
{"usage", "--last", "abc"},
{"usage", "--last", "0s"},
{"usage", "--days", "0"},
{"usage", "--days", "-1"},
{"usage", "--days"},
{"usage", "--last"},
{"usage", "--last", "24h", "--days", "1"},
{"usage", "--last", "24h", "7d"}, // which window was meant can't be told
{"usage", "--days", "NaN"},
{"usage", "--days", "Inf"},
{"usage", "--days", "1e30"},
} {
if err := usageTo(&strings.Builder{}, args); err == nil {
t.Errorf("%v: no error", args)
}
}
}
// --last composes with --account as a preset does: the window's calls that
// account answered, and no other's (#2075, #557).
func TestUsageRollingWindowWithAccount(t *testing.T) {
groupsHome(t)
now := holdUsageClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
stats.Append(stats.Record{Time: now.Add(-time.Hour), Agent: "codex", Provider: "a", ProviderAccount: "me@x", Model: "m", Input: 10, Status: 200})
stats.Append(stats.Record{Time: now.Add(-2 * time.Hour), Agent: "codex", Provider: "a", ProviderAccount: "other@x", Model: "m", Input: 10, Status: 200})
stats.Append(stats.Record{Time: now.Add(-30 * time.Hour), Agent: "codex", Provider: "a", ProviderAccount: "me@x", Model: "m", Input: 10, Status: 200})
var b strings.Builder
if err := usageTo(&b, []string{"usage", "--csv", "--account", "me@x", "--last", "24h"}); err != nil {
t.Fatal(err)
}
recs, err := csv.NewReader(strings.NewReader(b.String())).ReadAll()
if err != nil {
t.Fatal(err)
}
if len(recs) != 2 { // the header and me@x's call an hour old
t.Fatalf("%d rows, want 2:\n%s", len(recs), b.String())
}
}