Files
magpie/internal/gui/kept_sum_test.go
T
yetone 6a23d9d755 gui: the kept state and providers are marshaled only when they changed
keepState and keepProviders ran json.Marshal on every /api/state and
/api/providers poll to compare the answer with the one kept on disk.
They now keep a sha256 of what was written and hash the new answer
into it with an Encoder; only a changed answer is marshaled and
written. json.Marshal there was 38.6 MB (keepState 25.9 MB,
keepProviders 12.7 MB) of what twenty window polls allocated with
real accounts (m2463 profile).

Test: TestKeptStateMarshaledOnlyWhenChanged. Twenty identical keeps
marshal nothing, nor does a keep after the file is read again; a
changed state is marshaled once and read back.

Run on mote, at this tree on dd7442f28:
- gofmt, go vet (darwin, windows, linux): ok
- the new test alone 3 times: ok
- broken by hand (the sum check made never true): red, "20 polls of
  the same state and list marshaled them 20 times, want 0"
- go test ./internal/gui: every test ok except the known
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners (#3949)

Not run: an A/B with the real-account sandbox for this change alone;
it goes with the next batch's profile.
2026-11-01 04:46:38 +08:00

51 lines
1.7 KiB
Go

package gui
import (
"testing"
)
// Each poll of the Agents and Providers pages marshaled the state it keeps
// only to find it the same as kept: 38 MB of what twenty polls allocated.
// The same state is now compared by its hash, and marshaled only when it
// changed, by this run or against the file the run before kept.
func TestKeptStateMarshaledOnlyWhenChanged(t *testing.T) {
sandboxHome(t)
forgetLastState()
forgetLastProviders()
t.Cleanup(forgetLastState)
t.Cleanup(forgetLastProviders)
s := stateJSON{Agents: []agentJSON{{ID: "codex", Name: "Codex"}}}
ps := providersJSON{Providers: []providerJSON{{ID: "relay", Name: "Relay"}}}
keepState(s)
keepProviders(ps)
before := keptMarshaled.Load()
for range 20 {
keepState(s)
keepProviders(ps)
}
if n := keptMarshaled.Load() - before; n != 0 {
t.Fatalf("20 polls of the same state and list marshaled them %d times, want 0", n)
}
// a new run reads the file the last one kept, and finds it the same
forgetLastState()
forgetLastProviders()
keepState(s)
keepProviders(ps)
if n := keptMarshaled.Load() - before; n != 0 {
t.Fatalf("the state as the last run kept it was marshaled %d times, want 0", n)
}
s.Agents = append(s.Agents, agentJSON{ID: "claude", Name: "Claude Code"})
keepState(s)
if n := keptMarshaled.Load() - before; n != 1 {
t.Fatalf("a state changed was marshaled %d times, want 1", n)
}
got, ok := lastKnown()
if !ok || len(got.Agents) != 2 || got.Agents[1].ID != "claude" {
t.Fatalf("the changed state isn't the one kept: %+v", got.Agents)
}
forgetLastState()
if got, ok := lastKnown(); !ok || len(got.Agents) != 2 {
t.Fatalf("the changed state isn't in the file: %+v", got.Agents)
}
}