Files
magpie/internal/davsync/quota_share_test.go
T
yetone 9ccabdfbd8 usage: quota cards draw what was left over time, against an even burn, and the tray card its cycle's line (#651, songlairui) — each reading magpie makes anyway of a subscription's windows (and of a plan bought with a key) is kept as a point in quota-history.json beside the config: provider, account, window, when read, percent left, the window's start and reset — no token, no prompt. A failed reading, one kept from before (As of), an unlimited window and a model family's aren't kept; Antigravity's pools are kept by pool. Within a cycle points are 5 minutes apart at least and a run of one figure is its two ends; a cycle ends where the window starts again (its reset passed, or moved further than the time between two readings, so a rolling window's isn't one). 45 days and 6000 points a window are kept. The Usage page's card has, under its meters, a line a window (several tiers of one account on one chart), broken at each reset, a dashed even burn from each window's start to its reset and an upright now line, with 2 days / Cycle (the account's longest current window, start to reset) in the app's segmented control, remembered, turning every card without moving the page. The tray's card has a thin line of its first window's current cycle. GET /v1/magpie/quotas/history (?days=, ?provider=, ?user=; local or LAN with the key, as /v1/magpie/quotas) and /api/usage/quotas/history serve it; /v1/magpie/quotas and magpie quota stay snapshots. Computers sharing usage (WebDAV/S3, Settings › Sync › usage) put their history beside their days, sealed, as <id>.magpie-quotas, and merge the others': the union by account, window and time, two points within 30 s the newer, nothing twice however often they sync; an older magpie never reads the file. TestQuotaHistoryRecords, TestQuotaHistoryCycles, TestMergeQuotaHistory, TestQuotasHistory and TestQuotaHistorySharedBetweenComputers cover recording, cycles, pruning, merge, the API and two computers on one WebDAV folder; quota-curve.test.cjs (WebKit, en/zh, light and dark) the curves, the toggle, no curve without readings, and the tray's line (Chromium doesn't start in this sandbox). go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. Not tried with real accounts; pay-as-you-go balances aren't drawn.
2026-10-03 15:19:19 +08:00

92 lines
2.4 KiB
Go

package davsync
import (
"context"
"fmt"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// Two computers sharing usage share what they read of their accounts'
// windows too (#651): each puts its own sealed beside its days, and each
// ends with the points of both, merged by account and window, nothing
// twice however often it syncs.
func TestQuotaHistorySharedBetweenComputers(t *testing.T) {
old := usageEvery
usageEvery = 0
t.Cleanup(func() { usageEvery = old })
fake := &usageDAV{fakeDAV: &fakeDAV{files: map[string][]byte{}, etags: map[string]string{}, dirs: map[string]bool{"/dav": true}}}
srv := httptest.NewServer(fake)
defer srv.Close()
cfg := Config{URL: srv.URL + "/dav/", User: "me", Password: "pw", Passphrase: "correct horse", Keys: true, Usage: true}
sync := func(t *testing.T) {
t.Helper()
if err := Now(context.Background()); err != nil {
t.Fatal(err)
}
if v := Status(); v.UsageError != "" {
t.Fatalf("usage not shared: %+v", v)
}
}
now := time.Now().UTC().Truncate(time.Second)
seed := func(t *testing.T, mins ...int) {
t.Helper()
var pts []string
for _, m := range mins {
pts = append(pts, fmt.Sprintf(`{"at":%q,"left":%d}`, now.Add(-time.Duration(m)*time.Minute).Format(time.RFC3339), 100-m/10))
}
if err := provider.MergeQuotaHistory([]byte(`{"codex|a@x.com":{"5 hours":[`+strings.Join(pts, ",")+`]}}`), now); err != nil {
t.Fatal(err)
}
}
points := func(t *testing.T) int {
t.Helper()
hs := provider.QuotaHistories(time.Time{}, "codex", "a@x.com")
if len(hs) != 1 {
t.Fatalf("histories: %+v", hs)
}
return len(hs[0].Lines[0].Points)
}
a, b := newComputer(t), newComputer(t)
a.use(t)
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
seed(t, 100, 60)
sync(t)
var sealed []byte
for _, f := range fake.usageFiles() {
if strings.HasSuffix(f, quotasExt) {
sealed = fake.files["/dav/magpie/usage/"+f]
}
}
if sealed == nil || strings.Contains(string(sealed), "a@x.com") {
t.Fatalf("a's history on the server, sealed: %v", fake.usageFiles())
}
b.use(t)
if err := Configure(cfg); err != nil {
t.Fatal(err)
}
seed(t, 80, 20)
sync(t)
if n := points(t); n != 4 {
t.Fatalf("b has %d points, want its 2 and a's 2", n)
}
sync(t)
sync(t)
if n := points(t); n != 4 {
t.Fatalf("b, synced again: %d points", n)
}
a.use(t)
sync(t)
if n := points(t); n != 4 {
t.Fatalf("a has %d points, want its 2 and b's 2", n)
}
}