@@ -46,14 +46,18 @@ const BOOTSTRAP: RelayEnvironmentConnectResponse = {
4646 expiresAt : "2026-06-06T01:00:00.000Z" ,
4747} ;
4848
49- type RecordedResponse = Response | ( ( init : RequestInit ) => Response ) ;
49+ type RecordedResponse = Response | "stall" | ( ( init : RequestInit ) => Response ) ;
5050
51- function recordedFetch ( responses : ReadonlyArray < RecordedResponse > ) {
51+ function recordedFetch ( responses : ReadonlyArray < RecordedResponse > , onStall : ( ) => void ) {
5252 const calls : Array < readonly [ RequestInfo | URL , RequestInit ] > = [ ] ;
5353 let responseIndex = 0 ;
5454 const fetchFn = ( ( input , init ) => {
5555 calls . push ( [ input , init ?? { } ] ) ;
5656 const response = responses [ responseIndex ++ ] ;
57+ if ( response === "stall" ) {
58+ onStall ( ) ;
59+ return new Promise < Response > ( ( ) => { } ) ;
60+ }
5761 return response === undefined
5862 ? Promise . reject ( new Error ( `Unexpected fetch call to ${ String ( input ) } ` ) )
5963 : Promise . resolve ( typeof response === "function" ? response ( init ?? { } ) : response ) ;
@@ -136,7 +140,8 @@ const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (
136140 readonly accessToken ?: string ;
137141 } >
138142 > ( [ ] ) ;
139- const fetch = recordedFetch ( input . responses ) ;
143+ const stalls = yield * Queue . unbounded < void > ( ) ;
144+ const fetch = recordedFetch ( input . responses , ( ) => Queue . offerUnsafe ( stalls , undefined ) ) ;
140145
141146 const tokenStore = TokenStore . RemoteDpopAccessTokenStore . of ( {
142147 get : ( environmentId ) =>
@@ -227,6 +232,7 @@ const makeHarness = Effect.fn("TestRemoteAuthorization.makeHarness")(function* (
227232 session,
228233 thumbprint,
229234 tokenReads,
235+ stalls,
230236 } ;
231237} ) ;
232238
@@ -354,18 +360,36 @@ describe("RemoteEnvironmentAuthorization", () => {
354360 } ) ;
355361 const harness = yield * makeHarness ( {
356362 initialToken : cached ,
357- responses : [ Response . json ( DESCRIPTOR ) , websocketTicket ( "lan-ticket" ) ] ,
363+ responses : [
364+ Response . json ( DESCRIPTOR ) ,
365+ websocketTicket ( "lan-ticket" ) ,
366+ "stall" ,
367+ websocketTicket ( "fallback-ticket" ) ,
368+ ] ,
358369 } ) ;
359370
360371 const authorized = yield * Effect . gen ( function * ( ) {
361372 const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
362- return yield * remote . authorizeDpop ( {
373+ const direct = remote . authorizeDpop ( {
363374 expectedEnvironmentId : ENVIRONMENT_ID ,
364375 directEndpoint : {
365376 httpBaseUrl : "http://192.168.1.10:3773/" ,
366377 wsBaseUrl : "ws://192.168.1.10:3773/" ,
367378 } ,
368379 } ) ;
380+ const authorized = yield * direct ;
381+ const stalled = yield * direct . pipe ( Effect . flip , Effect . forkChild ) ;
382+ yield * Queue . take ( harness . stalls ) ;
383+ yield * TestClock . adjust ( "3 seconds" ) ;
384+ expect ( harness . fetch . calls ) . toHaveLength ( 3 ) ;
385+ expect ( yield * Fiber . join ( stalled ) ) . toMatchObject ( {
386+ _tag : "ConnectionTransientError" ,
387+ reason : "timeout" ,
388+ } ) ;
389+ const fallback = yield * remote . authorizeDpop ( { expectedEnvironmentId : ENVIRONMENT_ID } ) ;
390+ expect ( fallback . socketUrl ) . toContain ( "wsTicket=fallback-ticket" ) ;
391+ expect ( fallback . httpAuthorization ) . toMatchObject ( { accessToken : cached . accessToken } ) ;
392+ return authorized ;
369393 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
370394
371395 expect ( authorized . httpBaseUrl ) . toBe ( "http://192.168.1.10:3773/" ) ;
@@ -374,6 +398,8 @@ describe("RemoteEnvironmentAuthorization", () => {
374398 expect ( harness . fetch . calls . map ( ( [ url ] ) => String ( url ) ) ) . toEqual ( [
375399 "http://192.168.1.10:3773/.well-known/t3/environment" ,
376400 "http://192.168.1.10:3773/api/auth/websocket-ticket" ,
401+ "http://192.168.1.10:3773/api/auth/websocket-ticket" ,
402+ "https://environment.example.test/api/auth/websocket-ticket" ,
377403 ] ) ;
378404 } ) ,
379405 ) ;
@@ -666,6 +692,98 @@ describe("RemoteEnvironmentAuthorization", () => {
666692 } ) ,
667693 ) ;
668694
695+ it . effect ( "resets relay ticket timeouts after recovery and after credential replacement" , ( ) =>
696+ Effect . gen ( function * ( ) {
697+ const harness = yield * makeHarness ( {
698+ initialToken : persistedToken ( ) ,
699+ responses : [
700+ "stall" ,
701+ "stall" ,
702+ "stall" ,
703+ "stall" ,
704+ "stall" ,
705+ websocketTicket ( "recovered-ticket" ) ,
706+ "stall" ,
707+ "stall" ,
708+ "stall" ,
709+ "stall" ,
710+ Response . json ( DESCRIPTOR ) ,
711+ accessToken ( "replacement-token" ) ,
712+ "stall" ,
713+ "stall" ,
714+ websocketTicket ( "replacement-ticket" ) ,
715+ ] ,
716+ } ) ;
717+ yield * Effect . gen ( function * ( ) {
718+ const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
719+ const authorize = remote . authorizeDpop ( { expectedEnvironmentId : ENVIRONMENT_ID } ) ;
720+ const timeOut = ( expectedMints : number ) =>
721+ Effect . gen ( function * ( ) {
722+ const pending = yield * authorize . pipe ( Effect . flip , Effect . forkChild ) ;
723+ yield * Queue . take ( harness . stalls ) ;
724+ yield * TestClock . adjust ( "3 seconds" ) ;
725+ expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( expectedMints ) ;
726+ yield * Queue . take ( harness . stalls ) ;
727+ yield * TestClock . adjust ( "7 seconds" ) ;
728+ expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( expectedMints ) ;
729+ expect ( yield * Fiber . join ( pending ) ) . toMatchObject ( {
730+ _tag : "ConnectionTransientError" ,
731+ reason : "timeout" ,
732+ } ) ;
733+ } ) ;
734+ yield * timeOut ( 0 ) ;
735+ yield * timeOut ( 0 ) ;
736+ const recovery = yield * authorize . pipe ( Effect . forkChild ) ;
737+ yield * Queue . take ( harness . stalls ) ;
738+ yield * TestClock . adjust ( "3 seconds" ) ;
739+ expect ( ( yield * Fiber . join ( recovery ) ) . socketUrl ) . toContain ( "wsTicket=recovered-ticket" ) ;
740+ yield * timeOut ( 0 ) ;
741+ yield * timeOut ( 0 ) ;
742+
743+ yield * remote . authorizeDpopHttp ( {
744+ expectedEnvironmentId : ENVIRONMENT_ID ,
745+ rejectedAccessToken : "cached-access-token" ,
746+ } ) ;
747+ yield * timeOut ( 1 ) ;
748+ const replacement = yield * authorize ;
749+ expect ( replacement . socketUrl ) . toContain ( "wsTicket=replacement-ticket" ) ;
750+ expect ( replacement . httpAuthorization ) . toMatchObject ( { accessToken : "replacement-token" } ) ;
751+ expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( 1 ) ;
752+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
753+ } ) ,
754+ ) ;
755+
756+ it . effect ( "reuses a newly minted credential after its first relay ticket times out" , ( ) =>
757+ Effect . gen ( function * ( ) {
758+ const harness = yield * makeHarness ( {
759+ responses : [
760+ Response . json ( DESCRIPTOR ) ,
761+ accessToken ( "fresh-token" ) ,
762+ "stall" ,
763+ websocketTicket ( "recovered-ticket" ) ,
764+ ] ,
765+ } ) ;
766+ yield * Effect . gen ( function * ( ) {
767+ const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
768+ const authorize = remote . authorizeDpop ( { expectedEnvironmentId : ENVIRONMENT_ID } ) ;
769+ const pending = yield * authorize . pipe ( Effect . flip , Effect . forkChild ) ;
770+ yield * Queue . take ( harness . stalls ) ;
771+ yield * TestClock . adjust ( "10 seconds" ) ;
772+ expect ( yield * Fiber . join ( pending ) ) . toMatchObject ( {
773+ _tag : "ConnectionTransientError" ,
774+ reason : "timeout" ,
775+ } ) ;
776+ expect ( ( yield * Ref . get ( harness . tokens ) ) . get ( ENVIRONMENT_ID ) ?. accessToken ) . toBe (
777+ "fresh-token" ,
778+ ) ;
779+ const recovered = yield * authorize ;
780+ expect ( recovered . socketUrl ) . toContain ( "wsTicket=recovered-ticket" ) ;
781+ expect ( recovered . httpAuthorization ) . toMatchObject ( { accessToken : "fresh-token" } ) ;
782+ expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( 1 ) ;
783+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
784+ } ) ,
785+ ) ;
786+
669787 it . effect ( "removes a newly refreshed token when websocket ticket authorization fails" , ( ) =>
670788 Effect . gen ( function * ( ) {
671789 const harness = yield * makeHarness ( {
0 commit comments