@@ -17,9 +17,10 @@ import {
1717import { renderToStaticMarkup } from "react-dom/server" ;
1818import { create , type ReactTestRenderer } from "react-test-renderer" ;
1919import { shouldUseRestingComposerLayout } from "../composerFooterLayout" ;
20+ import { readTimelinePosition } from "./timelineScrollAnchoring" ;
2021import { useComposerFocusState } from "./useComposerFocusState" ;
2122import { beforeAll , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
22- import type { LegendListRef } from "@legendapp/list/react" ;
23+ import { LegendList , type LegendListRef } from "@legendapp/list/react" ;
2324
2425const activityTestState = vi . hoisted ( ( ) => ( {
2526 expanded : false ,
@@ -292,6 +293,7 @@ function buildProps() {
292293 onAnchorSizeChanged : ( ) => { } ,
293294 contentInsetEndAdjustment : 0 ,
294295 liveFollowEnabled : true ,
296+ isLiveFollowLatched : ( ) => true ,
295297 onIsAtEndChange : ( ) => { } ,
296298 onManualNavigation : ( ) => { } ,
297299 } ;
@@ -1108,6 +1110,92 @@ describe("MessagesTimeline", () => {
11081110 expect ( markup ) . not . toContain ( "<a " ) ;
11091111 } ) ;
11101112
1113+ it ( "remembers a live-following thread at the end while the follow scroll trails output" , ( ) => {
1114+ vi . stubGlobal ( "requestAnimationFrame" , ( ) => 0 ) ;
1115+ vi . stubGlobal ( "cancelAnimationFrame" , ( ) => { } ) ;
1116+ vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
1117+ const threadKey = "env-1:thread-following" ;
1118+ const entries = [
1119+ {
1120+ id : "entry-follow-work" ,
1121+ kind : "work" as const ,
1122+ createdAt : MESSAGE_CREATED_AT ,
1123+ entry : {
1124+ id : "work-follow" ,
1125+ createdAt : MESSAGE_CREATED_AT ,
1126+ toolCallId : "call-follow" ,
1127+ label : "Run lint" ,
1128+ tone : "tool" as const ,
1129+ itemType : "command_execution" as const ,
1130+ command : "pnpm lint" ,
1131+ toolLifecycleStatus : "completed" as const ,
1132+ } ,
1133+ } ,
1134+ ] ;
1135+ let renderer ! : ReactTestRenderer ;
1136+ const list = ( ) => renderer . root . findByType ( LegendList ) ;
1137+ let latched = true ;
1138+ const props = { ...buildProps ( ) , isLiveFollowLatched : ( ) => latched } ;
1139+ // Streamed output has grown the list 500px past the viewport, and the
1140+ // follow scroll has not caught up yet.
1141+ props . listRef . current = {
1142+ getState : ( ) => ( {
1143+ data : list ( ) . props . data ,
1144+ isAtEnd : false ,
1145+ contentLength : 2000 ,
1146+ scroll : 700 ,
1147+ scrollLength : 800 ,
1148+ positionAtIndex : ( ) => 0 ,
1149+ indexByKey : ( ) => 0 ,
1150+ elementAtIndex : ( ) => ( { getBoundingClientRect : ( ) => ( { top : - 700 } ) } ) ,
1151+ } ) ,
1152+ getScrollableNode : ( ) => ( { scrollTop : 700 , getBoundingClientRect : ( ) => ( { top : 0 } ) } ) ,
1153+ } as unknown as LegendListRef ;
1154+ const renderTimeline = (
1155+ liveFollowEnabled : boolean ,
1156+ anchorMessageId : MessageId | null = null ,
1157+ ) => (
1158+ < MessagesTimeline
1159+ { ...props }
1160+ isWorking
1161+ liveFollowEnabled = { liveFollowEnabled }
1162+ anchorMessageId = { anchorMessageId }
1163+ routeThreadKey = { threadKey }
1164+ timelineEntries = {
1165+ anchorMessageId ? [ buildUserTimelineEntry ( "First send" ) , ...entries ] : entries
1166+ }
1167+ />
1168+ ) ;
1169+ try {
1170+ act ( ( ) => {
1171+ renderer = create ( renderTimeline ( true ) ) ;
1172+ } ) ;
1173+ act ( ( ) => list ( ) . props . onScroll ( ) ) ;
1174+ expect ( readTimelinePosition ( threadKey ) ?. atEnd ) . toBe ( true ) ;
1175+
1176+ // A gesture releases the latch before the next render turns follow off.
1177+ latched = false ;
1178+ act ( ( ) => list ( ) . props . onScroll ( ) ) ;
1179+ expect ( readTimelinePosition ( threadKey ) ) . toMatchObject ( { atEnd : false , scrollOffset : 700 } ) ;
1180+ latched = true ;
1181+ act ( ( ) => list ( ) . props . onScroll ( ) ) ;
1182+ expect ( readTimelinePosition ( threadKey ) ?. atEnd ) . toBe ( true ) ;
1183+
1184+ // Once the user scrolls away, the same offset is a reading position.
1185+ act ( ( ) => renderer . update ( renderTimeline ( false ) ) ) ;
1186+ act ( ( ) => list ( ) . props . onScroll ( ) ) ;
1187+ expect ( readTimelinePosition ( threadKey ) ) . toMatchObject ( { atEnd : false , scrollOffset : 700 } ) ;
1188+
1189+ // A first send anchored near the top is not following the end either.
1190+ act ( ( ) => renderer . update ( renderTimeline ( true , MessageId . make ( "message-1" ) ) ) ) ;
1191+ act ( ( ) => list ( ) . props . onScroll ( ) ) ;
1192+ expect ( readTimelinePosition ( threadKey ) ) . toMatchObject ( { atEnd : false , scrollOffset : 700 } ) ;
1193+ } finally {
1194+ act ( ( ) => renderer ?. unmount ( ) ) ;
1195+ vi . unstubAllGlobals ( ) ;
1196+ }
1197+ } ) ;
1198+
11111199 it ( "keeps reserved end space when tool work starts while reading history" , ( ) => {
11121200 const runId = RunId . make ( "run-with-active-tool" ) ;
11131201 const firstEntry = buildUserTimelineEntry ( "Run the command." ) ;
0 commit comments