Skip to content

Commit f1035d7

Browse files
committed
test: Generate random increasing streamId for example data by default
This allows `eg.stream` to be used in contexts where it's important that the stream IDs don't collide or even that they're increasing, without the test having to explicitly give specific stream IDs when no other fact about them is relevant. Some tests are dependent on the stream IDs being static. Those are updated to use `eg.defaultStreamMessageStreamId` explicitly.
1 parent 0399af6 commit f1035d7

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

test/example_data.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ final User fourthUser = user(fullName: 'Fourth User', email: 'fourth@example');
147147
// Streams and subscriptions.
148148
//
149149

150+
/// A fresh stream ID, from a random but always strictly increasing sequence.
151+
int _nextStreamId() => (_lastStreamId += 1 + Random().nextInt(10));
152+
int _lastStreamId = 200;
153+
154+
/// Construct an example stream.
155+
///
156+
/// If stream ID `streamId` is not given, it will be generated from a random
157+
/// but increasing sequence.
158+
/// Use an explicit `streamId` only if the ID needs to correspond to some
159+
/// other data in the test, or if the IDs need to increase in a different order
160+
/// from the calls to [stream].
150161
ZulipStream stream({
151162
int? streamId,
152163
String? name,
@@ -163,7 +174,7 @@ ZulipStream stream({
163174
int? streamWeeklyTraffic,
164175
}) {
165176
return ZulipStream(
166-
streamId: streamId ?? 123, // TODO generate example IDs
177+
streamId: streamId ?? _nextStreamId(),
167178
name: name ?? 'A stream', // TODO generate example names
168179
description: description ?? 'A description', // TODO generate example descriptions
169180
renderedDescription: renderedDescription ?? '<p>A description</p>', // TODO generate random
@@ -278,6 +289,7 @@ Map<String, dynamic> _messagePropertiesFromContent(String? content, String? cont
278289
/// A fresh message ID, from a random but always strictly increasing sequence.
279290
int _nextMessageId() => (_lastMessageId += 1 + Random().nextInt(100));
280291
int _lastMessageId = 1000;
292+
const defaultStreamMessageStreamId = 123;
281293

282294
/// Construct an example stream message.
283295
///
@@ -287,6 +299,9 @@ int _lastMessageId = 1000;
287299
/// in the test, or if the IDs need to increase in a different order from the
288300
/// calls to [streamMessage] and [dmMessage].
289301
///
302+
/// If the message stream `stream` is not given, an example stream with
303+
/// `defaultStreamMessageStreamId` will be used.
304+
///
290305
/// See also:
291306
/// * [dmMessage], to construct an example direct message.
292307
StreamMessage streamMessage({
@@ -301,7 +316,7 @@ StreamMessage streamMessage({
301316
int? timestamp,
302317
List<MessageFlag>? flags,
303318
}) {
304-
final effectiveStream = stream ?? _stream();
319+
final effectiveStream = stream ?? _stream(streamId: defaultStreamMessageStreamId);
305320
// The use of JSON here is convenient in order to delegate parts of the data
306321
// to helper functions. The main downside is that it loses static typing
307322
// of the properties as we're constructing the data. That's probably OK

test/model/message_list_test.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void main() {
3838

3939
/// Initialize [model] and the rest of the test state.
4040
Future<void> prepare({Narrow narrow = const CombinedFeedNarrow()}) async {
41-
final stream = eg.stream();
41+
final stream = eg.stream(streamId: eg.defaultStreamMessageStreamId);
4242
subscription = eg.subscription(stream);
4343
store = eg.store();
4444
await store.addStream(stream);
@@ -248,13 +248,13 @@ void main() {
248248
});
249249

250250
test('MessageEvent, not in narrow', () async {
251-
final stream = eg.stream(streamId: 123);
251+
final stream = eg.stream();
252252
await prepare(narrow: StreamNarrow(stream.streamId));
253253
await prepareMessages(foundOldest: true, messages:
254254
List.generate(30, (i) => eg.streamMessage(stream: stream)));
255255

256256
check(model).messages.length.equals(30);
257-
final otherStream = eg.stream(streamId: 234);
257+
final otherStream = eg.stream();
258258
await store.handleEvent(MessageEvent(id: 0,
259259
message: eg.streamMessage(stream: otherStream)));
260260
checkNotNotified();
@@ -651,7 +651,7 @@ void main() {
651651
// doesn't need to exercise the different reasons that messages don't.
652652

653653
const timestamp = 1693602618;
654-
final stream = eg.stream();
654+
final stream = eg.stream(streamId: eg.defaultStreamMessageStreamId);
655655
Message streamMessage(int id) =>
656656
eg.streamMessage(id: id, stream: stream, topic: 'foo', timestamp: timestamp);
657657
Message dmMessage(int id) =>
@@ -732,7 +732,7 @@ void main() {
732732

733733
const t1 = 1693602618;
734734
const t2 = t1 + 86400;
735-
final stream = eg.stream();
735+
final stream = eg.stream(streamId: eg.defaultStreamMessageStreamId);
736736
Message streamMessage(int id, int timestamp, User sender) =>
737737
eg.streamMessage(id: id, sender: sender,
738738
stream: stream, topic: 'foo', timestamp: timestamp);
@@ -773,8 +773,8 @@ void main() {
773773
});
774774

775775
test('stream messages match just if same stream/topic', () {
776-
final stream0 = eg.stream(streamId: 123);
777-
final stream1 = eg.stream(streamId: 234);
776+
final stream0 = eg.stream();
777+
final stream1 = eg.stream();
778778
final messageAB = eg.streamMessage(stream: stream0, topic: 'foo');
779779
final messageXB = eg.streamMessage(stream: stream1, topic: 'foo');
780780
final messageAX = eg.streamMessage(stream: stream0, topic: 'bar');

test/model/message_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void main() {
3434

3535
/// Initialize [store] and the rest of the test state.
3636
Future<void> prepare({Narrow narrow = const CombinedFeedNarrow()}) async {
37-
final stream = eg.stream();
37+
final stream = eg.stream(streamId: eg.defaultStreamMessageStreamId);
3838
subscription = eg.subscription(stream);
3939
store = eg.store();
4040
await store.addStream(stream);

test/widgets/message_list_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void main() {
5252
UnreadMessagesSnapshot? unreadMsgs,
5353
}) async {
5454
addTearDown(testBinding.reset);
55-
streams ??= subscriptions ??= [eg.subscription(eg.stream())];
55+
streams ??= subscriptions ??= [eg.subscription(eg.stream(streamId: eg.defaultStreamMessageStreamId))];
5656
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot(
5757
streams: streams, subscriptions: subscriptions, unreadMsgs: unreadMsgs));
5858
store = await testBinding.globalStore.perAccount(eg.selfAccount.id);

0 commit comments

Comments
 (0)