@@ -9,11 +9,128 @@ import {
99 installMockGateway ,
1010 requireRecord ,
1111 sidebarSessionOrder ,
12+ waitForChatScrollIdle ,
1213} from "./chat-flow.test-support.ts" ;
1314
1415const suite = createChatFlowE2eSuite ( ) ;
1516
1617suite . define ( ( ) => {
18+ it ( "restores a scrolled session after switching away while new messages arrive" , async ( ) => {
19+ const context = await suite . newBrowserContext ( {
20+ locale : "en-US" ,
21+ serviceWorkers : "block" ,
22+ viewport : { height : 720 , width : 1280 } ,
23+ } ) ;
24+ const page = await context . newPage ( ) ;
25+ const sessionA = "agent:main:session-a" ;
26+ const sessionB = "agent:main:session-b" ;
27+ const messages = ( label : string , count : number ) =>
28+ Array . from ( { length : count } , ( _ , index ) => ( {
29+ role : index % 2 === 0 ? "user" : "assistant" ,
30+ content : `${ label } message ${ index } : ${ "wrapped transcript content " . repeat ( 8 ) } ` ,
31+ timestamp : 1_000 + index ,
32+ __openclaw : { seq : index + 1 } ,
33+ } ) ) ;
34+ const response = ( sessionKey : string , transcript : unknown [ ] ) => ( {
35+ messages : transcript ,
36+ sessionId : `${ sessionKey } :backing` ,
37+ thinkingLevel : null ,
38+ } ) ;
39+ const responseCases = ( messagesA : unknown [ ] , messagesB = messages ( "B" , 30 ) ) => ( {
40+ cases : [
41+ { match : { sessionKey : sessionA } , response : response ( sessionA , messagesA ) } ,
42+ { match : { sessionKey : sessionB } , response : response ( sessionB , messagesB ) } ,
43+ ] ,
44+ } ) ;
45+ const initialMessagesA = messages ( "A" , 70 ) ;
46+ const initialResponses = responseCases ( initialMessagesA ) ;
47+ const gateway = await installMockGateway ( page , {
48+ methodResponses : {
49+ "chat.history" : initialResponses ,
50+ "chat.startup" : initialResponses ,
51+ "sessions.list" : chatSessionListResponse ( ) ,
52+ } ,
53+ sessionKey : sessionA ,
54+ } ) ;
55+
56+ try {
57+ await page . goto ( `${ suite . server . baseUrl } chat` ) ;
58+ await waitForChatScrollIdle ( page ) ;
59+ const thread = page . locator ( ".chat-thread" ) ;
60+ await expect . poll ( ( ) => thread . count ( ) ) . toBe ( 1 ) ;
61+ const initialDistance = await thread . evaluate ( ( element ) => {
62+ const transcript = element as HTMLElement ;
63+ return transcript . scrollHeight - transcript . scrollTop - transcript . clientHeight ;
64+ } ) ;
65+ expect ( initialDistance ) . toBeLessThanOrEqual ( 8 ) ;
66+ const storedScrollTop = await thread . evaluate ( ( element ) => {
67+ const transcript = element as HTMLElement ;
68+ transcript . scrollTop = Math . floor ( ( transcript . scrollHeight - transcript . clientHeight ) / 3 ) ;
69+ transcript . dispatchEvent ( new Event ( "scroll" , { bubbles : true } ) ) ;
70+ return transcript . scrollTop ;
71+ } ) ;
72+ expect ( storedScrollTop ) . toBeGreaterThan ( 0 ) ;
73+
74+ const sessionLink = ( sessionKey : string ) =>
75+ page . locator (
76+ `.sidebar-recent-session[data-session-key="${ sessionKey } "] a.sidebar-recent-session__link` ,
77+ ) ;
78+ await sessionLink ( sessionB ) . click ( ) ;
79+ await expect . poll ( ( ) => new URL ( page . url ( ) ) . pathname ) . toBe ( controlUiSessionPath ( sessionB ) ) ;
80+ await waitForChatScrollIdle ( page ) ;
81+ const firstVisitDistance = await thread . evaluate ( ( element ) => {
82+ const transcript = element as HTMLElement ;
83+ return transcript . scrollHeight - transcript . scrollTop - transcript . clientHeight ;
84+ } ) ;
85+ expect ( firstVisitDistance ) . toBeLessThanOrEqual ( 8 ) ;
86+
87+ const messagesAWithNewTail = messages ( "A" , 78 ) ;
88+ const updatedResponses = responseCases ( messagesAWithNewTail ) ;
89+ await gateway . setMethodResponse ( "chat.history" , updatedResponses ) ;
90+ await gateway . setMethodResponse ( "chat.startup" , updatedResponses ) ;
91+ const historyRequestsBeforeReturn = ( await gateway . getRequests ( "chat.history" ) ) . length ;
92+ await sessionLink ( sessionA ) . click ( ) ;
93+ await expect . poll ( ( ) => new URL ( page . url ( ) ) . pathname ) . toBe ( controlUiSessionPath ( sessionA ) ) ;
94+ await expect
95+ . poll ( async ( ) => ( await gateway . getRequests ( "chat.history" ) ) . length )
96+ . toBeGreaterThan ( historyRequestsBeforeReturn ) ;
97+ await waitForChatScrollIdle ( page ) ;
98+
99+ const restored = await thread . evaluate ( ( element ) => {
100+ const transcript = element as HTMLElement ;
101+ return {
102+ distanceFromBottom :
103+ transcript . scrollHeight - transcript . scrollTop - transcript . clientHeight ,
104+ scrollTop : transcript . scrollTop ,
105+ } ;
106+ } ) ;
107+ expect (
108+ Math . abs ( restored . scrollTop - storedScrollTop ) ,
109+ JSON . stringify ( { restored, storedScrollTop } ) ,
110+ ) . toBeLessThanOrEqual ( 120 ) ;
111+ expect ( restored . distanceFromBottom ) . toBeGreaterThan ( 8 ) ;
112+
113+ const messagesBWithNewTail = messages ( "B" , 36 ) ;
114+ const endAnchoredResponses = responseCases ( messagesAWithNewTail , messagesBWithNewTail ) ;
115+ await gateway . setMethodResponse ( "chat.history" , endAnchoredResponses ) ;
116+ await gateway . setMethodResponse ( "chat.startup" , endAnchoredResponses ) ;
117+ const historyRequestsBeforeEndReturn = ( await gateway . getRequests ( "chat.history" ) ) . length ;
118+ await sessionLink ( sessionB ) . click ( ) ;
119+ await expect . poll ( ( ) => new URL ( page . url ( ) ) . pathname ) . toBe ( controlUiSessionPath ( sessionB ) ) ;
120+ await expect
121+ . poll ( async ( ) => ( await gateway . getRequests ( "chat.history" ) ) . length )
122+ . toBeGreaterThan ( historyRequestsBeforeEndReturn ) ;
123+ await waitForChatScrollIdle ( page ) ;
124+ const endAnchoredDistance = await thread . evaluate ( ( element ) => {
125+ const transcript = element as HTMLElement ;
126+ return transcript . scrollHeight - transcript . scrollTop - transcript . clientHeight ;
127+ } ) ;
128+ expect ( endAnchoredDistance ) . toBeLessThanOrEqual ( 8 ) ;
129+ } finally {
130+ await suite . closeBrowserContext ( context ) ;
131+ }
132+ } ) ;
133+
17134 it ( "renders always-on pane headers without desktop topbar chrome" , async ( ) => {
18135 const context = await suite . newBrowserContext ( {
19136 locale : "en-US" ,
0 commit comments