Skip to content

Commit 946a7bd

Browse files
committed
msglist: In TopicNarrow, assert in _messageVisible that message is in topic
It's a bug if we load a topic-narrow message list and it has some message(s) that are outside the specified channel/topic. Some tests were running afoul of this; we fix those here.
1 parent c3c9c6b commit 946a7bd

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

lib/model/message_list.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ class MessageListView with ChangeNotifier, _MessageSequence {
687687
return store.isTopicVisibleInStream(streamId, message.conversation.topic);
688688

689689
case TopicNarrow():
690+
assert((narrow as TopicNarrow).containsMessage(message));
691+
return true;
692+
690693
case DmNarrow():
691694
return true;
692695

test/widgets/action_sheet_test.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,15 +1922,13 @@ void main() {
19221922

19231923
final newStream = eg.stream();
19241924
const newTopic = 'other topic';
1925-
// This result isn't quite realistic for this request: it should get
1926-
// the updated channel/stream ID and topic, because we don't even
1927-
// start the request until after we get the move event.
1928-
// But constructing the right result is annoying at the moment, and
1929-
// it doesn't matter anyway: [MessageStoreImpl.reconcileMessages] will
1930-
// keep the version updated by the event. If that somehow changes in
1931-
// some future refactor, it'll cause this test to fail.
19321925
connection.prepare(json: eg.newestGetMessagesResult(
1933-
foundOldest: true, messages: [message]).toJson());
1926+
foundOldest: true,
1927+
messages: [
1928+
Message.fromJson(deepToJson(message) as Map<String, dynamic>
1929+
..['stream_id'] = newStream.streamId
1930+
..['subject'] = newTopic)
1931+
]).toJson());
19341932
await store.handleEvent(eg.updateMessageEventMoveFrom(
19351933
newStreamId: newStream.streamId, newTopicStr: newTopic,
19361934
propagateMode: PropagateMode.changeAll,

test/widgets/message_list_test.dart

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,14 @@ void main() {
165165
// Regression test for: https://github.com/zulip/zulip-flutter/issues/1717
166166
final stream = eg.stream();
167167
// Open the page on a topic with the literal name "general chat".
168-
final topic = eg.defaultRealmEmptyTopicDisplayName;
169-
final topicNarrow = eg.topicNarrow(stream.streamId, topic);
168+
final topicNarrow = eg.topicNarrow(
169+
stream.streamId,
170+
eg.defaultRealmEmptyTopicDisplayName);
171+
final message = eg.streamMessage(
172+
stream: stream, topic: '', content: "<p>a message</p>");
170173
await setupMessageListPage(tester, narrow: topicNarrow,
171174
subscriptions: [eg.subscription(stream)],
172-
messages: [eg.streamMessage(stream: stream, topic: topic, content: "<p>a message</p>")]);
175+
messages: [message]);
173176
final state = MessageListPage.ancestorOf(tester.element(find.text("a message")));
174177
// The page's narrow has been updated; the topic is "", not "general chat".
175178
check(state.narrow).equals(eg.topicNarrow(stream.streamId, ''));
@@ -240,7 +243,7 @@ void main() {
240243
await setupMessageListPage(tester,
241244
narrow: eg.topicNarrow(channel.streamId, ''),
242245
subscriptions: [eg.subscription(channel)],
243-
messageCount: 1);
246+
messages: [eg.streamMessage(stream: channel, topic: '')]);
244247
checkAppBarChannelTopic(
245248
channel.name, eg.defaultRealmEmptyTopicDisplayName);
246249
});
@@ -282,7 +285,8 @@ void main() {
282285
final channel = eg.stream();
283286
await setupMessageListPage(tester, narrow: eg.topicNarrow(channel.streamId, 'hi'),
284287
navObservers: [navObserver],
285-
subscriptions: [eg.subscription(channel)], messageCount: 1);
288+
subscriptions: [eg.subscription(channel)],
289+
messages: [eg.streamMessage(stream: channel, topic: 'hi')]);
286290

287291
// Clear out initial route.
288292
assert(pushedRoutes.length == 1);
@@ -320,7 +324,7 @@ void main() {
320324
await setupMessageListPage(tester,
321325
narrow: eg.topicNarrow(channel.streamId, topic),
322326
streams: [channel], subscriptions: [eg.subscription(channel)],
323-
messageCount: 1);
327+
messages: [eg.streamMessage(stream: channel, topic: topic)]);
324328
await store.handleEvent(eg.userTopicEvent(
325329
channel.streamId, topic, UserTopicVisibilityPolicy.muted));
326330
await tester.pump();
@@ -1400,9 +1404,9 @@ void main() {
14001404
final otherSubscription = eg.subscription(otherChannel);
14011405
final narrow = eg.topicNarrow(channel.streamId, topic);
14021406

1403-
void prepareGetMessageResponse(List<Message> messages) {
1407+
void prepareGetMessageResponse(List<Message> messages, {bool foundOldest = false}) {
14041408
connection.prepare(json: eg.newestGetMessagesResult(
1405-
foundOldest: false, messages: messages).toJson());
1409+
foundOldest: foundOldest, messages: messages).toJson());
14061410
}
14071411

14081412
Future<void> handleMessageMoveEvent(List<StreamMessage> messages, String newTopic, {int? newChannelId}) async {
@@ -1463,10 +1467,23 @@ void main() {
14631467
check(find.textContaining('Existing message').evaluate()).length.equals(0);
14641468
check(find.textContaining('Message to move').evaluate()).length.equals(1);
14651469

1470+
final newChannel = eg.stream();
14661471
final existingMessage = eg.streamMessage(
1467-
stream: eg.stream(), topic: 'new topic', content: 'Existing message');
1468-
prepareGetMessageResponse([existingMessage, message]);
1469-
await handleMessageMoveEvent([message], 'new topic');
1472+
stream: newChannel, topic: 'new topic', content: 'Existing message');
1473+
prepareGetMessageResponse(
1474+
// `foundOldest: true` just to avoid having to prepare a fetch-older
1475+
// response when a scroll-metrics notification occurs. (I don't really
1476+
// understand what causes that scroll-metrics notification, but it's not
1477+
// the focus of this test.)
1478+
foundOldest: true,
1479+
[
1480+
existingMessage,
1481+
Message.fromJson(deepToJson(message) as Map<String, dynamic>
1482+
..['stream_id'] = newChannel.streamId
1483+
..['subject'] = 'new topic'),
1484+
]);
1485+
await handleMessageMoveEvent([message],
1486+
'new topic', newChannelId: newChannel.streamId);
14701487
await tester.pump(const Duration(seconds: 1));
14711488

14721489
check(find.textContaining('Existing message').evaluate()).length.equals(1);
@@ -2274,7 +2291,7 @@ void main() {
22742291
doTest(expected: false, ChannelNarrow(subscription.streamId),
22752292
mkMessage: () => eg.streamMessage(stream: subscription));
22762293
doTest(expected: false, TopicNarrow(subscription.streamId, eg.t(topic)),
2277-
mkMessage: () => eg.streamMessage(stream: subscription));
2294+
mkMessage: () => eg.streamMessage(stream: subscription, topic: topic));
22782295
doTest(expected: false, DmNarrow.withUsers([], selfUserId: eg.selfUser.userId),
22792296
mkMessage: () => eg.streamMessage(stream: subscription, topic: topic));
22802297
doTest(expected: true, StarredMessagesNarrow(),

0 commit comments

Comments
 (0)