import 'package:buzz/app.dart'; import 'package:buzz/features/activity/activity_page.dart'; import 'package:buzz/features/age_gate/age_signal_provider.dart'; import 'package:buzz/shared/auth/auth.dart'; import 'package:buzz/shared/theme/theme_provider.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:async'; import 'package:buzz/features/activity/activity_provider.dart'; import 'package:buzz/features/activity/feed_item.dart'; import 'package:buzz/features/channels/channel.dart'; import 'package:buzz/features/channels/channel_management_provider.dart'; import 'package:buzz/features/channels/channels_provider.dart'; import 'package:buzz/shared/relay/relay.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; /// Records subscriptions and DM history queries for Activity projection tests. class _RecordingSessionNotifier extends RelaySessionNotifier { final List> dmQueries = []; final List queryFilterCounts = []; final List _history = []; final List<({NostrFilter filter, void Function(NostrEvent) onEvent})> _subscriptions = []; Completer? mentionFetchGate; bool failNextMentionFetch = false; bool failNextQueryRelay = false; int mentionFetchCount = 0; int activeMentionFetches = 0; int maxActiveMentionFetches = 0; Completer? hiddenSubscribeGate; bool failNextHiddenSubscribe = false; int hiddenUnsubscribeCount = 0; // Per-batch gates keyed by hidden-subscribe call order (0-based), so a test // can park an individual batch's REQ while letting earlier ones settle. final Map> hiddenSubscribeGatesByCall = {}; int hiddenSubscribeCallCount = 0; @override SessionState build() => const SessionState(status: SessionStatus.connected); @override Future> fetchHistory( NostrFilter filter, { Duration timeout = const Duration(seconds: 8), }) async { final h = filter.tags['#h']; if (h != null) dmQueries.add(h); final isMentionFetch = filter.tags.containsKey('#p') && filter.kinds.contains(40002); if (isMentionFetch) { mentionFetchCount += 1; activeMentionFetches += 1; if (activeMentionFetches > maxActiveMentionFetches) { maxActiveMentionFetches = activeMentionFetches; } try { final gate = mentionFetchGate; if (gate != null) await gate.future; if (failNextMentionFetch) { failNextMentionFetch = false; throw StateError('transient mention history failure'); } } finally { activeMentionFetches -= 1; } } return _window(filter); } @override Future> queryRelay( List filters, { Duration timeout = const Duration(seconds: 8), }) async { queryFilterCounts.add(filters.length); if (failNextQueryRelay) { failNextQueryRelay = false; throw StateError('transient HTTP query failure'); } for (final filter in filters) { final h = filter.tags['#h']; if (h != null) dmQueries.add(h); } final isMentionFetch = filters.any( (filter) => filter.tags.containsKey('#p') && filter.kinds.contains(40002), ); if (isMentionFetch) { mentionFetchCount += 1; activeMentionFetches += 1; if (activeMentionFetches > maxActiveMentionFetches) { maxActiveMentionFetches = activeMentionFetches; } try { final gate = mentionFetchGate; if (gate != null) await gate.future; if (failNextMentionFetch) { failNextMentionFetch = false; throw StateError('transient mention history failure'); } } finally { activeMentionFetches -= 1; } } // Like the production bridge: each filter's results, concatenated. return [for (final filter in filters) ..._window(filter)]; } @override Future subscribe( NostrFilter filter, void Function(NostrEvent) onEvent, { void Function(String message)? onClosed, }) async { // A hidden-DM resurface subscription carries the full channel-message kind // set plus an `#h` tag; the visible-DM sub uses only kind 9. Batching and // teardown assertions look only at the hidden-DM subscriptions. final isHidden = filter.tags.containsKey('#h') && filter.kinds.length == EventKind.channelMessageEventKinds.length; if (isHidden) { final callIndex = hiddenSubscribeCallCount++; final perCallGate = hiddenSubscribeGatesByCall[callIndex]; if (perCallGate != null) await perCallGate.future; final gate = hiddenSubscribeGate; if (gate != null) await gate.future; if (failNextHiddenSubscribe) { failNextHiddenSubscribe = false; throw StateError('relay rejected hidden-DM subscription'); } } final subscription = (filter: filter, onEvent: onEvent); _subscriptions.add(subscription); return () { if (isHidden) hiddenUnsubscribeCount += 1; _subscriptions.remove(subscription); }; } /// The `#h` value lists of every registered hidden-DM subscription, in order. List> get hiddenDmSubscriptionBatches => [ for (final subscription in _subscriptions) if (subscription.filter.tags.containsKey('#h') && subscription.filter.kinds.length == EventKind.channelMessageEventKinds.length) subscription.filter.tags['#h']!, ]; /// The `#h` value lists of every registered visible-DM subscription (kind 9 /// only), in order. List> get visibleDmSubscriptionBatches => [ for (final subscription in _subscriptions) if (subscription.filter.tags.containsKey('#h') && subscription.filter.kinds.length == 1 && subscription.filter.kinds.single == 9) subscription.filter.tags['#h']!, ]; void emit(NostrEvent event) { _history.add(event); for (final subscription in List.of(_subscriptions)) { if (_matches(subscription.filter, event)) { subscription.onEvent(event); } } } void seed(NostrEvent event) => _history.add(event); /// The newest [NostrFilter.limit] matches, as a relay returns them. List _window(NostrFilter filter) => (_history.where((event) => _matches(filter, event)).toList() ..sort((a, b) { final time = b.createdAt.compareTo(a.createdAt); return time != 0 ? time : a.id.compareTo(b.id); })) .take(filter.limit) .toList(); bool _matches(NostrFilter filter, NostrEvent event) { if (!filter.kinds.contains(event.kind)) return false; final until = filter.until; if (until != null && event.createdAt > until) return false; final beforeId = filter.extensions['before_id'] as String?; if (beforeId != null && event.createdAt == until && event.id.compareTo(beforeId) <= 0) { return false; } for (final entry in filter.tags.entries) { final tagName = entry.key.startsWith('#') ? entry.key.substring(1) : entry.key; final matchesTag = event.tags.any( (tag) => tag.length > 1 && tag[0] == tagName && entry.value.contains(tag[1]), ); if (!matchesTag) return false; } return true; } } /// Channels provider that starts loading and resolves on demand, modelling a /// cold start where the channel list arrives after Activity's first fetch. class _LateChannelsNotifier extends ChannelsNotifier { final Completer> _completer = Completer>(); @override Future> build() => _completer.future; void resolve(List channels) => _completer.complete(channels); } class _FixedRelayConfigNotifier extends RelayConfigNotifier { @override RelayConfig build() => const RelayConfig(baseUrl: 'https://relay.example', nsec: null); } Channel _dmChannel(String id) => Channel( id: id, name: 'dm', channelType: 'dm', visibility: 'private', description: '', createdBy: 'x', createdAt: DateTime(2025), memberCount: 2, isMember: true, ); NostrEvent _mentionEvent(String id, int createdAt) => NostrEvent( id: id, pubkey: 'other_pk', createdAt: createdAt, kind: 40002, tags: const [ ['p', 'me_pk'], ['h', 'channel-1'], ], content: 'Hello from the live relay', sig: '', ); Future _waitFor(bool Function() predicate) async { for (var attempt = 0; attempt < 100; attempt++) { if (predicate()) return; await Future.delayed(const Duration(milliseconds: 10)); } fail('Condition was not reached before timeout'); } void main() { TestWidgetsFlutterBinding.ensureInitialized(); test('refetches and includes DMs when channels resolve after first ' 'fetch (cold start)', () async { final session = _RecordingSessionNotifier(); final channels = _LateChannelsNotifier(); final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith(() => channels), ], ); addTearDown(container.dispose); // Cold start: channels still loading, so the first fetch has no DM ids. await container.read(activityProvider.future); expect(session.dmQueries, isEmpty); expect(session.queryFilterCounts, [3]); // Channel list resolves with a DM → Activity must rebuild and query it. channels.resolve([_dmChannel('dm1')]); await container.read(channelsProvider.future); await container.read(activityProvider.future); expect(session.dmQueries, hasLength(1)); expect(session.dmQueries.single, ['dm1']); expect(session.queryFilterCounts, [3, 4]); }); test('does not query DMs when the resolved channel list has none', () async { final session = _RecordingSessionNotifier(); final channels = _LateChannelsNotifier(); final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith(() => channels), ], ); addTearDown(container.dispose); await container.read(activityProvider.future); channels.resolve(const []); await container.read(channelsProvider.future); await container.read(activityProvider.future); expect(session.dmQueries, isEmpty); }); test('falls back to websocket history when the HTTP batch fails', () async { final session = _RecordingSessionNotifier() ..seed(_mentionEvent('fallback-mention', 1_700_000_001)) ..failNextQueryRelay = true; final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const []), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); final feed = await container.read(activityProvider.future); expect(session.queryFilterCounts, [3]); expect(session.mentionFetchCount, 1); expect(feed.mentions.map((item) => item.id), ['fallback-mention']); }); test('a workflow owner attribution is not a mention of the owner', () async { NostrEvent workflowMessage(String id, List> mentions) => NostrEvent( id: id, pubkey: 'relay_pk', createdAt: 1_700_000_001, kind: 9, tags: [ ['p', 'me_pk', '', 'owner'], ['h', 'channel-1'], ['buzz:workflow', 'true'], ['buzz:workflow-owner', 'me_pk'], ...mentions, ], content: 'workflow output', sig: '', ); final session = _RecordingSessionNotifier() ..seed( workflowMessage('mentions-agent', [ ['p', 'agent_pk'], ]), ) ..seed( workflowMessage('mentions-owner', [ ['p', 'me_pk'], ]), ); final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const []), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); final feed = await container.read(activityProvider.future); expect(feed.mentions.map((item) => item.id), ['mentions-owner']); }); test('quiet agent messages never become Activity items', () async { NostrEvent mention(String id, {bool quiet = false, int at = 0}) => NostrEvent( id: id, pubkey: 'agent_pk', createdAt: 1_700_000_001 + at, kind: 9, tags: [ ['p', 'me_pk'], ['h', 'channel-1'], if (quiet) ['audience', 'agents'], ], content: 'progress', sig: '', ); // A full mention window of quiet messages hides the older real one. // The real mention shares the boundary second with quiet ones that sort // before it, so a one-second-back cursor would skip it. final session = _RecordingSessionNotifier()..seed(mention('z-mention')); for (var i = 0; i < 50; i++) { session.seed( mention( 'quiet-${i.toString().padLeft(2, '0')}', quiet: true, at: i < 5 ? 0 : i, ), ); } final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const []), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); final feed = await container.read(activityProvider.future); expect(feed.all.map((item) => item.id), ['z-mention']); expect(container.read(inboxItemsProvider).map((item) => item.id), [ 'z-mention', ]); }); test('paging past quiet mentions refills only up to the limit', () async { NostrEvent mention(String id, int at, {bool quiet = false}) => NostrEvent( id: id, pubkey: 'agent_pk', createdAt: 1_700_000_000 + at, kind: 9, tags: [ ['p', 'me_pk'], ['h', 'channel-1'], if (quiet) ['audience', 'agents'], ], content: 'hi', sig: '', ); // The newest window holds one quiet mention and 49 real ones; a full // window of older real mentions sits behind it. final session = _RecordingSessionNotifier() ..seed(mention('quiet', 1000, quiet: true)); for (var i = 0; i < 49; i++) { session.seed(mention('new-$i', 900 + i)); } for (var i = 0; i < 50; i++) { session.seed(mention('old-$i', 100 + i)); } final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const []), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); final feed = await container.read(activityProvider.future); expect(feed.mentions, hasLength(50)); expect(feed.mentions.map((item) => item.id), contains('old-49')); expect(feed.mentions.map((item) => item.id), isNot(contains('old-48'))); }); test( 'an addressed DM mention counts once toward the mention window', () async { NostrEvent mention(String id, int at, {bool quiet = false}) => NostrEvent( id: id, pubkey: 'agent_pk', createdAt: 1_700_000_000 + at, kind: 9, tags: [ ['p', 'me_pk'], ['h', 'dm-1'], if (quiet) ['audience', 'agents'], ], content: 'hi', sig: '', ); // The DM filter returns the newest DM mentions a second time. final session = _RecordingSessionNotifier() ..seed(mention('quiet', 1000, quiet: true)) ..seed(mention('older', 1)); for (var i = 0; i < 49; i++) { session.seed(mention('new-$i', 900 + i)); } final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier([_dmChannel('dm-1')]), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); final feed = await container.read(activityProvider.future); expect(feed.mentions, hasLength(50)); expect(feed.mentions.map((item) => item.id), contains('older')); }, ); group('paging past quiet messages per source', () { NostrEvent message( String id, int at, { required String channel, bool mention = true, bool quiet = false, }) => NostrEvent( id: id, pubkey: 'agent_pk', createdAt: 1_700_000_000 + at, kind: 9, tags: [ if (mention) ['p', 'me_pk'], ['h', channel], if (quiet) ['audience', 'agents'], ], content: 'hi', sig: '', ); Future load(_RecordingSessionNotifier session) async { final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier([_dmChannel('dm-1')]), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); return container.read(activityProvider.future); } test('a DM window full of quiet messages is refilled', () async { final session = _RecordingSessionNotifier() ..seed(message('dm-older', 1, channel: 'dm-1', mention: false)); for (var i = 0; i < 30; i++) { session.seed( message('dm-quiet-$i', 100 + i, channel: 'dm-1', quiet: true), ); } final feed = await load(session); expect(feed.activity.map((item) => item.id), ['dm-older']); }); test('overlapping matches past a window stay on later pages', () async { // 50 quiet channel mentions fill the mention window. 30 older quiet DM // mentions match it too, through the DM filter, and fill its next page // with 20 more quiet channel mentions, so the real one is a page later. final session = _RecordingSessionNotifier() ..seed(message('real', 1, channel: 'channel-1')); for (var i = 0; i < 20; i++) { session.seed( message('older-quiet-$i', 20 + i, channel: 'channel-1', quiet: true), ); } for (var i = 0; i < 50; i++) { session.seed( message('quiet-$i', 500 + i, channel: 'channel-1', quiet: true), ); } for (var i = 0; i < 30; i++) { session.seed( message('dm-quiet-$i', 100 + i, channel: 'dm-1', quiet: true), ); } final feed = await load(session); expect(feed.mentions.map((item) => item.id), ['real']); }); test('a DM-only event never moves the mention cursor', () async { // The mention window: one quiet and 49 real channel mentions. An // older addressed DM comes back only through the DM filter, and a // real channel mention sits between it and the window. final session = _RecordingSessionNotifier() ..seed(message('quiet', 1000, channel: 'channel-1', quiet: true)) ..seed(message('gap', 500, channel: 'channel-1')) ..seed(message('dm-old', 100, channel: 'dm-1')); for (var i = 0; i < 49; i++) { session.seed(message('new-$i', 900 + i, channel: 'channel-1')); } final feed = await load(session); expect(feed.mentions.map((item) => item.id), contains('gap')); }); }); test( 'refreshes the inbox projection when addressed activity arrives', () async { final session = _RecordingSessionNotifier(); final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const []), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); expect(container.read(inboxItemsProvider), isEmpty); session.emit( const NostrEvent( id: 'live-mention', pubkey: 'other_pk', createdAt: 1_700_000_000, kind: 40002, tags: [ ['p', 'me_pk'], ['h', 'channel-1'], ], content: 'Hello from the live relay', sig: '', ), ); await Future.delayed(const Duration(milliseconds: 100)); expect(container.read(inboxItemsProvider).single.id, 'live-mention'); }, ); testWidgets( 'authenticated shell resurfaces hidden DMs without opening Inbox', (tester) async { const self = '1111111111111111111111111111111111111111111111111111111111111111'; const alice = '2222222222222222222222222222222222222222222222222222222222222222'; const bob = '3333333333333333333333333333333333333333333333333333333333333333'; final session = _RecordingSessionNotifier(); final reopened = >[]; SharedPreferences.setMockInitialValues({}); final prefs = await SharedPreferences.getInstance(); final container = ProviderContainer( overrides: [ savedPrefsProvider.overrideWithValue(prefs), authProvider.overrideWith(_ShellAuth.new), ageSignalProvider.overrideWith(_ShellAge.new), relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue(self), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier( const [], hiddenDmIds: const {'hidden-dm'}, ), ), channelMembersProvider('hidden-dm').overrideWith( (ref) async => [ ChannelMember( pubkey: self, role: 'member', joinedAt: DateTime(2026), ), ChannelMember( pubkey: alice, role: 'member', joinedAt: DateTime(2026), ), ChannelMember( pubkey: bob, role: 'member', joinedAt: DateTime(2026), ), ], ), dmResurfaceActionProvider.overrideWithValue((pubkeys) async { reopened.add(pubkeys); return 'hidden-dm'; }), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await tester.pumpWidget( UncontrolledProviderScope(container: container, child: const App()), ); for (var i = 0; i < 20; i++) { await tester.pump(); } expect(find.byType(ActivityPage), findsNothing); expect(session.hiddenDmSubscriptionBatches, [ ['hidden-dm'], ]); session.emit( const NostrEvent( id: 'hidden-dm-message', pubkey: alice, createdAt: 1_700_000_000, kind: EventKind.streamMessageV2, tags: [ ['p', self], ['h', 'hidden-dm'], ], content: 'Hello again', sig: '', ), ); for (var i = 0; i < 20 && reopened.isEmpty; i++) { await tester.pump(); } expect(reopened, [ [alice, bob], ]); await tester.pumpWidget(const SizedBox.shrink()); container.dispose(); }, ); test( 'queues a hidden DM message until channel discovery completes', () async { const self = '1111111111111111111111111111111111111111111111111111111111111111'; const alice = '2222222222222222222222222222222222222222222222222222222222222222'; final session = _RecordingSessionNotifier(); late _DeferredChannelsNotifier channels; final reopened = >[]; final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue(self), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => channels = _DeferredChannelsNotifier( hiddenDmIds: const {'hidden-dm'}, ), ), channelMembersProvider('hidden-dm').overrideWith( (ref) async => [ ChannelMember( pubkey: self, role: 'member', joinedAt: DateTime(2026), ), ChannelMember( pubkey: alice, role: 'member', joinedAt: DateTime(2026), ), ], ), dmResurfaceActionProvider.overrideWithValue((pubkeys) async { reopened.add(pubkeys); return 'hidden-dm'; }), ], ); addTearDown(container.dispose); container.read(channelsProvider); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); session.emit( const NostrEvent( id: 'hidden-dm-during-discovery', pubkey: alice, createdAt: 1_700_000_000, kind: EventKind.streamMessageV2, tags: [ ['p', self], ['h', 'hidden-dm'], ], content: 'Hello during startup', sig: '', ), ); expect(reopened, isEmpty); channels.complete(const []); await container.read(channelsProvider.future); await _waitFor(() => reopened.isNotEmpty); expect(reopened, [ [alice], ]); }, ); test('a concurrent follower survives a failed in-flight reopen', () async { const self = '1111111111111111111111111111111111111111111111111111111111111111'; const alice = '2222222222222222222222222222222222222222222222222222222222222222'; final session = _RecordingSessionNotifier(); final reopenAttempts = >[]; final firstReopenGate = Completer(); var reopenCalls = 0; final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue(self), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier( const [], hiddenDmIds: const {'hidden-dm'}, ), ), channelMembersProvider('hidden-dm').overrideWith( (ref) async => [ ChannelMember( pubkey: self, role: 'member', joinedAt: DateTime(2026), ), ChannelMember( pubkey: alice, role: 'member', joinedAt: DateTime(2026), ), ], ), dmResurfaceActionProvider.overrideWithValue((pubkeys) async { reopenCalls += 1; reopenAttempts.add(pubkeys); if (reopenCalls == 1) { // Hold the first attempt open so the follower coalesces into it, // then fail it — the follower must not be silently dropped. await firstReopenGate.future; throw StateError('transient reopen failure'); } return 'hidden-dm'; }), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); NostrEvent hiddenDmEvent(String id) => NostrEvent( id: id, pubkey: alice, createdAt: 1_700_000_000, kind: EventKind.streamMessageV2, tags: const [ ['p', self], ['h', 'hidden-dm'], ], content: 'Hello again', sig: '', ); session.emit(hiddenDmEvent('message-a')); await _waitFor(() => reopenCalls == 1); // Follower arrives while attempt A is in flight → coalesced for retry. session.emit(hiddenDmEvent('message-b')); await Future.delayed(const Duration(milliseconds: 10)); firstReopenGate.complete(); await _waitFor(() => reopenCalls >= 2); expect(reopenAttempts, [ [alice], [alice], ]); }); test( 'a follower on a rebuilt subscription reopens after the old attempt retires', () async { const self = '1111111111111111111111111111111111111111111111111111111111111111'; const alice = '2222222222222222222222222222222222222222222222222222222222222222'; final session = _RecordingSessionNotifier(); final reopenAttempts = []; final gateA = Completer(); final gateB = Completer(); var reopenCalls = 0; final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue(self), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier( const [], hiddenDmIds: const {'hidden-dm'}, ), ), channelMembersProvider('hidden-dm').overrideWith( (ref) async => [ ChannelMember( pubkey: self, role: 'member', joinedAt: DateTime(2026), ), ChannelMember( pubkey: alice, role: 'member', joinedAt: DateTime(2026), ), ], ), dmResurfaceActionProvider.overrideWithValue((pubkeys) async { final call = ++reopenCalls; reopenAttempts.add(pubkeys.single); // Suspend attempt A (call 1, old generation) and attempt B (call 2, // new generation) so both are in flight simultaneously: A must // retire while B's entry still occupies the pending map. B's first // attempt then fails so its retry loop drains the coalesced // follower C as call 3. if (call == 1) await gateA.future; if (call == 2) { await gateB.future; throw StateError('transient reopen failure'); } return 'hidden-dm'; }), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); NostrEvent hiddenDmEvent(String id) => NostrEvent( id: id, pubkey: alice, createdAt: 1_700_000_000, kind: EventKind.streamMessageV2, tags: const [ ['p', self], ['h', 'hidden-dm'], ], content: 'Hello again', sig: '', ); // Attempt A starts on generation N and suspends inside the reopen. session.emit(hiddenDmEvent('message-a')); await _waitFor(() => reopenCalls == 1); // Activity rebuilds (generation N+1). Follower B lands on the replacement // subscription: A's entry belongs to the old generation, so B does not // coalesce — it installs its own entry and starts a second attempt, which // suspends. A and B are now both in flight. container.invalidate(activityProvider); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); session.emit(hiddenDmEvent('message-b')); await _waitFor(() => reopenCalls == 2); // A retires while B is still pending. Its instance-checked cleanup must // leave B's entry in the map; unconditional removal would evict B's live // entry here. gateA.complete(); await Future.delayed(const Duration(milliseconds: 10)); // Follower C arrives on the current generation. Because B's entry is still // present, C coalesces into it (no new attempt). With unconditional // cleanup, A would have evicted B's entry and C would wrongly start a // third overlapping attempt. session.emit(hiddenDmEvent('message-c')); await Future.delayed(const Duration(milliseconds: 10)); expect(reopenCalls, 2); // Releasing B fails its first attempt; the coalesced follower C drives one // retry, which succeeds as the third reopen. gateB.complete(); await _waitFor(() => reopenCalls == 3); await Future.delayed(const Duration(milliseconds: 10)); expect(reopenAttempts, [alice, alice, alice]); }, ); test('a suspended membership read cannot mutate a rebuilt scope', () async { const self = '1111111111111111111111111111111111111111111111111111111111111111'; const alice = '2222222222222222222222222222222222222222222222222222222222222222'; final session = _RecordingSessionNotifier(); final membersRequested = Completer(); final membersGate = Completer>(); var reopenCount = 0; final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue(self), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier( const [], hiddenDmIds: const {'hidden-dm'}, ), ), channelMembersProvider('hidden-dm').overrideWith((ref) { if (!membersRequested.isCompleted) membersRequested.complete(); return membersGate.future; }), dmResurfaceActionProvider.overrideWithValue((pubkeys) async { reopenCount += 1; return 'hidden-dm'; }), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); session.emit( const NostrEvent( id: 'hidden-dm-stale-message', pubkey: alice, createdAt: 1_700_000_000, kind: EventKind.streamMessageV2, tags: [ ['p', self], ['h', 'hidden-dm'], ], content: 'Hello again', sig: '', ), ); await membersRequested.future; container.invalidate(activityProvider); await container.read(activityProvider.future); membersGate.complete([ ChannelMember(pubkey: self, role: 'member', joinedAt: DateTime(2026)), ChannelMember(pubkey: alice, role: 'member', joinedAt: DateTime(2026)), ]); await Future.delayed(const Duration(milliseconds: 20)); expect(reopenCount, 0); }); test( 'serializes live refreshes and catches up events queued mid-fetch', () async { final session = _RecordingSessionNotifier(); final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const []), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); session.mentionFetchGate = Completer(); session.emit(_mentionEvent('live-one', 1_700_000_001)); await _waitFor(() => session.activeMentionFetches == 1); session.emit(_mentionEvent('live-two', 1_700_000_002)); await Future.delayed(const Duration(milliseconds: 100)); expect(session.maxActiveMentionFetches, 1); session.mentionFetchGate!.complete(); await _waitFor(() => session.mentionFetchCount >= 3); await _waitFor(() => container.read(inboxItemsProvider).length == 2); expect(session.maxActiveMentionFetches, 1); expect( container.read(inboxItemsProvider).map((item) => item.id), containsAll(['live-one', 'live-two']), ); }, ); test('serializes manual and live inbox refreshes', () async { final session = _RecordingSessionNotifier(); final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const []), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); session.mentionFetchGate = Completer(); final manualRefresh = container.read(activityProvider.notifier).refresh(); await _waitFor(() => session.activeMentionFetches == 1); session.emit(_mentionEvent('live-during-manual', 1_700_000_003)); await Future.delayed(const Duration(milliseconds: 100)); expect(session.maxActiveMentionFetches, 1); session.mentionFetchGate!.complete(); await manualRefresh; await _waitFor(() => session.mentionFetchCount >= 3); await _waitFor( () => container.read(inboxItemsProvider).single.id == 'live-during-manual', ); expect(session.maxActiveMentionFetches, 1); }); test('recovers a live refresh through websocket history fallback', () async { final session = _RecordingSessionNotifier() ..seed(_mentionEvent('existing', 1_700_000_001)); final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue('me_pk'), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const []), ), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await Future.delayed(const Duration(milliseconds: 10)); expect(container.read(inboxItemsProvider).single.id, 'existing'); session.failNextMentionFetch = true; session.emit(_mentionEvent('newer', 1_700_000_002)); await _waitFor(() => session.mentionFetchCount >= 2); await Future.delayed(const Duration(milliseconds: 10)); expect( container.read(inboxItemsProvider).map((item) => item.id), containsAll(['existing', 'newer']), ); }); group('hidden-DM subscription batching', () { const self = '1111111111111111111111111111111111111111111111111111111111111111'; Set hiddenIds(int count) => { for (var i = 0; i < count; i++) 'dm-${i.toString().padLeft(4, '0')}', }; ProviderContainer containerFor( _RecordingSessionNotifier session, Set hidden, ) => ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue(self), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const [], hiddenDmIds: hidden), ), ], ); test( '128 hidden DMs register a single subscription within the cap', () async { final session = _RecordingSessionNotifier(); final container = containerFor(session, hiddenIds(128)); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await _waitFor(() => session.hiddenDmSubscriptionBatches.isNotEmpty); final batches = session.hiddenDmSubscriptionBatches; expect(batches, hasLength(1)); expect(batches.single, hasLength(128)); }, ); test( '129 hidden DMs split into two subscriptions, both within the cap', () async { final session = _RecordingSessionNotifier(); final container = containerFor(session, hiddenIds(129)); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await _waitFor(() => session.hiddenDmSubscriptionBatches.length == 2); final batches = session.hiddenDmSubscriptionBatches; expect(batches.map((batch) => batch.length), [128, 1]); final all = batches.expand((batch) => batch).toSet(); expect(all, hasLength(129)); for (final batch in batches) { expect(batch.length, lessThanOrEqualTo(128)); } }, ); test('activity on the final batch resurfaces its DM', () async { const alice = '2222222222222222222222222222222222222222222222222222222222222222'; final hidden = hiddenIds(129); final target = 'dm-0128'; final session = _RecordingSessionNotifier(); final reopened = >[]; final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue(self), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier(const [], hiddenDmIds: hidden), ), channelMembersProvider(target).overrideWith( (ref) async => [ ChannelMember( pubkey: self, role: 'member', joinedAt: DateTime(2026), ), ChannelMember( pubkey: alice, role: 'member', joinedAt: DateTime(2026), ), ], ), dmResurfaceActionProvider.overrideWithValue((pubkeys) async { reopened.add(pubkeys); return target; }), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await _waitFor(() => session.hiddenDmSubscriptionBatches.length == 2); // The target lives only in the second batch; delivering it exercises // that batch's live subscription. session.emit( NostrEvent( id: 'final-batch-message', pubkey: alice, createdAt: 1_700_000_000, kind: EventKind.streamMessageV2, tags: [ ['p', self], ['h', target], ], content: 'Hello again', sig: '', ), ); await _waitFor(() => reopened.isNotEmpty); expect(reopened.single, [alice]); }); test('a batch subscription failure does not abort later batches', () async { final session = _RecordingSessionNotifier() ..failNextHiddenSubscribe = true; final container = containerFor(session, hiddenIds(129)); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); // The first batch throws; the loop continues and registers the second. await _waitFor(() => session.hiddenDmSubscriptionBatches.isNotEmpty); final batches = session.hiddenDmSubscriptionBatches; expect(batches, hasLength(1)); expect(batches.single, hasLength(1)); }); test('teardown during batch-1 setup self-disposes batch 1 and never starts ' 'batch 2', () async { final session = _RecordingSessionNotifier() ..hiddenSubscribeGatesByCall[0] = Completer(); final container = containerFor(session, hiddenIds(129)); await container.read(channelsProvider.future); await container.read(activityProvider.future); // Batch 1's REQ is parked in flight; batch 2 has not been requested. await _waitFor(() => session.hiddenSubscribeCallCount == 1); // Supersede this generation mid-setup, then let batch 1's REQ resolve. container.dispose(); session.hiddenSubscribeGatesByCall[0]!.complete(); // Batch 1 resolves stale and self-disposes; batch 2 is never requested. await _waitFor(() => session.hiddenUnsubscribeCount == 1); expect(session.hiddenUnsubscribeCount, 1); expect(session.hiddenSubscribeCallCount, 1); expect(session.hiddenDmSubscriptionBatches, isEmpty); }); test( 'teardown after batch 1 settles while batch 2 is pending tears down both', () async { final session = _RecordingSessionNotifier() ..hiddenSubscribeGatesByCall[1] = Completer(); final container = containerFor(session, hiddenIds(129)); await container.read(channelsProvider.future); await container.read(activityProvider.future); // Batch 1 has settled and registered; batch 2's REQ is parked in flight. await _waitFor(() => session.hiddenSubscribeCallCount == 2); expect(session.hiddenDmSubscriptionBatches, hasLength(1)); // Supersede this generation with batch 2 pending, then let it resolve. container.dispose(); session.hiddenSubscribeGatesByCall[1]!.complete(); // Batch 2 resolves stale and disposes itself plus the settled batch 1. await _waitFor(() => session.hiddenUnsubscribeCount == 2); expect(session.hiddenUnsubscribeCount, 2); expect(session.hiddenDmSubscriptionBatches, isEmpty); }, ); test( '129 visible DMs batch under the cap and hidden activity still resurfaces', () async { // An over-cap visible-DM set previously rejected as one REQ, aborting // the whole live setup — so the hidden batches never registered and // resurfacing silently died. Batching the visible set keeps every REQ // within the cap and leaves the hidden subscription intact. const alice = '2222222222222222222222222222222222222222222222222222222222222222'; final visibleChannels = [ for (var i = 0; i < 129; i++) _dmChannel('visible-${i.toString().padLeft(4, '0')}'), ]; const hiddenTarget = 'hidden-dm'; final session = _RecordingSessionNotifier(); final reopened = >[]; final container = ProviderContainer( overrides: [ relayConfigProvider.overrideWith(_FixedRelayConfigNotifier.new), myPubkeyProvider.overrideWithValue(self), relaySessionProvider.overrideWith(() => session), channelsProvider.overrideWith( () => _FixedChannelsNotifier( visibleChannels, hiddenDmIds: const {hiddenTarget}, ), ), channelMembersProvider(hiddenTarget).overrideWith( (ref) async => [ ChannelMember( pubkey: self, role: 'member', joinedAt: DateTime(2026), ), ChannelMember( pubkey: alice, role: 'member', joinedAt: DateTime(2026), ), ], ), dmResurfaceActionProvider.overrideWithValue((pubkeys) async { reopened.add(pubkeys); return hiddenTarget; }), ], ); addTearDown(container.dispose); await container.read(channelsProvider.future); await container.read(activityProvider.future); await _waitFor(() => session.hiddenDmSubscriptionBatches.isNotEmpty); // Every visible-DM REQ stays within the cap, split 128 + 1. final visibleBatches = session.visibleDmSubscriptionBatches; expect(visibleBatches.map((batch) => batch.length), [128, 1]); expect(visibleBatches.expand((batch) => batch).toSet(), hasLength(129)); // The hidden subscription registered despite the over-cap visible set. expect(session.hiddenDmSubscriptionBatches, hasLength(1)); // Hidden activity still resurfaces its DM. session.emit( NostrEvent( id: 'hidden-message', pubkey: alice, createdAt: 1_700_000_000, kind: EventKind.streamMessageV2, tags: [ ['p', self], ['h', hiddenTarget], ], content: 'Hello again', sig: '', ), ); await _waitFor(() => reopened.isNotEmpty); expect(reopened.single, [alice]); }, ); }); } class _FixedChannelsNotifier extends ChannelsNotifier { final List channels; final Set _hiddenDmIds; _FixedChannelsNotifier(this.channels, {Set hiddenDmIds = const {}}) : _hiddenDmIds = hiddenDmIds; @override bool get hasLoaded => true; @override Set get hiddenDmIds => _hiddenDmIds; @override Future> build() async => channels; } class _DeferredChannelsNotifier extends ChannelsNotifier { _DeferredChannelsNotifier({required Set hiddenDmIds}) : _hiddenDmIds = hiddenDmIds; final Set _hiddenDmIds; final _gate = Completer>(); bool _hasLoaded = false; @override bool get hasLoaded => _hasLoaded; @override Set get hiddenDmIds => _hiddenDmIds; @override Future> build() => _gate.future; void complete(List channels) { _hasLoaded = true; _gate.complete(channels); } } class _ShellAuth extends AuthNotifier { @override Future build() async => const AuthState(status: AuthStatus.authenticated); } class _ShellAge extends AgeSignalNotifier { @override AgeSignalState build() => AgeSignalState.allowed; @override Future request() async {} }