@@ -5027,6 +5027,80 @@ void main() {
50275027 expect(findRichText('Reachable unread'), findsOneWidget);
50285028 });
50295029
5030+ testWidgets(
5031+ 'settles the unread jump each time history silently reaches its oldest page',
5032+ (tester) async {
5033+ final readMessage = _textMsg(
5034+ id: 'read',
5035+ pubkey: 'alice',
5036+ content: 'Already read',
5037+ createdAt: 1010,
5038+ );
5039+ final messagesNotifier = _SilentOldestMessagesNotifier([readMessage]);
5040+ final channelsNotifier = _FakeChannelsNotifier(
5041+ [_testChannel],
5042+ observedUnread: {
5043+ _channelId: [
5044+ for (final (id, createdAt) in [
5045+ ('never-loaded', 1021),
5046+ ('reachable-unread', 1022),
5047+ ])
5048+ makeObservedUnreadEvent(
5049+ id: id,
5050+ createdAt: createdAt,
5051+ rootId: null,
5052+ highPriority: false,
5053+ channelType: 'stream',
5054+ isThreadedReply: false,
5055+ ),
5056+ ],
5057+ },
5058+ );
5059+ final readState = _SynchronousReadStateNotifier(
5060+ const ReadStateState(
5061+ isReady: true,
5062+ pubkey: 'self',
5063+ contexts: {_channelId: 1020},
5064+ version: 0,
5065+ ),
5066+ );
5067+ final unreadButton = find.byKey(
5068+ const ValueKey('channel-jump-to-oldest-unread'),
5069+ );
5070+
5071+ await tester.pumpWidget(
5072+ _buildTestable(
5073+ messages: const [],
5074+ messagesNotifier: messagesNotifier,
5075+ channelsNotifier: channelsNotifier,
5076+ readStateNotifier: readState,
5077+ users: const {
5078+ 'alice': UserProfile(pubkey: 'alice', displayName: 'Alice'),
5079+ },
5080+ ),
5081+ );
5082+ await _settleThenFinishFetch(tester);
5083+ // The first fetch reached the oldest page with no unread row loaded.
5084+ expect(messagesNotifier.fetchOlderCalls, 1);
5085+ expect(unreadButton, findsNothing);
5086+
5087+ // A reconnect forgets the oldest page and reloads the window, now
5088+ // with an unread row; the next fetch reaches the oldest page again.
5089+ messagesNotifier.reconnect([
5090+ readMessage,
5091+ _textMsg(
5092+ id: 'reachable-unread',
5093+ pubkey: 'alice',
5094+ content: 'Reachable unread',
5095+ createdAt: 1022,
5096+ ),
5097+ ]);
5098+ await _settleThenFinishFetch(tester);
5099+ expect(messagesNotifier.fetchOlderCalls, 2);
5100+ expect(unreadButton, findsOneWidget);
5101+ },
5102+ );
5103+
50305104 testWidgets('pages past a loaded forced unread for an older target', (
50315105 tester,
50325106 ) async {
@@ -16822,6 +16896,39 @@ class _FakeMessagesNotifier extends ChannelMessagesNotifier {
1682216896 }
1682316897}
1682416898
16899+ /// Lets the rows settle while an older fetch is in flight, then lets it finish,
16900+ /// so nothing but the fetch's own completion can rebuild the list afterwards.
16901+ Future<void> _settleThenFinishFetch(WidgetTester tester) async {
16902+ await tester.pumpAndSettle();
16903+ await tester.pump(_SilentOldestMessagesNotifier.fetchDelay);
16904+ await tester.pumpAndSettle();
16905+ }
16906+
16907+ /// Each older fetch takes a while, then finds the oldest page without
16908+ /// publishing anything, like a window whose cursor has run out.
16909+ class _SilentOldestMessagesNotifier extends _FakeMessagesNotifier {
16910+ _SilentOldestMessagesNotifier(super.messages);
16911+
16912+ static const fetchDelay = Duration(seconds: 5);
16913+ bool _reachedOldest = false;
16914+
16915+ @override
16916+ bool get reachedOldest => _reachedOldest;
16917+
16918+ @override
16919+ Future<bool> fetchOlder() async {
16920+ fetchOlderCalls += 1;
16921+ await Future<void>.delayed(fetchDelay);
16922+ _reachedOldest = true;
16923+ return false;
16924+ }
16925+
16926+ void reconnect(List<NostrEvent> messages) {
16927+ _reachedOldest = false;
16928+ setMessages(messages);
16929+ }
16930+ }
16931+
1682516932/// Counts loads; each settles after mount, like the real network load.
1682616933class _RetryCountingMessagesNotifier extends ChannelMessagesNotifier {
1682716934 _RetryCountingMessagesNotifier(this.nextResult) : super(_channelId);
0 commit comments