@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
+ import { MatrixEvent } from "../../../src" ;
17
18
import {
18
19
isTimestampInDuration ,
19
20
Beacon ,
@@ -65,9 +66,9 @@ describe('Beacon', () => {
65
66
// beacon_info events
66
67
// created 'an hour ago'
67
68
// without timeout of 3 hours
68
- let liveBeaconEvent ;
69
- let notLiveBeaconEvent ;
70
- let user2BeaconEvent ;
69
+ let liveBeaconEvent : MatrixEvent ;
70
+ let notLiveBeaconEvent : MatrixEvent ;
71
+ let user2BeaconEvent : MatrixEvent ;
71
72
72
73
const advanceDateAndTime = ( ms : number ) => {
73
74
// bc liveness check uses Date.now we have to advance this mock
@@ -77,21 +78,24 @@ describe('Beacon', () => {
77
78
} ;
78
79
79
80
beforeEach ( ( ) => {
80
- // go back in time to create the beacon
81
- jest . spyOn ( global . Date , 'now' ) . mockReturnValue ( now - HOUR_MS ) ;
82
81
liveBeaconEvent = makeBeaconInfoEvent (
83
82
userId ,
84
83
roomId ,
85
84
{
86
85
timeout : HOUR_MS * 3 ,
87
86
isLive : true ,
87
+ timestamp : now - HOUR_MS ,
88
88
} ,
89
89
'$live123' ,
90
90
) ;
91
91
notLiveBeaconEvent = makeBeaconInfoEvent (
92
92
userId ,
93
93
roomId ,
94
- { timeout : HOUR_MS * 3 , isLive : false } ,
94
+ {
95
+ timeout : HOUR_MS * 3 ,
96
+ isLive : false ,
97
+ timestamp : now - HOUR_MS ,
98
+ } ,
95
99
'$dead123' ,
96
100
) ;
97
101
user2BeaconEvent = makeBeaconInfoEvent (
@@ -100,11 +104,12 @@ describe('Beacon', () => {
100
104
{
101
105
timeout : HOUR_MS * 3 ,
102
106
isLive : true ,
107
+ timestamp : now - HOUR_MS ,
103
108
} ,
104
109
'$user2live123' ,
105
110
) ;
106
111
107
- // back to now
112
+ // back to ' now'
108
113
jest . spyOn ( global . Date , 'now' ) . mockReturnValue ( now ) ;
109
114
} ) ;
110
115
@@ -131,17 +136,81 @@ describe('Beacon', () => {
131
136
} ) ;
132
137
133
138
it ( 'returns false when beacon is expired' , ( ) => {
134
- // time travel to beacon creation + 3 hours
135
- jest . spyOn ( global . Date , 'now' ) . mockReturnValue ( now - 3 * HOUR_MS ) ;
136
- const beacon = new Beacon ( liveBeaconEvent ) ;
139
+ const expiredBeaconEvent = makeBeaconInfoEvent (
140
+ userId2 ,
141
+ roomId ,
142
+ {
143
+ timeout : HOUR_MS ,
144
+ isLive : true ,
145
+ timestamp : now - HOUR_MS * 2 ,
146
+ } ,
147
+ '$user2live123' ,
148
+ ) ;
149
+ const beacon = new Beacon ( expiredBeaconEvent ) ;
137
150
expect ( beacon . isLive ) . toEqual ( false ) ;
138
151
} ) ;
139
152
140
- it ( 'returns false when beacon timestamp is in future' , ( ) => {
141
- // time travel to before beacon events timestamp
142
- // event was created now - 1 hour
143
- jest . spyOn ( global . Date , 'now' ) . mockReturnValue ( now - HOUR_MS - HOUR_MS ) ;
144
- const beacon = new Beacon ( liveBeaconEvent ) ;
153
+ it ( 'returns false when beacon timestamp is in future by an hour' , ( ) => {
154
+ const beaconStartsInHour = makeBeaconInfoEvent (
155
+ userId2 ,
156
+ roomId ,
157
+ {
158
+ timeout : HOUR_MS ,
159
+ isLive : true ,
160
+ timestamp : now + HOUR_MS ,
161
+ } ,
162
+ '$user2live123' ,
163
+ ) ;
164
+ const beacon = new Beacon ( beaconStartsInHour ) ;
165
+ expect ( beacon . isLive ) . toEqual ( false ) ;
166
+ } ) ;
167
+
168
+ it ( 'returns true when beacon timestamp is one minute in the future' , ( ) => {
169
+ const beaconStartsInOneMin = makeBeaconInfoEvent (
170
+ userId2 ,
171
+ roomId ,
172
+ {
173
+ timeout : HOUR_MS ,
174
+ isLive : true ,
175
+ timestamp : now + 60000 ,
176
+ } ,
177
+ '$user2live123' ,
178
+ ) ;
179
+ const beacon = new Beacon ( beaconStartsInOneMin ) ;
180
+ expect ( beacon . isLive ) . toEqual ( true ) ;
181
+ } ) ;
182
+
183
+ it ( 'returns true when beacon timestamp is one minute before expiry' , ( ) => {
184
+ // this test case is to check the start time leniency doesn't affect
185
+ // strict expiry time checks
186
+ const expiresInOneMin = makeBeaconInfoEvent (
187
+ userId2 ,
188
+ roomId ,
189
+ {
190
+ timeout : HOUR_MS ,
191
+ isLive : true ,
192
+ timestamp : now - HOUR_MS + 60000 ,
193
+ } ,
194
+ '$user2live123' ,
195
+ ) ;
196
+ const beacon = new Beacon ( expiresInOneMin ) ;
197
+ expect ( beacon . isLive ) . toEqual ( true ) ;
198
+ } ) ;
199
+
200
+ it ( 'returns false when beacon timestamp is one minute after expiry' , ( ) => {
201
+ // this test case is to check the start time leniency doesn't affect
202
+ // strict expiry time checks
203
+ const expiredOneMinAgo = makeBeaconInfoEvent (
204
+ userId2 ,
205
+ roomId ,
206
+ {
207
+ timeout : HOUR_MS ,
208
+ isLive : true ,
209
+ timestamp : now - HOUR_MS - 60000 ,
210
+ } ,
211
+ '$user2live123' ,
212
+ ) ;
213
+ const beacon = new Beacon ( expiredOneMinAgo ) ;
145
214
expect ( beacon . isLive ) . toEqual ( false ) ;
146
215
} ) ;
147
216
@@ -232,19 +301,17 @@ describe('Beacon', () => {
232
301
} ) ;
233
302
234
303
it ( 'checks liveness of beacon at expected start time' , ( ) => {
235
- // go forward in time to make beacon with timestamp in future
236
- jest . spyOn ( global . Date , 'now' ) . mockReturnValue ( now + HOUR_MS ) ;
237
304
const futureBeaconEvent = makeBeaconInfoEvent (
238
305
userId ,
239
306
roomId ,
240
307
{
241
308
timeout : HOUR_MS * 3 ,
242
309
isLive : true ,
310
+ // start timestamp hour in future
311
+ timestamp : now + HOUR_MS ,
243
312
} ,
244
313
'$live123' ,
245
314
) ;
246
- // go back to now
247
- jest . spyOn ( global . Date , 'now' ) . mockReturnValue ( now ) ;
248
315
249
316
const beacon = new Beacon ( futureBeaconEvent ) ;
250
317
expect ( beacon . isLive ) . toBeFalsy ( ) ;
@@ -345,6 +412,57 @@ describe('Beacon', () => {
345
412
expect ( emitSpy ) . not . toHaveBeenCalled ( ) ;
346
413
} ) ;
347
414
415
+ describe ( 'when beacon is live with a start timestamp is in the future' , ( ) => {
416
+ it ( 'ignores locations before the beacon start timestamp' , ( ) => {
417
+ const startTimestamp = now + 60000 ;
418
+ const beacon = new Beacon ( makeBeaconInfoEvent (
419
+ userId ,
420
+ roomId ,
421
+ { isLive : true , timeout : 60000 , timestamp : startTimestamp } ,
422
+ ) ) ;
423
+ const emitSpy = jest . spyOn ( beacon , 'emit' ) ;
424
+
425
+ beacon . addLocations ( [
426
+ // beacon has now + 60000 live period
427
+ makeBeaconEvent (
428
+ userId ,
429
+ {
430
+ beaconInfoId : beacon . beaconInfoId ,
431
+ // now < location timestamp < beacon timestamp
432
+ timestamp : now + 10 ,
433
+ } ,
434
+ ) ,
435
+ ] ) ;
436
+
437
+ expect ( beacon . latestLocationState ) . toBeFalsy ( ) ;
438
+ expect ( emitSpy ) . not . toHaveBeenCalled ( ) ;
439
+ } ) ;
440
+ it ( 'sets latest location when location timestamp is after startTimestamp' , ( ) => {
441
+ const startTimestamp = now + 60000 ;
442
+ const beacon = new Beacon ( makeBeaconInfoEvent (
443
+ userId ,
444
+ roomId ,
445
+ { isLive : true , timeout : 600000 , timestamp : startTimestamp } ,
446
+ ) ) ;
447
+ const emitSpy = jest . spyOn ( beacon , 'emit' ) ;
448
+
449
+ beacon . addLocations ( [
450
+ // beacon has now + 600000 live period
451
+ makeBeaconEvent (
452
+ userId ,
453
+ {
454
+ beaconInfoId : beacon . beaconInfoId ,
455
+ // now < beacon timestamp < location timestamp
456
+ timestamp : startTimestamp + 10 ,
457
+ } ,
458
+ ) ,
459
+ ] ) ;
460
+
461
+ expect ( beacon . latestLocationState ) . toBeTruthy ( ) ;
462
+ expect ( emitSpy ) . toHaveBeenCalled ( ) ;
463
+ } ) ;
464
+ } ) ;
465
+
348
466
it ( 'sets latest location state to most recent location' , ( ) => {
349
467
const beacon = new Beacon ( makeBeaconInfoEvent ( userId , roomId , { isLive : true , timeout : 60000 } ) ) ;
350
468
const emitSpy = jest . spyOn ( beacon , 'emit' ) ;
0 commit comments