@@ -12,6 +12,7 @@ import * as Option from "effect/Option";
1212import * as Ref from "effect/Ref" ;
1313import * as Stream from "effect/Stream" ;
1414import * as SubscriptionRef from "effect/SubscriptionRef" ;
15+ import { Atom , AtomRegistry } from "effect/reactivity" ;
1516
1617import * as ManagedRelay from "./managedRelay.ts" ;
1718import * as ClientCapabilities from "../platform/capabilities.ts" ;
@@ -20,6 +21,7 @@ import { ConnectionBlockedError, type NetworkStatus } from "../connection/model.
2021import * as ConnectionWakeups from "../connection/wakeups.ts" ;
2122import * as RelayEnvironmentDiscovery from "./discovery.ts" ;
2223import { NETWORK_BLOCKING_HINT } from "../errors/network.ts" ;
24+ import { createRelayEnvironmentDiscoveryAtoms } from "../state/relayDiscovery.ts" ;
2325
2426const environments = [
2527 {
@@ -59,6 +61,8 @@ function status(
5961const makeHarness = Effect . fn ( "RelayDiscoveryTest.makeHarness" ) ( function * ( ) {
6062 const networkStatus = yield * SubscriptionRef . make < NetworkStatus > ( "online" ) ;
6163 const listCalls = yield * Ref . make ( 0 ) ;
64+ const listedEnvironments =
65+ yield * Ref . make < ReadonlyArray < RelayClientEnvironmentRecord > > ( environments ) ;
6266 const listFailure = yield * Ref . make < ManagedRelay . ManagedRelayClientError | null > ( null ) ;
6367 const secondListCall = yield * Deferred . make < void > ( ) ;
6468 const clerkToken = yield * Ref . make < string | null > ( "clerk-token" ) ;
@@ -100,7 +104,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
100104 if ( failure ) {
101105 return yield * failure ;
102106 }
103- return environments ;
107+ return yield * Ref . get ( listedEnvironments ) ;
104108 } ) ,
105109 getEnvironmentStatus : ( { environmentId } ) =>
106110 Ref . get ( statusRequests ) . pipe (
@@ -165,6 +169,7 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
165169 return {
166170 layer,
167171 listCalls,
172+ listedEnvironments,
168173 listFailure,
169174 clerkToken,
170175 networkStatus,
@@ -179,6 +184,51 @@ const makeHarness = Effect.fn("RelayDiscoveryTest.makeHarness")(function* () {
179184} ) ;
180185
181186describe ( "RelayEnvironmentDiscovery" , ( ) => {
187+ it . effect ( "fetches a fresh list after removal while an older refresh is checking statuses" , ( ) =>
188+ Effect . scoped (
189+ Effect . gen ( function * ( ) {
190+ const harness = yield * makeHarness ( ) ;
191+ yield * Effect . gen ( function * ( ) {
192+ const discovery = yield * RelayEnvironmentDiscovery . RelayEnvironmentDiscovery ;
193+ const atoms = createRelayEnvironmentDiscoveryAtoms (
194+ Atom . runtime (
195+ Layer . succeed ( RelayEnvironmentDiscovery . RelayEnvironmentDiscovery , discovery ) ,
196+ ) ,
197+ ) ;
198+ const registry = AtomRegistry . make ( ) ;
199+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => registry . dispose ( ) ) ) ;
200+
201+ const refresh = atoms . refresh . run ( registry , undefined ) ;
202+ yield * SubscriptionRef . changes ( discovery . state ) . pipe (
203+ Stream . filter ( ( state ) => state . environments . size === environments . length ) ,
204+ Stream . runHead ,
205+ ) ;
206+ const coalescedRefresh = atoms . refresh . run ( registry , undefined ) ;
207+ yield * Ref . set ( harness . listedEnvironments , [ environments [ 1 ] ! ] ) ;
208+ const mutationRefresh = atoms . refreshAfterMutation . run ( registry , undefined ) ;
209+
210+ const requests = yield * Ref . get ( harness . statusRequests ) ;
211+ for ( const environment of environments ) {
212+ yield * Deferred . succeed (
213+ requests . get ( environment . environmentId ) ! ,
214+ status ( environment , "online" ) ,
215+ ) ;
216+ }
217+ const results = yield * Effect . promise ( ( ) =>
218+ Promise . all ( [ refresh , coalescedRefresh , mutationRefresh ] ) ,
219+ ) ;
220+ for ( const result of results ) {
221+ expect ( result . _tag ) . toBe ( "Success" ) ;
222+ }
223+ expect ( yield * Ref . get ( harness . listCalls ) ) . toBe ( 2 ) ;
224+ const complete = yield * SubscriptionRef . get ( discovery . state ) ;
225+ expect ( [ ...complete . environments . keys ( ) ] ) . toEqual ( [ environments [ 1 ] ! . environmentId ] ) ;
226+ expect ( complete . refreshing ) . toBe ( false ) ;
227+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
228+ } ) ,
229+ ) ,
230+ ) ;
231+
182232 it . effect ( "publishes each environment status as soon as that lookup completes" , ( ) =>
183233 Effect . gen ( function * ( ) {
184234 const harness = yield * makeHarness ( ) ;
@@ -228,6 +278,38 @@ describe("RelayEnvironmentDiscovery", () => {
228278 } ) ,
229279 ) ;
230280
281+ it . effect ( "reports an environment as unregistered only once the relay list has loaded" , ( ) =>
282+ Effect . gen ( function * ( ) {
283+ const harness = yield * makeHarness ( ) ;
284+ yield * Effect . gen ( function * ( ) {
285+ const discovery = yield * RelayEnvironmentDiscovery . RelayEnvironmentDiscovery ;
286+ const listed = environments [ 0 ] ! . environmentId ;
287+ const deregistered = "deregistered-environment" ;
288+ const before = yield * SubscriptionRef . get ( discovery . state ) ;
289+ expect ( RelayEnvironmentDiscovery . relayEnvironmentRegistration ( before , deregistered ) ) . toBe (
290+ "unknown" ,
291+ ) ;
292+
293+ const requests = yield * Ref . get ( harness . statusRequests ) ;
294+ for ( const environment of environments ) {
295+ yield * Deferred . succeed (
296+ requests . get ( environment . environmentId ) ! ,
297+ status ( environment , "online" ) ,
298+ ) ;
299+ }
300+ yield * discovery . refresh ;
301+
302+ const after = yield * SubscriptionRef . get ( discovery . state ) ;
303+ expect ( RelayEnvironmentDiscovery . relayEnvironmentRegistration ( after , listed ) ) . toBe (
304+ "registered" ,
305+ ) ;
306+ expect ( RelayEnvironmentDiscovery . relayEnvironmentRegistration ( after , deregistered ) ) . toBe (
307+ "unregistered" ,
308+ ) ;
309+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
310+ } ) ,
311+ ) ;
312+
231313 it . effect (
232314 "preserves discovered rows while offline and refreshes after connectivity returns" ,
233315 ( ) =>
@@ -350,6 +432,12 @@ describe("RelayEnvironmentDiscovery", () => {
350432 const failed = yield * SubscriptionRef . get ( discovery . state ) ;
351433 expect ( failed . environments . size ) . toBe ( 0 ) ;
352434 expect ( Option . isSome ( failed . error ) ) . toBe ( true ) ;
435+ expect (
436+ RelayEnvironmentDiscovery . relayEnvironmentRegistration (
437+ failed ,
438+ environments [ 0 ] ! . environmentId ,
439+ ) ,
440+ ) . toBe ( "unknown" ) ;
353441 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
354442 } ) ,
355443 ) ;
@@ -397,6 +485,12 @@ describe("RelayEnvironmentDiscovery", () => {
397485 expect ( state . environments . size ) . toBe ( 0 ) ;
398486 expect ( state . refreshing ) . toBe ( false ) ;
399487 expect ( Option . isNone ( state . error ) ) . toBe ( true ) ;
488+ expect (
489+ RelayEnvironmentDiscovery . relayEnvironmentRegistration (
490+ state ,
491+ environments [ 0 ] ! . environmentId ,
492+ ) ,
493+ ) . toBe ( "unknown" ) ;
400494 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
401495 } ) ,
402496 ) ;
@@ -429,7 +523,14 @@ describe("RelayEnvironmentDiscovery", () => {
429523 yield * Fiber . join ( refreshFiber ) ;
430524 yield * Effect . yieldNow ;
431525
432- expect ( ( yield * SubscriptionRef . get ( discovery . state ) ) . environments . size ) . toBe ( 0 ) ;
526+ const signedOut = yield * SubscriptionRef . get ( discovery . state ) ;
527+ expect ( signedOut . environments . size ) . toBe ( 0 ) ;
528+ expect (
529+ RelayEnvironmentDiscovery . relayEnvironmentRegistration (
530+ signedOut ,
531+ environments [ 0 ] ! . environmentId ,
532+ ) ,
533+ ) . toBe ( "unknown" ) ;
433534 } ) . pipe ( Effect . provide ( harness . layer ) , Effect . scoped ) ;
434535 } ) ,
435536 ) ;
0 commit comments