@@ -359,10 +359,12 @@ void main() {
359359 Future <void > prepare ({
360360 List <User > users = const [],
361361 List <RecentDmConversation > dmConversations = const [],
362+ List <Message > messages = const [],
362363 }) async {
363364 store = eg.store (initialSnapshot: eg.initialSnapshot (
364365 recentPrivateConversations: dmConversations));
365366 await store.addUsers (users);
367+ await store.addMessages (messages);
366368 }
367369
368370 group ('MentionAutocompleteView.compareNullable' , () {
@@ -379,6 +381,62 @@ void main() {
379381
380382 test ('both of [a] and [b] are null' , () async {
381383 check (MentionAutocompleteView .compareNullable (null , null )).equals (0 );
384+ check (MentionAutocompleteView .compareNullable (null , null )).equals (0 );
385+ });
386+ });
387+
388+ group ('MentionAutocompleteView.compareByRecency' , () {
389+ final userA = eg.otherUser;
390+ final userB = eg.thirdUser;
391+ final stream = eg.stream ();
392+ const topic1 = 'topic1' ;
393+ const topic2 = 'topic2' ;
394+
395+ Message message (User sender, String topic) {
396+ return eg.streamMessage (
397+ sender: sender,
398+ stream: stream,
399+ topic: topic,
400+ );
401+ }
402+
403+ int compareAB ({required String ? topic}) {
404+ return MentionAutocompleteView .compareByRecency (userA, userB,
405+ streamId: stream.streamId,
406+ topic: topic,
407+ store: store,
408+ );
409+ }
410+
411+ test ('prioritizes the user with more recent activity in the topic' , () async {
412+ await prepare (messages: [
413+ message (userA, topic1),
414+ message (userB, topic1),
415+ ]);
416+ check (compareAB (topic: topic1)).isGreaterThan (0 );
417+ });
418+
419+ test ('prioritizes the user with more recent activity in the stream '
420+ 'if there is no activity in the topic from both users' , () async {
421+ await prepare (messages: [
422+ message (userA, topic1),
423+ message (userB, topic1),
424+ ]);
425+ check (compareAB (topic: topic2)).isGreaterThan (0 );
426+ });
427+
428+ test ('prioritizes the user with more recent activity in the stream '
429+ 'if there is no topic provided' , () async {
430+ await prepare (messages: [
431+ message (userA, topic1),
432+ message (userB, topic2),
433+ ]);
434+ check (compareAB (topic: null )).isGreaterThan (0 );
435+ });
436+
437+ test ('prioritizes none of the users if there is no activity in the stream from both users' , () async {
438+ await prepare (messages: []);
439+ check (compareAB (topic: null )).equals (0 );
382440 });
383441 });
384442
@@ -436,8 +494,11 @@ void main() {
436494 });
437495
438496 group ('autocomplete suggests relevant users in the intended order' , () {
439- // The order should be:
440- // 1. Users most recent in the DM conversations
497+ // 1. Users most recent in the current topic/stream.
498+ // 2. Users most recent in the DM conversations.
499+
500+ final stream = eg.stream ();
501+ const topic = 'topic' ;
441502
442503 Future <void > checkResultsIn (Narrow narrow, {required List <int > expected}) async {
443504 final users = [
@@ -454,7 +515,15 @@ void main() {
454515 RecentDmConversation (userIds: [0 , 1 ], maxMessageId: 100 ),
455516 ];
456517
457- await prepare (users: users, dmConversations: dmConversations);
518+ final messages = [
519+ eg.streamMessage (sender: users[0 ], stream: stream, topic: topic),
520+ eg.streamMessage (sender: users[4 ], stream: stream),
521+ ];
522+
523+ await prepare (
524+ users: users,
525+ dmConversations: dmConversations,
526+ messages: messages);
458527 final view = MentionAutocompleteView .init (store: store, narrow: narrow);
459528
460529 bool done = false ;
@@ -468,11 +537,11 @@ void main() {
468537 }
469538
470539 test ('StreamNarrow' , () async {
471- await checkResultsIn (const StreamNarrow (1 ), expected: [3 , 0 , 1 , 2 , 4 ]);
540+ await checkResultsIn (StreamNarrow (stream.streamId ), expected: [4 , 0 , 3 , 1 , 2 ]);
472541 });
473542
474543 test ('TopicNarrow' , () async {
475- await checkResultsIn (const TopicNarrow (1 , ' topic' ), expected: [3 , 0 , 1 , 2 , 4 ]);
544+ await checkResultsIn (TopicNarrow (stream.streamId, topic), expected: [0 , 4 , 3 , 1 , 2 ]);
476545 });
477546
478547 test ('DmNarrow' , () async {
0 commit comments