Files
magpie/internal/gui/overview_kept_test.go
T
yetone 4c331696ec gui: an overview is summed once per stats and filters
The Sessions page asks /api/sessions/overview on every poll, and each
ask summed every session again (Stats.Overview: days, shapes, tool and
skill use, the top sessions) and encoded it, to the same answer while the
stats stood. The answer is now kept, encoded, by statsForGen's gen (it
moves only when the stats do) and the filters, the last 8 of them; the
Codex top rows' model and effort are added again only when
CodexModelEffort hands back another map (the map itself is held, so a
new one can't take the old one's address).

Measured on mote with the owner's real account copy (network blocked),
30 asks of /api/sessions/overview: 0.92s of CPU on v0.1.3435's tree,
0.24s with this (~31 ms -> ~8 ms an ask).

Run on mote: go vet + go test ./internal/gui, ok. TestOverviewSummed-
OncePerStats is red with the kept answer ignored by hand ("two asks of
the same stats summed 2 times, want 1").
Not tried: the GUI suite; the answer's bytes are json.Marshal plus a
newline, as the Encoder wrote them.
2026-11-10 01:17:43 +08:00

79 lines
2.5 KiB
Go

package gui
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// TestOverviewSummedOncePerStats: the Sessions page asks for the overview
// on every poll, and the same stats under the same filters are summed
// once; new stats, or another filter, are summed again.
func TestOverviewSummedOncePerStats(t *testing.T) {
h := sandboxHome(t)
claude := filepath.Join(h, ".claude")
t.Setenv("CLAUDE_CONFIG_DIR", claude)
proj := filepath.Join(claude, "projects", "-work")
if err := os.MkdirAll(proj, 0o755); err != nil {
t.Fatal(err)
}
write := func(id string, out int) {
at := time.Now().Add(-time.Hour).Format(time.RFC3339)
line := fmt.Sprintf(`{"type":"user","message":{"role":"user","content":"hello"},"uuid":"u%s","timestamp":%q,"cwd":"/work","sessionId":%q}`+"\n"+
`{"type":"assistant","message":{"model":"claude-sonnet-5","id":"msg_%s","role":"assistant","content":[{"type":"text","text":"done"}],"usage":{"input_tokens":10,"output_tokens":%d}},"uuid":"a%s","timestamp":%q,"cwd":"/work","sessionId":%q}`+"\n", id, at, id, id, out, id, at, id)
if err := os.WriteFile(filepath.Join(proj, id+".jsonl"), []byte(line), 0o600); err != nil {
t.Fatal(err)
}
}
forget := func() {
sessions.Reset()
statsMemo.Lock()
statsMemo.m = nil
statsMemo.Unlock()
}
write("one", 2)
forget()
t.Cleanup(forget)
mux := http.NewServeMux()
sessionRoutes(mux, folderOnly{})
ask := func(q string) []byte {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/sessions/overview?"+q, nil))
if w.Code != 200 {
t.Fatalf("overview: %d %s", w.Code, w.Body)
}
return w.Body.Bytes()
}
made := overviewsMade.Load()
first := ask("days=7")
if again := ask("days=7"); !bytes.Equal(first, again) {
t.Fatalf("the same stats answered differently:\n%s\n%s", first, again)
}
if n := overviewsMade.Load() - made; n != 1 {
t.Fatalf("two asks of the same stats summed %d times, want 1", n)
}
ask("days=7&agent=codex")
if n := overviewsMade.Load() - made; n != 2 {
t.Fatalf("another filter summed %d times in all, want 2", n)
}
// the stats read again, with a second session: summed again, and
// the answer says so
write("two", 5)
forget()
if next := ask("days=7"); bytes.Equal(first, next) {
t.Fatalf("new stats answered with the old overview: %s", next)
}
if n := overviewsMade.Load() - made; n != 3 {
t.Fatalf("new stats summed %d times in all, want 3", n)
}
}