Files
magpie/internal/sessions/files_kept_test.go
T
yetone 0b4b249e54 sessions: an unchanged Sessions poll keeps the last file list
Every 15 s poll made the whole deduped file list again (~1.5 MB on a
real history, distinctOf's 58 MB over 40 polls). allFiles and
allFilesForTitles now compare each pass with the last list, held
weakly so a magpie with the page shut keeps none of it, and give the
same array back while nothing differs. A changed or gone file makes a
new list; the old one is never written into (cap == len). An empty
pass is an empty slice, never nil: refresh reads nil as "not listed"
and kept the parses of deleted files (TestGoneAgentLeavesNoSession
went red on the first try, caught before commit).

Run (mica): go test -count=1 -timeout 45m ./internal/sessions/
./internal/dirent/ ok; go vet of both ok; gofmt clean.
Broken by hand: the nil return makes TestFilesKeptWhileTheSame ("an
empty pass: nil") and TestGoneAgentLeavesNoSession ("5 parses kept")
red; a fresh slice on every pass makes TestFilesKeptWhileTheSame red.
Real account on mote, 40 unchanged /api/sessions polls, alloc_space:
179.5 MB (v0.1.3794 build) -> 147.9 MB; distinctOf gone, the kept
list's own compare 12 MB. /api/sessions body sha identical on both.
Not run: the full ./... suite (mote over its cores; this touches the
sessions package only) and the GUI suite.
2026-11-12 02:30:15 +08:00

56 lines
1.8 KiB
Go

package sessions
import (
"slices"
"testing"
"unsafe"
)
func sameArray(a, b []file) bool {
return len(a) > 0 && len(b) > 0 && unsafe.SliceData(a) == unsafe.SliceData(b)
}
// A pass that lists the files the last one did gives its list back and
// allocates nothing for it; any change, a file gone included, is a new
// list, and each path is still listed once.
func TestFilesKeptWhileTheSame(t *testing.T) {
var k filesKept
a := []file{{agent: "claude", key: "claude:1", path: "/a"}, {agent: "claude", key: "claude:2", path: "/b"}}
b := []file{{agent: "codex", key: "codex:3", path: "/c"}, {agent: "claude", key: "claude:1", path: "/a"}}
first := k.distinct(a, b)
if want := []file{a[0], a[1], b[0]}; !slices.Equal(first, want) {
t.Fatalf("first = %v, want %v", first, want)
}
again := k.distinct(a, b)
if !sameArray(first, again) {
t.Fatal("the same files were listed in a new slice")
}
if n := testing.AllocsPerRun(20, func() { k.distinct(a, b) }); n != 0 {
t.Fatalf("an unchanged pass allocated %v times", n)
}
grown := slices.Clone(b)
grown[0].size = 10
changed := k.distinct(a, grown)
if sameArray(changed, first) || changed[2].size != 10 || !slices.Equal(first, []file{a[0], a[1], b[0]}) {
t.Fatalf("a grown file: %v (last list now %v)", changed, first)
}
gone := k.distinct(a)
if sameArray(gone, changed) || !slices.Equal(gone, a) {
t.Fatalf("a file gone from the end: %v", gone)
}
// nil reads as "not listed": the parses of files gone would be kept
if l := k.distinct(); l == nil || len(l) != 0 {
t.Fatalf("an empty pass: %#v", l)
}
if l := k.distinct(); l == nil {
t.Fatal("a second empty pass gave nil")
}
if len(k.distinct(a)) != 2 {
t.Fatal("a pass after an empty one")
}
if l := k.distinct(a); cap(l) != len(l) {
t.Fatalf("cap %d, len %d: an append would write into the kept list", cap(l), len(l))
}
}