Skip to content

Commit 13e54f6

Browse files
gnpricechrisbobbe
authored andcommitted
test: Generate random, increasing message IDs for example data by default
This allows `eg.streamMessage` and `eg.dmMessage` to be used in contexts where it's important that the message IDs don't collide, or even that they're increasing, without the test having to explicitly give specific message IDs when no other fact about them is relevant. Many call sites that were passing explicit message IDs no longer need to do so after this change. We'll clear those out in the next commit.
1 parent f4fec85 commit 13e54f6

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

test/example_data.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:math';
2+
13
import 'package:zulip/api/model/events.dart';
24
import 'package:zulip/api/model/initial_snapshot.dart';
35
import 'package:zulip/api/model/model.dart';
@@ -164,6 +166,20 @@ Map<String, dynamic> _messagePropertiesFromContent(String? content, String? cont
164166
}
165167
}
166168

169+
/// A fresh message ID, from a random but always strictly increasing sequence.
170+
int _nextMessageId() => (_lastMessageId += 1 + Random().nextInt(100));
171+
int _lastMessageId = 1000;
172+
173+
/// Construct an example stream message.
174+
///
175+
/// If the message ID `id` is not given, it will be generated from a random
176+
/// but increasing sequence, which is shared with [dmMessage].
177+
/// Use an explicit `id` only if the ID needs to correspond to some other data
178+
/// in the test, or if the IDs need to increase in a different order from the
179+
/// calls to [streamMessage] and [dmMessage].
180+
///
181+
/// See also:
182+
/// * [dmMessage], to construct an example direct message.
167183
StreamMessage streamMessage({
168184
int? id,
169185
User? sender,
@@ -190,7 +206,7 @@ StreamMessage streamMessage({
190206
'stream_id': effectiveStream.streamId,
191207
'reactions': reactions == null ? [] : Reactions(reactions),
192208
'flags': flags ?? [],
193-
'id': id ?? 1234567, // TODO generate example IDs
209+
'id': id ?? _nextMessageId(),
194210
'last_edit_timestamp': lastEditTimestamp,
195211
'subject': topic ?? 'example topic',
196212
'timestamp': timestamp ?? 1678139636,
@@ -200,6 +216,12 @@ StreamMessage streamMessage({
200216

201217
/// Construct an example direct message.
202218
///
219+
/// If the message ID `id` is not given, it will be generated from a random
220+
/// but increasing sequence, which is shared with [streamMessage].
221+
/// Use an explicit `id` only if the ID needs to correspond to some other data
222+
/// in the test, or if the IDs need to increase in a different order from the
223+
/// calls to [streamMessage] and [dmMessage].
224+
///
203225
/// See also:
204226
/// * [streamMessage], to construct an example stream message.
205227
DmMessage dmMessage({
@@ -222,7 +244,7 @@ DmMessage dmMessage({
222244
.toList(growable: false),
223245
'reactions': [],
224246
'flags': flags ?? [],
225-
'id': id ?? 1234567, // TODO generate example IDs
247+
'id': id ?? _nextMessageId(),
226248
'last_edit_timestamp': lastEditTimestamp,
227249
'subject': '',
228250
'timestamp': timestamp ?? 1678139636,

0 commit comments

Comments
 (0)