|
2 | 2 | import 'package:checks/checks.dart';
|
3 | 3 | import 'package:test/scaffolding.dart';
|
4 | 4 | import 'package:zulip/api/model/events.dart';
|
| 5 | +import 'package:zulip/api/model/initial_snapshot.dart'; |
5 | 6 | import 'package:zulip/api/model/model.dart';
|
| 7 | +import 'package:zulip/model/store.dart'; |
6 | 8 | import 'package:zulip/model/stream.dart';
|
7 | 9 |
|
8 | 10 | import '../example_data.dart' as eg;
|
@@ -96,4 +98,196 @@ void main() {
|
96 | 98 | check(store.subscriptions[stream.streamId]!.isMuted).isTrue();
|
97 | 99 | });
|
98 | 100 | });
|
| 101 | + |
| 102 | + group('topic visibility', () { |
| 103 | + final stream1 = eg.stream(streamId: 1, name: 'stream 1'); |
| 104 | + final stream2 = eg.stream(streamId: 2, name: 'stream 2'); |
| 105 | + |
| 106 | + group('getter topicVisibilityPolicy', () { |
| 107 | + test('with nothing for stream', () { |
| 108 | + final store = eg.store(); |
| 109 | + check(store.topicVisibilityPolicy(stream1.streamId, 'topic')) |
| 110 | + .equals(UserTopicVisibilityPolicy.none); |
| 111 | + }); |
| 112 | + |
| 113 | + test('with nothing for topic', () { |
| 114 | + final store = eg.store() |
| 115 | + ..addUserTopic(stream1, 'other topic', UserTopicVisibilityPolicy.muted); |
| 116 | + check(store.topicVisibilityPolicy(stream1.streamId, 'topic')) |
| 117 | + .equals(UserTopicVisibilityPolicy.none); |
| 118 | + }); |
| 119 | + |
| 120 | + test('with topic present', () { |
| 121 | + final store = eg.store(); |
| 122 | + for (final policy in [ |
| 123 | + UserTopicVisibilityPolicy.muted, |
| 124 | + UserTopicVisibilityPolicy.unmuted, |
| 125 | + UserTopicVisibilityPolicy.followed, |
| 126 | + ]) { |
| 127 | + store.addUserTopic(stream1, 'topic', policy); |
| 128 | + check(store.topicVisibilityPolicy(stream1.streamId, 'topic')) |
| 129 | + .equals(policy); |
| 130 | + } |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + group('isTopicVisible/InStream', () { |
| 135 | + test('with policy none, stream not muted', () { |
| 136 | + final store = eg.store()..addStream(stream1) |
| 137 | + ..addSubscription(eg.subscription(stream1)); |
| 138 | + check(store.isTopicVisibleInStream(stream1.streamId, 'topic')).isTrue(); |
| 139 | + check(store.isTopicVisible (stream1.streamId, 'topic')).isTrue(); |
| 140 | + }); |
| 141 | + |
| 142 | + test('with policy none, stream muted', () { |
| 143 | + final store = eg.store()..addStream(stream1) |
| 144 | + ..addSubscription(eg.subscription(stream1, isMuted: true)); |
| 145 | + check(store.isTopicVisibleInStream(stream1.streamId, 'topic')).isTrue(); |
| 146 | + check(store.isTopicVisible (stream1.streamId, 'topic')).isFalse(); |
| 147 | + }); |
| 148 | + |
| 149 | + test('with policy none, stream unsubscribed', () { |
| 150 | + final store = eg.store()..addStream(stream1); |
| 151 | + check(store.isTopicVisibleInStream(stream1.streamId, 'topic')).isTrue(); |
| 152 | + check(store.isTopicVisible (stream1.streamId, 'topic')).isFalse(); |
| 153 | + }); |
| 154 | + |
| 155 | + test('with policy muted', () { |
| 156 | + final store = eg.store()..addStream(stream1) |
| 157 | + ..addSubscription(eg.subscription(stream1)) |
| 158 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.muted); |
| 159 | + check(store.isTopicVisibleInStream(stream1.streamId, 'topic')).isFalse(); |
| 160 | + check(store.isTopicVisible (stream1.streamId, 'topic')).isFalse(); |
| 161 | + }); |
| 162 | + |
| 163 | + test('with policy unmuted', () { |
| 164 | + final store = eg.store()..addStream(stream1) |
| 165 | + ..addSubscription(eg.subscription(stream1, isMuted: true)) |
| 166 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.unmuted); |
| 167 | + check(store.isTopicVisibleInStream(stream1.streamId, 'topic')).isTrue(); |
| 168 | + check(store.isTopicVisible (stream1.streamId, 'topic')).isTrue(); |
| 169 | + }); |
| 170 | + |
| 171 | + test('with policy followed', () { |
| 172 | + final store = eg.store()..addStream(stream1) |
| 173 | + ..addSubscription(eg.subscription(stream1, isMuted: true)) |
| 174 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.followed); |
| 175 | + check(store.isTopicVisibleInStream(stream1.streamId, 'topic')).isTrue(); |
| 176 | + check(store.isTopicVisible (stream1.streamId, 'topic')).isTrue(); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + UserTopicItem makeUserTopicItem( |
| 181 | + ZulipStream stream, String topic, UserTopicVisibilityPolicy policy) { |
| 182 | + return UserTopicItem( |
| 183 | + streamId: stream.streamId, |
| 184 | + topicName: topic, |
| 185 | + lastUpdated: 1234567890, |
| 186 | + visibilityPolicy: policy, |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + void compareTopicVisibility(PerAccountStore store, List<UserTopicItem> expected) { |
| 191 | + final expectedStore = eg.store(initialSnapshot: eg.initialSnapshot( |
| 192 | + userTopics: expected, |
| 193 | + )); |
| 194 | + check(store.debugStreamStore.topicVisibility) |
| 195 | + .deepEquals(expectedStore.debugStreamStore.topicVisibility); |
| 196 | + } |
| 197 | + |
| 198 | + test('data structure', () { |
| 199 | + final store = eg.store(initialSnapshot: eg.initialSnapshot( |
| 200 | + streams: [stream1, stream2], |
| 201 | + userTopics: [ |
| 202 | + makeUserTopicItem(stream1, 'topic 1', UserTopicVisibilityPolicy.muted), |
| 203 | + makeUserTopicItem(stream1, 'topic 2', UserTopicVisibilityPolicy.unmuted), |
| 204 | + makeUserTopicItem(stream2, 'topic 3', UserTopicVisibilityPolicy.unknown), |
| 205 | + makeUserTopicItem(stream2, 'topic 4', UserTopicVisibilityPolicy.followed), |
| 206 | + ])); |
| 207 | + check(store.debugStreamStore.topicVisibility).deepEquals({ |
| 208 | + stream1.streamId: { |
| 209 | + 'topic 1': UserTopicVisibilityPolicy.muted, |
| 210 | + 'topic 2': UserTopicVisibilityPolicy.unmuted, |
| 211 | + }, |
| 212 | + stream2.streamId: { |
| 213 | + // 'topic 3' -> unknown treated as no policy |
| 214 | + 'topic 4': UserTopicVisibilityPolicy.followed, |
| 215 | + } |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + group('events', () { |
| 220 | + test('add with new stream', () { |
| 221 | + final store = eg.store() |
| 222 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.muted); |
| 223 | + compareTopicVisibility(store, [ |
| 224 | + makeUserTopicItem(stream1, 'topic', UserTopicVisibilityPolicy.muted), |
| 225 | + ]); |
| 226 | + }); |
| 227 | + |
| 228 | + test('add in existing stream', () { |
| 229 | + final store = eg.store() |
| 230 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.muted) |
| 231 | + ..addUserTopic(stream1, 'other topic', UserTopicVisibilityPolicy.unmuted); |
| 232 | + compareTopicVisibility(store, [ |
| 233 | + makeUserTopicItem(stream1, 'topic', UserTopicVisibilityPolicy.muted), |
| 234 | + makeUserTopicItem(stream1, 'other topic', UserTopicVisibilityPolicy.unmuted), |
| 235 | + ]); |
| 236 | + }); |
| 237 | + |
| 238 | + test('update existing policy', () { |
| 239 | + final store = eg.store() |
| 240 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.muted) |
| 241 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.unmuted); |
| 242 | + compareTopicVisibility(store, [ |
| 243 | + makeUserTopicItem(stream1, 'topic', UserTopicVisibilityPolicy.unmuted), |
| 244 | + ]); |
| 245 | + }); |
| 246 | + |
| 247 | + test('remove, with others in stream', () { |
| 248 | + final store = eg.store() |
| 249 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.muted) |
| 250 | + ..addUserTopic(stream1, 'other topic', UserTopicVisibilityPolicy.unmuted) |
| 251 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.none); |
| 252 | + compareTopicVisibility(store, [ |
| 253 | + makeUserTopicItem(stream1, 'other topic', UserTopicVisibilityPolicy.unmuted), |
| 254 | + ]); |
| 255 | + }); |
| 256 | + |
| 257 | + test('remove, as last in stream', () { |
| 258 | + final store = eg.store() |
| 259 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.muted) |
| 260 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.none); |
| 261 | + compareTopicVisibility(store, [ |
| 262 | + ]); |
| 263 | + }); |
| 264 | + |
| 265 | + test('treat unknown enum value as removing', () { |
| 266 | + final store = eg.store() |
| 267 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.muted) |
| 268 | + ..addUserTopic(stream1, 'topic', UserTopicVisibilityPolicy.unknown); |
| 269 | + compareTopicVisibility(store, [ |
| 270 | + ]); |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
| 274 | + test('smoke', () { |
| 275 | + final stream = eg.stream(); |
| 276 | + final store = eg.store(initialSnapshot: eg.initialSnapshot( |
| 277 | + streams: [stream], |
| 278 | + userTopics: [ |
| 279 | + makeUserTopicItem(stream, 'topic 1', UserTopicVisibilityPolicy.muted), |
| 280 | + makeUserTopicItem(stream, 'topic 2', UserTopicVisibilityPolicy.unmuted), |
| 281 | + makeUserTopicItem(stream, 'topic 3', UserTopicVisibilityPolicy.followed), |
| 282 | + ])); |
| 283 | + check(store.topicVisibilityPolicy(stream.streamId, 'topic 1')) |
| 284 | + .equals(UserTopicVisibilityPolicy.muted); |
| 285 | + check(store.topicVisibilityPolicy(stream.streamId, 'topic 2')) |
| 286 | + .equals(UserTopicVisibilityPolicy.unmuted); |
| 287 | + check(store.topicVisibilityPolicy(stream.streamId, 'topic 3')) |
| 288 | + .equals(UserTopicVisibilityPolicy.followed); |
| 289 | + check(store.topicVisibilityPolicy(stream.streamId, 'topic 4')) |
| 290 | + .equals(UserTopicVisibilityPolicy.none); |
| 291 | + }); |
| 292 | + }); |
99 | 293 | }
|
0 commit comments