Skip to content

Commit 31ab506

Browse files
committed
api: Assert that unread dm and stream snapshots have sorted message IDs
See discussion, which should produce a docs change soon: https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20.60unread_message_ids.60.20sorted.3F/near/1647932
1 parent d559b07 commit 31ab506

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/api/model/initial_snapshot.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/foundation.dart';
22
import 'package:json_annotation/json_annotation.dart';
33

4+
import '../../model/algorithms.dart';
45
import 'model.dart';
56

67
part 'initial_snapshot.g.dart';
@@ -263,7 +264,7 @@ class UnreadDmSnapshot {
263264
UnreadDmSnapshot({
264265
required this.otherUserId,
265266
required this.unreadMessageIds,
266-
});
267+
}) : assert(isSortedWithoutDuplicates(unreadMessageIds));
267268

268269
factory UnreadDmSnapshot.fromJson(Map<String, dynamic> json) =>
269270
_$UnreadDmSnapshotFromJson(json);
@@ -282,7 +283,7 @@ class UnreadStreamSnapshot {
282283
required this.topic,
283284
required this.streamId,
284285
required this.unreadMessageIds,
285-
});
286+
}) : assert(isSortedWithoutDuplicates(unreadMessageIds));
286287

287288
factory UnreadStreamSnapshot.fromJson(Map<String, dynamic> json) =>
288289
_$UnreadStreamSnapshotFromJson(json);
@@ -299,7 +300,7 @@ class UnreadHuddleSnapshot {
299300
UnreadHuddleSnapshot({
300301
required this.userIdsString,
301302
required this.unreadMessageIds,
302-
});
303+
}) : assert(isSortedWithoutDuplicates(unreadMessageIds));
303304

304305
factory UnreadHuddleSnapshot.fromJson(Map<String, dynamic> json) =>
305306
_$UnreadHuddleSnapshotFromJson(json);
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'package:checks/checks.dart';
2+
import 'package:test/scaffolding.dart';
3+
import 'package:zulip/api/model/initial_snapshot.dart';
4+
5+
void main() {
6+
test('UnreadDmSnapshot: require sorted unreadMessageIds', () {
7+
check(() => UnreadDmSnapshot.fromJson({
8+
'other_user_id': 1,
9+
'unread_message_ids': [1, 2, 3],
10+
})).returnsNormally();
11+
12+
check(() => UnreadDmSnapshot.fromJson({
13+
'other_user_id': 1,
14+
'unread_message_ids': [11, 2, 3],
15+
})).throws<AssertionError>();
16+
});
17+
18+
test('UnreadStreamSnapshot: require sorted unreadMessageIds', () {
19+
check(() => UnreadStreamSnapshot.fromJson({
20+
'topic': 'a',
21+
'stream_id': 1,
22+
'unread_message_ids': [1, 2, 3],
23+
})).returnsNormally();
24+
25+
check(() => UnreadStreamSnapshot.fromJson({
26+
'topic': 'a',
27+
'stream_id': 1,
28+
'unread_message_ids': [11, 2, 3],
29+
})).throws<AssertionError>();
30+
});
31+
32+
test('UnreadHuddleSnapshot: require sorted unreadMessageIds', () {
33+
check(() => UnreadHuddleSnapshot.fromJson({
34+
'user_ids_string': '1,2',
35+
'unread_message_ids': [1, 2, 3],
36+
})).returnsNormally();
37+
38+
check(() => UnreadHuddleSnapshot.fromJson({
39+
'user_ids_string': '1,2',
40+
'unread_message_ids': [11, 2, 3],
41+
})).throws<AssertionError>();
42+
});
43+
}

0 commit comments

Comments
 (0)