@@ -930,6 +930,72 @@ describe("MessagesTimeline history and thread links", () => {
930930 expect ( onOpenThread ) . toHaveBeenCalledWith ( "thread-parent" ) ;
931931 } ) ;
932932
933+ it ( "opens a thread at its newest message after an empty pane and restores it on return" , async ( ) => {
934+ const props = baseProps ( ) ;
935+ const entries = Array . from ( { length : 12 } , ( _ , index ) =>
936+ message (
937+ Array . from ( { length : 60 } , ( _ , line ) => `Paragraph ${ index } .${ line } ` ) . join ( "\n\n" ) ,
938+ { role : "assistant" } ,
939+ `message-${ index } ` ,
940+ ) ,
941+ ) ;
942+ await renderTimeline ( [ ] , {
943+ ...props ,
944+ routeThreadKey : `${ props . routeThreadKey } -loading` ,
945+ hideEmptyPlaceholder : true ,
946+ } ) ;
947+
948+ // Each row is much taller than LegendList's estimate.
949+ await act ( async ( ) =>
950+ roots . at ( - 1 ) ! . render ( < MessagesTimeline { ...props } timelineEntries = { entries } /> ) ,
951+ ) ;
952+ const viewport = props . listRef . current ! . getScrollableNode ( ) ;
953+ await vi . waitFor ( ( ) => {
954+ expect ( viewport . scrollHeight ) . toBeGreaterThan ( viewport . clientHeight ) ;
955+ expect ( text ( ) ) . toContain ( "Paragraph 11.59" ) ;
956+ expect (
957+ viewport . scrollHeight - viewport . clientHeight - viewport . scrollTop ,
958+ ) . toBeLessThanOrEqual ( 1 ) ;
959+ } ) ;
960+
961+ const otherThreadKey = `${ props . routeThreadKey } -other` ;
962+ await act ( async ( ) =>
963+ roots
964+ . at ( - 1 ) !
965+ . render (
966+ < MessagesTimeline
967+ { ...props }
968+ routeThreadKey = { otherThreadKey }
969+ timelineEntries = { entries }
970+ liveFollowEnabled = { false }
971+ /> ,
972+ ) ,
973+ ) ;
974+ await vi . waitFor ( ( ) =>
975+ expect (
976+ viewport . scrollHeight - viewport . clientHeight - viewport . scrollTop ,
977+ ) . toBeLessThanOrEqual ( 1 ) ,
978+ ) ;
979+ // Move beyond LegendList's one-viewport follow threshold so it cannot mask restoration.
980+ await userEvent . wheel ( viewport , { delta : { y : - 2000 } } ) ;
981+ await vi . waitFor ( ( ) => {
982+ expect ( readTimelinePosition ( otherThreadKey ) ?. atEnd ) . toBe ( false ) ;
983+ expect ( viewport . scrollHeight - viewport . clientHeight - viewport . scrollTop ) . toBeGreaterThan (
984+ viewport . clientHeight ,
985+ ) ;
986+ } ) ;
987+
988+ await act ( async ( ) =>
989+ roots . at ( - 1 ) ! . render ( < MessagesTimeline { ...props } timelineEntries = { entries } /> ) ,
990+ ) ;
991+ await vi . waitFor ( ( ) => {
992+ expect ( text ( ) ) . toContain ( "Paragraph 11.59" ) ;
993+ expect (
994+ viewport . scrollHeight - viewport . clientHeight - viewport . scrollTop ,
995+ ) . toBeLessThanOrEqual ( 1 ) ;
996+ } ) ;
997+ } ) ;
998+
933999 it ( "does not render the transient V2 interruption request" , async ( ) => {
9341000 await renderTimeline ( [
9351001 {
@@ -1299,7 +1365,7 @@ describe("MessagesTimeline scroll position", () => {
12991365 expect ( maxNestedCommits ) . toBeLessThan ( 8 ) ;
13001366 } ) ;
13011367
1302- it ( "remembers a manual reading position when the chat closes " , async ( ) => {
1368+ it ( "restores a manual reading position after reopening and switching threads " , async ( ) => {
13031369 const props = baseProps ( ) ;
13041370 function Chat ( ) {
13051371 const [ liveFollowEnabled , setLiveFollowEnabled ] = useState ( true ) ;
@@ -1326,6 +1392,26 @@ describe("MessagesTimeline scroll position", () => {
13261392 atEnd : false ,
13271393 scrollOffset : offset ,
13281394 } ) ;
1395+
1396+ const timeline = ( routeThreadKey : string ) => (
1397+ < MessagesTimeline { ...props } routeThreadKey = { routeThreadKey } timelineEntries = { tallEntries } />
1398+ ) ;
1399+ await renderElement ( timeline ( props . routeThreadKey ) ) ;
1400+ const reopenedViewport = await waitForList ( props ) ;
1401+ await vi . waitFor ( ( ) =>
1402+ expect ( Math . abs ( reopenedViewport . scrollTop - offset ) ) . toBeLessThanOrEqual ( 1 ) ,
1403+ ) ;
1404+
1405+ await act ( async ( ) => roots . at ( - 1 ) ! . render ( timeline ( `${ props . routeThreadKey } -other` ) ) ) ;
1406+ await vi . waitFor ( ( ) =>
1407+ expect (
1408+ reopenedViewport . scrollHeight - reopenedViewport . clientHeight - reopenedViewport . scrollTop ,
1409+ ) . toBeLessThanOrEqual ( 1 ) ,
1410+ ) ;
1411+ await act ( async ( ) => roots . at ( - 1 ) ! . render ( timeline ( props . routeThreadKey ) ) ) ;
1412+ await vi . waitFor ( ( ) =>
1413+ expect ( Math . abs ( reopenedViewport . scrollTop - offset ) ) . toBeLessThanOrEqual ( 1 ) ,
1414+ ) ;
13291415 } ) ;
13301416
13311417 it ( "restores the composer after closing tool output only at the end" , async ( ) => {
0 commit comments