@@ -19,7 +19,7 @@ import {
1919 Beacon ,
2020 BeaconEvent ,
2121} from "../../../src/models/beacon" ;
22- import { makeBeaconInfoEvent } from "../../test-utils/beacon" ;
22+ import { makeBeaconEvent , makeBeaconInfoEvent } from "../../test-utils/beacon" ;
2323
2424jest . useFakeTimers ( ) ;
2525
@@ -282,5 +282,93 @@ describe('Beacon', () => {
282282 expect ( emitSpy ) . toHaveBeenCalledTimes ( 1 ) ;
283283 } ) ;
284284 } ) ;
285+
286+ describe ( 'addLocations' , ( ) => {
287+ it ( 'ignores locations when beacon is not live' , ( ) => {
288+ const beacon = new Beacon ( makeBeaconInfoEvent ( userId , roomId , { isLive : false } ) ) ;
289+ const emitSpy = jest . spyOn ( beacon , 'emit' ) ;
290+
291+ beacon . addLocations ( [
292+ makeBeaconEvent ( userId , { beaconInfoId : beacon . beaconInfoId , timestamp : now + 1 } ) ,
293+ ] ) ;
294+
295+ expect ( beacon . latestLocationState ) . toBeFalsy ( ) ;
296+ expect ( emitSpy ) . not . toHaveBeenCalled ( ) ;
297+ } ) ;
298+
299+ it ( 'ignores locations outside the beacon live duration' , ( ) => {
300+ const beacon = new Beacon ( makeBeaconInfoEvent ( userId , roomId , { isLive : true , timeout : 60000 } ) ) ;
301+ const emitSpy = jest . spyOn ( beacon , 'emit' ) ;
302+
303+ beacon . addLocations ( [
304+ // beacon has now + 60000 live period
305+ makeBeaconEvent ( userId , { beaconInfoId : beacon . beaconInfoId , timestamp : now + 100000 } ) ,
306+ ] ) ;
307+
308+ expect ( beacon . latestLocationState ) . toBeFalsy ( ) ;
309+ expect ( emitSpy ) . not . toHaveBeenCalled ( ) ;
310+ } ) ;
311+
312+ it ( 'sets latest location state to most recent location' , ( ) => {
313+ const beacon = new Beacon ( makeBeaconInfoEvent ( userId , roomId , { isLive : true , timeout : 60000 } ) ) ;
314+ const emitSpy = jest . spyOn ( beacon , 'emit' ) ;
315+
316+ const locations = [
317+ // older
318+ makeBeaconEvent (
319+ userId , { beaconInfoId : beacon . beaconInfoId , uri : 'geo:foo' , timestamp : now + 1 } ,
320+ ) ,
321+ // newer
322+ makeBeaconEvent (
323+ userId , { beaconInfoId : beacon . beaconInfoId , uri : 'geo:bar' , timestamp : now + 10000 } ,
324+ ) ,
325+ // not valid
326+ makeBeaconEvent (
327+ userId , { beaconInfoId : beacon . beaconInfoId , uri : 'geo:baz' , timestamp : now - 5 } ,
328+ ) ,
329+ ] ;
330+
331+ beacon . addLocations ( locations ) ;
332+
333+ const expectedLatestLocation = {
334+ description : undefined ,
335+ timestamp : now + 10000 ,
336+ uri : 'geo:bar' ,
337+ } ;
338+
339+ // the newest valid location
340+ expect ( beacon . latestLocationState ) . toEqual ( expectedLatestLocation ) ;
341+ expect ( emitSpy ) . toHaveBeenCalledWith ( BeaconEvent . LocationUpdate , expectedLatestLocation ) ;
342+ } ) ;
343+
344+ it ( 'ignores locations that are less recent that the current latest location' , ( ) => {
345+ const beacon = new Beacon ( makeBeaconInfoEvent ( userId , roomId , { isLive : true , timeout : 60000 } ) ) ;
346+
347+ const olderLocation = makeBeaconEvent (
348+ userId , { beaconInfoId : beacon . beaconInfoId , uri : 'geo:foo' , timestamp : now + 1 } ,
349+ ) ;
350+ const newerLocation = makeBeaconEvent (
351+ userId , { beaconInfoId : beacon . beaconInfoId , uri : 'geo:bar' , timestamp : now + 10000 } ,
352+ ) ;
353+
354+ beacon . addLocations ( [ newerLocation ] ) ;
355+ // latest location set to newerLocation
356+ expect ( beacon . latestLocationState ) . toEqual ( expect . objectContaining ( {
357+ uri : 'geo:bar' ,
358+ } ) ) ;
359+
360+ const emitSpy = jest . spyOn ( beacon , 'emit' ) . mockClear ( ) ;
361+
362+ // add older location
363+ beacon . addLocations ( [ olderLocation ] ) ;
364+
365+ // no change
366+ expect ( beacon . latestLocationState ) . toEqual ( expect . objectContaining ( {
367+ uri : 'geo:bar' ,
368+ } ) ) ;
369+ // no emit
370+ expect ( emitSpy ) . not . toHaveBeenCalled ( ) ;
371+ } ) ;
372+ } ) ;
285373 } ) ;
286374} ) ;
0 commit comments