Files
magpie/internal/sessions/stats_day_size_test.go
T
yetone cd0ba05c21 sessions: each day's usage and active rows are made to size
statsOfRange grew every day's Usage and Active rows by append, and the
overview keeps three ranges (7, 30, every day), each holding the slack
of every doubling. The rows are now counted per date first and made at
their length.

Run (mote, real-account copy, idle 180s, network blocked):
- statsOfRange's own inuse 9.90MB -> 8.37MB (-1.54MB in the diff
  against the v0.1.3411 build's profile).
- TestStatsDayRowsHoldNoSlack: green; red on the old code by hand
  ("usage 5 of 8, active 5 of 8").
- go test ./internal/sessions/ ok; go vet darwin, linux, windows ok;
  gofmt clean.

Not tried: the full suite (one package changed, nothing outside it reads
the rows' capacity).
2026-11-09 20:50:24 +08:00

43 lines
1.4 KiB
Go

package sessions
import (
"os"
"path/filepath"
"strings"
"testing"
)
// Each day's usage and active rows are held at their own length: every
// range the overview keeps holds them, and rows grown by append kept the
// slack of each doubling (#2278). Five folders on one day grow to eight.
func TestStatsDayRowsHoldNoSlack(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
for i, dir := range []string{"a", "b", "c", "d", "e"} {
id := "4444444" + itoa(i) + "-2222-3333-4444-555555555555"
path := filepath.Join(claude, "projects", "-work-"+dir, id+".jsonl")
os.MkdirAll(filepath.Dir(path), 0o755)
line := ccMsg("m"+dir, "2026-09-27T10:00:00.000Z", 100, 10) + ccMsg("n"+dir, "2026-09-27T10:01:00.000Z", 100, 10)
line = strings.ReplaceAll(line, "/work/night", "/work/"+dir)
line = strings.ReplaceAll(line, "33333333-2222-3333-4444-555555555555", id)
os.WriteFile(path, []byte(line), 0o644)
}
s := statsAt(0, statsNow)
found := false
for _, d := range s.Days {
if d.Date != "2026-09-27" {
continue
}
found = true
if len(d.Usage) < 5 || len(d.Active) < 5 {
t.Fatalf("the 27th has %d usage and %d active rows, want 5 of each", len(d.Usage), len(d.Active))
}
if cap(d.Usage) != len(d.Usage) || cap(d.Active) != len(d.Active) {
t.Fatalf("usage %d of %d, active %d of %d", len(d.Usage), cap(d.Usage), len(d.Active), cap(d.Active))
}
}
if !found {
t.Fatal("no 2026-09-27 in the stats")
}
}