@@ -323,6 +323,74 @@ describe("environment grouping", () => {
323323 expect ( entries [ 1 ] ?. group . displayName ) . toBe ( "separate" ) ;
324324 } ) ;
325325
326+ it ( "keeps the current environment when available and falls back otherwise" , ( ) => {
327+ const currentPrimary = makeProject ( { repositoryIdentity } ) ;
328+ const currentRemote = makeProject ( {
329+ id : ProjectId . make ( "current-remote" ) ,
330+ environmentId : remoteEnvironmentId ,
331+ repositoryIdentity,
332+ } ) ;
333+ const destinationRepositoryIdentity = {
334+ canonicalKey : "github.com/example/destination" ,
335+ locator : {
336+ source : "git-remote" as const ,
337+ remoteName : "origin" ,
338+ remoteUrl : "https://github.com/example/destination.git" ,
339+ } ,
340+ } ;
341+ const destinationPrimary = makeProject ( {
342+ id : ProjectId . make ( "destination-primary" ) ,
343+ title : "destination" ,
344+ workspaceRoot : "/tmp/destination" ,
345+ repositoryIdentity : destinationRepositoryIdentity ,
346+ } ) ;
347+ const destinationRemote = makeProject ( {
348+ id : ProjectId . make ( "destination-remote" ) ,
349+ environmentId : remoteEnvironmentId ,
350+ title : "destination" ,
351+ workspaceRoot : "/remote/destination" ,
352+ repositoryIdentity : destinationRepositoryIdentity ,
353+ } ) ;
354+ const fallbackPrimary = makeProject ( {
355+ id : ProjectId . make ( "fallback-primary" ) ,
356+ title : "fallback" ,
357+ workspaceRoot : "/tmp/fallback" ,
358+ } ) ;
359+ const groups = buildSidebarProjectSnapshots ( {
360+ projects : [
361+ currentPrimary ,
362+ currentRemote ,
363+ destinationPrimary ,
364+ destinationRemote ,
365+ fallbackPrimary ,
366+ ] ,
367+ settings : defaultGroupingSettings ,
368+ primaryEnvironmentId,
369+ resolveEnvironmentLabel : ( ) => null ,
370+ } ) ;
371+
372+ const entries = buildSidebarProjectPickerEntries ( {
373+ groups,
374+ preferredProjectRef : {
375+ environmentId : remoteEnvironmentId ,
376+ projectId : currentRemote . id ,
377+ } ,
378+ } ) ;
379+ const destination = entries . find (
380+ ( entry ) => entry . group . projectKey === destinationRepositoryIdentity . canonicalKey ,
381+ ) ;
382+ const fallback = entries . find ( ( entry ) => entry . group . displayName === "fallback" ) ;
383+
384+ expect ( destination ?. targetProject ) . toMatchObject ( {
385+ environmentId : remoteEnvironmentId ,
386+ id : destinationRemote . id ,
387+ } ) ;
388+ expect ( fallback ?. targetProject ) . toMatchObject ( {
389+ environmentId : primaryEnvironmentId ,
390+ id : fallbackPrimary . id ,
391+ } ) ;
392+ } ) ;
393+
326394 it ( "keeps manual project order when building grouped sidebar entries" , ( ) => {
327395 const primary = makeProject ( { repositoryIdentity } ) ;
328396 const remote = makeProject ( {
0 commit comments