@@ -557,18 +557,64 @@ void main() {
557557 expect (manager.getEffectiveTimestamp ('channel-1' ), 300 );
558558 });
559559
560- test ('a reconnect during initialize leaves one live subscription' , () async {
560+ test (
561+ 'a connect during a successful initialize does not sync again' ,
562+ () async {
563+ SharedPreferences .setMockInitialValues ({});
564+ final prefs = await SharedPreferences .getInstance ();
565+ final keychain = nostr.Keys .generate ();
566+ final crypto = ReadStateCrypto .tryCreate (
567+ nsec: keychain.nsec,
568+ pubkey: keychain.public,
569+ )! ;
570+ // First connect: initialize waits on a history REQ held for the socket
571+ // while the session reports connected and the provider's listener fires.
572+ final gate = Completer <void >();
573+ final relay = _FakeRelaySession ()..fetchGate = gate.future;
574+ final manager = ReadStateManager (
575+ pubkey: keychain.public,
576+ relayUrl: _relayUrl,
577+ prefs: prefs,
578+ crypto: crypto,
579+ relaySession: relay,
580+ signedEventRelay: _FakeSignedEventRelay (),
581+ remoteEnabled: true ,
582+ onChanged: () {},
583+ );
584+
585+ final initializing = manager.initialize ();
586+ final reinitializing = manager.reinitializeRemote ();
587+ relay.fetchGate = null ;
588+ gate.complete ();
589+ await Future .wait ([initializing, reinitializing]);
590+
591+ expect (relay.fetchCount, 1 );
592+ expect (relay.subscribeCount, 1 );
593+ expect (relay.liveSubscriptions, 1 );
594+
595+ // A later reconnect syncs again and still leaves one subscription.
596+ await manager.reinitializeRemote ();
597+ expect (relay.fetchCount, 2 );
598+ expect (relay.subscribeCount, 2 );
599+ expect (relay.liveSubscriptions, 1 );
600+
601+ manager.dispose ();
602+ expect (relay.liveSubscriptions, 0 );
603+ },
604+ );
605+
606+ test ('a connect during a failed initialize syncs again' , () async {
561607 SharedPreferences .setMockInitialValues ({});
562608 final prefs = await SharedPreferences .getInstance ();
563609 final keychain = nostr.Keys .generate ();
564610 final crypto = ReadStateCrypto .tryCreate (
565611 nsec: keychain.nsec,
566612 pubkey: keychain.public,
567613 )! ;
568- // First connect: initialize waits on a history REQ held for the socket
569- // while the session reports connected and the provider's listener fires.
570614 final gate = Completer <void >();
571- final relay = _FakeRelaySession ()..fetchGate = gate.future;
615+ final relay = _FakeRelaySession ()
616+ ..fetchGate = gate.future
617+ ..failNextFetch = true ;
572618 final manager = ReadStateManager (
573619 pubkey: keychain.public,
574620 relayUrl: _relayUrl,
@@ -587,13 +633,58 @@ void main() {
587633 await Future .wait ([initializing, reinitializing]);
588634
589635 // Both passes subscribed, but the later one replaced the earlier one.
636+ expect (relay.fetchCount, 2 );
590637 expect (relay.subscribeCount, 2 );
591638 expect (relay.liveSubscriptions, 1 );
592639
593640 manager.dispose ();
594641 expect (relay.liveSubscriptions, 0 );
595642 });
596643
644+ test ('reconnects during a re-sync leave one live subscription' , () async {
645+ SharedPreferences .setMockInitialValues ({});
646+ final prefs = await SharedPreferences .getInstance ();
647+ final keychain = nostr.Keys .generate ();
648+ final crypto = ReadStateCrypto .tryCreate (
649+ nsec: keychain.nsec,
650+ pubkey: keychain.public,
651+ )! ;
652+ final relay = _FakeRelaySession ();
653+ final manager = ReadStateManager (
654+ pubkey: keychain.public,
655+ relayUrl: _relayUrl,
656+ prefs: prefs,
657+ crypto: crypto,
658+ relaySession: relay,
659+ signedEventRelay: _FakeSignedEventRelay (),
660+ remoteEnabled: true ,
661+ onChanged: () {},
662+ );
663+ await manager.initialize ();
664+
665+ // The first re-sync holds its fetch; three more connects arrive.
666+ final gate = Completer <void >();
667+ relay.fetchGate = gate.future;
668+ final first = manager.reinitializeRemote ();
669+ await Future <void >.delayed (Duration .zero);
670+ final later = [
671+ manager.reinitializeRemote (),
672+ manager.reinitializeRemote (),
673+ manager.reinitializeRemote (),
674+ ];
675+ relay.fetchGate = null ;
676+ gate.complete ();
677+ await Future .wait ([first, ...later]);
678+
679+ // One initial pass, the held re-sync, and one shared re-sync after it.
680+ expect (relay.fetchCount, 3 );
681+ expect (relay.subscribeCount, 3 );
682+ expect (relay.liveSubscriptions, 1 );
683+
684+ manager.dispose ();
685+ expect (relay.liveSubscriptions, 0 );
686+ });
687+
597688 test ('remote read-state rollback is ignored' , () async {
598689 SharedPreferences .setMockInitialValues ({});
599690 final prefs = await SharedPreferences .getInstance ();
@@ -779,6 +870,8 @@ NostrEvent _readStateEvent({
779870class _FakeRelaySession extends RelaySessionNotifier {
780871 List <NostrEvent > historyEvents = [];
781872 Future <void >? fetchGate;
873+ bool failNextFetch = false ;
874+ int fetchCount = 0 ;
782875 int subscribeCount = 0 ;
783876 int liveSubscriptions = 0 ;
784877
@@ -787,7 +880,12 @@ class _FakeRelaySession extends RelaySessionNotifier {
787880 NostrFilter filter, {
788881 Duration timeout = const Duration (seconds: 8 ),
789882 }) async {
883+ fetchCount++ ;
790884 await fetchGate;
885+ if (failNextFetch) {
886+ failNextFetch = false ;
887+ throw TimeoutException ('history timed out' );
888+ }
791889 return historyEvents;
792890 }
793891
0 commit comments