@@ -5,9 +5,11 @@ import 'package:checks/checks.dart';
5
5
import 'package:fake_async/fake_async.dart' ;
6
6
import 'package:flutter/cupertino.dart' ;
7
7
import 'package:test/scaffolding.dart' ;
8
+ import 'package:zulip/api/model/initial_snapshot.dart' ;
8
9
import 'package:zulip/api/model/model.dart' ;
9
10
import 'package:zulip/model/autocomplete.dart' ;
10
11
import 'package:zulip/model/narrow.dart' ;
12
+ import 'package:zulip/model/store.dart' ;
11
13
import 'package:zulip/widgets/compose_box.dart' ;
12
14
13
15
import '../example_data.dart' as eg;
@@ -349,4 +351,85 @@ void main() {
349
351
doCheck ('Four F' , eg.user (fullName: 'Full Name Four Words' ), false );
350
352
});
351
353
});
354
+
355
+ group ('MentionAutocompleteView users results' , () {
356
+ late PerAccountStore store;
357
+ late MentionAutocompleteView view;
358
+
359
+ void prepare ({
360
+ required List <User > users,
361
+ required List <RecentDmConversation > dmConversations,
362
+ required Narrow narrow,
363
+ }) {
364
+ store = eg.store (
365
+ initialSnapshot: eg.initialSnapshot (
366
+ recentPrivateConversations: dmConversations
367
+ ));
368
+ for (final user in users) {
369
+ store.addUser (user);
370
+ }
371
+ view = MentionAutocompleteView .init (store: store, narrow: narrow);
372
+ }
373
+
374
+ test ('compareByDms gives priority to user with DM exchanged more recently' , () {
375
+ prepare (
376
+ users: [],
377
+ dmConversations: [
378
+ RecentDmConversation (userIds: [1 ], maxMessageId: 200 ),
379
+ RecentDmConversation (userIds: [1 , 2 ], maxMessageId: 100 ),
380
+ ],
381
+ narrow: const AllMessagesNarrow (),
382
+ );
383
+
384
+ final userA = eg.user (userId: 1 );
385
+ final userB = eg.user (userId: 2 );
386
+ final compareAB = view.compareByDms (userA, userB);
387
+ check (compareAB).isNegative ();
388
+ });
389
+
390
+ test ('autocomplete suggests relevant users in the following order: '
391
+ '1. Users most recent in the DM conversations' , () async {
392
+ final users = [
393
+ eg.user (userId: 1 ),
394
+ eg.user (userId: 2 ),
395
+ eg.user (userId: 3 ),
396
+ eg.user (userId: 4 ),
397
+ eg.user (userId: 5 ),
398
+ ];
399
+
400
+ final dmConversations = [
401
+ RecentDmConversation (userIds: [4 ], maxMessageId: 300 ),
402
+ RecentDmConversation (userIds: [1 ], maxMessageId: 200 ),
403
+ RecentDmConversation (userIds: [1 , 2 ], maxMessageId: 100 ),
404
+ ];
405
+
406
+ Future <void > checkResultsIn (Narrow narrow, {required List <int > expected}) async {
407
+ prepare (users: users, dmConversations: dmConversations, narrow: narrow);
408
+
409
+ bool done = false ;
410
+ view.addListener (() { done = true ; });
411
+ view.query = MentionAutocompleteQuery ('' );
412
+ await Future (() {});
413
+ check (done).isTrue ();
414
+ final results = view.results
415
+ .map ((e) => (e as UserMentionAutocompleteResult ).userId)
416
+ .toList ();
417
+ check (results).deepEquals (expected);
418
+ }
419
+
420
+ const streamNarrow = StreamNarrow (1 );
421
+ await checkResultsIn (streamNarrow, expected: [4 , 1 , 2 , 3 , 5 ]);
422
+
423
+ const topicNarrow = TopicNarrow (1 , 'topic' );
424
+ await checkResultsIn (topicNarrow, expected: [4 , 1 , 2 , 3 , 5 ]);
425
+
426
+ final dmNarrow = DmNarrow (allRecipientIds: [eg.selfUser.userId], selfUserId: eg.selfUser.userId);
427
+ await checkResultsIn (dmNarrow, expected: [4 , 1 , 2 , 3 , 5 ]);
428
+
429
+ const allMessagesNarrow = AllMessagesNarrow ();
430
+ // Results are in the original order as we do not sort them for
431
+ // [AllMessagesNarrow] because we can not access autocomplete for now.
432
+ await checkResultsIn (allMessagesNarrow, expected: [1 , 2 , 3 , 4 , 5 ]);
433
+ });
434
+ });
352
435
}
0 commit comments