@@ -148,6 +148,7 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
148148 readonly initialConfig ?: ( attempt : number ) => Effect . Effect < ServerConfig , ConnectionAttemptError > ;
149149 readonly checkRoute ?: ( route : ConnectionRoute ) => Effect . Effect < ConnectionDriver . RouteCheck > ;
150150 readonly checkHost ?: ( prepared : PreparedConnection ) => Effect . Effect < ConnectionDriver . RouteCheck > ;
151+ readonly preflight ?: ( route : ConnectionRoute ) => Effect . Effect < ConnectionDriver . RoutePreflight > ;
151152 readonly inboundFrames ?: Effect . Effect < number > ;
152153} ) {
153154 const networkStatus = yield * SubscriptionRef . make < NetworkStatus > (
@@ -244,7 +245,12 @@ const makeHarness = Effect.fn("TestConnectionHarness.make")(function* (options?:
244245 ConnectionDriver . ConnectionDriver . of ( {
245246 connect,
246247 preflight : ( _entry , route ) =>
247- checkRoute ( route ) . pipe ( Effect . map ( ( check ) => check === "answered" ) ) ,
248+ options ?. preflight ?.( route ) ??
249+ checkRoute ( route ) . pipe (
250+ Effect . map ( ( check ) : ConnectionDriver . RoutePreflight =>
251+ check === "answered" ? "ready" : check ,
252+ ) ,
253+ ) ,
248254 checkHost : ( prepared ) =>
249255 options ?. checkHost ?.( prepared ) ??
250256 Effect . succeed < ConnectionDriver . RouteCheck > ( "unchecked" ) ,
@@ -1656,6 +1662,79 @@ describe("EnvironmentSupervisor routes", () => {
16561662 } ) ,
16571663 ) ;
16581664
1665+ it . effect ( "keeps checking a LAN route every minute when it answers but cannot be prepared" , ( ) =>
1666+ Effect . gen ( function * ( ) {
1667+ const lanPreflights = yield * Ref . make ( 0 ) ;
1668+ const harness = yield * makeHarness ( {
1669+ checkRoute : ( route ) =>
1670+ Effect . succeed ( route . target . _tag === "BearerConnectionTarget" ? "silent" : "unchecked" ) ,
1671+ preflight : ( route ) =>
1672+ route . target . _tag === "BearerConnectionTarget"
1673+ ? Ref . update ( lanPreflights , ( count ) => count + 1 ) . pipe ( Effect . as ( "unprepared" as const ) )
1674+ : Effect . succeed ( "unchecked" ) ,
1675+ prepare : ( _attempt , target ) => Effect . succeed ( preparedFor ( target ) ) ,
1676+ } ) ;
1677+ const supervisor = yield * EnvironmentSupervisor . make ( LAN_THEN_RELAY_ENTRY , {
1678+ initiallyDesired : true ,
1679+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1680+
1681+ yield * awaitState (
1682+ supervisor . state ,
1683+ ( state ) => state . phase === "connected" && state . generation === 1 ,
1684+ ) ;
1685+ for ( let minute = 0 ; minute < 10 ; minute += 1 ) {
1686+ yield * TestClock . adjust ( "60 seconds" ) ;
1687+ }
1688+ expect ( yield * Ref . get ( lanPreflights ) ) . toBe ( 10 ) ;
1689+ } ) ,
1690+ ) ;
1691+
1692+ it . effect ( "ignores a dead LAN check that was running when the network changed" , ( ) =>
1693+ Effect . gen ( function * ( ) {
1694+ const lanChecks = yield * Ref . make ( 0 ) ;
1695+ const checkStarted = yield * Deferred . make < void > ( ) ;
1696+ const finishCheck = yield * Deferred . make < void > ( ) ;
1697+ const harness = yield * makeHarness ( {
1698+ checkRoute : ( route ) =>
1699+ route . target . _tag === "BearerConnectionTarget"
1700+ ? Ref . updateAndGet ( lanChecks , ( count ) => count + 1 ) . pipe (
1701+ Effect . flatMap ( ( check ) =>
1702+ check === 1
1703+ ? Effect . succeed ( "silent" as const )
1704+ : check === 2
1705+ ? Deferred . succeed ( checkStarted , undefined ) . pipe (
1706+ Effect . andThen ( Deferred . await ( finishCheck ) ) ,
1707+ Effect . as ( "silent" as const ) ,
1708+ )
1709+ : Effect . succeed ( "answered" as const ) ,
1710+ ) ,
1711+ )
1712+ : Effect . succeed ( "unchecked" ) ,
1713+ prepare : ( _attempt , target ) => Effect . succeed ( preparedFor ( target ) ) ,
1714+ } ) ;
1715+ const supervisor = yield * EnvironmentSupervisor . make ( LAN_THEN_RELAY_ENTRY , {
1716+ initiallyDesired : true ,
1717+ } ) . pipe ( Effect . provide ( harness . dependencies ) ) ;
1718+
1719+ yield * awaitState (
1720+ supervisor . state ,
1721+ ( state ) => state . phase === "connected" && state . generation === 1 ,
1722+ ) ;
1723+ yield * TestClock . adjust ( "60 seconds" ) ;
1724+ yield * Deferred . await ( checkStarted ) ;
1725+ yield * harness . wake ( "network-changed" ) ;
1726+ yield * Deferred . succeed ( finishCheck , undefined ) ;
1727+
1728+ yield * awaitState (
1729+ supervisor . state ,
1730+ ( state ) => state . phase === "connected" && state . generation === 2 ,
1731+ ) ;
1732+ expect ( Option . getOrThrow ( yield * SubscriptionRef . get ( supervisor . prepared ) ) . target . _tag ) . toBe (
1733+ "BearerConnectionTarget" ,
1734+ ) ;
1735+ } ) ,
1736+ ) ;
1737+
16591738 it . effect ( "moves off a dead LAN socket when the phone switches to cellular" , ( ) =>
16601739 Effect . gen ( function * ( ) {
16611740 const lanReachable = yield * Ref . make ( true ) ;
0 commit comments