@@ -4,6 +4,9 @@ import 'package:checks/checks.dart';
4
4
import 'package:flutter/foundation.dart' ;
5
5
import 'package:http/http.dart' as http;
6
6
import 'package:test/scaffolding.dart' ;
7
+ import 'package:zulip/api/model/events.dart' ;
8
+ import 'package:zulip/api/model/model.dart' ;
9
+ import 'package:zulip/api/route/events.dart' ;
7
10
import 'package:zulip/api/route/messages.dart' ;
8
11
import 'package:zulip/model/store.dart' ;
9
12
import 'package:zulip/notifications.dart' ;
@@ -131,6 +134,75 @@ void main() {
131
134
});
132
135
});
133
136
137
+ group ('UpdateMachine.poll' , () {
138
+ late UpdateMachine updateMachine;
139
+ late PerAccountStore store;
140
+ late FakeApiConnection connection;
141
+
142
+ void prepareStore ({int ? lastEventId}) {
143
+ updateMachine = eg.updateMachine (initialSnapshot: eg.initialSnapshot (
144
+ lastEventId: lastEventId,
145
+ ));
146
+ store = updateMachine.store;
147
+ connection = store.connection as FakeApiConnection ;
148
+ }
149
+
150
+ void checkLastRequest ({required int lastEventId}) {
151
+ check (connection.lastRequest).isA< http.Request > ()
152
+ ..method.equals ('GET' )
153
+ ..url.path.equals ('/api/v1/events' )
154
+ ..url.queryParameters.deepEquals ({
155
+ 'queue_id' : updateMachine.queueId,
156
+ 'last_event_id' : lastEventId.toString (),
157
+ });
158
+ }
159
+
160
+ test ('loops on success' , () async {
161
+ prepareStore (lastEventId: 1 );
162
+ check (updateMachine.lastEventId).equals (1 );
163
+
164
+ updateMachine.debugPauseLoop ();
165
+ updateMachine.poll ();
166
+
167
+ // Loop makes first request, and processes result.
168
+ connection.prepare (json: GetEventsResult (events: [
169
+ HeartbeatEvent (id: 2 ),
170
+ ], queueId: null ).toJson ());
171
+ updateMachine.debugAdvanceLoop ();
172
+ await null ;
173
+ checkLastRequest (lastEventId: 1 );
174
+ await Future .delayed (Duration .zero);
175
+ check (updateMachine.lastEventId).equals (2 );
176
+
177
+ // Loop makes second request, and processes result.
178
+ connection.prepare (json: GetEventsResult (events: [
179
+ HeartbeatEvent (id: 3 ),
180
+ ], queueId: null ).toJson ());
181
+ updateMachine.debugAdvanceLoop ();
182
+ await null ;
183
+ checkLastRequest (lastEventId: 2 );
184
+ await Future .delayed (Duration .zero);
185
+ check (updateMachine.lastEventId).equals (3 );
186
+ });
187
+
188
+ test ('handles events' , () async {
189
+ prepareStore ();
190
+ updateMachine.debugPauseLoop ();
191
+ updateMachine.poll ();
192
+
193
+ // Pick some arbitrary event and check it gets processed on the store.
194
+ check (store.userSettings! .twentyFourHourTime).isFalse ();
195
+ connection.prepare (json: GetEventsResult (events: [
196
+ UserSettingsUpdateEvent (id: 2 ,
197
+ property: UserSettingName .twentyFourHourTime, value: true ),
198
+ ], queueId: null ).toJson ());
199
+ updateMachine.debugAdvanceLoop ();
200
+ await null ;
201
+ await Future .delayed (Duration .zero);
202
+ check (store.userSettings! .twentyFourHourTime).isTrue ();
203
+ });
204
+ });
205
+
134
206
group ('UpdateMachine.registerNotificationToken' , () {
135
207
late UpdateMachine updateMachine;
136
208
late FakeApiConnection connection;
0 commit comments