Fix relaunch-restore bugs found on device (spec §7 verification)

On-device runs exposed three bugs the e2e suite could not (it never
feeds layout feedback, and runs headless without size events):

1. WrapIndex poisoning from zero-width shapes. The first frames are
   built before the window size is known (px=0x0); the renderer shapes
   the editor window at zero width, where every line wraps into many
   visual lines. When that feedback arrives, applyWrapCounts writes the
   inflated counts to the window's lines. Normally the app stays on
   those lines and re-shapes them at a real width, which corrects the
   counts before anyone notices. A restored scroll moves the viewport
   away instead, so the poisoned counts persist and map the restored
   scroll offset to the wrong line (2000 landed on line 11 of 200).
   The frame now carries ViewportDegenerate (set at frame-build time,
   since feedback delivery lags shaping by a frame), and the main loop
   drops layout feedback for such frames.

2. Session saving suppressed forever after a successful restore.
   saveSessionIfChanged suppresses saves while l.session is set, and
   only abortRestore cleared it — the success path never did, so an
   app restored from a session never persisted new state. The
   snapshot has fully landed once the cursor/selection/find have
   landed with the content and the armed scroll has landed (or there
   was none); clear l.session at both points.

3. gofmt on restore_test.go (comment alignment).
This commit is contained in:
gmp
2026-08-20 17:31:55 -04:00
parent e3690d6b98
commit 03595af27d
5 changed files with 145 additions and 45 deletions
+40
View File
@@ -119,6 +119,46 @@ func TestRestore_FileCursorScrollSelection(t *testing.T) {
}
}
// TestRestore_ScrollLandsWhenScalePrecedesSize reproduces the on-device
// startup ordering, where the first FrameEvent (and hence the ScaleEvent)
// arrives before the size ConfigEvent. The scroll must still land, clamped
// by the layout at the real scale: an earlier revision applied it at the
// ScaleEvent against a not-yet-sized viewport and lost it.
func TestRestore_ScrollLandsWhenScalePrecedesSize(t *testing.T) {
content := strings.Repeat("hello world line\n", 100)
dir := t.TempDir()
diskPath := filepath.Join(dir, "late.txt")
if err := os.WriteFile(diskPath, []byte(content), 0644); err != nil {
t.Fatal(err)
}
h := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
h.Logic().BeginRestore(editor.SessionState{File: "/late.txt", Cursor: 123, Scroll: 100})
h.Run()
h.SendScale(2.0) // scale first
h.SendConfig(780, 1688) // size second (on-device ordering)
defer h.Cleanup()
for i := 0; i < 100; i++ {
v, err := h.Inspect(func(st *editor.State) any {
cb := st.Editor.ChunkedBuffer
if cb == nil || cb.FileLen() == 0 || cb.LineIndex == nil {
return false
}
full, err := cb.FullContent()
return err == nil && int64(len(full)) == cb.FileLen() &&
cb.LineIndex.Size == cb.FileLen() && st.ScrollOffset == ui.Dp(100)
})
if err != nil {
t.Fatalf("Inspect: %v", err)
}
if v.(bool) {
return
}
time.Sleep(50 * time.Millisecond)
}
t.Fatal("timed out: scroll did not land when the scale event preceded the size")
}
// TestRestore_ClampsToShorterFile: the file was truncated since the last
// session; the cursor and selection must clamp to the shorter content, not
// restore past EOF.