@@ -500,11 +500,8 @@ describe("RemoteEnvironmentAuthorization", () => {
500500 } ) ;
501501 const harness = yield * makeHarness ( {
502502 initialToken : expired ,
503- responses : [
504- Response . json ( DESCRIPTOR ) ,
505- accessToken ( "fresh-access-token" ) ,
506- websocketTicket ( "fresh-ticket" ) ,
507- ] ,
503+ // Prior tunnel host is reused, so the descriptor fetch is skipped.
504+ responses : [ accessToken ( "fresh-access-token" ) , websocketTicket ( "fresh-ticket" ) ] ,
508505 } ) ;
509506
510507 const authorized = yield * Effect . gen ( function * ( ) {
@@ -520,9 +517,64 @@ describe("RemoteEnvironmentAuthorization", () => {
520517 expect . objectContaining ( {
521518 accessToken : "fresh-access-token" ,
522519 dpopThumbprint : "thumbprint-1" ,
520+ label : DESCRIPTOR . label ,
523521 } ) ,
524522 ) ;
525- expect ( harness . fetch . calls ) . toHaveLength ( 3 ) ;
523+ expect ( harness . fetch . calls ) . toHaveLength ( 2 ) ;
524+ } ) ,
525+ ) ;
526+
527+ it . effect ( "renews early when authorizeDpopHttp asks for a wider refresh skew" , ( ) =>
528+ Effect . gen ( function * ( ) {
529+ const harness = yield * makeHarness ( {
530+ initialToken : persistedToken ( { expiresAtEpochMs : 4 * 60_000 } ) ,
531+ responses : [ accessToken ( "wake-renewed-token" ) ] ,
532+ } ) ;
533+
534+ const authorized = yield * Effect . gen ( function * ( ) {
535+ const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
536+ return yield * remote . authorizeDpopHttp ( {
537+ expectedEnvironmentId : ENVIRONMENT_ID ,
538+ refreshSkewMs : 5 * 60_000 ,
539+ } ) ;
540+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
541+
542+ expect ( authorized . httpAuthorization . accessToken ) . toBe ( "wake-renewed-token" ) ;
543+ expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( 1 ) ;
544+ } ) ,
545+ ) ;
546+
547+ it . effect ( "fetches a descriptor when the relay returns a new tunnel endpoint" , ( ) =>
548+ Effect . gen ( function * ( ) {
549+ const moved = {
550+ ...ENDPOINT ,
551+ httpBaseUrl : "https://moved.example.test" ,
552+ wsBaseUrl : "wss://moved.example.test" ,
553+ } ;
554+ const harness = yield * makeHarness ( {
555+ initialToken : persistedToken ( { expiresAtEpochMs : 0 } ) ,
556+ bootstrap : { ...BOOTSTRAP , endpoint : moved } ,
557+ responses : [
558+ Response . json ( { ...DESCRIPTOR , label : "Moved environment" } ) ,
559+ accessToken ( "moved-access-token" ) ,
560+ websocketTicket ( "moved-ticket" ) ,
561+ ] ,
562+ } ) ;
563+
564+ const authorized = yield * Effect . gen ( function * ( ) {
565+ const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
566+ return yield * remote . authorizeDpop ( {
567+ expectedEnvironmentId : ENVIRONMENT_ID ,
568+ } ) ;
569+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
570+
571+ expect ( authorized . socketUrl ) . toContain ( "wsTicket=moved-ticket" ) ;
572+ expect ( authorized . label ) . toBe ( "Moved environment" ) ;
573+ expect ( harness . fetch . calls . map ( ( [ url ] ) => String ( url ) ) ) . toEqual ( [
574+ "https://moved.example.test/.well-known/t3/environment" ,
575+ "https://moved.example.test/oauth/token" ,
576+ "https://moved.example.test/api/auth/websocket-ticket/rpc" ,
577+ ] ) ;
526578 } ) ,
527579 ) ;
528580
@@ -556,7 +608,6 @@ describe("RemoteEnvironmentAuthorization", () => {
556608 Response . json ( DESCRIPTOR ) ,
557609 exchangeGrant ,
558610 websocketTicket ( "granted-ticket" ) ,
559- Response . json ( DESCRIPTOR ) ,
560611 exchangeGrant ,
561612 ] ,
562613 } ) ;
@@ -621,12 +672,7 @@ describe("RemoteEnvironmentAuthorization", () => {
621672 ) ,
622673 ) ,
623674 initialToken : persistedToken ( { expiresAtEpochMs : 3_600_000 } ) ,
624- responses : [
625- Response . json ( DESCRIPTOR ) ,
626- accessToken ( "renewed-1" ) ,
627- Response . json ( DESCRIPTOR ) ,
628- accessToken ( "renewed-2" ) ,
629- ] ,
675+ responses : [ accessToken ( "renewed-1" ) , accessToken ( "renewed-2" ) ] ,
630676 beforePut : Queue . offer ( puts , undefined ) . pipe ( Effect . asVoid ) ,
631677 } ) ;
632678
@@ -670,7 +716,6 @@ describe("RemoteEnvironmentAuthorization", () => {
670716 initialToken : cached ,
671717 responses : [
672718 authInvalid ( ) ,
673- Response . json ( DESCRIPTOR ) ,
674719 accessToken ( "replacement-access-token" ) ,
675720 websocketTicket ( "replacement-ticket" ) ,
676721 ] ,
@@ -690,7 +735,7 @@ describe("RemoteEnvironmentAuthorization", () => {
690735 accessToken : "replacement-access-token" ,
691736 } ) ,
692737 ) ;
693- expect ( harness . fetch . calls ) . toHaveLength ( 4 ) ;
738+ expect ( harness . fetch . calls ) . toHaveLength ( 3 ) ;
694739 } ) ,
695740 ) ;
696741
@@ -709,7 +754,6 @@ describe("RemoteEnvironmentAuthorization", () => {
709754 initialToken : cached ,
710755 responses : [
711756 new Response ( "endpoint unavailable" , { status : 503 } ) ,
712- Response . json ( DESCRIPTOR ) ,
713757 accessToken ( "replacement-access-token" ) ,
714758 websocketTicket ( "replacement-ticket" ) ,
715759 ] ,
@@ -729,7 +773,7 @@ describe("RemoteEnvironmentAuthorization", () => {
729773 accessToken : "replacement-access-token" ,
730774 } ) ,
731775 ) ;
732- expect ( harness . fetch . calls ) . toHaveLength ( 4 ) ;
776+ expect ( harness . fetch . calls ) . toHaveLength ( 3 ) ;
733777 } ) ,
734778 ) ;
735779
@@ -760,7 +804,7 @@ describe("RemoteEnvironmentAuthorization", () => {
760804 Effect . gen ( function * ( ) {
761805 const harness = yield * makeHarness ( {
762806 initialToken : persistedToken ( ) ,
763- responses : [ Response . json ( DESCRIPTOR ) , accessToken ( "replacement-token" ) ] ,
807+ responses : [ accessToken ( "replacement-token" ) ] ,
764808 } ) ;
765809 yield * Effect . gen ( function * ( ) {
766810 const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
@@ -776,7 +820,7 @@ describe("RemoteEnvironmentAuthorization", () => {
776820 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
777821
778822 expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( 1 ) ;
779- expect ( harness . fetch . calls ) . toHaveLength ( 2 ) ;
823+ expect ( harness . fetch . calls ) . toHaveLength ( 1 ) ;
780824 expect ( ( yield * Ref . get ( harness . tokens ) ) . get ( ENVIRONMENT_ID ) ?. accessToken ) . toBe (
781825 "replacement-token" ,
782826 ) ;
@@ -792,7 +836,7 @@ describe("RemoteEnvironmentAuthorization", () => {
792836 beforeBootstrap : Deferred . succeed ( started , undefined ) . pipe (
793837 Effect . andThen ( Deferred . await ( release ) ) ,
794838 ) ,
795- responses : [ Response . json ( DESCRIPTOR ) , accessToken ( "shared-token" ) ] ,
839+ responses : [ accessToken ( "shared-token" ) ] ,
796840 } ) ;
797841 yield * Effect . gen ( function * ( ) {
798842 const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
@@ -813,7 +857,7 @@ describe("RemoteEnvironmentAuthorization", () => {
813857 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
814858
815859 expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( 1 ) ;
816- expect ( harness . fetch . calls ) . toHaveLength ( 2 ) ;
860+ expect ( harness . fetch . calls ) . toHaveLength ( 1 ) ;
817861 } ) ,
818862 ) ;
819863
@@ -1025,7 +1069,8 @@ describe("RemoteEnvironmentAuthorization", () => {
10251069 Effect . gen ( function * ( ) {
10261070 const harness = yield * makeHarness ( {
10271071 initialToken : persistedToken ( ) ,
1028- responses : [ Response . json ( DESCRIPTOR ) , accessToken ( "new-session-token" ) ] ,
1072+ // Same account can reuse the prior tunnel host; only the oauth exchange runs.
1073+ responses : [ accessToken ( "new-session-token" ) ] ,
10291074 } ) ;
10301075 yield * Effect . gen ( function * ( ) {
10311076 const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
@@ -1036,6 +1081,7 @@ describe("RemoteEnvironmentAuthorization", () => {
10361081 expect ( signedIn . httpAuthorization . accessToken ) . toBe ( "new-session-token" ) ;
10371082 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
10381083 expect ( yield * Ref . get ( harness . bootstrapCalls ) ) . toBe ( 1 ) ;
1084+ expect ( harness . fetch . calls ) . toHaveLength ( 1 ) ;
10391085 } ) ,
10401086 ) ;
10411087
0 commit comments