@@ -1259,8 +1259,31 @@ describe('WorkspaceManager openWorkspace promotion', () => {
12591259 . toEqual ( [ workspaceB . id , workspaceC . id , workspaceA . id ] ) ;
12601260 } ) ;
12611261
1262+ it ( 'shows the promoted order before the host finishes persisting it' , async ( ) => {
1263+ const manager = await getFreshWorkspaceManager ( ) ;
1264+ await manager . initialize ( ) ;
1265+ let finishReorder ! : ( ) => void ;
1266+ globalStateMocks . reorderOpenedWorkspaces . mockReturnValueOnce (
1267+ new Promise < void > ( resolve => { finishReorder = resolve ; } ) ,
1268+ ) ;
1269+ globalStateMocks . openWorkspace . mockResolvedValue ( workspaceB ) ;
1270+ const events : string [ ] = [ ] ;
1271+ manager . addEventListener ( event => { events . push ( event . type ) ; } ) ;
1272+
1273+ const opening = manager . openWorkspace ( workspaceB . rootPath ) ;
1274+ await flushAsyncWork ( ) ;
1275+
1276+ expect ( Array . from ( manager . getState ( ) . openedWorkspaces . keys ( ) ) )
1277+ . toEqual ( [ workspaceB . id , workspaceA . id , workspaceC . id ] ) ;
1278+ expect ( manager . getState ( ) . loading ) . toBe ( false ) ;
1279+ // Views measure the old layout on `promoted`, before the new order is published.
1280+ expect ( events . indexOf ( 'workspace:promoted' ) ) . toBeLessThan ( events . indexOf ( 'workspace:opened' ) ) ;
1281+ finishReorder ( ) ;
1282+ await expect ( opening ) . resolves . toBe ( workspaceB ) ;
1283+ } ) ;
1284+
12621285 it . each ( [ 'local' , 'remote' ] as const ) (
1263- 'does not apply a completed %s promotion after the device changes' ,
1286+ 'keeps a %s promotion on its own device when the device changes during persistence ' ,
12641287 async kind => {
12651288 const { manager, deviceSurface } = await getFreshWorkspaceHarness ( ) ;
12661289 await manager . initialize ( ) ;
@@ -1269,6 +1292,7 @@ describe('WorkspaceManager openWorkspace promotion', () => {
12691292 globalStateMocks . reorderOpenedWorkspaces . mockReturnValueOnce ( reordered ) ;
12701293 globalStateMocks . openWorkspace . mockResolvedValue ( workspaceB ) ;
12711294 globalStateMocks . openRemoteWorkspace . mockResolvedValue ( workspaceC ) ;
1295+ const promotedId = kind === 'local' ? workspaceB . id : workspaceC . id ;
12721296
12731297 const opening = ( kind === 'local'
12741298 ? manager . openWorkspace ( workspaceB . rootPath )
@@ -1278,18 +1302,88 @@ describe('WorkspaceManager openWorkspace promotion', () => {
12781302 await flushAsyncWork ( ) ;
12791303 expect ( globalStateMocks . reorderOpenedWorkspaces ) . toHaveBeenCalledTimes ( 1 ) ;
12801304
1281- // The promotion's await resumes first; activation then precedes its caller's continuation.
1282- const switched = reordered . then ( ( ) => deviceSurface . activateSurface ( 'peer-b' ) ) ;
1305+ deviceSurface . activateSurface ( 'peer-b' ) ;
12831306 finishReorder ( ) ;
1284- await switched ;
12851307
12861308 expect ( isSurfaceChangedError ( await opening ) ) . toBe ( true ) ;
12871309 expect ( manager . getState ( ) . openedWorkspaces . size ) . toBe ( 0 ) ;
12881310 expect ( manager . getState ( ) . currentWorkspace ) . toBeNull ( ) ;
12891311 expect ( manager . getState ( ) . error ) . toBeNull ( ) ;
1312+ expect ( manager . getSurfaceOpenedWorkspaces ( deviceSurface . LOCAL_SURFACE_ID ) [ 0 ] ?. id ) . toBe ( promotedId ) ;
12901313 } ,
12911314 ) ;
12921315
1316+ it ( 'does not promote a new workspace that a concurrent list refresh published first' , async ( ) => {
1317+ const manager = await getFreshWorkspaceManager ( ) ;
1318+ await manager . initialize ( ) ;
1319+ const added = { ...workspaceA , id : 'workspace-new' , rootPath : '/repo/new' } ;
1320+ const hostOpened = [ workspaceA , workspaceB , workspaceC , added ] ;
1321+
1322+ // A switch whose background list refresh is still in flight.
1323+ let releaseRefresh ! : ( workspaces : typeof hostOpened ) => void ;
1324+ globalStateMocks . setActiveWorkspace . mockResolvedValue ( workspaceB ) ;
1325+ globalStateMocks . getOpenedWorkspaces . mockImplementationOnce (
1326+ ( ) => new Promise ( resolve => { releaseRefresh = resolve ; } ) ,
1327+ ) ;
1328+ await manager . setActiveWorkspace ( workspaceB . id ) ;
1329+
1330+ globalStateMocks . getOpenedWorkspaces . mockResolvedValue ( hostOpened ) ;
1331+ globalStateMocks . openWorkspace . mockImplementation ( async ( ) => {
1332+ releaseRefresh ( hostOpened ) ;
1333+ await flushAsyncWork ( ) ;
1334+ expect ( manager . getState ( ) . openedWorkspaces . has ( added . id ) ) . toBe ( true ) ;
1335+ return added ;
1336+ } ) ;
1337+
1338+ await manager . openWorkspace ( added . rootPath ) ;
1339+
1340+ expect ( Array . from ( manager . getState ( ) . openedWorkspaces . keys ( ) ) )
1341+ . toEqual ( [ workspaceA . id , workspaceB . id , workspaceC . id , added . id ] ) ;
1342+ expect ( globalStateMocks . reorderOpenedWorkspaces ) . not . toHaveBeenCalled ( ) ;
1343+ } ) ;
1344+
1345+ it ( 'promotes within the project section and leaves the assistant slot alone' , async ( ) => {
1346+ const assistant = { ...workspaceA , id : 'assistant' , rootPath : '/assistant' , workspaceKind : 'assistant' } ;
1347+ const opened = [ assistant , workspaceA , workspaceB ] ;
1348+ globalStateMocks . initializeWorkspaceStartupState . mockResolvedValue ( {
1349+ cleanupRemovedCount : 0 ,
1350+ recentWorkspaces : opened ,
1351+ openedWorkspaces : opened ,
1352+ currentWorkspace : workspaceA ,
1353+ legacyRemoteWorkspace : null ,
1354+ } ) ;
1355+ globalStateMocks . getOpenedWorkspaces . mockResolvedValue ( opened ) ;
1356+ const manager = await getFreshWorkspaceManager ( ) ;
1357+ await manager . initialize ( ) ;
1358+
1359+ globalStateMocks . openWorkspace . mockResolvedValue ( workspaceA ) ;
1360+ await manager . openWorkspace ( workspaceA . rootPath ) ;
1361+ expect ( globalStateMocks . reorderOpenedWorkspaces ) . not . toHaveBeenCalled ( ) ;
1362+
1363+ globalStateMocks . openWorkspace . mockResolvedValue ( workspaceB ) ;
1364+ await manager . openWorkspace ( workspaceB . rootPath ) ;
1365+ expect ( Array . from ( manager . getState ( ) . openedWorkspaces . keys ( ) ) )
1366+ . toEqual ( [ assistant . id , workspaceB . id , workspaceA . id ] ) ;
1367+ expect ( globalStateMocks . reorderOpenedWorkspaces )
1368+ . toHaveBeenCalledWith ( [ assistant . id , workspaceB . id , workspaceA . id ] ) ;
1369+ } ) ;
1370+
1371+ it ( 'does not move a remote record that a background restore reopens' , async ( ) => {
1372+ const manager = await getFreshWorkspaceManager ( ) ;
1373+ await manager . initialize ( ) ;
1374+ globalStateMocks . openRemoteWorkspace . mockResolvedValue ( workspaceC ) ;
1375+
1376+ await manager . openRemoteWorkspace ( {
1377+ connectionId : 'conn-1' , connectionName : 'Remote' , remotePath : workspaceC . rootPath ,
1378+ } , { promoteIfOpened : false } ) ;
1379+
1380+ expect ( Array . from ( manager . getState ( ) . openedWorkspaces . keys ( ) ) )
1381+ . toEqual ( [ workspaceA . id , workspaceB . id , workspaceC . id ] ) ;
1382+ expect ( globalStateMocks . reorderOpenedWorkspaces ) . not . toHaveBeenCalled ( ) ;
1383+ // The manager-only flag never reaches the host.
1384+ expect ( globalStateMocks . openRemoteWorkspace . mock . calls [ 0 ] [ 4 ] ) . toEqual ( { } ) ;
1385+ } ) ;
1386+
12931387 it ( 'does not persist a reorder when the workspace is already first' , async ( ) => {
12941388 const manager = await getFreshWorkspaceManager ( ) ;
12951389 await manager . initialize ( ) ;
0 commit comments