From d2a9f91dd7f7f66d3283dabc71812a6383e9acdd Mon Sep 17 00:00:00 2001 From: Zixuan James Li Date: Thu, 7 Nov 2024 14:52:32 -0500 Subject: [PATCH] example_data [nfc]: Move {newest,older}Result Shared by a few tests, they fit better in example_data. Signed-off-by: Zixuan James Li --- integration_test/unreadmarker_test.dart | 3 +- test/example_data.dart | 38 ++++++++ test/model/message_list_test.dart | 114 ++++++++---------------- test/model/message_test.dart | 4 +- test/widgets/action_sheet_test.dart | 5 +- test/widgets/message_list_test.dart | 11 ++- 6 files changed, 86 insertions(+), 89 deletions(-) diff --git a/integration_test/unreadmarker_test.dart b/integration_test/unreadmarker_test.dart index 548a446841c..d97a7c225e0 100644 --- a/integration_test/unreadmarker_test.dart +++ b/integration_test/unreadmarker_test.dart @@ -9,7 +9,6 @@ import 'package:zulip/widgets/message_list.dart'; import '../test/api/fake_api.dart'; import '../test/example_data.dart' as eg; import '../test/model/binding.dart'; -import '../test/model/message_list_test.dart'; import '../test/widgets/test_app.dart'; void main() { @@ -29,7 +28,7 @@ void main() { final messages = List.generate(messageCount, (i) => eg.streamMessage(flags: [MessageFlag.read])); connection.prepare(json: - newestResult(foundOldest: true, messages: messages).toJson()); + eg.newestResult(foundOldest: true, messages: messages).toJson()); await tester.pumpWidget(TestZulipApp(accountId: eg.selfAccount.id, child: const MessageListPage(initNarrow: CombinedFeedNarrow()))); diff --git a/test/example_data.dart b/test/example_data.dart index ab5c1ef5b7a..ff1f2405647 100644 --- a/test/example_data.dart +++ b/test/example_data.dart @@ -5,6 +5,7 @@ import 'package:zulip/api/model/events.dart'; import 'package:zulip/api/model/initial_snapshot.dart'; import 'package:zulip/api/model/model.dart'; import 'package:zulip/api/model/submessage.dart'; +import 'package:zulip/api/route/messages.dart'; import 'package:zulip/api/route/realm.dart'; import 'package:zulip/api/route/channels.dart'; import 'package:zulip/model/narrow.dart'; @@ -451,6 +452,43 @@ DmMessage dmMessage({ }) as Map); } +/// A GetMessagesResult the server might return on an `anchor=newest` request. +GetMessagesResult newestResult({ + required bool foundOldest, + bool historyLimited = false, + required List messages, +}) { + return GetMessagesResult( + // These anchor, foundAnchor, and foundNewest values are what the server + // appears to always return when the request had `anchor=newest`. + anchor: 10000000000000000, // that's 16 zeros + foundAnchor: false, + foundNewest: true, + + foundOldest: foundOldest, + historyLimited: historyLimited, + messages: messages, + ); +} + +/// A GetMessagesResult the server might return when we request older messages. +GetMessagesResult olderResult({ + required int anchor, + bool foundAnchor = false, // the value if the server understood includeAnchor false + required bool foundOldest, + bool historyLimited = false, + required List messages, +}) { + return GetMessagesResult( + anchor: anchor, + foundAnchor: foundAnchor, + foundNewest: false, // empirically always this, even when anchor happens to be latest + foundOldest: foundOldest, + historyLimited: historyLimited, + messages: messages, + ); +} + PollWidgetData pollWidgetData({ required String question, required List options, diff --git a/test/model/message_list_test.dart b/test/model/message_list_test.dart index 01581ffa5eb..b537d437a47 100644 --- a/test/model/message_list_test.dart +++ b/test/model/message_list_test.dart @@ -6,7 +6,6 @@ import 'package:test/scaffolding.dart'; import 'package:zulip/api/model/events.dart'; import 'package:zulip/api/model/model.dart'; import 'package:zulip/api/model/narrow.dart'; -import 'package:zulip/api/route/messages.dart'; import 'package:zulip/model/algorithms.dart'; import 'package:zulip/model/content.dart'; import 'package:zulip/model/message_list.dart'; @@ -67,7 +66,7 @@ void main() { required List messages, }) async { connection.prepare(json: - newestResult(foundOldest: foundOldest, messages: messages).toJson()); + eg.newestResult(foundOldest: foundOldest, messages: messages).toJson()); await model.fetchInitial(); checkNotifiedOnce(); } @@ -94,7 +93,7 @@ void main() { test('fetchInitial', () async { const narrow = CombinedFeedNarrow(); await prepare(narrow: narrow); - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: false, messages: List.generate(kMessageListFetchBatchSize, (i) => eg.streamMessage()), @@ -118,7 +117,7 @@ void main() { test('fetchInitial, short history', () async { await prepare(); - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: true, messages: List.generate(30, (i) => eg.streamMessage()), ).toJson()); @@ -131,7 +130,7 @@ void main() { test('fetchInitial, no messages found', () async { await prepare(); - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: true, messages: [], ).toJson()); @@ -152,7 +151,7 @@ void main() { // Not subscribed to the stream with id 10. eg.streamMessage(stream: eg.stream(streamId: 10)), ]; - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: false, messages: messages, ).toJson()); @@ -168,7 +167,7 @@ void main() { await prepareMessages(foundOldest: false, messages: List.generate(100, (i) => eg.streamMessage(id: 1000 + i))); - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 1000, foundOldest: false, messages: List.generate(100, (i) => eg.streamMessage(id: 900 + i)), ).toJson()); @@ -196,7 +195,7 @@ void main() { await prepareMessages(foundOldest: false, messages: List.generate(100, (i) => eg.streamMessage(id: 1000 + i))); - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 1000, foundOldest: false, messages: List.generate(100, (i) => eg.streamMessage(id: 900 + i)), ).toJson()); @@ -243,7 +242,7 @@ void main() { messages: List.generate(100, (i) => eg.streamMessage(id: 1000 + i))); // The old behavior is to include the anchor message regardless of includeAnchor. - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 1000, foundOldest: false, foundAnchor: true, messages: List.generate(101, (i) => eg.streamMessage(id: 900 + i)), ).toJson()); @@ -264,7 +263,7 @@ void main() { final oldMessages = List.generate(10, (i) => eg.streamMessage(id: 89 + i)) // Not subscribed to the stream with id 10. ..add(eg.streamMessage(id: 99, stream: eg.stream(streamId: 10))); - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 100, foundOldest: false, messages: oldMessages, ).toJson()); @@ -452,7 +451,7 @@ void main() { checkHasMessageIds([1]); connection.prepare( - json: newestResult(foundOldest: true, messages: messages).toJson()); + json: eg.newestResult(foundOldest: true, messages: messages).toJson()); await setVisibility(UserTopicVisibilityPolicy.unmuted); checkNotifiedOnce(); check(model).fetched.isFalse(); @@ -471,7 +470,7 @@ void main() { ]; connection.prepare( - json: newestResult(foundOldest: true, messages: messages).toJson()); + json: eg.newestResult(foundOldest: true, messages: messages).toJson()); final fetchFuture = model.fetchInitial(); await setVisibility(UserTopicVisibilityPolicy.unmuted); @@ -709,7 +708,7 @@ void main() { test('old channel -> channel: refetch', () => awaitFakeAsync((async) async { await prepareNarrow(narrow, initialMessages); - connection.prepare(delay: const Duration(seconds: 2), json: newestResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.newestResult( foundOldest: false, messages: initialMessages + movedMessages, ).toJson()); @@ -763,7 +762,7 @@ void main() { void testMessageMove(PropagateMode propagateMode) => awaitFakeAsync((async) async { await prepareNarrow(narrow, initialMessages + movedMessages); - connection.prepare(delay: const Duration(seconds: 1), json: newestResult( + connection.prepare(delay: const Duration(seconds: 1), json: eg.newestResult( foundOldest: false, messages: movedMessages, ).toJson()); @@ -811,7 +810,7 @@ void main() { test(description, () => awaitFakeAsync((async) async { await prepareNarrow(narrow, initialMessages); - connection.prepare(delay: const Duration(seconds: 2), json: newestResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.newestResult( foundOldest: false, messages: initialMessages + movedMessages, ).toJson()); @@ -881,7 +880,7 @@ void main() { void handleMoveEvent(PropagateMode propagateMode) => awaitFakeAsync((async) async { await prepareNarrow(narrow, initialMessages + movedMessages); - connection.prepare(delay: const Duration(seconds: 1), json: newestResult( + connection.prepare(delay: const Duration(seconds: 1), json: eg.newestResult( foundOldest: false, messages: movedMessages, ).toJson()); @@ -923,7 +922,7 @@ void main() { await store.addSubscription(subscription); final followedMessage = eg.streamMessage(stream: stream, topic: 'new'); - connection.prepare(delay: const Duration(seconds: 2), json: newestResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.newestResult( foundOldest: true, messages: [followedMessage], ).toJson()); @@ -951,7 +950,7 @@ void main() { test('fetchOlder, _reset, fetchOlder returns, move fetch finishes', () => awaitFakeAsync((async) async { await prepareNarrow(narrow, initialMessages); - connection.prepare(delay: const Duration(seconds: 1), json: olderResult( + connection.prepare(delay: const Duration(seconds: 1), json: eg.olderResult( anchor: model.messages[0].id, foundOldest: true, messages: olderMessages, @@ -961,7 +960,7 @@ void main() { checkHasMessages(initialMessages); checkNotifiedOnce(); - connection.prepare(delay: const Duration(seconds: 2), json: newestResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.newestResult( foundOldest: false, messages: initialMessages + movedMessages, ).toJson()); @@ -986,7 +985,7 @@ void main() { test('fetchOlder, _reset, move fetch finishes, fetchOlder returns', () => awaitFakeAsync((async) async { await prepareNarrow(narrow, initialMessages); - connection.prepare(delay: const Duration(seconds: 2), json: olderResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.olderResult( anchor: model.messages[0].id, foundOldest: true, messages: olderMessages, @@ -996,7 +995,7 @@ void main() { check(model).fetchingOlder.isTrue(); checkNotifiedOnce(); - connection.prepare(delay: const Duration(seconds: 1), json: newestResult( + connection.prepare(delay: const Duration(seconds: 1), json: eg.newestResult( foundOldest: false, messages: initialMessages + movedMessages, ).toJson()); @@ -1021,7 +1020,7 @@ void main() { test('fetchInitial, _reset, initial fetch finishes, move fetch finishes', () => awaitFakeAsync((async) async { await prepareNarrow(narrow, null); - connection.prepare(delay: const Duration(seconds: 1), json: newestResult( + connection.prepare(delay: const Duration(seconds: 1), json: eg.newestResult( foundOldest: false, messages: initialMessages, ).toJson()); @@ -1029,7 +1028,7 @@ void main() { checkHasMessages([]); check(model).fetched.isFalse(); - connection.prepare(delay: const Duration(seconds: 2), json: newestResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.newestResult( foundOldest: false, messages: initialMessages + movedMessages, ).toJson()); @@ -1055,7 +1054,7 @@ void main() { test('fetchInitial, _reset, move fetch finishes, initial fetch finishes', () => awaitFakeAsync((async) async { await prepareNarrow(narrow, null); - connection.prepare(delay: const Duration(seconds: 2), json: newestResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.newestResult( foundOldest: false, messages: initialMessages, ).toJson()); @@ -1063,7 +1062,7 @@ void main() { checkHasMessages([]); check(model).fetched.isFalse(); - connection.prepare(delay: const Duration(seconds: 1), json: newestResult( + connection.prepare(delay: const Duration(seconds: 1), json: eg.newestResult( foundOldest: false, messages: initialMessages + movedMessages, ).toJson()); @@ -1087,7 +1086,7 @@ void main() { 'fetchOlder #1 finishes, fetchOlder #2 finishes', () => awaitFakeAsync((async) async { await prepareNarrow(narrow, initialMessages); - connection.prepare(delay: const Duration(seconds: 2), json: olderResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.olderResult( anchor: model.messages[0].id, foundOldest: true, messages: olderMessages, @@ -1097,7 +1096,7 @@ void main() { check(model).fetchingOlder.isTrue(); checkNotifiedOnce(); - connection.prepare(delay: const Duration(seconds: 1), json: newestResult( + connection.prepare(delay: const Duration(seconds: 1), json: eg.newestResult( foundOldest: false, messages: initialMessages + movedMessages, ).toJson()); @@ -1113,7 +1112,7 @@ void main() { async.elapse(const Duration(seconds: 1)); checkNotifiedOnce(); - connection.prepare(delay: const Duration(seconds: 2), json: olderResult( + connection.prepare(delay: const Duration(seconds: 2), json: eg.olderResult( anchor: model.messages[0].id, foundOldest: true, messages: olderMessages @@ -1157,7 +1156,7 @@ void main() { ..addListener(() => notifiedCount2++); for (final m in [model1, model2]) { - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: false, messages: [eg.streamMessage(stream: stream, topic: 'hello')]).toJson()); await m.fetchInitial(); @@ -1197,7 +1196,7 @@ void main() { checkInvariants(model); connection.prepare(json: - newestResult(foundOldest: false, messages: [message]).toJson()); + eg.newestResult(foundOldest: false, messages: [message]).toJson()); await model.fetchInitial(); checkInvariants(model); doCheckMessageAfterFetch( @@ -1290,7 +1289,7 @@ void main() { .deepEquals(expected..addAll([201, 203, 205])); // … and on fetchOlder… - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 201, foundOldest: true, messages: [ eg.streamMessage(id: 101, stream: stream1, topic: 'A'), eg.streamMessage(id: 102, stream: stream1, topic: 'B'), @@ -1349,7 +1348,7 @@ void main() { .deepEquals(expected..addAll([201, 202])); // … and on fetchOlder… - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 201, foundOldest: true, messages: [ eg.streamMessage(id: 101, stream: stream, topic: 'A'), eg.streamMessage(id: 102, stream: stream, topic: 'B'), @@ -1393,7 +1392,7 @@ void main() { .deepEquals(expected..addAll([201])); // … and on fetchOlder… - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 201, foundOldest: true, messages: [ eg.streamMessage(id: 101, stream: stream, topic: 'A'), ]).toJson()); @@ -1433,7 +1432,7 @@ void main() { .deepEquals(expected..addAll([201, 202, 203])); // … and on fetchOlder… - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 201, foundOldest: true, messages: getMessages(101)).toJson()); await model.fetchOlder(); checkNotified(count: 2); @@ -1471,7 +1470,7 @@ void main() { .deepEquals(expected..addAll([201, 202])); // … and on fetchOlder… - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: 201, foundOldest: true, messages: getMessages(101)).toJson()); await model.fetchOlder(); checkNotified(count: 2); @@ -1547,7 +1546,7 @@ void main() { // First, test fetchInitial, where some headers are needed and others not. await prepare(); - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: false, messages: [streamMessage(10), streamMessage(11), dmMessage(12)], ).toJson()); @@ -1555,7 +1554,7 @@ void main() { checkNotifiedOnce(); // Then fetchOlder, where a header is needed in between… - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: model.messages[0].id, foundOldest: false, messages: [streamMessage(7), streamMessage(8), dmMessage(9)], @@ -1564,7 +1563,7 @@ void main() { checkNotified(count: 2); // … and fetchOlder where there's no header in between. - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: model.messages[0].id, foundOldest: false, messages: [streamMessage(6)], @@ -1598,7 +1597,7 @@ void main() { checkNotifiedOnce(); // Have a new fetchOlder reach the oldest, so that a history-start marker appears… - connection.prepare(json: olderResult( + connection.prepare(json: eg.olderResult( anchor: model.messages[0].id, foundOldest: true, messages: [streamMessage(5)], @@ -1848,40 +1847,3 @@ extension MessageListViewChecks on Subject { Subject get haveOldest => has((x) => x.haveOldest, 'haveOldest'); Subject get fetchingOlder => has((x) => x.fetchingOlder, 'fetchingOlder'); } - -/// A GetMessagesResult the server might return on an `anchor=newest` request. -GetMessagesResult newestResult({ - required bool foundOldest, - bool historyLimited = false, - required List messages, -}) { - return GetMessagesResult( - // These anchor, foundAnchor, and foundNewest values are what the server - // appears to always return when the request had `anchor=newest`. - anchor: 10000000000000000, // that's 16 zeros - foundAnchor: false, - foundNewest: true, - - foundOldest: foundOldest, - historyLimited: historyLimited, - messages: messages, - ); -} - -/// A GetMessagesResult the server might return when we request older messages. -GetMessagesResult olderResult({ - required int anchor, - bool foundAnchor = false, // the value if the server understood includeAnchor false - required bool foundOldest, - bool historyLimited = false, - required List messages, -}) { - return GetMessagesResult( - anchor: anchor, - foundAnchor: foundAnchor, - foundNewest: false, // empirically always this, even when anchor happens to be latest - foundOldest: foundOldest, - historyLimited: historyLimited, - messages: messages, - ); -} diff --git a/test/model/message_test.dart b/test/model/message_test.dart index a2b9cfa28aa..c42ee30f8dc 100644 --- a/test/model/message_test.dart +++ b/test/model/message_test.dart @@ -65,7 +65,7 @@ void main() { }) async { assert(messages.every((message) => message.poll == null)); connection.prepare(json: - newestResult(foundOldest: foundOldest, messages: messages).toJson()); + eg.newestResult(foundOldest: foundOldest, messages: messages).toJson()); await messageList.fetchInitial(); checkNotifiedOnce(); } @@ -645,7 +645,7 @@ void main() { // Perform a single-message initial message fetch for [messageList] with // submessages. connection.prepare(json: - newestResult(foundOldest: true, messages: []).toJson() + eg.newestResult(foundOldest: true, messages: []).toJson() ..['messages'] = [{ ...message.toJson(), "submessages": submessages.map(deepToJson).toList(), diff --git a/test/widgets/action_sheet_test.dart b/test/widgets/action_sheet_test.dart index aaf13c0b176..a5ec2996291 100644 --- a/test/widgets/action_sheet_test.dart +++ b/test/widgets/action_sheet_test.dart @@ -26,7 +26,6 @@ import '../api/fake_api.dart'; import '../example_data.dart' as eg; import '../flutter_checks.dart'; import '../model/binding.dart'; -import '../model/message_list_test.dart'; import '../model/test_store.dart'; import '../stdlib_checks.dart'; import '../test_clipboard.dart'; @@ -55,7 +54,7 @@ Future setupToMessageActionSheet(WidgetTester tester, { } connection = store.connection as FakeApiConnection; - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: true, messages: [message]).toJson()); await tester.pumpWidget(TestZulipApp(accountId: eg.selfAccount.id, child: MessageListPage(initNarrow: narrow))); @@ -443,7 +442,7 @@ void main() { // it doesn't matter anyway: [MessageStoreImpl.reconcileMessages] will // keep the version updated by the event. If that somehow changes in // some future refactor, it'll cause this test to fail. - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: true, messages: [message]).toJson()); await store.handleEvent(eg.updateMessageEventMoveFrom( newStreamId: newStream.streamId, newTopic: newTopic, diff --git a/test/widgets/message_list_test.dart b/test/widgets/message_list_test.dart index d27514416da..db4e219ad24 100644 --- a/test/widgets/message_list_test.dart +++ b/test/widgets/message_list_test.dart @@ -28,7 +28,6 @@ import '../api/fake_api.dart'; import '../example_data.dart' as eg; import '../model/binding.dart'; import '../model/content_test.dart'; -import '../model/message_list_test.dart'; import '../model/test_store.dart'; import '../flutter_checks.dart'; import '../stdlib_checks.dart'; @@ -70,7 +69,7 @@ void main() { return eg.streamMessage(sender: eg.selfUser); }); connection.prepare(json: - newestResult(foundOldest: foundOldest, messages: messages).toJson()); + eg.newestResult(foundOldest: foundOldest, messages: messages).toJson()); await tester.pumpWidget(TestZulipApp(accountId: eg.selfAccount.id, child: MessageListPage(initNarrow: narrow))); @@ -189,7 +188,7 @@ void main() { await tester.pump(); // ... and we should fetch more messages as we go. - connection.prepare(json: olderResult(anchor: 950, foundOldest: false, + connection.prepare(json: eg.olderResult(anchor: 950, foundOldest: false, messages: List.generate(100, (i) => eg.streamMessage(id: 850 + i, sender: eg.selfUser))).toJson()); await tester.pump(const Duration(seconds: 3)); // Fast-forward to end of fling. await tester.pump(Duration.zero); // Allow a frame for the response to arrive. @@ -208,7 +207,7 @@ void main() { await tester.pump(); // ... and we fetch more messages as we go. - connection.prepare(json: olderResult(anchor: 950, foundOldest: false, + connection.prepare(json: eg.olderResult(anchor: 950, foundOldest: false, messages: List.generate(100, (i) => eg.streamMessage(id: 850 + i, sender: eg.selfUser))).toJson()); for (int i = 0; i < 30; i++) { // Find the point in the fling where the fetch starts. @@ -220,7 +219,7 @@ void main() { // On the next frame, we promptly fetch *another* batch. // This is a glitch and it'd be nicer if we didn't. - connection.prepare(json: olderResult(anchor: 850, foundOldest: false, + connection.prepare(json: eg.olderResult(anchor: 850, foundOldest: false, messages: List.generate(100, (i) => eg.streamMessage(id: 750 + i, sender: eg.selfUser))).toJson()); await tester.pump(const Duration(milliseconds: 1)); await tester.pump(Duration.zero); @@ -619,7 +618,7 @@ void main() { final narrow = TopicNarrow(channel.streamId, topic); void prepareGetMessageResponse(List messages) { - connection.prepare(json: newestResult( + connection.prepare(json: eg.newestResult( foundOldest: false, messages: messages).toJson()); }