@@ -23,23 +23,30 @@ for (const endpoint of ["/api/location", "/api/agent"]) {
2323 let requests = 0
2424 await page . route ( "**/api/**" , ( route ) => {
2525 const url = new URL ( route . request ( ) . url ( ) )
26+
2627 if ( url . pathname !== endpoint || url . searchParams . get ( "location[directory]" ) !== directory )
2728 return route . fallback ( )
2829 requests ++
30+
2931 if ( recover && requests > 1 ) return route . fallback ( )
32+
3033 return route . fulfill ( { status : 500 , body : "" , headers : { "access-control-allow-origin" : "*" } } )
3134 } )
35+
3236 const failure = page . waitForResponse (
3337 ( response ) => new URL ( response . url ( ) ) . pathname === endpoint && response . status ( ) === 500 ,
3438 )
39+
3540 const settled = page . waitForResponse ( ( response ) => {
3641 const url = new URL ( response . url ( ) )
42+
3743 return (
3844 url . pathname === endpoint &&
3945 url . searchParams . get ( "location[directory]" ) === directory &&
4046 ( recover ? response . ok ( ) : requests === 3 && response . status ( ) === 500 )
4147 )
4248 } )
49+
4350 await page . goto ( `/server/${ base64Encode ( fixture . serverKey ) } /session/${ sessionID } ` )
4451 await failure
4552 await expect ( page . getByText ( "Keep working in this worktree" , { exact : true } ) ) . toBeVisible ( )
@@ -86,10 +93,12 @@ test("follows a live session move while the agent catalog is still loading", asy
8693 } )
8794 await page . route ( "**/api/**" , async ( route ) => {
8895 const url = new URL ( route . request ( ) . url ( ) )
96+
8997 if ( url . pathname === "/api/agent" && url . searchParams . get ( "location[directory]" ) === directory ) {
9098 requested . resolve ( )
9199 await release . promise
92100 }
101+
93102 return route . fallback ( )
94103 } )
95104 await page . goto ( `/server/${ base64Encode ( fixture . serverKey ) } /session/${ sessionID } ` )
@@ -99,10 +108,13 @@ test("follows a live session move while the agent catalog is still loading", asy
99108 await prompt . fill ( "Keep this draft" )
100109 await expect ( page . getByText ( "Session location unavailable" , { exact : true } ) ) . toHaveCount ( 0 )
101110 await transport . waitForConnection ( )
111+
102112 const resolved = page . waitForResponse ( ( response ) => {
103113 const url = new URL ( response . url ( ) )
114+
104115 return url . pathname === "/api/agent" && url . searchParams . get ( "location[directory]" ) === destination && response . ok ( )
105116 } )
117+
106118 session . directory = destination
107119 await transport . send ( {
108120 id : "evt_location_moved_while_loading" ,
@@ -112,10 +124,13 @@ test("follows a live session move while the agent catalog is still loading", asy
112124 data : { sessionID, location : { directory : destination } , projectID : fixture . project . id } ,
113125 } )
114126 await resolved
127+
115128 const delayed = page . waitForResponse ( ( response ) => {
116129 const url = new URL ( response . url ( ) )
130+
117131 return url . pathname === "/api/agent" && url . searchParams . get ( "location[directory]" ) === directory && response . ok ( )
118132 } )
133+
119134 release . resolve ( )
120135 await delayed
121136 await expect ( prompt ) . toBeEditable ( )
@@ -127,6 +142,76 @@ test("follows a live session move while the agent catalog is still loading", asy
127142 expect ( recovery ) . toEqual ( [ ] )
128143} )
129144
145+ test ( "keeps the side panel's tabs and selection when the session moves" , async ( { page } ) => {
146+ const directory = "/projects/tabs-before-move"
147+ const destination = "/projects/tabs-after-move"
148+ const sessionID = "ses_location_moved_tabs"
149+ const session = { id : sessionID , projectID : fixture . project . id , directory, title : "Moved tabs" }
150+ const transport = await installSseTransport ( page , { server : fixture . serverKey } )
151+ await mockOpenCodeServer ( page , {
152+ directory : fixture . directory ,
153+ project : fixture . project ,
154+ provider : fixture . provider ,
155+ sessions : [ session ] ,
156+ fileList : ( ) => [ ] ,
157+ findFiles : ( input ) => [ "README.md" , "NOTES.md" ] . filter ( ( name ) => name . startsWith ( input . query ) ) ,
158+ fileContent : ( path ) => ( { type : "text" , content : `contents:${ session . directory } :${ path } ` } ) ,
159+ pageMessages : ( ) => ( {
160+ items : [ { id : "msg_saved" , type : "user" , text : "Keep my tabs" , time : { created : 1 } } ] ,
161+ } ) ,
162+ } )
163+ await page . goto ( `/server/${ base64Encode ( fixture . serverKey ) } /session/${ sessionID } ` )
164+ await expect ( page . getByRole ( "textbox" , { name : "Prompt" , exact : true } ) ) . toBeEditable ( )
165+
166+ const panel = page . getByRole ( "complementary" , { name : "Review and files" } )
167+ const files = panel . getByRole ( "tab" , { name : / \. m d $ / } )
168+ const notes = panel . getByRole ( "tab" , { name : "NOTES.md" , exact : true } )
169+
170+ // A lost selection would fall back to the first file tab, README.md, so NOTES.md stays selected only if it carries.
171+ for ( const name of [ "README" , "NOTES" ] ) {
172+ await page . keyboard . press ( "ControlOrMeta+Shift+P" )
173+
174+ const dialog = page . getByRole ( "dialog" )
175+ const search = dialog . getByRole ( "textbox" )
176+ await search . fill ( name )
177+ await expect ( dialog . getByRole ( "option" , { name : `/ ${ name } .md` , exact : true } ) ) . toHaveAttribute (
178+ "aria-selected" ,
179+ "true" ,
180+ )
181+ await search . press ( "Enter" )
182+ await expect ( dialog ) . toHaveCount ( 0 )
183+ await expect ( panel . getByRole ( "tab" , { name : `${ name } .md` , exact : true } ) ) . toHaveAttribute ( "aria-selected" , "true" )
184+ }
185+
186+ await expect ( files ) . toHaveText ( [ "README.md" , "NOTES.md" ] )
187+ await expect ( panel . getByText ( `contents:${ directory } :NOTES.md` , { exact : true } ) ) . toBeVisible ( )
188+
189+ await transport . waitForConnection ( )
190+
191+ const resolved = page . waitForResponse ( ( response ) => {
192+ const url = new URL ( response . url ( ) )
193+
194+ return (
195+ url . pathname === "/api/location" && url . searchParams . get ( "location[directory]" ) === destination && response . ok ( )
196+ )
197+ } )
198+
199+ session . directory = destination
200+ await transport . send ( {
201+ id : "evt_location_moved_tabs" ,
202+ type : "session.moved" ,
203+ created : 2 ,
204+ durable : { aggregateID : sessionID , seq : 1 , version : 1 } ,
205+ data : { sessionID, location : { directory : destination } , projectID : fixture . project . id } ,
206+ } )
207+ await resolved
208+ await expect ( page . locator ( '[data-type="location-switched"]' ) . getByText ( destination , { exact : true } ) ) . toBeVisible ( )
209+ await expect ( files ) . toHaveText ( [ "README.md" , "NOTES.md" ] )
210+ await expect ( notes ) . toHaveAttribute ( "aria-selected" , "true" )
211+ // The carried tab shows the file in the new directory.
212+ await expect ( panel . getByText ( `contents:${ destination } :NOTES.md` , { exact : true } ) ) . toBeVisible ( )
213+ } )
214+
130215test ( "refreshes a session moved during disconnection without losing the draft" , async ( { page } ) => {
131216 const recovery = recoveryRequests ( page )
132217 const directory = "/projects/before-reconnect"
@@ -149,12 +234,15 @@ test("refreshes a session moved during disconnection without losing the draft",
149234 await expect ( prompt ) . toBeEditable ( )
150235 await prompt . fill ( "Draft before disconnect" )
151236 const connection = await transport . waitForConnection ( )
237+
152238 const resolved = page . waitForResponse ( ( response ) => {
153239 const url = new URL ( response . url ( ) )
240+
154241 return (
155242 url . pathname === "/api/location" && url . searchParams . get ( "location[directory]" ) === destination && response . ok ( )
156243 )
157244 } )
245+
158246 session . directory = destination
159247 await transport . close ( )
160248 await transport . waitForConnection ( { after : connection . id } )
@@ -186,9 +274,11 @@ test("ignores an old failed location read after reconnecting", async ({ page })
186274 await page . route ( "**/api/location?**" , async ( route ) => {
187275 if ( new URL ( route . request ( ) . url ( ) ) . searchParams . get ( "location[directory]" ) !== directory ) return route . fallback ( )
188276 requests ++
277+
189278 if ( requests > 1 ) return route . fallback ( )
190279 requested . resolve ( )
191280 await release . promise
281+
192282 return route . fulfill ( {
193283 status : 500 ,
194284 body : "" ,
@@ -202,12 +292,15 @@ test("ignores an old failed location read after reconnecting", async ({ page })
202292 await prompt . fill ( "Keep typing here" )
203293 const connection = await transport . waitForConnection ( )
204294 const metadata = page . waitForResponse ( ( response ) => new URL ( response . url ( ) ) . pathname === `/api/session/${ sessionID } ` )
295+
205296 const resolved = page . waitForResponse ( ( response ) => {
206297 const url = new URL ( response . url ( ) )
298+
207299 return (
208300 url . pathname === "/api/location" && url . searchParams . get ( "location[directory]" ) === directory && response . ok ( )
209301 )
210302 } )
303+
211304 await transport . close ( )
212305 await transport . waitForConnection ( { after : connection . id } )
213306 await metadata
@@ -227,7 +320,9 @@ function recoveryRequests(page: Page) {
227320 const requests : string [ ] = [ ]
228321 page . on ( "request" , ( request ) => {
229322 const path = new URL ( request . url ( ) ) . pathname
323+
230324 if ( request . method ( ) === "POST" && / ^ \/ a p i \/ ( s e s s i o n \/ [ ^ / ] + \/ m o v e | w o r k t r e e ) $ / . test ( path ) ) requests . push ( path )
231325 } )
326+
232327 return requests
233328}
0 commit comments