test(web): extract chat auto-scroll state machine into pure helper#365
Closed
neilhtennek wants to merge 1 commit intoOpenKnots:mainfrom
Closed
test(web): extract chat auto-scroll state machine into pure helper#365neilhtennek wants to merge 1 commit intoOpenKnots:mainfrom
neilhtennek wants to merge 1 commit intoOpenKnots:mainfrom
Conversation
Pull the auto-scroll intent logic out of ChatView.onMessagesScroll and into a pure computeNextAutoScrollState helper in chat-scroll.ts, with unit-test coverage for the edge cases listed in OpenKnots#13. The previous implementation lived inline as a four-branch tower of conditionals operating on refs, which made it impossible to exercise without rendering the full chat tree. Behavior is intentionally unchanged: each branch maps 1:1 to the same outcome as before, including the subtle 'wheel intent is consumed even on a no-op scroll' case. Tests added cover: - user scrolled up, then submits a message -> re-engages auto-scroll - streaming response while user is at the bottom - multiple rapid optimistic submits do not flip the flag - wheel-flagged scroll up beyond tolerance disables auto-scroll - wheel intent is cleared even on a no-op scroll - pointer-drag scroll up disables auto-scroll - sub-pixel jitter during a pointer drag is ignored - keyboard / assistive scroll up disables auto-scroll - downward keyboard scroll is a no-op - baseline no-op when already off and still above the bottom Also adds a few isScrollContainerNearBottom edge cases (rubber-band overshoot, exact threshold boundary, zero-size container). Closes OpenKnots#13
|
@neilhtennek is attempting to deploy a commit to the 0xBuns Team on Vercel. A member of the Team first needs to authorize it. |
This comment was marked as abuse.
This comment was marked as abuse.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #13.
Pulls the auto-scroll intent logic out of
ChatView.onMessagesScrolland into a purecomputeNextAutoScrollStatehelper inchat-scroll.ts, with unit-test coverage for the edge cases listed in the issue.The previous implementation lived inline as a four-branch tower of conditionals operating on refs, which made it impossible to exercise without rendering the full chat tree. Behavior is intentionally unchanged — each branch maps 1:1 to the same outcome as before, including the subtle "wheel intent is consumed even on a no-op scroll" case.
Why a refactor instead of browser tests
The issue suggests Playwright browser tests, and the repo does have a
vitest-browsersetup. But the auto-scroll decisions are pure state transitions over a handful of booleans and numbers — they don't need a real DOM to verify. Extracting them to a pure function gives:currentScrollTop < lastKnownScrollTopRef.current - 1three times)The actual DOM-level "does it scroll?" path is already covered by
ChatView.browser.tsx, so this PR focuses on the part that wasn't testable.Changes
apps/web/src/chat-scroll.ts— addcomputeNextAutoScrollState+SCROLL_UP_DETECTION_TOLERANCE_PXconstant + typesapps/web/src/chat-scroll.test.ts— 10 new tests for the state machine, plus 3 new edge cases forisScrollContainerNearBottom(rubber-band overshoot, exact boundary, zero-size container)apps/web/src/components/ChatView.tsx— replace the inline branch tower inonMessagesScrollwith one call to the helperIssue #13 checklist coverage
re-engages auto-scroll when the user returns to the bottomforceStickToBottom(existing); state machine verifies it stays stickykeeps auto-scroll on for a streaming response while user is at bottomforceStickToBottom+ virtualizer; state machine no longer flips off mid-scrollkeeps auto-scroll on across rapid optimistic submits at the bottomPlus extra coverage for paths the inline code had but no test for: pointer-drag scroll up, sub-pixel jitter during pointer drag, keyboard scroll up vs down, wheel-intent consumption on no-op scrolls.
Test plan
npx vitest run src/chat-scroll.test.ts→ 18/18 passingtsc --noEmit→ cleanFiled by @neilhtennek / @claudeloop