Files
magpie/internal/gui/lightweight_rule_test.go
T
yetone 65f1fdd1d1 gui: the history's warm-up starts with the app's page, not at start
sessionRoutes started warmSessions when the routes were made, so a
magpie started at login, by `magpie tray`, or as `magpie web` with no
page read every session's stats, the Requests page's whole history and
Codex's effort for nobody. What those reads kept went two minutes later
(statsIdle, requestIdle), so the reads were pure cost unless a window
opened in those minutes. The warm-up now starts when the app's page is
first served (GET / or /index.html, not mode=panel); the steady GC
setting is still made with the routes. On Linux and Windows the main
window is still made at start, so the warm-up runs there as before.

Measured on mote with the real account's history (~15.6k sessions),
the sandbox HOME at ~/r2278/acct, proxies to 127.0.0.1:9:
- `tray` start, CPU in the first 30 s: w34 2.67 s, w35 0.96 / 1.04 s.
- `web` with no page, at 150 s: footprint 104 -> 64 MB, live heap
  62 -> 9 MB, CPU 2.52 -> 0.65 s.
- normal launch (window shown): the warm-up still runs in the first
  30 s (warmStart 0.84 s, warmStats 0.47 s, QueryPage 0.31 s), as on
  w34 (0.73 s).
- `panel` start: no warm-up in the profile, 1.62 s CPU, no panic.

Tests: TestWarmsSessions (app page, panel page, assets, HEAD), and
-run 'TestWarmsSessions|TestPanelOnFirstOpen|TestMainOnFirstOpen|Warm|Cold'
./internal/gui ok on mica; go vet ./internal/gui and GOOS=windows go vet
./internal/gui ok.
Not run: the full suite, the GUI suite (no page script changed), Linux
GUI vet (no gtk4 dev packages in WSL).
Not tried: a first Sessions page asked within seconds of opening the
window after a hidden start; it takes turns with the warm-up
(coldTurns), as a first start's window did before.
2026-11-13 00:32:17 +08:00

83 lines
2.7 KiB
Go

package gui
import (
"net/http/httptest"
"testing"
)
// A closed window's webview goes on the second look in a row that finds it
// closed with lightweight mode on (#580), never while it is shown or the
// mode is off, and a window shown between looks starts over.
func TestLightStep(t *testing.T) {
type look struct{ on, shown, release bool }
for name, looks := range map[string][]look{
"closed": {{true, false, false}, {true, false, true}},
"off": {{false, false, false}, {false, false, false}, {false, false, false}},
"shown": {{true, true, false}, {true, true, false}, {true, true, false}},
"shown between": {{true, false, false}, {true, true, false}, {true, false, false}, {true, false, true}},
"turned off": {{true, false, false}, {false, false, false}, {true, false, false}, {true, false, true}},
"again after it": {{true, false, false}, {true, false, true}, {true, false, false}, {true, false, true}},
} {
ticks := 0
for i, l := range looks {
var release bool
ticks, release = lightStep(l.on, l.shown, ticks)
if release != l.release {
t.Fatalf("%s: look %d released %v", name, i, release)
}
if release {
ticks = 0 // as lighten does once the window is let go
}
}
}
}
// The Mac's tray panel is made when it is first opened, not at start: a
// hidden one kept a WebContent process of ~67 MB for a user who never
// opened it. Elsewhere it is still made at start.
func TestPanelOnFirstOpen(t *testing.T) {
for goos, want := range map[string]bool{"darwin": true, "linux": false, "windows": false} {
if got := panelOnFirstOpen(goos); got != want {
t.Errorf("panelOnFirstOpen(%s) = %v, want %v", goos, got, want)
}
}
}
func TestMainOnFirstOpen(t *testing.T) {
for _, c := range []struct {
goos string
showMain bool
want bool
}{
{"darwin", false, true},
{"darwin", true, false},
{"linux", false, false},
{"windows", false, false},
} {
if got := mainOnFirstOpen(c.goos, c.showMain); got != c.want {
t.Errorf("mainOnFirstOpen(%q, %v) = %v, want %v", c.goos, c.showMain, got, c.want)
}
}
}
// The warm-up's read of the history starts with the app's page, never
// with the tray panel's page or a file the page loads.
func TestWarmsSessions(t *testing.T) {
for _, c := range []struct {
method, url string
want bool
}{
{"GET", "/?theme=dark", true},
{"GET", "/", true},
{"GET", "/index.html", true},
{"GET", "/?mode=panel&theme=dark", false},
{"GET", "/app.js", false},
{"HEAD", "/", false},
} {
r := httptest.NewRequest(c.method, c.url, nil)
if got := warmsSessions(r); got != c.want {
t.Errorf("%s %s: %v, want %v", c.method, c.url, got, c.want)
}
}
}