@@ -342,10 +342,17 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
342342 connect : ( entry , reportProgress ) =>
343343 Effect . gen ( function * ( ) {
344344 const target = entry . target ;
345- const prepared = {
345+ const credential =
346+ target . _tag === "BearerConnectionTarget"
347+ ? yield * credentialStore . get ( target . connectionId )
348+ : Option . none ( ) ;
349+ const prepared : PreparedConnection = {
346350 ...PREPARED ,
347351 environmentId : target . environmentId ,
348352 label : target . label ,
353+ httpAuthorization : Option . isSome ( credential )
354+ ? { _tag : "Bearer" , token : credential . value . token }
355+ : null ,
349356 target,
350357 } ;
351358 yield * reportProgress ( { stage : "preparing" } ) ;
@@ -709,6 +716,80 @@ describe("EnvironmentRegistry", () => {
709716 } ) ,
710717 ) ;
711718
719+ it . effect ( "moves prepared credential observers when re-pairing an unchanged environment" , ( ) =>
720+ Effect . gen ( function * ( ) {
721+ const replacementCredential = new BearerConnectionCredential ( { token : "replacement-token" } ) ;
722+ const harness = yield * makeHarness (
723+ [ BEARER_TARGET ] ,
724+ [ BEARER_PROFILE ] ,
725+ [ [ BEARER_TARGET . connectionId , BEARER_CREDENTIAL ] ] ,
726+ ) ;
727+
728+ yield * Effect . gen ( function * ( ) {
729+ const registry = yield * EnvironmentRegistry . EnvironmentRegistry ;
730+ const initialObserved = yield * Deferred . make < void > ( ) ;
731+ const replacementObserved = yield * Deferred . make < void > ( ) ;
732+ const tokens = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
733+ const followedSupervisors = yield * Ref . make ( 0 ) ;
734+ yield * registry . start ;
735+
736+ const subscription = yield * Effect . forkChild (
737+ registry
738+ . followStream (
739+ BEARER_TARGET . environmentId ,
740+ Stream . unwrap (
741+ Effect . gen ( function * ( ) {
742+ const supervisor = yield * EnvironmentSupervisor . EnvironmentSupervisor ;
743+ yield * Ref . update ( followedSupervisors , ( count ) => count + 1 ) ;
744+ return SubscriptionRef . changes ( supervisor . prepared ) ;
745+ } ) ,
746+ ) ,
747+ )
748+ . pipe (
749+ Stream . filterMap ( ( prepared ) =>
750+ Option . isSome ( prepared ) && prepared . value . httpAuthorization ?. _tag === "Bearer"
751+ ? Result . succeed ( prepared . value . httpAuthorization . token )
752+ : Result . failVoid ,
753+ ) ,
754+ Stream . changes ,
755+ Stream . runForEach ( ( token ) =>
756+ Effect . gen ( function * ( ) {
757+ yield * Ref . update ( tokens , ( current ) => [ ...current , token ] ) ;
758+ yield * Deferred . succeed (
759+ token === replacementCredential . token ? replacementObserved : initialObserved ,
760+ undefined ,
761+ ) ;
762+ } ) ,
763+ ) ,
764+ ) ,
765+ ) ;
766+
767+ yield * Deferred . await ( initialObserved ) ;
768+ yield * registry . register ( new RelayConnectionRegistration ( { target : RELAY_TARGET } ) ) ;
769+ yield * awaitConnectionState (
770+ registry ,
771+ RELAY_TARGET . environmentId ,
772+ ( state ) => state . phase === "connected" ,
773+ ) ;
774+ yield * registry . register (
775+ new BearerConnectionRegistration ( {
776+ target : new BearerConnectionTarget ( { ...BEARER_TARGET } ) ,
777+ profile : new BearerConnectionProfile ( { ...BEARER_PROFILE } ) ,
778+ credential : replacementCredential ,
779+ } ) ,
780+ ) ;
781+ yield * Deferred . await ( replacementObserved ) ;
782+ yield * Fiber . interrupt ( subscription ) ;
783+
784+ expect ( yield * Ref . get ( tokens ) ) . toEqual ( [
785+ BEARER_CREDENTIAL . token ,
786+ replacementCredential . token ,
787+ ] ) ;
788+ expect ( yield * Ref . get ( followedSupervisors ) ) . toBe ( 2 ) ;
789+ } ) . pipe ( Effect . provide ( harness . layer ) , Effect . scoped ) ;
790+ } ) ,
791+ ) ;
792+
712793 it . effect ( "ignores retry signals for environments that are no longer registered" , ( ) =>
713794 Effect . gen ( function * ( ) {
714795 const harness = yield * makeHarness ( [ ] ) ;
0 commit comments