Something went wrong. Try again.
Main coves client
Something went wrong. Try again.
9.3 kB · 264 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265import 'package:coves_flutter/constants/app_theme.dart';import 'package:coves_flutter/models/comment.dart';import 'package:coves_flutter/models/post.dart';import 'package:coves_flutter/providers/auth_provider.dart';import 'package:coves_flutter/providers/block_provider.dart';import 'package:coves_flutter/providers/vote_provider.dart';import 'package:coves_flutter/screens/home/focused_thread_screen.dart';import 'package:coves_flutter/services/comments_provider_cache.dart';import 'package:coves_flutter/widgets/loading_error_states.dart';import 'package:flutter/material.dart';import 'package:flutter_test/flutter_test.dart';import 'package:mockito/mockito.dart';import 'package:provider/provider.dart';
import '../test_helpers/error_copy_rows.dart';import '../test_helpers/test_mocks.dart';
// Reply-load failures on the focused thread screen: a malformed comment URI// (no rkey) makes CommentsProvider.loadMoreReplies throw// MalformedCommentUriException. The screen must turn that into its normal// failure UI instead of letting it escape as an uncaught async error:// - a "Load more replies" tap shows the failure snackbar;// - the entry hydration of the anchor subtree shows the retryable error// state rather than claiming the anchor has no replies.// Both surfaces show the shared load-replies copy for an API failure.void main() { const postUri = 'at://did:plc:author/social.coves.community.post/post'; const postCid = 'post-cid'; const malformedUri = 'at://did:plc:author/social.coves.community.comment/'; const replyLoadFailureMessage = 'Could not load replies. Please try again.';
late MockAuthProvider auth; late MockVoteProvider votes; late MockCovesApiService api; late BlockProvider blocks; late CommentsProviderCache commentsCache; // parentRkey of every getComments call, in order (null = whole tree). late List<String?> requests;
ThreadViewComment thread( String rkey, { String? uri, String? content, bool hasMore = false, List<ThreadViewComment>? replies, }) { return ThreadViewComment( comment: CommentView( uri: uri ?? 'at://did:plc:author/social.coves.community.comment/$rkey', cid: 'cid-$rkey', record: CommentRecord(content: content ?? 'Comment $rkey'), createdAt: DateTime(2025), indexedAt: DateTime(2025), author: AuthorView(did: 'did:plc:author', handle: 'author.test'), post: CommentRef(uri: postUri, cid: postCid), stats: CommentStats(replyCount: hasMore ? 3 : 0), ), replies: replies, hasMore: hasMore, ); }
CommentsResponse response(List<ThreadViewComment> items) => CommentsResponse(post: null, comments: items);
/// Serves every getComments call from [responder], recording the /// parentRkey of each request. void stubGetComments( CommentsResponse Function(String? parentRkey) responder, ) { when( api.getComments( postUri: anyNamed('postUri'), sort: anyNamed('sort'), timeframe: anyNamed('timeframe'), depth: anyNamed('depth'), limit: anyNamed('limit'), cursor: anyNamed('cursor'), parentRkey: anyNamed('parentRkey'), ), ).thenAnswer((invocation) async { final parent = invocation.namedArguments[#parentRkey] as String?; requests.add(parent); return responder(parent); }); }
setUp(() { auth = MockAuthProvider(); votes = MockVoteProvider(); api = MockCovesApiService(); when(auth.isAuthenticated).thenReturn(false); when(auth.did).thenReturn(null); when(votes.isLiked(any)).thenReturn(false); when(votes.getVoteState(any)).thenReturn(null); when(votes.isPending(any)).thenReturn(false); when(votes.getAdjustedScore(any, any)) .thenAnswer((invocation) => invocation.positionalArguments[1] as int); blocks = BlockProvider(apiService: api, authProvider: auth); // The app-wide cache the screen pins the post's provider in. commentsCache = CommentsProviderCache( authProvider: auth, voteProvider: votes, commentService: MockCommentService(), apiService: api, ); requests = []; });
tearDown(() { commentsCache.dispose(); blocks.dispose(); });
Widget app(ThreadViewComment anchor) { // As in the app, the post screen underneath acquires the post's // provider from the cache and hands it to the focused thread. final commentsProvider = commentsCache.acquireProvider( postUri: postUri, postCid: postCid, ); return MultiProvider( providers: [ ChangeNotifierProvider<AuthProvider>.value(value: auth), ChangeNotifierProvider<VoteProvider>.value(value: votes), ChangeNotifierProvider<BlockProvider>.value(value: blocks), Provider<CommentsProviderCache>.value(value: commentsCache), ], child: MaterialApp( theme: AppTheme.dark, home: FocusedThreadScreen( thread: anchor, ancestors: const [], commentsProvider: commentsProvider, ), ), ); }
testWidgets('load more replies on a nested reply with a malformed URI shows ' 'the failure snackbar without an uncaught error', (tester) async { // Entry hydration of the valid anchor succeeds and delivers a nested // reply whose URI has no rkey but advertises more replies. stubGetComments((parentRkey) { expect(parentRkey, 'anchor', reason: 'only the anchor is hydrated'); return response([ thread( 'anchor', content: 'Anchor comment', replies: [ thread( 'malformed', uri: malformedUri, content: 'Reply with a malformed URI', hasMore: true, ), ], ), ]); });
await tester.pumpWidget(app(thread('anchor', content: 'Anchor comment'))); await tester.pumpAndSettle(); expect(requests, ['anchor']); expect(find.text('Reply with a malformed URI'), findsOneWidget);
final loadMore = find.text('Load more replies'); expect(loadMore, findsOneWidget); await tester.tap(loadMore); await tester.pump(); await tester.pump(const Duration(milliseconds: 750));
expect(tester.takeException(), isNull); expect(find.text(replyLoadFailureMessage), findsOneWidget); expect(requests, [ 'anchor', ], reason: 'a URI without an rkey must never reach the API'); expect(requests, isNot(contains(''))); });
testWidgets('entry hydration of an anchor with a malformed URI shows the ' 'retryable error state without an uncaught error', (tester) async { stubGetComments((parentRkey) => response([]));
await tester.pumpWidget( app(thread('anchor', uri: malformedUri, content: 'Malformed anchor')), ); await tester.pumpAndSettle();
expect(tester.takeException(), isNull); expect(find.text('Malformed anchor'), findsOneWidget); expect(find.byType(InlineError), findsOneWidget); expect(find.text('Retry'), findsOneWidget); expect(find.text('No replies yet'), findsNothing); expect( requests, isEmpty, reason: 'a URI without an rkey must never reach the API', ); });
testWidgets('failed reply loads show the shared load-replies copy in the ' 'snackbar and the hydration error state', (tester) async { for (final row in errorCopyRows(replyLoadFailureMessage)) { // Entry hydration fails: the retryable error state shows the copy. stubGetComments((parentRkey) => throw row.error); await tester.pumpWidget(app(thread('anchor', content: 'Anchor comment'))); await tester.pumpAndSettle();
expect( find.descendant( of: find.byType(InlineError), matching: find.text(row.expected), ), findsOneWidget, reason: 'hydration: ${row.name}', ); if (row.rawText != null) { expect( find.textContaining(row.rawText!), findsNothing, reason: 'hydration: ${row.name}', ); } await tester.pumpWidget(const SizedBox.shrink());
// Hydration succeeds; loading a nested reply's replies fails. stubGetComments((parentRkey) { if (parentRkey == 'nested') { throw row.error; } return response([ thread( 'anchor', content: 'Anchor comment', replies: [thread('nested', content: 'Nested reply', hasMore: true)], ), ]); }); await tester.pumpWidget(app(thread('anchor', content: 'Anchor comment'))); await tester.pumpAndSettle(); await tester.tap(find.text('Load more replies')); await tester.pump(); await tester.pump(const Duration(milliseconds: 750));
expect( find.descendant( of: find.byType(SnackBar), matching: find.text(row.expected), ), findsOneWidget, reason: 'load more: ${row.name}', ); if (row.rawText != null) { expect( find.textContaining(row.rawText!), findsNothing, reason: 'load more: ${row.name}', ); } await tester.pumpWidget(const SizedBox.shrink()); } });}