Files
magpie/internal/sessions/by_key_test.go
T
yetone 205e3b8ae7 sessions: a poll groups its files by session in one copy, not an append at a time
listSessions, statsRead (twice), nativeIDs, the Codex provider's list
and the two manage paths each grouped the files by key with
groups[f.key] = append(groups[f.key], f): every group grown by
doubling, copying a few-hundred-byte file struct each time, over ~16k
files. byKey counts the kept files per key, makes one slice for all of
them and cuts each group from it with its capacity capped, so a later
append to one group can't write into the next. Each group keeps the
files' order, as before.

Run:
- TestByKeyGroupsAsAppending (new): byKey equals the append loop with
  and without a keep, and is unchanged after every group is appended to.
  Red with the cap removed (kept[at:at]): a group's first file became
  the previous group's appended one.
- go test -timeout 45m ./internal/sessions ./internal/gui
  ./internal/usageapi: ok (15.1s, 118.9s, 3.4s) at this change on
  f3c4d7b7a. gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/sessions ok.
- A/B, 8 sequential polls of the Sessions and Usage pages (state,
  sessions, stats, usage, usage/requests, quotas, providers) under a
  sandbox HOME holding the real ~/.codex and ~/.claude sessions, alloc
  diff between heap profiles: listSessions flat 143 -> 77 MB, statsRead
  flat 170 -> 84 MB, byKey's own 92 MB, so ~60 MB less in all;
  sessionRoutes 1026 -> 993 MB.
Not tried: the GUI against a real window; the JSON the routes answer is
built from the same groups.
2026-10-27 10:01:09 +08:00

36 lines
995 B
Go

package sessions
import (
"fmt"
"reflect"
"testing"
)
// byKey groups as appending file by file did: the kept files of each
// session, in their order, and a group grown later doesn't write into
// another's files.
func TestByKeyGroupsAsAppending(t *testing.T) {
var files []file
for i := range 40 {
files = append(files, file{agent: []string{"codex", "claude"}[i%2], key: fmt.Sprint("k", i%7), path: fmt.Sprint("p", i)})
}
for _, keep := range []func(*file) bool{nil, func(f *file) bool { return f.agent == "codex" }} {
want := map[string][]file{}
for _, f := range files {
if keep == nil || keep(&f) {
want[f.key] = append(want[f.key], f)
}
}
got := byKey(files, keep)
if !reflect.DeepEqual(got, want) {
t.Fatalf("byKey = %v\nwant %v", got, want)
}
for k := range got {
_ = append(got[k], file{key: "grown", path: "grown"})
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("after each group grew, byKey's groups = %v\nwant %v", got, want)
}
}
}