@@ -356,9 +356,10 @@ void main() {
356
356
late PerAccountStore store;
357
357
late MentionAutocompleteView view;
358
358
359
- prepare ({
359
+ void prepare ({
360
360
required List <User > users,
361
361
required List <RecentDmConversation > dmConversations,
362
+ required List <Message > messages,
362
363
required Narrow narrow,
363
364
}) {
364
365
store = eg.store (
@@ -368,16 +369,100 @@ void main() {
368
369
for (final user in users) {
369
370
store.addUser (user);
370
371
}
372
+ for (final message in messages) {
373
+ store.addMessage (message);
374
+ }
371
375
view = MentionAutocompleteView .init (store: store, narrow: narrow);
372
376
}
373
377
374
- test ('compareByDms give priority to user with DM exchanged more recently' , () {
378
+ group ('compareByRecency gives priority to the user with latter message in topic/stream' , () {
379
+ final userA = eg.otherUser;
380
+ final userB = eg.thirdUser;
381
+ final stream = eg.stream ();
382
+ const topic1 = 'topic1' ;
383
+ const topic2 = 'topic2' ;
384
+
385
+ void addMessage (User sender, String topic) {
386
+ store.addMessage (eg.streamMessage (
387
+ sender: sender,
388
+ stream: stream,
389
+ topic: topic,
390
+ ));
391
+ }
392
+
393
+ /// Determines the priority between [userA] and [userB] based on their activity.
394
+ ///
395
+ /// The activity is first looked for in [topic] then in [stream] .
396
+ ///
397
+ /// Returns a negative number if [userA] has more recent activity,
398
+ /// returns a positive number if [userB] has more recent activity, and
399
+ /// returns `0` if the activity is the same or there is no activity at all.
400
+ int compareAB ({required String ? topic}) {
401
+ return view.compareByRecency (
402
+ userA,
403
+ userB,
404
+ streamId: stream.streamId,
405
+ topic: topic,
406
+ );
407
+ }
408
+
409
+ test ('prioritizes the user with more recent activity in the topic' , () {
410
+ prepare (
411
+ users: [],
412
+ dmConversations: [],
413
+ messages: [],
414
+ narrow: const AllMessagesNarrow (),
415
+ );
416
+ addMessage (userA, topic1);
417
+ addMessage (userB, topic1);
418
+ check (compareAB (topic: topic1)).isGreaterThan (0 );
419
+ });
420
+
421
+ test ('prioritizes the user with more recent activity in the stream '
422
+ 'if there is no activity in the topic from both users' , () {
423
+ prepare (
424
+ users: [],
425
+ dmConversations: [],
426
+ messages: [],
427
+ narrow: const AllMessagesNarrow (),
428
+ );
429
+ addMessage (userA, topic1);
430
+ addMessage (userB, topic1);
431
+ check (compareAB (topic: topic2)).isGreaterThan (0 );
432
+ });
433
+
434
+ test ('prioritizes the user with more recent activity in the stream '
435
+ 'if there is no topic provided' , () {
436
+ prepare (
437
+ users: [],
438
+ dmConversations: [],
439
+ messages: [],
440
+ narrow: const AllMessagesNarrow (),
441
+ );
442
+ addMessage (userA, topic1);
443
+ addMessage (userB, topic2);
444
+ check (compareAB (topic: null )).isGreaterThan (0 );
445
+ });
446
+
447
+ test ('prioritizes none of the users if there is no activity in the stream from both users' , () {
448
+ prepare (
449
+ users: [],
450
+ dmConversations: [],
451
+ messages: [],
452
+ narrow: const AllMessagesNarrow (),
453
+ );
454
+ check (compareAB (topic: null )).equals (0 );
455
+ });
456
+ });
457
+
458
+ test ('compareByDms gives priority to user with DM exchanged more recently' , () {
375
459
prepare (
376
460
users: [],
377
461
dmConversations: [
378
462
RecentDmConversation (userIds: [1 ], maxMessageId: 200 ),
379
463
RecentDmConversation (userIds: [1 , 2 ], maxMessageId: 100 ),
380
464
],
465
+ messages: [],
381
466
narrow: const AllMessagesNarrow (),
382
467
);
383
468
@@ -388,23 +473,44 @@ void main() {
388
473
});
389
474
390
475
test ('autocomplete suggests relevant users in the following order: '
391
- '1. Users most recent in the DM conversations' , () async {
476
+ '1. User most recent in the current topic/stream '
477
+ '2. Users most recent in the DM conversations' , () async {
392
478
final users = [
479
+ eg.user (userId: 0 ),
393
480
eg.user (userId: 1 ),
394
481
eg.user (userId: 2 ),
395
482
eg.user (userId: 3 ),
396
483
eg.user (userId: 4 ),
397
- eg.user (userId: 5 ),
398
484
];
399
485
400
486
final dmConversations = [
401
- RecentDmConversation (userIds: [4 ], maxMessageId: 300 ),
402
- RecentDmConversation (userIds: [1 ], maxMessageId: 200 ),
403
- RecentDmConversation (userIds: [1 , 2 ], maxMessageId: 100 ),
487
+ RecentDmConversation (userIds: [3 ], maxMessageId: 300 ),
488
+ RecentDmConversation (userIds: [0 ], maxMessageId: 200 ),
489
+ RecentDmConversation (userIds: [0 , 1 ], maxMessageId: 100 ),
490
+ ];
491
+
492
+ const streamId = 1 ;
493
+ const topic = 'topic' ;
494
+
495
+ final messages = [
496
+ eg.streamMessage (
497
+ sender: users[0 ],
498
+ stream: eg.stream (streamId: streamId),
499
+ topic: topic,
500
+ ),
501
+ eg.streamMessage (
502
+ sender: users[4 ],
503
+ stream: eg.stream (streamId: streamId),
504
+ ),
404
505
];
405
506
406
507
Future <void > checkResultsIn (Narrow narrow, {required List <int > expected}) async {
407
- prepare (users: users, dmConversations: dmConversations, narrow: narrow);
508
+ prepare (
509
+ users: users,
510
+ dmConversations: dmConversations,
511
+ messages: messages,
512
+ narrow: narrow,
513
+ );
408
514
409
515
bool done = false ;
410
516
view.addListener (() { done = true ; });
@@ -417,19 +523,19 @@ void main() {
417
523
check (results).deepEquals (expected);
418
524
}
419
525
420
- const streamNarrow = StreamNarrow (1 );
421
- await checkResultsIn (streamNarrow, expected: [4 , 1 , 2 , 3 , 5 ]);
526
+ const streamNarrow = StreamNarrow (streamId );
527
+ await checkResultsIn (streamNarrow, expected: [4 , 0 , 3 , 1 , 2 ]);
422
528
423
- const topicNarrow = TopicNarrow (1 , ' topic' );
424
- await checkResultsIn (topicNarrow, expected: [4 , 1 , 2 , 3 , 5 ]);
529
+ const topicNarrow = TopicNarrow (streamId, topic);
530
+ await checkResultsIn (topicNarrow, expected: [0 , 4 , 3 , 1 , 2 ]);
425
531
426
532
final dmNarrow = DmNarrow (allRecipientIds: [eg.selfUser.userId], selfUserId: eg.selfUser.userId);
427
- await checkResultsIn (dmNarrow, expected: [4 , 1 , 2 , 3 , 5 ]);
533
+ await checkResultsIn (dmNarrow, expected: [3 , 0 , 1 , 2 , 4 ]);
428
534
429
535
const allMessagesNarrow = AllMessagesNarrow ();
430
536
// Results are in the original order as we do not sort them for
431
537
// [AllMessagesNarrow] because we can not access autocomplete for now.
432
- await checkResultsIn (allMessagesNarrow, expected: [1 , 2 , 3 , 4 , 5 ]);
538
+ await checkResultsIn (allMessagesNarrow, expected: [0 , 1 , 2 , 3 , 4 ]);
433
539
});
434
540
});
435
541
}
0 commit comments