@@ -685,6 +685,7 @@ it.effect("pins a provider session to its initial host despite later focus chang
685685 environmentId : scope . environmentId ,
686686 connectionId : "connection-stale" ,
687687 focused : true ,
688+ liveTabs : [ { threadId : scope . threadId , tabId : PreviewTabId . make ( "stale-tab" ) } ] ,
688689 } ) ;
689690 expect ( yield * broker . invoke < string > ( { scope, operation : "status" , input : { } } ) ) . toBe (
690691 "second" ,
@@ -725,6 +726,130 @@ it.effect("pins a provider session to its initial host despite later focus chang
725726 ) ,
726727) ;
727728
729+ it . effect ( "prefers the live tab owner for new sessions without moving existing leases" , ( ) =>
730+ Effect . scoped (
731+ Effect . gen ( function * ( ) {
732+ const broker = yield * makeBroker ;
733+ const connections = new Map < string , string > ( ) ;
734+ for ( const clientId of [ "owner" , "other" ] ) {
735+ const requests = requestsFrom (
736+ yield * broker . connect ( makeHost ( { clientId } ) ) ,
737+ ( connectionId ) => connections . set ( clientId , connectionId ) ,
738+ ) ;
739+ yield * Stream . runForEach ( requests , ( request ) =>
740+ broker . respond ( {
741+ clientId,
742+ connectionId : request . connectionId ,
743+ requestId : request . requestId ,
744+ ok : true ,
745+ result : clientId ,
746+ } ) ,
747+ ) . pipe ( Effect . forkScoped ) ;
748+ }
749+ yield * Effect . yieldNow ;
750+ yield * broker . focusHost ( {
751+ clientId : "owner" ,
752+ environmentId : scope . environmentId ,
753+ connectionId : connections . get ( "owner" ) ! ,
754+ focused : false ,
755+ liveTabs : [
756+ { threadId : scope . threadId , tabId : PreviewTabId . make ( "signed-in" ) , visible : true } ,
757+ ] ,
758+ } ) ;
759+ yield * broker . focusHost ( {
760+ clientId : "other" ,
761+ environmentId : scope . environmentId ,
762+ connectionId : connections . get ( "other" ) ! ,
763+ focused : true ,
764+ liveTabs : [
765+ { threadId : scope . threadId , tabId : PreviewTabId . make ( "signed-in" ) , visible : false } ,
766+ {
767+ threadId : ThreadId . make ( "another-thread" ) ,
768+ tabId : PreviewTabId . make ( "different-tab" ) ,
769+ visible : true ,
770+ } ,
771+ ] ,
772+ } ) ;
773+ expect ( yield * broker . invoke < string > ( { scope, operation : "evaluate" , input : { } } ) ) . toBe (
774+ "owner" ,
775+ ) ;
776+ expect (
777+ yield * broker . invoke < string > ( {
778+ scope : { ...scope , providerSessionId : "explicit-owner" } ,
779+ tabId : PreviewTabId . make ( "signed-in" ) ,
780+ operation : "snapshot" ,
781+ input : { } ,
782+ } ) ,
783+ ) . toBe ( "owner" ) ;
784+ expect (
785+ yield * broker . invoke < string > ( {
786+ scope : { ...scope , providerSessionId : "other-tab" } ,
787+ tabId : PreviewTabId . make ( "different-tab" ) ,
788+ operation : "evaluate" ,
789+ input : { } ,
790+ } ) ,
791+ ) . toBe ( "other" ) ;
792+
793+ yield * broker . focusHost ( {
794+ clientId : "owner" ,
795+ environmentId : scope . environmentId ,
796+ connectionId : connections . get ( "owner" ) ! ,
797+ focused : false ,
798+ liveTabs : [ ] ,
799+ } ) ;
800+ expect ( yield * broker . invoke < string > ( { scope, operation : "evaluate" , input : { } } ) ) . toBe (
801+ "owner" ,
802+ ) ;
803+ expect (
804+ yield * broker . invoke < string > ( {
805+ scope : { ...scope , providerSessionId : "after-tab-closed" } ,
806+ operation : "evaluate" ,
807+ input : { } ,
808+ } ) ,
809+ ) . toBe ( "other" ) ;
810+ } ) ,
811+ ) ,
812+ ) ;
813+
814+ it . effect ( "prefers a focused host over unrelated extra capabilities for a new session" , ( ) =>
815+ Effect . scoped (
816+ Effect . gen ( function * ( ) {
817+ const broker = yield * makeBroker ;
818+ let focusedConnectionId = "" ;
819+ for ( const [ clientId , supportedOperations ] of [
820+ [ "focused" , [ "status" ] ] ,
821+ [ "background" , [ "status" , "resize" ] ] ,
822+ ] as const ) {
823+ const requests = requestsFrom (
824+ yield * broker . connect ( makeHost ( { clientId, supportedOperations } ) ) ,
825+ ( connectionId ) => {
826+ if ( clientId === "focused" ) focusedConnectionId = connectionId ;
827+ } ,
828+ ) ;
829+ yield * Stream . runForEach ( requests , ( request ) =>
830+ broker . respond ( {
831+ clientId,
832+ connectionId : request . connectionId ,
833+ requestId : request . requestId ,
834+ ok : true ,
835+ result : clientId ,
836+ } ) ,
837+ ) . pipe ( Effect . forkScoped ) ;
838+ }
839+ yield * Effect . yieldNow ;
840+ yield * broker . focusHost ( {
841+ clientId : "focused" ,
842+ environmentId : scope . environmentId ,
843+ connectionId : focusedConnectionId ,
844+ focused : true ,
845+ } ) ;
846+ expect ( yield * broker . invoke < string > ( { scope, operation : "status" , input : { } } ) ) . toBe (
847+ "focused" ,
848+ ) ;
849+ } ) ,
850+ ) ,
851+ ) ;
852+
728853it . effect ( "does not route new operations to legacy hosts that did not advertise support" , ( ) =>
729854 Effect . scoped (
730855 Effect . gen ( function * ( ) {
@@ -921,6 +1046,7 @@ it.effect("fails over a pinned provider session only after its host disconnects"
9211046 environmentId : scope . environmentId ,
9221047 connectionId : firstConnectionId ,
9231048 focused : true ,
1049+ liveTabs : [ { threadId : scope . threadId , tabId : firstTabId } ] ,
9241050 } ) ;
9251051 expect ( yield * broker . invoke ( { scope, operation : "open" , input : { } } ) ) . toEqual ( {
9261052 host : "first" ,
0 commit comments