diff --git a/internal/tui/select_raw_test.go b/internal/tui/select_raw_test.go index a5f5a93f..777e5eff 100644 --- a/internal/tui/select_raw_test.go +++ b/internal/tui/select_raw_test.go @@ -506,24 +506,34 @@ func dragEveryFrameFollow(t *testing.T, h *rawHarness, state string, following b dragFrame(t, h, state+" release", sgrLeftRelease(25, 6), base) } -func TestSelectionDragEveryFrameMiddle(t *testing.T) { - h := newRawHarness(t, 200) - for i := 0; i < 6; i++ { - h.in.WriteString(sgrWheelUp(5, 20)) - h.settle() - } - // Wheel lines clamp at the top, so the middle is reached by - // pressing the mouse wheel up repeatedly; if it never moves the - // precondition fails loudly. - deadline := time.Now().Add(500 * time.Millisecond) +// scrollUpNTimes scrolls the view up by sending n wheel-up events, +// waiting for each one to settle on screen before sending the next. +// waitFor reads the settled screen grid, not a quiet output window: +// a wheel-up can be queued while the program is still rendering, +// and settle() alone cannot tell a finished program from a busy +// one. Returns the first visible content line after the last scroll. +// The caller asserts its own precondition (middle, above bottom). +func scrollUpNTimes(t *testing.T, h *rawHarness, n int) int { + t.Helper() base := firstContentLine(h.screenGridNow().gridLines()) - for time.Now().Before(deadline) && base <= 0 { - time.Sleep(50 * time.Millisecond) - h.settle() + if base < 0 { + t.Fatalf("no content line visible before wheel-up (first = %d)", base) + } + for i := 0; i < n; i++ { + before := base h.in.WriteString(sgrWheelUp(5, 20)) - h.settle() + h.waitFor("a wheel-up scrolls up", func() bool { + got := firstContentLine(h.screenGridNow().gridLines()) + return got >= 0 && got < before + }) base = firstContentLine(h.screenGridNow().gridLines()) } + return base +} + +func TestSelectionDragEveryFrameMiddle(t *testing.T) { + h := newRawHarness(t, 200) + base := scrollUpNTimes(t, h, 6) if base <= 0 { t.Fatalf("wheel-up did not scroll to the middle (first visible = %d)", base) } @@ -533,11 +543,7 @@ func TestSelectionDragEveryFrameMiddle(t *testing.T) { func TestSelectionDragEveryFrameOneScreenAboveBottom(t *testing.T) { h := newRawHarness(t, 200) - for i := 0; i < 3; i++ { - h.in.WriteString(sgrWheelUp(5, 20)) - h.settle() - } - base := firstContentLine(h.screenGridNow().gridLines()) + base := scrollUpNTimes(t, h, 3) if base <= 0 { t.Fatalf("wheel-up did not scroll above the bottom (first visible = %d)", base) } @@ -624,25 +630,7 @@ func TestOSC52EscapeCanBeCoalescedAway(t *testing.T) { // followBottom and the offset). Raw SGR bytes, every frame checked. func TestSelectionWheelMidDrag(t *testing.T) { h := newRawHarness(t, 200) - for i := 0; i < 6; i++ { - h.in.WriteString(sgrWheelUp(5, 20)) - h.settle() - } - // Poll until the view reaches the middle BEFORE capturing base: - // a setup wheel-up can land late (one settle read the old frame - // while the program was still rendering), so a base captured - // without settling to the middle is stale and every later - // dragFrame assertion compares against the wrong frame. This is - // the pattern TestSelectionDragEveryFrameMiddle already uses. - deadline := time.Now().Add(500 * time.Millisecond) - base := firstContentLine(h.screenGridNow().gridLines()) - for time.Now().Before(deadline) && base <= 0 { - time.Sleep(50 * time.Millisecond) - h.settle() - h.in.WriteString(sgrWheelUp(5, 20)) - h.settle() - base = firstContentLine(h.screenGridNow().gridLines()) - } + base := scrollUpNTimes(t, h, 6) if base <= 0 { t.Fatalf("did not scroll to the middle (first visible = %d)", base) }