|
| 1 | +part of 'channel_detail_page_test.dart'; |
| 2 | + |
| 3 | +void threadReplyRefreshTests() { |
| 4 | + const args = ThreadRepliesArgs(channelId: _channelId, rootId: 'refresh-root'); |
| 5 | + final root = _textMsg( |
| 6 | + id: args.rootId, |
| 7 | + pubkey: 'alice', |
| 8 | + content: 'Refresh root', |
| 9 | + ); |
| 10 | + final reply = _textMsg( |
| 11 | + id: 'cached-reply', |
| 12 | + pubkey: 'bob', |
| 13 | + content: 'Cached reply', |
| 14 | + createdAt: 1001, |
| 15 | + extraTags: [ |
| 16 | + ['e', args.rootId, '', 'reply'], |
| 17 | + ], |
| 18 | + ); |
| 19 | + |
| 20 | + Future<(NavigatorState, ProviderContainer)> mount( |
| 21 | + WidgetTester tester, |
| 22 | + Future<List<NostrEvent>> Function() load, { |
| 23 | + bool retry = false, |
| 24 | + TextScaler textScaler = TextScaler.noScaling, |
| 25 | + }) async { |
| 26 | + await tester.pumpWidget( |
| 27 | + _buildTestable( |
| 28 | + messages: [root], |
| 29 | + textScaler: textScaler, |
| 30 | + threadReplyLoaders: {args.rootId: load}, |
| 31 | + providerRetry: (_, _) => |
| 32 | + retry ? const Duration(milliseconds: 200) : null, |
| 33 | + ), |
| 34 | + ); |
| 35 | + await tester.pumpAndSettle(); |
| 36 | + final context = tester.element(find.byType(ChannelDetailPage)); |
| 37 | + return (Navigator.of(context), ProviderScope.containerOf(context)); |
| 38 | + } |
| 39 | + |
| 40 | + void open(NavigatorState navigator) { |
| 41 | + final head = formatTimeline([root]).single; |
| 42 | + navigator.push( |
| 43 | + MaterialPageRoute<void>( |
| 44 | + builder: (_) => ThreadDetailPage( |
| 45 | + threadHead: head, |
| 46 | + allMessages: [head], |
| 47 | + channelId: _channelId, |
| 48 | + currentPubkey: 'self', |
| 49 | + isMember: true, |
| 50 | + isArchived: false, |
| 51 | + ), |
| 52 | + ), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + testWidgets( |
| 57 | + 'thread refresh keeps cached replies through retry and recovery', |
| 58 | + (tester) async { |
| 59 | + var calls = 0; |
| 60 | + final refresh = Completer<List<NostrEvent>>(); |
| 61 | + final retry = Completer<List<NostrEvent>>(); |
| 62 | + final (navigator, container) = await mount(tester, () { |
| 63 | + calls++; |
| 64 | + return switch (calls) { |
| 65 | + 1 => Future.value([reply]), |
| 66 | + 2 => refresh.future, |
| 67 | + _ => retry.future, |
| 68 | + }; |
| 69 | + }, retry: true); |
| 70 | + open(navigator); |
| 71 | + await tester.pumpAndSettle(); |
| 72 | + expect(find.text('1 reply'), findsOneWidget); |
| 73 | + container.invalidate(threadRepliesProvider(args)); |
| 74 | + await tester.pump(); |
| 75 | + expect(find.text('1 reply'), findsOneWidget); |
| 76 | + refresh.completeError(Exception('Transient refresh failure')); |
| 77 | + await tester.pump(); |
| 78 | + for (var frame = 0; frame < 20; frame++) { |
| 79 | + await tester.pump(const Duration(milliseconds: 16)); |
| 80 | + expect(find.text('0 replies'), findsNothing, reason: 'frame $frame'); |
| 81 | + expect( |
| 82 | + find.byKey(const ValueKey('thread-message-group-cached-reply')), |
| 83 | + findsOneWidget, |
| 84 | + ); |
| 85 | + expect(container.read(threadRepliesProvider(args)).value, [reply]); |
| 86 | + } |
| 87 | + expect(calls, 3); |
| 88 | + retry.complete([ |
| 89 | + reply, |
| 90 | + _textMsg( |
| 91 | + id: 'fresh-reply', |
| 92 | + pubkey: 'bob', |
| 93 | + content: 'Fresh reply', |
| 94 | + createdAt: 1002, |
| 95 | + extraTags: [ |
| 96 | + ['e', args.rootId, '', 'reply'], |
| 97 | + ], |
| 98 | + ), |
| 99 | + ]); |
| 100 | + await tester.pumpAndSettle(); |
| 101 | + expect(find.text('2 replies'), findsOneWidget); |
| 102 | + expect( |
| 103 | + find.byKey(const ValueKey('thread-message-group-fresh-reply')), |
| 104 | + findsOneWidget, |
| 105 | + ); |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + testWidgets('thread refresh error keeps cached replies visible', ( |
| 110 | + tester, |
| 111 | + ) async { |
| 112 | + final semantics = tester.ensureSemantics(); |
| 113 | + var calls = 0; |
| 114 | + final refresh = Completer<List<NostrEvent>>(); |
| 115 | + tester.view.physicalSize = const Size(320, 844); |
| 116 | + tester.view.devicePixelRatio = 1; |
| 117 | + addTearDown(tester.view.resetPhysicalSize); |
| 118 | + addTearDown(tester.view.resetDevicePixelRatio); |
| 119 | + final (navigator, container) = await mount( |
| 120 | + tester, |
| 121 | + () => ++calls == 1 ? Future.value([reply]) : refresh.future, |
| 122 | + textScaler: const TextScaler.linear(2), |
| 123 | + ); |
| 124 | + open(navigator); |
| 125 | + await tester.pumpAndSettle(); |
| 126 | + container.invalidate(threadRepliesProvider(args)); |
| 127 | + await tester.pump(); |
| 128 | + refresh.completeError(Exception('Refresh failed')); |
| 129 | + await tester.pumpAndSettle(); |
| 130 | + expect( |
| 131 | + find.byKey(const ValueKey('thread-message-group-cached-reply')), |
| 132 | + findsOneWidget, |
| 133 | + ); |
| 134 | + expect(find.text('0 replies'), findsNothing); |
| 135 | + expect(find.text('1 reply · Couldn’t refresh'), findsOneWidget); |
| 136 | + expect( |
| 137 | + tester |
| 138 | + .getSemantics(find.text('1 reply · Couldn’t refresh')) |
| 139 | + .getSemanticsData() |
| 140 | + .flagsCollection |
| 141 | + .isLiveRegion, |
| 142 | + isTrue, |
| 143 | + ); |
| 144 | + expect(tester.takeException(), isNull); |
| 145 | + expect(container.read(threadRepliesProvider(args)).hasError, isTrue); |
| 146 | + semantics.dispose(); |
| 147 | + }); |
| 148 | + |
| 149 | + testWidgets('thread reopen shows loading until the fresh query completes', ( |
| 150 | + tester, |
| 151 | + ) async { |
| 152 | + var calls = 0; |
| 153 | + final reopened = Completer<List<NostrEvent>>(); |
| 154 | + final (navigator, _) = await mount( |
| 155 | + tester, |
| 156 | + () => ++calls == 1 ? Future.value([reply]) : reopened.future, |
| 157 | + ); |
| 158 | + open(navigator); |
| 159 | + await tester.pumpAndSettle(); |
| 160 | + navigator.pop(); |
| 161 | + await tester.pumpAndSettle(); |
| 162 | + open(navigator); |
| 163 | + await tester.pumpAndSettle(); |
| 164 | + expect(calls, 2); |
| 165 | + expect(find.text('0 replies'), findsNothing); |
| 166 | + expect(find.text('Loading replies…'), findsOneWidget); |
| 167 | + reopened.complete([reply]); |
| 168 | + await tester.pumpAndSettle(); |
| 169 | + expect(find.text('1 reply'), findsOneWidget); |
| 170 | + expect(find.text('Loading replies…'), findsNothing); |
| 171 | + }); |
| 172 | + |
| 173 | + testWidgets('thread initial error is distinct from an empty thread', ( |
| 174 | + tester, |
| 175 | + ) async { |
| 176 | + final query = Completer<List<NostrEvent>>(); |
| 177 | + final (navigator, _) = await mount(tester, () => query.future); |
| 178 | + open(navigator); |
| 179 | + await tester.pumpAndSettle(); |
| 180 | + query.completeError(Exception('Initial load failed')); |
| 181 | + await tester.pumpAndSettle(); |
| 182 | + expect(find.text('0 replies'), findsNothing); |
| 183 | + expect(find.text('Couldn’t load replies'), findsOneWidget); |
| 184 | + }); |
| 185 | + testWidgets('thread successful empty query displays zero replies', ( |
| 186 | + tester, |
| 187 | + ) async { |
| 188 | + final (navigator, _) = await mount(tester, () async => []); |
| 189 | + open(navigator); |
| 190 | + await tester.pumpAndSettle(); |
| 191 | + expect(find.text('0 replies'), findsOneWidget); |
| 192 | + expect(find.text('Loading replies…'), findsNothing); |
| 193 | + expect(find.text('Couldn’t load replies'), findsNothing); |
| 194 | + }); |
| 195 | +} |
0 commit comments