Pad/internal/browser/lazy_loading_test.go
Greg Pomerantz 58725a5e6c Make state single-owner and stabilize race detector
Frame now carries view state (scale/focus/query) to the main goroutine,
which reads only the frame-receiver-stored snapshot. Renderer owns Gio
widget editors (registered by ID) and the draw scale. Autosave timer
sends a token to the logic goroutine instead of touching state;
Shutdown waits for the owner to exit before FlushAll.

Tests access state only through owner-side Inspect/WithState helpers
(harness + in-package). Fixed double-Harness.Run race in two e2e tests,
made TestWorkerPool_PriorityPreemption deterministic, and raised the
lazy-loading test timeout that was too short under -race.

go test -race ./... is now green.
2026-08-16 01:32:27 -04:00

123 lines
3.1 KiB
Go

package browser
import (
"fmt"
"testing"
"time"
"pad/internal/io/pool"
"pad/internal/io/pool/mock"
)
// TestLazyLoadingLargeDirectory verifies that page-based loading works
// correctly for large directories (10,000 entries), ensuring only visible
// and prefetched pages are kept in memory.
func TestLazyLoadingLargeDirectory(t *testing.T) {
state := NewBrowserState()
state.TotalEntries = 10000
state.EntryHeight = 48.0
state.VisibleCount = 10
mfs := mock.NewFileSystem()
wp := pool.NewWorkerPool(4)
wp.Start()
// Populate mock filesystem with 10k entries
mfs.AddDir("/dir", time.Now())
for i := 0; i < 10000; i++ {
mfs.AddFile(fmt.Sprintf("/dir/file_%d.txt", i), []byte("data"), time.Now())
}
bm, err := NewBrowserManager(state, wp, mfs)
if err != nil {
t.Fatalf("NewBrowserManager failed: %v", err)
}
// 1. Navigate to directory
bm.NavigateTo("/dir")
// Helper to get result with timeout
getResult := func(timeout time.Duration) (pool.Result, bool) {
select {
case res := <-wp.ResultChan():
return res, true
case <-time.After(timeout):
return pool.Result{}, false
}
}
// Wait for index build result and process it
res, ok := getResult(15 * time.Second)
if !ok {
t.Fatal("Timeout waiting for BuildIndex result")
}
bm.HandleResult(res)
// Wait for initial page load result and process it
res, ok = getResult(15 * time.Second)
if !ok {
t.Fatal("Timeout waiting for initial LoadPages result")
}
bm.HandleResult(res)
// 2. Verify only initial pages loaded (visible + prefetch)
if len(state.Pages) == 0 {
t.Error("Expected pages to be loaded")
}
// 3. Scroll down to page 50
state.ScrollOffset = 50 * 100 * state.EntryHeight // Scroll to middle
bm.OnScroll()
// Wait for load pages result for scroll
res, ok = getResult(15 * time.Second)
if !ok {
t.Fatal("Timeout waiting for scroll LoadPages result")
}
bm.HandleResult(res)
// 5. Verify old pages are evicted (e.g., page 0)
if page, ok := state.Pages[0]; ok && page.Loaded {
t.Error("Expected page 0 to be evicted")
}
}
// TestPrefetchOnScroll verifies that scroll triggers prefetch for
// pages within PrefetchDist.
func TestPrefetchOnScroll(t *testing.T) {
state := NewBrowserState()
state.TotalEntries = 1000
state.EntryHeight = 48.0
state.VisibleCount = 10
mfs := mock.NewFileSystem()
wp := pool.NewWorkerPool(4)
wp.Start()
// Populate mock filesystem
mfs.AddDir("/dir", time.Now())
for i := 0; i < 1000; i++ {
mfs.AddFile(fmt.Sprintf("/dir/file_%d.txt", i), []byte("data"), time.Now())
}
bm, err := NewBrowserManager(state, wp, mfs)
if err != nil {
t.Fatalf("NewBrowserManager failed: %v", err)
}
bm.NavigateTo("/dir")
// Process BuildIndex and initial load
bm.HandleResult(<-wp.ResultChan())
bm.HandleResult(<-wp.ResultChan())
// Current visible ~page 0. Pages 0, 1, 2 should be loaded.
// Scroll to page 5.
state.ScrollOffset = 5 * 100 * state.EntryHeight
bm.OnScroll()
// Process prefetch load
bm.HandleResult(<-wp.ResultChan())
// Prefetch should load pages around 5 (e.g., 3, 4, 5, 6, 7)
if _, ok := state.Pages[6]; !ok {
t.Error("Expected page 6 to be prefetched")
}
}