diff --git a/src/pepper_cli/tui/thread.py b/src/pepper_cli/tui/thread.py index 9f39af4..ad3943d 100644 --- a/src/pepper_cli/tui/thread.py +++ b/src/pepper_cli/tui/thread.py @@ -14,6 +14,9 @@ from textual.app import ComposeResult from textual.widget import Widget +#: Refreshes an older page's scroll-past waits through for the new rows to be measured. +_KEEP_PLACE_TRIES = 5 + class ThreadScroll(VerticalScroll): """The scrolling body. ↑/↓ select a message (for ``r``) instead of scrolling a line; @@ -102,6 +105,10 @@ def __init__(self) -> None: super().__init__(id="thread") self._unseen = 0 self._was_pinned = True + #: Bumped when a different conversation is shown, so a keep-place retry left over + #: from the previous one stands down. Not by every ``show``: a live refresh of the same + #: conversation must not cancel the scroll past an older page it has not reached yet. + self._conversation = 0 def compose(self) -> ComposeResult: with ThreadScroll(id="thread-scroll"): @@ -139,11 +146,24 @@ def show( stick = reset or (self.pinned and not prepended) scroller = self.scroller old_height, old_y = scroller.virtual_size.height, scroller.scroll_y + if reset: + self._conversation += 1 + conversation = self._conversation self.query_one("#thread-body", Static).update(body) if prepended and not reset: - def keep_place() -> None: + def keep_place(tries: int = _KEEP_PLACE_TRIES) -> None: + if self._conversation != conversation or scroller.scroll_y != old_y: + # Another conversation was opened, or the reader scrolled, while this + # waited: either one owns the position now. + return grown = scroller.virtual_size.height - old_height + if grown <= 0 and tries > 1: + # The refresh this waited for can land before the layout that measures + # the new rows (a loaded loop); scrolling by 0 would leave the reader at + # the top of the longer thread. Wait for the next one. + scroller.call_after_refresh(keep_place, tries - 1) + return scroller.scroll_to(y=old_y + max(grown, 0), animate=False) scroller.call_after_refresh(keep_place) diff --git a/tests/test_tui_parity.py b/tests/test_tui_parity.py index 5d90c1e..a824dfa 100644 --- a/tests/test_tui_parity.py +++ b/tests/test_tui_parity.py @@ -3,11 +3,14 @@ from __future__ import annotations +import inspect import threading from datetime import UTC, datetime from typing import TYPE_CHECKING import pytest +from textual.geometry import Size +from textual.widget import Widget from pepper_cli.tui import app as tui_app from pepper_cli.tui import rows, state @@ -329,6 +332,119 @@ async def test_older_rows_keep_the_view_where_it_was() -> None: await until(pilot, lambda: scroller.scroll_y > 0) # not left at the top +def _older_thread_server() -> ts.FakeServer: + server = ts.standard_server() + _a1( + server, + *( + ts.message(f"m{i:02d}", f"2026-09-16T10:{i:02d}:00+00:00", f"body {i}") + for i in range(14) + ), + ) + return server + + +async def test_keep_place_retries_until_the_older_rows_are_measured(monkeypatch) -> None: + """The retry branch, driven by hand: the first callback runs before the layout that + measures the older rows, waits, and the next one scrolls past them. Out of tries, it + stops waiting.""" + app = tui_app.PepperApp(_older_thread_server().client()) + async with app.run_test(size=(100, 14)) as pilot: + await _open_a1(app, pilot, "body 13") + thread = app.thread + scroller = thread.scroller + scroller.scroll_y = 0 + await pilot.pause() + held: list = [] + scrolled: list[float] = [] + + def hold(cb, *_a, **_k) -> None: + # Only keep-place's own callbacks: the app may schedule others meanwhile. + if getattr(cb, "__name__", "") == "keep_place": + held.append(cb) + + monkeypatch.setattr(scroller, "call_after_refresh", hold) + monkeypatch.setattr(scroller, "scroll_to", lambda *, y, **_: scrolled.append(y)) + # Hold the measured height where it was, as a loaded loop does until its next layout. + real = inspect.getattr_static(Widget, "virtual_size") + old = scroller.virtual_size + measured = [old] + monkeypatch.setattr( + type(scroller), + "virtual_size", + property(lambda _w: measured[0], lambda w, v: real.__set__(w, v)), + raising=False, + ) + # Read here, not assumed 0: the app may itself have loaded an older page by now. + at = scroller.scroll_y + thread.show("\n".join(f"older {i}" for i in range(40)), prepended=True) + keep_place = held.pop() + keep_place() # not measured yet: grown is 0 + assert scrolled == [] # it did not scroll by 0 + retry = held.pop() # it waits for the next refresh instead + measured[0] = Size(old.width, old.height + 12) # that layout measures the rows + retry() + assert scrolled == [at + 12] # scrolled past exactly the rows added above + measured[0] = old + keep_place(1) # out of tries with nothing measured: scrolls by 0, waits no longer + assert scrolled == [at + 12, at] + assert held == [] + + +async def test_a_reset_stands_down_a_pending_keep_place(monkeypatch) -> None: + """A keep-place retry from an older page must not move a conversation shown after it.""" + app = tui_app.PepperApp(_older_thread_server().client()) + async with app.run_test(size=(100, 14)) as pilot: + await _open_a1(app, pilot, "body 13") + thread = app.thread + scroller = thread.scroller + scroller.scroll_y = 0 + await pilot.pause() + held: list = [] + monkeypatch.setattr(scroller, "call_after_refresh", lambda cb, *a, **k: held.append(cb)) + # Read here, not assumed 0: the app may itself have loaded an older page by now. + at = scroller.scroll_y + longer = "\n".join(f"older {i}" for i in range(40)) + "\n" + body_text(app) + thread.show(longer, prepended=True) + stale = next(cb for cb in reversed(held) if getattr(cb, "__name__", "") == "keep_place") + monkeypatch.undo() + thread.show("\n".join(f"other {i}" for i in range(120)), reset=True) + await until(pilot, lambda: scroller.scroll_y == scroller.max_scroll_y > 0) + # Back where the stale one started, so only its conversation tells it apart; with no + # await in between, nothing else gets to move the view. + scroller.scroll_y = at + scrolled: list[float] = [] + monkeypatch.setattr(scroller, "scroll_to", lambda *, y, **_: scrolled.append(y)) + stale(1) # its last try, where it would otherwise scroll whatever it measures + assert scrolled == [] + + +async def test_a_scroll_made_while_keep_place_waits_is_not_undone() -> None: + """VR-6b3a8f9f: the scroll past older rows waits a refresh or more; a reader who moves + in that window keeps the place they moved to.""" + server = ts.standard_server() + _a1( + server, + *( + ts.message(f"m{i:02d}", f"2026-09-16T10:{i:02d}:00+00:00", f"body {i}") + for i in range(14) + ), + ) + app = tui_app.PepperApp(server.client()) + async with app.run_test(size=(100, 14)) as pilot: + await _open_a1(app, pilot, "body 13") + thread = app.thread + scroller = thread.scroller + scroller.scroll_to(y=0, animate=False) + await pilot.pause() + longer = "\n".join(f"older {i}" for i in range(40)) + "\n" + body_text(app) + thread.show(longer, prepended=True) + scroller.scroll_y = 3 # the reader moves before keep_place runs + for _ in range(8): + await pilot.pause() + assert scroller.scroll_y == 3 + + async def test_o_opens_the_first_link_and_never_a_non_web_url() -> None: server = ts.standard_server() opened: list[str] = []