@@ -14,6 +14,7 @@ import * as Exit from "effect/Exit";
1414import * as Fiber from "effect/Fiber" ;
1515import * as Layer from "effect/Layer" ;
1616import * as Option from "effect/Option" ;
17+ import * as Queue from "effect/Queue" ;
1718import * as Ref from "effect/Ref" ;
1819import * as Result from "effect/Result" ;
1920import * as Scheduler from "effect/Scheduler" ;
@@ -23,6 +24,7 @@ import * as SubscriptionRef from "effect/SubscriptionRef";
2324import * as TestClock from "effect/testing/TestClock" ;
2425import * as Tracer from "effect/Tracer" ;
2526
27+ import * as RemoteEnvironmentAuthorization from "../authorization/service.ts" ;
2628import * as ClientCapabilities from "../platform/capabilities.ts" ;
2729import * as TokenStore from "../authorization/tokenStore.ts" ;
2830import {
@@ -172,6 +174,7 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
172174 readonly environmentId : EnvironmentId ;
173175 readonly atEpochMs : number ;
174176 } > ;
177+ readonly onDpopPrefetch ?: ( environmentId : EnvironmentId ) => Effect . Effect < void > ;
175178 readonly directEndpoints ?: ServerConfig [ "directEndpoints" ] ;
176179 } ,
177180) {
@@ -345,6 +348,33 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
345348 changes : SubscriptionRef . changes ( networkStatus ) ,
346349 privateNetworkLikely : Effect . succeed ( true ) ,
347350 } ) ;
351+ const dpopPrefetchCalls = yield * Ref . make ( 0 ) ;
352+ const wakeups = yield * SubscriptionRef . make < {
353+ readonly sequence : number ;
354+ readonly reason : ConnectionWakeups . ConnectionWakeup ;
355+ } > ( {
356+ sequence : 0 ,
357+ reason : "application-active" ,
358+ } ) ;
359+ const remoteAuthorization = RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization . of ( {
360+ authorizeBearer : ( ) => Effect . die ( "unused" ) ,
361+ authorizeDpop : ( ) => Effect . die ( "unused" ) ,
362+ authorizeDpopHttp : ( input ) =>
363+ Ref . update ( dpopPrefetchCalls , ( count ) => count + 1 ) . pipe (
364+ Effect . andThen ( options ?. onDpopPrefetch ?.( input . expectedEnvironmentId ) ?? Effect . void ) ,
365+ Effect . as ( {
366+ environmentId : input . expectedEnvironmentId ,
367+ label : "Prefetched" ,
368+ httpBaseUrl : "https://relay.example.test" ,
369+ httpAuthorization : {
370+ _tag : "Dpop" as const ,
371+ accessToken : "prefetch-token" ,
372+ expiresAtEpochMs : Number . MAX_SAFE_INTEGER ,
373+ } ,
374+ } ) ,
375+ ) ,
376+ renewDpopBeforeExpiry : ( ) => Effect . die ( "unused" ) ,
377+ } ) ;
348378 const profileStore = ConnectionProfileStore . ConnectionProfileStore . of ( {
349379 get : ( connectionId ) =>
350380 Ref . update ( profileReadCount , ( count ) => count + 1 ) . pipe (
@@ -500,7 +530,16 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
500530 Layer . succeed ( Connectivity . Connectivity , connectivity ) ,
501531 Layer . succeed (
502532 ConnectionWakeups . ConnectionWakeups ,
503- ConnectionWakeups . ConnectionWakeups . of ( { changes : Stream . never } ) ,
533+ ConnectionWakeups . ConnectionWakeups . of ( {
534+ changes : SubscriptionRef . changes ( wakeups ) . pipe (
535+ Stream . drop ( 1 ) ,
536+ Stream . map ( ( event ) => event . reason ) ,
537+ ) ,
538+ } ) ,
539+ ) ,
540+ Layer . succeed (
541+ RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ,
542+ remoteAuthorization ,
504543 ) ,
505544 Layer . succeed ( ConnectionDriver . ConnectionDriver , driver ) ,
506545 layerCache ,
@@ -526,6 +565,12 @@ const makeHarness = Effect.fn("TestEnvironmentRegistry.makeHarness")(function* (
526565 disconnectedSshTargets,
527566 networkStatus,
528567 connectedRoutes,
568+ dpopPrefetchCalls,
569+ wake : ( reason : ConnectionWakeups . ConnectionWakeup ) =>
570+ SubscriptionRef . update ( wakeups , ( event ) => ( {
571+ sequence : event . sequence + 1 ,
572+ reason,
573+ } ) ) ,
529574 } ;
530575} ) ;
531576
@@ -717,6 +762,31 @@ describe("EnvironmentRegistry", () => {
717762 } ) ,
718763 ) ;
719764
765+ it . effect (
766+ "prefetches T3 Connect credentials on start and wake for recent relay environments" ,
767+ ( ) =>
768+ Effect . gen ( function * ( ) {
769+ const prefetches = yield * Queue . unbounded < EnvironmentId > ( ) ;
770+ const harness = yield * makeHarness ( [ RELAY_TARGET ] , [ ] , [ ] , {
771+ onDpopPrefetch : ( environmentId ) => Queue . offer ( prefetches , environmentId ) ,
772+ } ) ;
773+
774+ yield * Effect . gen ( function * ( ) {
775+ const registry = yield * EnvironmentRegistry . EnvironmentRegistry ;
776+ yield * registry . start ;
777+ expect ( yield * Queue . take ( prefetches ) ) . toBe ( RELAY_TARGET . environmentId ) ;
778+ yield * awaitConnectionState (
779+ registry ,
780+ RELAY_TARGET . environmentId ,
781+ ( state ) => state . phase === "connected" ,
782+ ) ;
783+
784+ yield * harness . wake ( "application-active" ) ;
785+ expect ( yield * Queue . take ( prefetches ) ) . toBe ( RELAY_TARGET . environmentId ) ;
786+ } ) . pipe ( Effect . provide ( harness . layer ) , Effect . scoped ) ;
787+ } ) ,
788+ ) ;
789+
720790 it . effect ( "does not expose a persisted environment as disconnected while it starts" , ( ) =>
721791 Effect . gen ( function * ( ) {
722792 const connectionStarted = yield * Deferred . make < void > ( ) ;
0 commit comments