2026-05-08 18:02:05 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
|
|
|
UI_JS = (REPO / "static" / "ui.js").read_text(encoding="utf-8")
|
|
|
|
|
SESSIONS_JS = (REPO / "static" / "sessions.js").read_text(encoding="utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _function_body(src: str, signature: str) -> str:
|
|
|
|
|
start = src.index(signature)
|
|
|
|
|
brace = src.index("{", start)
|
|
|
|
|
depth = 0
|
|
|
|
|
for i in range(brace, len(src)):
|
|
|
|
|
if src[i] == "{":
|
|
|
|
|
depth += 1
|
|
|
|
|
elif src[i] == "}":
|
|
|
|
|
depth -= 1
|
|
|
|
|
if depth == 0:
|
|
|
|
|
return src[start : i + 1]
|
|
|
|
|
raise AssertionError(f"function body not found: {signature}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _scroll_listener_block() -> str:
|
|
|
|
|
start = UI_JS.index("el.addEventListener('scroll'")
|
|
|
|
|
return UI_JS[start : UI_JS.index("})();", start)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_clicking_current_session_is_noop_before_load_session_side_effects():
|
|
|
|
|
load_session = _function_body(SESSIONS_JS, "async function loadSession")
|
|
|
|
|
|
|
|
|
|
current_idx = load_session.index("const currentSid = S.session ? S.session.session_id : null")
|
2026-05-14 21:51:38 +00:00
|
|
|
noop_idx = load_session.index("if(currentSid===sid && !forceReload) return")
|
2026-05-08 18:02:05 +00:00
|
|
|
loading_idx = load_session.index("_loadingSessionId = sid")
|
|
|
|
|
stop_idx = load_session.index("stopApprovalPolling")
|
|
|
|
|
|
|
|
|
|
assert current_idx < noop_idx < loading_idx < stop_idx, (
|
|
|
|
|
"clicking the already-open sidebar row must be a no-op before loadSession() "
|
|
|
|
|
"mutates loading/runtime state or scroll-affecting UI"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_scroll_to_bottom_settles_across_late_markdown_layout_growth():
|
|
|
|
|
settle = _function_body(UI_JS, "function _settleMessageScrollToBottom")
|
|
|
|
|
scroll = _function_body(UI_JS, "function scrollToBottom")
|
|
|
|
|
pinned = _function_body(UI_JS, "function scrollIfPinned")
|
|
|
|
|
|
2026-06-13 03:39:11 +00:00
|
|
|
# The settle survives late markdown layout growth (Prism/KaTeX/Mermaid/images)
|
|
|
|
|
# via a ResizeObserver on the growing content node (#msgInner) plus a 2s
|
|
|
|
|
# static-content fallback — this replaced the old [0,16,80,180]ms setTimeout
|
|
|
|
|
# fan-out + double-rAF scrollHeight polling (#3920, Firefox reflow jitter).
|
2026-05-08 18:02:05 +00:00
|
|
|
assert "requestAnimationFrame" in settle
|
|
|
|
|
assert "setTimeout" in settle
|
2026-06-13 03:39:11 +00:00
|
|
|
assert "new ResizeObserver" in settle
|
|
|
|
|
assert "getElementById('msgInner')" in settle, (
|
|
|
|
|
"the ResizeObserver must observe the growing content node (#msgInner), "
|
|
|
|
|
"not the fixed #messages scroll container"
|
|
|
|
|
)
|
|
|
|
|
assert "2000" in settle, "a 2s fallback must cover fully-static content that never resizes"
|
2026-06-13 04:20:25 +00:00
|
|
|
# scrollToBottom uses force=false so the observer runs, and explicit=true so the
|
|
|
|
|
# settle still runs even when Auto-follow is off (explicit user jump). The
|
|
|
|
|
# automatic scrollIfPinned() path passes no explicit flag (stays auto-gated).
|
|
|
|
|
assert "_settleMessageScrollToBottom(false, true)" in scroll
|
2026-05-08 18:02:05 +00:00
|
|
|
assert "_settleMessageScrollToBottom(false)" in pinned
|
|
|
|
|
assert "!_scrollPinned" in settle
|
|
|
|
|
assert "const token=++_bottomSettleToken" in settle
|
|
|
|
|
assert "token!==_bottomSettleToken" in settle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_scroll_to_bottom_writes_scroll_position_immediately_before_delayed_settle():
|
|
|
|
|
scroll = _function_body(UI_JS, "function scrollToBottom")
|
|
|
|
|
|
|
|
|
|
immediate_idx = scroll.index("_setMessageScrollToBottom();")
|
2026-06-13 04:20:25 +00:00
|
|
|
settle_idx = scroll.index("_settleMessageScrollToBottom(false, true)")
|
2026-05-08 18:02:05 +00:00
|
|
|
|
|
|
|
|
assert immediate_idx < settle_idx, (
|
2026-06-13 03:39:11 +00:00
|
|
|
"scrollToBottom() must write scrollTop synchronously before scheduling the "
|
|
|
|
|
"ResizeObserver settle; otherwise a DOM-rebuild scroll event can cancel the "
|
|
|
|
|
"delayed settle and strand the viewport at the top"
|
2026-05-08 18:02:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_message_scroll_listener_does_not_downgrade_explicit_bottom_pin_on_first_near_bottom_event():
|
|
|
|
|
listener_block = _scroll_listener_block()
|
|
|
|
|
set_bottom = _function_body(UI_JS, "function _setMessageScrollToBottom")
|
|
|
|
|
|
|
|
|
|
assert "_nearBottomCount=2" in set_bottom
|
|
|
|
|
assert "_scrollPinned=_nearBottomCount>=2" not in listener_block
|
|
|
|
|
assert "if(_nearBottomCount>=2) _scrollPinned=true" in listener_block
|
2026-06-01 20:34:06 +00:00
|
|
|
assert "_scrollPinned=false" in listener_block
|
2026-05-08 18:02:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_user_scroll_cancels_delayed_bottom_settling():
|
|
|
|
|
listener_block = _scroll_listener_block()
|
|
|
|
|
record = _function_body(UI_JS, "function _recordNonMessageScrollIntent")
|
2026-06-06 19:12:37 +00:00
|
|
|
pinned = _function_body(UI_JS, "function scrollIfPinned")
|
2026-05-08 18:02:05 +00:00
|
|
|
|
|
|
|
|
assert "function _cancelBottomSettle" in UI_JS
|
|
|
|
|
assert "_cancelBottomSettle();" in listener_block
|
|
|
|
|
assert "e.deltaY<0" in record
|
|
|
|
|
assert "_cancelBottomSettle();" in record
|
|
|
|
|
assert "_scrollPinned=false" in record
|
2026-06-06 19:12:37 +00:00
|
|
|
assert "if(_messageUserUnpinned) return;" in pinned
|
|
|
|
|
assert "_recentMessageUpwardIntent()" not in pinned
|
2026-05-09 00:02:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_preserve_scroll_restores_unpinned_viewport_after_dom_rebuild():
|
|
|
|
|
render = _function_body(UI_JS, "function renderMessages")
|
|
|
|
|
after_render = _function_body(UI_JS, "function _scrollAfterMessageRender")
|
2026-06-06 19:12:37 +00:00
|
|
|
follow = _function_body(UI_JS, "function _followMessagesAfterDomReplace")
|
2026-05-09 00:02:03 +00:00
|
|
|
restore = _function_body(UI_JS, "function _restoreMessageScrollSnapshot")
|
|
|
|
|
|
2026-06-13 04:20:25 +00:00
|
|
|
snapshot_idx = render.index("const scrollSnapshot=(preserveScroll||(!_autoScrollFollow&&_messageUserUnpinned))?_captureMessageScrollSnapshot():null")
|
2026-05-09 00:02:03 +00:00
|
|
|
inner_idx = render.index("const inner=$('msgInner')")
|
|
|
|
|
final_scroll_idx = render.rindex("_scrollAfterMessageRender(preserveScroll, scrollSnapshot)")
|
|
|
|
|
|
|
|
|
|
assert snapshot_idx < inner_idx < final_scroll_idx, (
|
|
|
|
|
"renderMessages({preserveScroll:true}) must capture #messages.scrollTop before "
|
|
|
|
|
"replacing transcript DOM, then pass that snapshot to the post-render scroll helper"
|
|
|
|
|
)
|
fix(scroll): guard _scrollAfterMessageRender and _finishDone against pin hysteresis snap-back
Two scroll-state bugs cause the viewport to snap to the bottom after
streaming completes, even when the user has scrolled up to read:
1. _scrollAfterMessageRender (ui.js): when preserveScroll=true and
_scrollPinned=true (re-set by the 250px near-bottom hysteresis),
_followMessagesAfterDomReplace() calls scrollToBottom(), overriding
the user's position. Added !_messageUserUnpinned guard so explicitly
scrolled-up users get their snapshot restored instead.
2. _finishDone (messages.js): the explicit scrollToBottom() at L3302
fires when shouldFollowOnDone is true, but that flag only checks a
120px near-bottom threshold. Added _isMessagePaneNearBottom(250)
gate so users reading 250px+ above bottom keep their position.
Also adds a new 'auto_scroll_follow' setting (default: off) that gates
scrollIfPinned() and _shouldFollowMessagesOnDomReplace(). When disabled,
the viewport never auto-scrolls during streaming — the user controls
scroll position manually and uses the ↓ button to jump to bottom.
7-file setting-add pattern applied:
- api/config.py: _SETTINGS_DEFAULTS + _SETTINGS_BOOL_KEYS
- static/boot.js: both init paths
- static/ui.js: scrollIfPinned() + _shouldFollowMessagesOnDomReplace()
- static/index.html: checkbox in appearance settings
- static/i18n.js: en + zh-CN translations
- static/panels.js: payload, autosave, populate, full save
- messages.js: no additional changes (gated via _shouldFollowMessagesOnDomReplace)
Tests updated: test_tars_scroll_reset_regressions, test_issue1690,
test_issue3545. All 116 scroll-related tests pass.
2026-06-11 19:48:14 +00:00
|
|
|
assert "if(!_messageUserUnpinned && _followMessagesAfterDomReplace()) return;" in after_render
|
2026-06-08 22:58:13 +00:00
|
|
|
assert "_restoreMessageScrollSnapshot(scrollSnapshot);\n _maybeShowNewMessageScrollCue(scrollSnapshot);" in after_render
|
2026-06-06 19:12:37 +00:00
|
|
|
assert "_shouldFollowMessagesOnDomReplace()" in follow
|
|
|
|
|
assert "scrollToBottom();" in follow
|
2026-05-09 00:02:03 +00:00
|
|
|
assert "el.scrollTop=Math.max(0,Math.min(Number(snapshot.top)||0,maxTop))" in restore
|
|
|
|
|
assert "_programmaticScroll=true" in restore
|