@@ -24,6 +24,9 @@ import * as ConnectionCredentialStore from "../connection/credentialStore.ts";
2424import { BearerConnectionTarget } from "../connection/model.ts" ;
2525import * as ConnectionProfileStore from "../connection/profileStore.ts" ;
2626import * as ConnectionResolver from "../connection/resolver.ts" ;
27+ import * as ConnectionDriver from "../connection/driver.ts" ;
28+ import * as RpcSession from "../rpc/session.ts" ;
29+ import { ConnectionTransientError } from "../connection/model.ts" ;
2730import { CheckedEnvironmentDescriptors } from "../environment/descriptor.ts" ;
2831
2932const ENVIRONMENT_ID = EnvironmentId . make ( "environment-1" ) ;
@@ -431,6 +434,108 @@ describe("RemoteEnvironmentAuthorization", () => {
431434 } ) ,
432435 ) ;
433436
437+ it . effect (
438+ "rechecks a LAN descriptor after another route attempt before sending credentials" ,
439+ ( ) =>
440+ Effect . gen ( function * ( ) {
441+ const lanUrl = "http://192.168.1.2:3773" ;
442+ const route = ( connectionId : string , httpBaseUrl : string ) => ( {
443+ target : new BearerConnectionTarget ( {
444+ environmentId : ENVIRONMENT_ID ,
445+ label : DESCRIPTOR . label ,
446+ connectionId,
447+ } ) ,
448+ profile : Option . some (
449+ new BearerConnectionProfile ( {
450+ connectionId,
451+ environmentId : ENVIRONMENT_ID ,
452+ label : DESCRIPTOR . label ,
453+ httpBaseUrl,
454+ wsBaseUrl : httpBaseUrl . replace ( "http" , "ws" ) ,
455+ } ) ,
456+ ) ,
457+ } ) ;
458+ const harness = yield * makeHarness ( {
459+ responses : [
460+ Response . json ( DESCRIPTOR ) ,
461+ Response . json ( DESCRIPTOR ) ,
462+ websocketTicket ( "first-ticket" ) ,
463+ Response . json ( { ...DESCRIPTOR , environmentId : "another-machine" } ) ,
464+ ] ,
465+ } ) ;
466+ const result = yield * Effect . gen ( function * ( ) {
467+ const remote = yield * RemoteEnvironmentAuthorization . RemoteEnvironmentAuthorization ;
468+ const driver = yield * ConnectionDriver . ConnectionDriver . pipe (
469+ Effect . provide (
470+ ConnectionDriver . layer . pipe (
471+ Layer . provide (
472+ Layer . mergeAll (
473+ Layer . succeed ( ConnectionResolver . ConnectionResolver , {
474+ prepare : ( entry ) =>
475+ Effect . gen ( function * ( ) {
476+ const profile = Option . getOrThrow ( entry . profile ) ;
477+ if ( profile . _tag !== "BearerConnectionProfile" )
478+ return yield * Effect . die ( "Unexpected profile" ) ;
479+ return {
480+ ...( yield * remote . authorizeBearer ( {
481+ expectedEnvironmentId : ENVIRONMENT_ID ,
482+ httpBaseUrl : profile . httpBaseUrl ,
483+ wsBaseUrl : profile . wsBaseUrl ,
484+ bearerToken : "bearer-token" ,
485+ connectionMethod : "direct" ,
486+ } ) ) ,
487+ target : entry . target ,
488+ } ;
489+ } ) ,
490+ prepareForUpdate : ( ) => Effect . die ( "Unexpected update" ) ,
491+ } ) ,
492+ Layer . succeed ( RpcSession . RpcSessionFactory , {
493+ connect : ( ) =>
494+ Effect . fail (
495+ new ConnectionTransientError ( {
496+ reason : "endpoint-unavailable" ,
497+ detail : "First route disconnected" ,
498+ } ) ,
499+ ) ,
500+ } ) ,
501+ ) ,
502+ ) ,
503+ ) ,
504+ ) ,
505+ ) ;
506+ return yield * driver
507+ . connect (
508+ {
509+ ...route ( "first" , "http://127.0.0.1:3773" ) ,
510+ alternateRoutes : [ route ( "lan" , lanUrl ) ] ,
511+ enabled : true ,
512+ } ,
513+ ( ) => Effect . void ,
514+ )
515+ . pipe ( Effect . result ) ;
516+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
517+
518+ expect ( result . _tag ) . toBe ( "Failure" ) ;
519+ for ( const [ url , init ] of harness . fetch . calls . filter ( ( [ url ] ) =>
520+ String ( url ) . startsWith ( lanUrl ) ,
521+ ) ) {
522+ expect ( new URL ( String ( url ) ) . pathname ) . toBe ( "/.well-known/t3/environment" ) ;
523+ expect ( new Headers ( init . headers ) . has ( "authorization" ) ) . toBe ( false ) ;
524+ expect ( init . cache ) . toBe ( "no-store" ) ;
525+ }
526+ expect (
527+ harness . fetch . calls
528+ . filter ( ( [ url ] ) => String ( url ) . startsWith ( lanUrl ) )
529+ . map ( ( [ url ] ) => new URL ( String ( url ) ) . pathname ) ,
530+ ) . toEqual ( [ "/.well-known/t3/environment" , "/.well-known/t3/environment" ] ) ;
531+ expect (
532+ harness . fetch . calls . filter ( ( [ url ] ) =>
533+ String ( url ) . endsWith ( "/api/auth/websocket-ticket/rpc" ) ,
534+ ) ,
535+ ) . toHaveLength ( 1 ) ;
536+ } ) ,
537+ ) ;
538+
434539 it . effect ( "reuses a same-account persisted environment token without contacting the relay" , ( ) =>
435540 Effect . gen ( function * ( ) {
436541 const cached = new TokenStore . RemoteDpopAccessToken ( {
0 commit comments