Skip to content

Commit 4762e4e

Browse files
committed
autocomplete [nfc]: Expose debugCompareUsers, for testing
1 parent 72caacf commit 4762e4e

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

lib/model/autocomplete.dart

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ class MentionAutocompleteView extends ChangeNotifier {
186186
static List<User> _usersByRelevance({
187187
required PerAccountStore store,
188188
required Narrow narrow,
189+
}) {
190+
return store.users.values.toList()
191+
..sort(_comparator(store: store, narrow: narrow));
192+
}
193+
194+
/// Compare the users the same way they would be sorted as
195+
/// autocomplete candidates.
196+
///
197+
/// This behaves the same as the comparator used for sorting in
198+
/// [_usersByRelevance], but calling this for each comparison would be a bit
199+
/// less efficient because some of the logic is independent of the users and
200+
/// can be precomputed.
201+
///
202+
/// This is useful for tests in order to distinguish "A comes before B"
203+
/// from "A ranks equal to B, and the sort happened to put A before B",
204+
/// particularly because [List.sort] makes no guarantees about the order
205+
/// of items that compare equal.
206+
int debugCompareUsers(User userA, User userB) {
207+
return _comparator(store: store, narrow: narrow)(userA, userB);
208+
}
209+
210+
static int Function(User, User) _comparator({
211+
required PerAccountStore store,
212+
required Narrow narrow,
189213
}) {
190214
int? streamId;
191215
String? topic;
@@ -200,12 +224,9 @@ class MentionAutocompleteView extends ChangeNotifier {
200224
case CombinedFeedNarrow():
201225
assert(false, 'No compose box, thus no autocomplete is available in ${narrow.runtimeType}.');
202226
}
203-
204-
return store.users.values.toList()
205-
..sort((userA, userB) => _compareByRelevance(userA, userB,
206-
streamId: streamId,
207-
topic: topic,
208-
store: store));
227+
return (userA, userB) => _compareByRelevance(userA, userB,
228+
streamId: streamId, topic: topic,
229+
store: store);
209230
}
210231

211232
static int _compareByRelevance(User userA, User userB, {

0 commit comments

Comments
 (0)