Files
magpie/internal/settings/usage_cleared_test.go
T
yetone 012c584cc1 usage: clear a model's usage, or all of it, from the Usage page
CrazyMouse_TT on X asked for a way to clear usage, all of it or one
model's, to measure a local model fresh after deploying it.

The Usage page's Models list has a "…" on each row (and a right-click
menu) with "Clear usage…", and "Clear all usage…" sits under the lists
below the "Counted from…" note. Each asks first, in a confirmation that
says what is deleted, that it can't be undone, and that key spending
limits and allowances still count the calls.

A clear is a mark in settings (usageCleared: provider, model, unix ms),
read at read time; usage.jsonl is never rewritten. The Overview hides
calls up to the mark in summarizeFrom; the Requests page, heatmap and
ledger/CSV hide them in Filter.keepsRow, their one choke point, and the
request index's meta carries the marks so its cache is dropped. Key
budgets, window holds and Codex reads go through usage.Visit and the raw
log, so a clear never resets a key's spending limit or a quota. A later
clear of the same model or of everything takes the place of an earlier
one. The marks ride the settings part of a sync (sync-backup.md) and a
save of the Settings page keeps them (api.go's keep list; the guard
test TestSettingsSaveKeepsWhatItDoesNotSend caught it missing).
gateway-sessions.md and sync-backup.md say so.

Run on mote (mote-run) at this tree, rebased on origin/main c26b2992e
(the Go packages and usage-clear.test.cjs; vet and the 68 GUI files
ran at the earlier base f835bd713, four commits behind):
- go test -tags nogui -timeout 45m ./internal/usage ./internal/settings
  ./internal/usageapi ./internal/gui: all ok after the
  keep-list fix (before it, TestSettingsSaveKeepsWhatItDoesNotSend red).
- gofmt -l: clean. GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./...: exit 0. (Linux vet without nogui fails in
  wails' cgo, as on main.)
- usage-clear.test.cjs: 32/32 in Chromium and WebKit, en/zh/zh-TW/ja/de,
  at 1200, 560 and 390 (web). With the old page.js/client.js/i18n.js it
  fails (TimeoutError).
- 68 GUI files the change can reach (every usage-*, gui-de/ja/zh-tw,
  i18n-keys, click-scroll, idle-ticks, mobile-web, panel-usage,
  sessions-purge): 941/980. The reds are filed and red on main
  (usage-ledger run alone at origin/main 84a6a2c42 too): i18n-keys "masking…" (#4053), mobile-web
  (#3952), usage-agents-hide (#2276, #3414), usage-ledger "newest live
  route needs no return-to-live button" (#3104, run alone at main too).
  i18n-keys first also named "Clear usage…" in Go comments of this
  change; reworded.
Broken by hand, each red: hides() always false (usage and gui tests),
keepsRow's clear check removed ("Requests after the clear: 5 rows"),
clearedKey out of the request meta ("after clearing everything: 3"),
the api.go keep line removed (UsageCleared [] after a save).

Not tried: the full go ./... suite and the whole GUI suite (mote was
busy with other sessions' full suites), the real app, a sync between
two computers. usage.jsonl doesn't shrink, and the Sessions tab's
figures are not cleared.
2026-10-28 21:13:09 +08:00

56 lines
1.8 KiB
Go

package settings
import (
"os"
"testing"
"time"
)
// A clear of usage from the Usage page (CrazyMouse_TT on X) keeps one mark a
// model: a later clear of the same model, or of everything, takes the
// place of an earlier one, and a clear of a provider names its model.
func TestClearUsageKeepsOneMarkAModel(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := os.MkdirAll(Dir(), 0o755); err != nil {
t.Fatal(err)
}
at := time.Date(2026, 10, 28, 9, 0, 0, 0, time.UTC)
if err := ClearUsage("ollama", "qwen3", at); err != nil {
t.Fatal(err)
}
if err := ClearUsage("ollama", "llama4", at); err != nil {
t.Fatal(err)
}
// the same model again, later: one mark, the later
if err := ClearUsage(" ollama ", "qwen3 ", at.Add(time.Hour)); err != nil {
t.Fatal(err)
}
got := UsageClearedNow()
if len(got) != 2 || got[0].Model != "llama4" || got[1].Model != "qwen3" || got[1].At != at.Add(time.Hour).UnixMilli() {
t.Fatalf("after three clears of two models: %+v", got)
}
if s := Load(); len(s.UsageCleared) != 2 {
t.Fatalf("Load: %+v", s.UsageCleared)
}
if err := ClearUsage("ollama", "", at); err == nil {
t.Fatal("a clear of a provider with no model was taken")
}
if err := ClearUsage("", "", time.Time{}); err == nil {
t.Fatal("a clear with no time was taken")
}
// everything, at the latest: the models' marks it covers go
if err := ClearUsage("", "", at.Add(2*time.Hour)); err != nil {
t.Fatal(err)
}
if got := UsageClearedNow(); len(got) != 1 || got[0].Provider != "" || got[0].Model != "" {
t.Fatalf("after clearing everything: %+v", got)
}
// a model cleared after that is kept beside it
if err := ClearUsage("ollama", "qwen3", at.Add(3*time.Hour)); err != nil {
t.Fatal(err)
}
if got := UsageClearedNow(); len(got) != 2 {
t.Fatalf("a model cleared after everything: %+v", got)
}
}