@@ -2,6 +2,8 @@ import { AuthStandardClientScopes, EnvironmentId, type ServerConfig } from "@t3t
22import { describe , expect , it } from "@effect/vitest" ;
33import * as Deferred from "effect/Deferred" ;
44import * as Effect from "effect/Effect" ;
5+ import * as Exit from "effect/Exit" ;
6+ import * as Scope from "effect/Scope" ;
57import * as Layer from "effect/Layer" ;
68import * as Option from "effect/Option" ;
79import * as Random from "effect/Random" ;
@@ -20,6 +22,7 @@ import { fetchEnvironmentSessionState } from "../state/session.ts";
2022import type { ConnectionCatalogEntry , ConnectionRoute } from "./catalog.ts" ;
2123import * as Connectivity from "./connectivity.ts" ;
2224import * as ConnectionDriver from "./driver.ts" ;
25+ import * as ConnectionResolver from "./resolver.ts" ;
2326import { BearerConnectionProfile } from "./catalog.ts" ;
2427import {
2528 BearerConnectionTarget ,
@@ -231,8 +234,6 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
231234 ConnectionDriver . ConnectionDriver . of ( {
232235 connect,
233236 checkRoute : ( _entry , route ) => checkRoute ( route ) ,
234- preflight : ( _entry , route ) =>
235- checkRoute ( route ) . pipe ( Effect . map ( ( check ) => check === "answered" ) ) ,
236237 } ) ,
237238 ) ,
238239 ) ;
@@ -1003,6 +1004,111 @@ describe("EnvironmentSupervisor", () => {
10031004} ) ;
10041005
10051006describe ( "EnvironmentSupervisor routes" , ( ) => {
1007+ it . effect . each ( [ "opening" , "synchronizing" ] as const ) (
1008+ "keeps a healthy tailnet session when LAN passes preflight but fails %s" ,
1009+ ( failureStage ) =>
1010+ Effect . gen ( function * ( ) {
1011+ const tailnetTarget = new BearerConnectionTarget ( {
1012+ environmentId : TARGET . environmentId ,
1013+ label : TARGET . label ,
1014+ connectionId : "bearer:tailnet" ,
1015+ } ) ;
1016+ const entry = entryWithRoutes ( LAN_THEN_RELAY_ENTRY , [
1017+ LAN_ROUTE ,
1018+ {
1019+ target : tailnetTarget ,
1020+ profile : Option . some (
1021+ new BearerConnectionProfile ( {
1022+ connectionId : tailnetTarget . connectionId ,
1023+ environmentId : TARGET . environmentId ,
1024+ label : TARGET . label ,
1025+ httpBaseUrl : "http://100.64.0.1:3773/" ,
1026+ wsBaseUrl : "ws://100.64.0.1:3773/" ,
1027+ network : "tailscale" ,
1028+ } ) ,
1029+ ) ,
1030+ } ,
1031+ ] ) ;
1032+ const harness = yield * makeHarness ( ) ;
1033+ const opened = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
1034+ const released = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
1035+ const driver = yield * ConnectionDriver . make . pipe (
1036+ Effect . provideService ( ConnectionResolver . ConnectionResolver , {
1037+ prepare : ( route ) => Effect . succeed ( preparedFor ( route . target ) ) ,
1038+ prepareForUpdate : ( ) => Effect . die ( "unused" ) ,
1039+ } ) ,
1040+ Effect . provideService ( RpcSession . RpcSessionFactory , {
1041+ connect : ( prepared ) =>
1042+ Effect . gen ( function * ( ) {
1043+ const id = connectionRouteId ( prepared . target ) ;
1044+ yield * Ref . update ( opened , ( ids ) => [ ...ids , id ] ) ;
1045+ const fails = id === LAN_TARGET . connectionId ;
1046+ if ( fails && failureStage === "opening" ) return yield * transient ( ) ;
1047+ return yield * Effect . acquireRelease (
1048+ Effect . succeed ( {
1049+ client : TEST_RPC_CLIENT ,
1050+ initialConfig : Effect . die ( "unused" ) ,
1051+ subscribeServerConfig : ( ) => Stream . die ( "unused" ) ,
1052+ ready : fails ? Effect . fail ( transient ( ) ) : Effect . void ,
1053+ probe : Effect . void ,
1054+ closed : Effect . never ,
1055+ } satisfies RpcSession . RpcSession ) ,
1056+ ( ) => Ref . update ( released , ( ids ) => [ ...ids , id ] ) ,
1057+ ) ;
1058+ } ) ,
1059+ } ) ,
1060+ Effect . provide (
1061+ RpcHttp . layerRemoteHttpClient ( ( ( ) =>
1062+ Promise . resolve (
1063+ Response . json ( {
1064+ environmentId : TARGET . environmentId ,
1065+ label : TARGET . label ,
1066+ platform : { os : "darwin" , arch : "arm64" } ,
1067+ serverVersion : "0.0.0-test" ,
1068+ capabilities : { } ,
1069+ } ) ,
1070+ ) ) satisfies typeof fetch ) ,
1071+ ) ,
1072+ ) ;
1073+ const scope = yield * Scope . fork ( yield * Scope . Scope ) ;
1074+ const supervisor = yield * EnvironmentSupervisor . make ( entry , {
1075+ initiallyDesired : true ,
1076+ } ) . pipe (
1077+ Effect . provideService ( ConnectionDriver . ConnectionDriver , driver ) ,
1078+ Effect . provide ( harness . dependencies ) ,
1079+ Scope . provide ( scope ) ,
1080+ ) ;
1081+ yield * awaitState ( supervisor . state , ( state ) => state . phase === "connected" ) ;
1082+ const original = Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ;
1083+ const baseline = yield * Ref . get ( opened ) ;
1084+ const baselineReleased = yield * Ref . get ( released ) ;
1085+ expect ( baseline ) . toEqual ( [ LAN_TARGET . connectionId , tailnetTarget . connectionId ] ) ;
1086+ expect ( yield * driver . checkRoute ( entry , LAN_ROUTE ) ) . toBe ( "answered" ) ;
1087+
1088+ yield * harness . wake ( "network-changed" ) ;
1089+ yield * harness . setNetworkStatus ( "unknown" ) ;
1090+ yield * harness . setNetworkStatus ( "online" ) ;
1091+ yield * supervisor . retryNow ;
1092+ yield * TestClock . adjust ( "12 minutes" ) ;
1093+ expect ( yield * Ref . get ( opened ) ) . toEqual ( baseline ) ;
1094+ expect ( yield * Ref . get ( released ) ) . toEqual ( baselineReleased ) ;
1095+ expect ( Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . toBe ( original ) ;
1096+ expect ( yield * SubscriptionRef . get ( supervisor . state ) ) . toMatchObject ( {
1097+ phase : "connected" ,
1098+ generation : 1 ,
1099+ } ) ;
1100+
1101+ yield * Scope . close ( scope , Exit . void ) ;
1102+ expect ( Option . isNone ( yield * SubscriptionRef . get ( supervisor . session ) ) ) . toBe ( true ) ;
1103+ expect (
1104+ ( yield * Ref . get ( released ) ) . filter ( ( id ) => id === tailnetTarget . connectionId ) ,
1105+ ) . toHaveLength ( 1 ) ;
1106+ expect (
1107+ ( yield * Ref . get ( released ) ) . filter ( ( id ) => id === LAN_TARGET . connectionId ) ,
1108+ ) . toHaveLength ( failureStage === "opening" ? 0 : 1 ) ;
1109+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1110+ ) ;
1111+
10061112 it . effect ( "connects over relay before a slow LAN reachability check times out" , ( ) =>
10071113 Effect . gen ( function * ( ) {
10081114 const checkingLan = yield * Deferred . make < void > ( ) ;
@@ -1273,7 +1379,7 @@ describe("EnvironmentSupervisor routes", () => {
12731379 } ) ,
12741380 ) ;
12751381
1276- it . effect ( "moves back to the LAN route when the network changes and it answers again " , ( ) =>
1382+ it . effect ( "uses a newly reachable LAN route on the next necessary connection " , ( ) =>
12771383 Effect . gen ( function * ( ) {
12781384 const lanReachable = yield * Ref . make ( false ) ;
12791385 const harness = yield * makeHarness ( {
@@ -1300,6 +1406,7 @@ describe("EnvironmentSupervisor routes", () => {
13001406 yield * Ref . set ( lanReachable , true ) ;
13011407 yield * harness . setNetworkStatus ( "unknown" ) ;
13021408 yield * harness . setNetworkStatus ( "online" ) ;
1409+ yield * harness . wake ( "application-active-reconnect" ) ;
13031410
13041411 yield * awaitState (
13051412 supervisor . state ,
@@ -1354,7 +1461,7 @@ describe("EnvironmentSupervisor routes", () => {
13541461 } ) ,
13551462 ) ;
13561463
1357- it . effect ( "learns the LAN address over T3 Connect and moves to it " , ( ) =>
1464+ it . effect ( "uses learned LAN routes on the next necessary connection " , ( ) =>
13581465 Effect . gen ( function * ( ) {
13591466 const relayEntry : ConnectionCatalogEntry = {
13601467 target : RELAY_TARGET ,
@@ -1378,6 +1485,7 @@ describe("EnvironmentSupervisor routes", () => {
13781485 ) ,
13791486 } ) ;
13801487 const current = yield * Ref . make ( relayEntry ) ;
1488+ const routesLearned = yield * Deferred . make < void > ( ) ;
13811489 const supervisor = yield * EnvironmentSupervisor . make ( relayEntry , {
13821490 initiallyDesired : true ,
13831491 learnRoutes : ( { activeRoute, reported } ) =>
@@ -1396,10 +1504,16 @@ describe("EnvironmentSupervisor routes", () => {
13961504 ...all ,
13971505 ...routes . filter ( isLearned ) . map ( ( route ) => connectionRouteId ( route . target ) ) ,
13981506 ] ) ;
1507+ yield * Deferred . succeed ( routesLearned , undefined ) ;
13991508 return Option . some ( next ) ;
14001509 } ) ,
14011510 } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
14021511
1512+ yield * Deferred . await ( routesLearned ) ;
1513+ yield * TestClock . adjust ( "12 minutes" ) ;
1514+ expect ( yield * Ref . get ( harness . sessionCount ) ) . toBe ( 1 ) ;
1515+ expect ( yield * Ref . get ( harness . releaseCount ) ) . toBe ( 0 ) ;
1516+ yield * harness . wake ( "application-active-reconnect" ) ;
14031517 yield * awaitState (
14041518 supervisor . state ,
14051519 ( state ) => state . phase === "connected" && state . generation === 2 ,
@@ -1410,6 +1524,6 @@ describe("EnvironmentSupervisor routes", () => {
14101524 expect ( yield * Ref . get ( learned ) ) . toEqual ( [
14111525 `learned:${ TARGET . environmentId } :http://192.168.1.10:3773` ,
14121526 ] ) ;
1413- } ) ,
1527+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
14141528 ) ;
14151529} ) ;
0 commit comments