Files
magpie/internal/cli/quota_history_cli_test.go
T
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00

89 lines
2.9 KiB
Go

package cli
import (
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// stdoutOf is what f prints.
func stdoutOf(t *testing.T, f func() error) (string, error) {
t.Helper()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
old := os.Stdout
os.Stdout = w
done := make(chan []byte)
go func() { b, _ := io.ReadAll(r); done <- b }()
err = f()
w.Close()
os.Stdout = old
return string(<-done), err
}
// magpie quota history tells each account's windows over time as magpie
// kept them, narrowed to a provider or an account and to --days; --json is
// what GET /v1/magpie/quotas/history answers (#802).
func TestQuotaHistoryCmd(t *testing.T) {
groupsHome(t)
now := time.Now().UTC().Truncate(time.Second)
reset := now.Add(2 * time.Hour)
pt := func(ago time.Duration, left float64, r time.Time) provider.QuotaPoint {
return provider.QuotaPoint{At: now.Add(-ago), Left: left, ResetsAt: &r}
}
h := map[string]map[string][]provider.QuotaPoint{
"codex|a@x.com": {"5 hours": {pt(30*time.Hour, 40, now.Add(-26*time.Hour)), pt(3*time.Hour, 100, reset), pt(time.Hour, 72.5, reset)}},
"claude|b@x.com": {"Weekly": {pt(2*time.Hour, 55, now.Add(72*time.Hour))}},
}
b, _ := json.Marshal(h)
if err := os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "quota-history.json"), b, 0o600); err != nil {
t.Fatal(err)
}
out, err := stdoutOf(t, func() error { return quotaCmd([]string{"quota", "history", "codex", "--days", "1", "--json"}) })
if err != nil {
t.Fatal(err)
}
var hs []provider.QuotaHistory
if err := json.Unmarshal([]byte(out), &hs); err != nil {
t.Fatal(err, out)
}
// the day's two, and the one before so the line reaches its edge
if len(hs) != 1 || hs[0].Provider != "codex" || hs[0].User != "a@x.com" || len(hs[0].Lines) != 1 || len(hs[0].Lines[0].Points) != 3 {
t.Fatalf("codex, a day: %s", out)
}
if p := hs[0].Lines[0].Points[2]; !p.At.Equal(now.Add(-time.Hour)) || p.Left != 72.5 || !p.ResetsAt.Equal(reset) {
t.Errorf("last point = %+v", p)
}
out, err = stdoutOf(t, func() error { return quotaCmd([]string{"quota", "history", "B@x.com", "--json"}) })
if err != nil || !strings.Contains(out, `"provider": "claude"`) || strings.Contains(out, "codex") {
t.Fatalf("by account: %v %s", err, out)
}
out, err = stdoutOf(t, func() error { return quotaCmd([]string{"quota", "history"}) })
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"codex · a@x.com", "claude · b@x.com", "5 hours", "72.5%", "↻ ", "Weekly", "55%"} {
if !strings.Contains(out, want) {
t.Errorf("text lacks %q:\n%s", want, out)
}
}
if _, err := stdoutOf(t, func() error { return quotaCmd([]string{"quota", "history", "nobody"}) }); err == nil {
t.Error("an account with nothing kept wasn't an error")
}
if _, err := stdoutOf(t, func() error { return quotaCmd([]string{"quota", "history", "--days", "x"}) }); err == nil {
t.Error("--days x wasn't an error")
}
}