2022-03-16 19:35:09 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
import { Room, Beacon, BeaconEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { makeBeaconContent } from "matrix-js-sdk/src/content-helpers";
|
|
|
|
import { M_BEACON, M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
|
2022-03-28 19:46:39 +03:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
import { OwnBeaconStore, OwnBeaconStoreEvent } from "../../src/stores/OwnBeaconStore";
|
2022-03-28 13:48:38 +03:00
|
|
|
import {
|
|
|
|
advanceDateAndTime,
|
|
|
|
flushPromisesWithFakeTimers,
|
|
|
|
resetAsyncStoreWithClient,
|
|
|
|
setupAsyncStoreWithClient,
|
|
|
|
} from "../test-utils";
|
|
|
|
import {
|
|
|
|
makeBeaconInfoEvent,
|
|
|
|
makeGeolocationPosition,
|
|
|
|
mockGeolocation,
|
|
|
|
watchPositionMockImplementation,
|
|
|
|
} from "../test-utils/beacon";
|
2022-03-16 19:35:09 +03:00
|
|
|
import { getMockClientWithEventEmitter } from "../test-utils/client";
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
// modern fake timers and lodash.debounce are a faff
|
|
|
|
// short circuit it
|
|
|
|
jest.mock("lodash", () => ({
|
|
|
|
debounce: jest.fn().mockImplementation(callback => callback),
|
|
|
|
}));
|
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
jest.useFakeTimers();
|
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
describe('OwnBeaconStore', () => {
|
2022-03-28 13:48:38 +03:00
|
|
|
let geolocation;
|
2022-03-16 19:35:09 +03:00
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
const HOUR_MS = 3600000;
|
|
|
|
|
|
|
|
const aliceId = '@alice:server.org';
|
|
|
|
const bobId = '@bob:server.org';
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getUserId: jest.fn().mockReturnValue(aliceId),
|
|
|
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
2022-03-18 16:38:41 +03:00
|
|
|
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: '1' }),
|
2022-03-28 13:48:38 +03:00
|
|
|
sendEvent: jest.fn().mockResolvedValue({ event_id: '1' }),
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
const room1Id = '$room1:server.org';
|
|
|
|
const room2Id = '$room2:server.org';
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
// returned by default geolocation mocks
|
|
|
|
const defaultLocation = makeGeolocationPosition({});
|
|
|
|
const defaultLocationUri = 'geo:54.001927,-8.253491;u=1';
|
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
// beacon_info events
|
|
|
|
// created 'an hour ago'
|
|
|
|
// with timeout of 3 hours
|
|
|
|
|
|
|
|
// event creation sets timestamp to Date.now()
|
|
|
|
jest.spyOn(global.Date, 'now').mockReturnValue(now - HOUR_MS);
|
2022-03-22 16:57:12 +03:00
|
|
|
const alicesRoom1BeaconInfo = makeBeaconInfoEvent(aliceId,
|
|
|
|
room1Id,
|
|
|
|
{ isLive: true },
|
|
|
|
'$alice-room1-1'
|
|
|
|
, '$alice-room1-1',
|
|
|
|
);
|
|
|
|
const alicesRoom2BeaconInfo = makeBeaconInfoEvent(aliceId,
|
|
|
|
room2Id,
|
|
|
|
{ isLive: true },
|
|
|
|
'$alice-room2-1'
|
|
|
|
, '$alice-room2-1',
|
|
|
|
);
|
|
|
|
const alicesOldRoomIdBeaconInfo = makeBeaconInfoEvent(aliceId,
|
|
|
|
room1Id,
|
|
|
|
{ isLive: false },
|
|
|
|
'$alice-room1-2'
|
|
|
|
, '$alice-room1-2',
|
|
|
|
);
|
|
|
|
const bobsRoom1BeaconInfo = makeBeaconInfoEvent(bobId,
|
|
|
|
room1Id,
|
|
|
|
{ isLive: true },
|
|
|
|
'$bob-room1-1'
|
|
|
|
, '$bob-room1-1',
|
|
|
|
);
|
|
|
|
const bobsOldRoom1BeaconInfo = makeBeaconInfoEvent(bobId,
|
|
|
|
room1Id,
|
|
|
|
{ isLive: false },
|
|
|
|
'$bob-room1-2'
|
|
|
|
, '$bob-room1-2',
|
|
|
|
);
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
// make fresh rooms every time
|
|
|
|
// as we update room state
|
|
|
|
const makeRoomsWithStateEvents = (stateEvents = []): [Room, Room] => {
|
|
|
|
const room1 = new Room(room1Id, mockClient, aliceId);
|
|
|
|
const room2 = new Room(room2Id, mockClient, aliceId);
|
|
|
|
|
|
|
|
room1.currentState.setStateEvents(stateEvents);
|
|
|
|
room2.currentState.setStateEvents(stateEvents);
|
|
|
|
mockClient.getVisibleRooms.mockReturnValue([room1, room2]);
|
|
|
|
|
|
|
|
return [room1, room2];
|
|
|
|
};
|
|
|
|
|
|
|
|
const makeOwnBeaconStore = async () => {
|
|
|
|
const store = OwnBeaconStore.instance;
|
|
|
|
|
|
|
|
await setupAsyncStoreWithClient(store, mockClient);
|
|
|
|
return store;
|
|
|
|
};
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
const expireBeaconAndEmit = (store, beaconInfoEvent: MatrixEvent): void => {
|
|
|
|
const beacon = store.getBeaconById(beaconInfoEvent.getType());
|
|
|
|
// time travel until beacon is expired
|
|
|
|
advanceDateAndTime(beacon.beaconInfo.timeout + 100);
|
|
|
|
|
|
|
|
// force an update on the beacon
|
|
|
|
// @ts-ignore
|
|
|
|
beacon.setBeaconInfo(beaconInfoEvent);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.LivenessChange, false, beacon);
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateBeaconLivenessAndEmit = (store, beaconInfoEvent: MatrixEvent, isLive: boolean): void => {
|
|
|
|
const beacon = store.getBeaconById(beaconInfoEvent.getType());
|
|
|
|
// matches original state of event content
|
|
|
|
// except for live property
|
|
|
|
const updateEvent = makeBeaconInfoEvent(
|
|
|
|
beaconInfoEvent.getSender(),
|
|
|
|
beaconInfoEvent.getRoomId(),
|
|
|
|
{ isLive, timeout: beacon.beaconInfo.timeout },
|
|
|
|
undefined,
|
|
|
|
);
|
|
|
|
updateEvent.event.type = beaconInfoEvent.getType();
|
|
|
|
beacon.update(updateEvent);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.Update, beaconInfoEvent, beacon);
|
|
|
|
mockClient.emit(BeaconEvent.LivenessChange, false, beacon);
|
|
|
|
};
|
|
|
|
|
|
|
|
const addNewBeaconAndEmit = (beaconInfoEvent: MatrixEvent): void => {
|
|
|
|
const beacon = new Beacon(beaconInfoEvent);
|
|
|
|
mockClient.emit(BeaconEvent.New, beaconInfoEvent, beacon);
|
|
|
|
};
|
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
beforeEach(() => {
|
2022-03-28 13:48:38 +03:00
|
|
|
geolocation = mockGeolocation();
|
2022-03-16 19:35:09 +03:00
|
|
|
mockClient.getVisibleRooms.mockReturnValue([]);
|
2022-03-18 16:38:41 +03:00
|
|
|
mockClient.unstable_setLiveBeacon.mockClear().mockResolvedValue({ event_id: '1' });
|
2022-03-28 13:48:38 +03:00
|
|
|
mockClient.sendEvent.mockClear().mockResolvedValue({ event_id: '1' });
|
2022-03-16 19:35:09 +03:00
|
|
|
jest.spyOn(global.Date, 'now').mockReturnValue(now);
|
|
|
|
jest.spyOn(OwnBeaconStore.instance, 'emit').mockRestore();
|
2022-03-28 19:46:39 +03:00
|
|
|
jest.spyOn(logger, 'error').mockRestore();
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await resetAsyncStoreWithClient(OwnBeaconStore.instance);
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
jest.clearAllTimers();
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('onReady()', () => {
|
|
|
|
it('initialises correctly with no beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents();
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
expect(store.getLiveBeaconIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not add other users beacons to beacon state', async () => {
|
|
|
|
makeRoomsWithStateEvents([bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
expect(store.getLiveBeaconIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adds own users beacons to state', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
expect(store.getLiveBeaconIds()).toEqual([
|
2022-03-22 16:57:12 +03:00
|
|
|
alicesRoom1BeaconInfo.getType(),
|
|
|
|
alicesRoom2BeaconInfo.getType(),
|
2022-03-16 19:35:09 +03:00
|
|
|
]);
|
|
|
|
});
|
2022-03-28 13:48:38 +03:00
|
|
|
|
|
|
|
it('does not do any geolocation when user has no live beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(geolocation.watchPosition).not.toHaveBeenCalled();
|
|
|
|
expect(mockClient.sendEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does geolocation and sends location immediatley when user has live beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
]);
|
|
|
|
await makeOwnBeaconStore();
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(geolocation.watchPosition).toHaveBeenCalled();
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
|
|
|
room1Id,
|
|
|
|
M_BEACON.name,
|
|
|
|
makeBeaconContent(defaultLocationUri, defaultLocation.timestamp, alicesRoom1BeaconInfo.getId()),
|
|
|
|
);
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
|
|
|
room2Id,
|
|
|
|
M_BEACON.name,
|
|
|
|
makeBeaconContent(defaultLocationUri, defaultLocation.timestamp, alicesRoom2BeaconInfo.getId()),
|
|
|
|
);
|
|
|
|
});
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('onNotReady()', () => {
|
|
|
|
it('removes listeners', async () => {
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const removeSpy = jest.spyOn(mockClient, 'removeListener');
|
|
|
|
// @ts-ignore
|
|
|
|
store.onNotReady();
|
|
|
|
|
|
|
|
expect(removeSpy.mock.calls[0]).toEqual(expect.arrayContaining([BeaconEvent.LivenessChange]));
|
|
|
|
expect(removeSpy.mock.calls[1]).toEqual(expect.arrayContaining([BeaconEvent.New]));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('destroys beacons', async () => {
|
|
|
|
const [room1] = makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
2022-03-22 16:57:12 +03:00
|
|
|
const beacon = room1.currentState.beacons.get(alicesRoom1BeaconInfo.getType());
|
2022-03-16 19:35:09 +03:00
|
|
|
const destroySpy = jest.spyOn(beacon, 'destroy');
|
|
|
|
// @ts-ignore
|
|
|
|
store.onNotReady();
|
|
|
|
|
|
|
|
expect(destroySpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasLiveBeacons()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true when user has live beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false when user does not have live beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns true when user has live beacons for roomId', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false when user does not have live beacons for roomId', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons(room2Id)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getLiveBeaconIds()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns live beacons when user has live beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.getLiveBeaconIds()).toEqual([
|
2022-03-22 16:57:12 +03:00
|
|
|
alicesRoom1BeaconInfo.getType(),
|
2022-03-16 19:35:09 +03:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns empty array when user does not have live beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.getLiveBeaconIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns beacon ids for room when user has live beacons for roomId', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.getLiveBeaconIds(room1Id)).toEqual([
|
2022-03-22 16:57:12 +03:00
|
|
|
alicesRoom1BeaconInfo.getType(),
|
2022-03-16 19:35:09 +03:00
|
|
|
]);
|
|
|
|
expect(store.getLiveBeaconIds(room2Id)).toEqual([
|
2022-03-22 16:57:12 +03:00
|
|
|
alicesRoom2BeaconInfo.getType(),
|
2022-03-16 19:35:09 +03:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns empty array when user does not have live beacons for roomId', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.getLiveBeaconIds(room2Id)).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('on new beacon event', () => {
|
|
|
|
it('ignores events for irrelevant beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const bobsLiveBeacon = new Beacon(bobsRoom1BeaconInfo);
|
|
|
|
const monitorSpy = jest.spyOn(bobsLiveBeacon, 'monitorLiveness');
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.New, bobsRoom1BeaconInfo, bobsLiveBeacon);
|
|
|
|
|
|
|
|
// we dont care about bob
|
|
|
|
expect(monitorSpy).not.toHaveBeenCalled();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adds users beacons to state and monitors liveness', async () => {
|
|
|
|
makeRoomsWithStateEvents([]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);
|
|
|
|
const monitorSpy = jest.spyOn(alicesLiveBeacon, 'monitorLiveness');
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);
|
|
|
|
|
|
|
|
expect(monitorSpy).toHaveBeenCalled();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits a liveness change event when new beacons change live state', async () => {
|
|
|
|
makeRoomsWithStateEvents([]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const emitSpy = jest.spyOn(store, 'emit');
|
|
|
|
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);
|
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, [alicesRoom1BeaconInfo.getType()]);
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
it('emits a liveness change event when new beacons do not change live state', async () => {
|
2022-03-16 19:35:09 +03:00
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// already live
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
const emitSpy = jest.spyOn(store, 'emit');
|
|
|
|
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);
|
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
expect(emitSpy).toHaveBeenCalled();
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('on liveness change event', () => {
|
|
|
|
it('ignores events for irrelevant beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const emitSpy = jest.spyOn(store, 'emit');
|
|
|
|
const oldLiveBeaconIds = store.getLiveBeaconIds();
|
|
|
|
const bobsLiveBeacon = new Beacon(bobsRoom1BeaconInfo);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.LivenessChange, true, bobsLiveBeacon);
|
|
|
|
|
|
|
|
expect(emitSpy).not.toHaveBeenCalled();
|
|
|
|
// strictly equal
|
|
|
|
expect(store.getLiveBeaconIds()).toBe(oldLiveBeaconIds);
|
|
|
|
});
|
|
|
|
|
2022-03-18 16:38:41 +03:00
|
|
|
it('updates state and emits beacon liveness changes from true to false', async () => {
|
2022-03-16 19:35:09 +03:00
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
// live before
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
const emitSpy = jest.spyOn(store, 'emit');
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
await expireBeaconAndEmit(store, alicesRoom1BeaconInfo);
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(false);
|
2022-03-22 16:57:12 +03:00
|
|
|
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, []);
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
|
2022-03-18 16:38:41 +03:00
|
|
|
it('stops beacon when liveness changes from true to false and beacon is expired', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
2022-03-28 13:48:38 +03:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-03-18 16:38:41 +03:00
|
|
|
const prevEventContent = alicesRoom1BeaconInfo.getContent();
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
await expireBeaconAndEmit(store, alicesRoom1BeaconInfo);
|
2022-03-18 16:38:41 +03:00
|
|
|
|
|
|
|
// matches original state of event content
|
|
|
|
// except for live property
|
|
|
|
const expectedUpdateContent = {
|
|
|
|
...prevEventContent,
|
|
|
|
[M_BEACON_INFO.name]: {
|
|
|
|
...prevEventContent[M_BEACON_INFO.name],
|
|
|
|
live: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith(
|
|
|
|
room1Id,
|
|
|
|
alicesRoom1BeaconInfo.getType(),
|
|
|
|
expectedUpdateContent,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-03-16 19:35:09 +03:00
|
|
|
it('updates state and when beacon liveness changes from false to true', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
// not live before
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
const emitSpy = jest.spyOn(store, 'emit');
|
|
|
|
|
2022-03-28 13:48:38 +03:00
|
|
|
updateBeaconLivenessAndEmit(store, alicesOldRoomIdBeaconInfo, true);
|
2022-03-16 19:35:09 +03:00
|
|
|
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(true);
|
2022-03-22 16:57:12 +03:00
|
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
|
|
|
OwnBeaconStoreEvent.LivenessChange,
|
|
|
|
[alicesOldRoomIdBeaconInfo.getType()],
|
|
|
|
);
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-18 16:38:41 +03:00
|
|
|
describe('stopBeacon()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesOldRoomIdBeaconInfo,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does nothing for an unknown beacon id', async () => {
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
await store.stopBeacon('randomBeaconId');
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does nothing for a beacon that is already not live', async () => {
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
await store.stopBeacon(alicesOldRoomIdBeaconInfo.getId());
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-03-16 19:35:09 +03:00
|
|
|
|
2022-03-18 16:38:41 +03:00
|
|
|
it('updates beacon to live:false when it is unexpired', async () => {
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
await store.stopBeacon(alicesOldRoomIdBeaconInfo.getType());
|
2022-03-18 16:38:41 +03:00
|
|
|
const prevEventContent = alicesRoom1BeaconInfo.getContent();
|
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
await store.stopBeacon(alicesRoom1BeaconInfo.getType());
|
2022-03-18 16:38:41 +03:00
|
|
|
|
|
|
|
// matches original state of event content
|
|
|
|
// except for live property
|
|
|
|
const expectedUpdateContent = {
|
|
|
|
...prevEventContent,
|
|
|
|
[M_BEACON_INFO.name]: {
|
|
|
|
...prevEventContent[M_BEACON_INFO.name],
|
|
|
|
live: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith(
|
|
|
|
room1Id,
|
|
|
|
alicesRoom1BeaconInfo.getType(),
|
|
|
|
expectedUpdateContent,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates beacon to live:false when it is expired but live property is true', async () => {
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
await store.stopBeacon(alicesOldRoomIdBeaconInfo.getType());
|
2022-03-18 16:38:41 +03:00
|
|
|
const prevEventContent = alicesRoom1BeaconInfo.getContent();
|
|
|
|
|
|
|
|
// time travel until beacon is expired
|
|
|
|
advanceDateAndTime(HOUR_MS * 3);
|
|
|
|
|
2022-03-22 16:57:12 +03:00
|
|
|
await store.stopBeacon(alicesRoom1BeaconInfo.getType());
|
2022-03-18 16:38:41 +03:00
|
|
|
|
|
|
|
// matches original state of event content
|
|
|
|
// except for live property
|
|
|
|
const expectedUpdateContent = {
|
|
|
|
...prevEventContent,
|
|
|
|
[M_BEACON_INFO.name]: {
|
|
|
|
...prevEventContent[M_BEACON_INFO.name],
|
|
|
|
live: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith(
|
|
|
|
room1Id,
|
|
|
|
alicesRoom1BeaconInfo.getType(),
|
|
|
|
expectedUpdateContent,
|
|
|
|
);
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|
|
|
|
});
|
2022-03-28 13:48:38 +03:00
|
|
|
|
|
|
|
describe('sending positions', () => {
|
|
|
|
it('stops watching position when user has no more live beacons', async () => {
|
|
|
|
// geolocation is only going to emit 1 position
|
|
|
|
geolocation.watchPosition.mockImplementation(
|
|
|
|
watchPositionMockImplementation([0]),
|
|
|
|
);
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
// two locations were published
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
// expire the beacon
|
|
|
|
// user now has no live beacons
|
|
|
|
await expireBeaconAndEmit(store, alicesRoom1BeaconInfo);
|
|
|
|
|
|
|
|
// stop watching location
|
|
|
|
expect(geolocation.clearWatch).toHaveBeenCalled();
|
2022-03-28 19:46:39 +03:00
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when store is initialised with live beacons', () => {
|
|
|
|
it('starts watching position', async () => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(geolocation.watchPosition).toHaveBeenCalled();
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('kills live beacon when geolocation is unavailable', async () => {
|
|
|
|
const errorLogSpy = jest.spyOn(logger, 'error').mockImplementation(() => { });
|
|
|
|
// remove the mock we set
|
|
|
|
// @ts-ignore
|
|
|
|
navigator.geolocation = undefined;
|
|
|
|
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
|
|
|
expect(errorLogSpy).toHaveBeenCalledWith('Geolocation failed', "Unavailable");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('kills live beacon when geolocation permissions are not granted', async () => {
|
|
|
|
// similar case to the test above
|
|
|
|
// but these errors are handled differently
|
|
|
|
// above is thrown by element, this passed to error callback by geolocation
|
|
|
|
// return only a permission denied error
|
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation(
|
|
|
|
[0], [1]),
|
|
|
|
);
|
|
|
|
|
|
|
|
const errorLogSpy = jest.spyOn(logger, 'error').mockImplementation(() => { });
|
|
|
|
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
|
|
|
expect(errorLogSpy).toHaveBeenCalledWith('Geolocation failed', "PermissionDenied");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('adding a new beacon', () => {
|
|
|
|
it('publishes position for new beacon immediately', async () => {
|
|
|
|
makeRoomsWithStateEvents([]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
addNewBeaconAndEmit(alicesRoom1BeaconInfo);
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalled();
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('kills live beacons when geolocation is unavailable', async () => {
|
|
|
|
jest.spyOn(logger, 'error').mockImplementation(() => { });
|
|
|
|
// @ts-ignore
|
|
|
|
navigator.geolocation = undefined;
|
|
|
|
makeRoomsWithStateEvents([]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
addNewBeaconAndEmit(alicesRoom1BeaconInfo);
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
// stop beacon
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalled();
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('publishes position for new beacon immediately when there were already live beacons', async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom2BeaconInfo]);
|
|
|
|
await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
addNewBeaconAndEmit(alicesRoom1BeaconInfo);
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(geolocation.getCurrentPosition).toHaveBeenCalled();
|
|
|
|
// once for original event,
|
|
|
|
// then both live beacons get current position published
|
|
|
|
// after new beacon is added
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
|
|
|
|
});
|
2022-03-28 13:48:38 +03:00
|
|
|
});
|
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
it('publishes subsequent positions', async () => {
|
|
|
|
// modern fake timers + debounce + promises are not friends
|
|
|
|
// just testing that positions are published
|
|
|
|
// not that the debounce works
|
|
|
|
|
|
|
|
geolocation.watchPosition.mockImplementation(
|
|
|
|
watchPositionMockImplementation([0, 1000, 3000]),
|
|
|
|
);
|
|
|
|
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(0);
|
2022-03-28 13:48:38 +03:00
|
|
|
await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
jest.advanceTimersByTime(5000);
|
2022-03-28 13:48:38 +03:00
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
|
2022-03-28 13:48:38 +03:00
|
|
|
});
|
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
it('stops live beacons when geolocation permissions are revoked', async () => {
|
|
|
|
jest.spyOn(logger, 'error').mockImplementation(() => { });
|
|
|
|
// return two good positions, then a permission denied error
|
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation(
|
|
|
|
[0, 1000, 3000], [0, 0, 1]),
|
|
|
|
);
|
2022-03-28 13:48:38 +03:00
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(0);
|
|
|
|
const store = await makeOwnBeaconStore();
|
2022-03-28 13:48:38 +03:00
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
jest.advanceTimersByTime(5000);
|
2022-03-28 13:48:38 +03:00
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
// first two events were sent successfully
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
|
2022-03-28 13:48:38 +03:00
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
// stop beacon
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalled();
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('keeps sharing positions when geolocation has a non fatal error', async () => {
|
|
|
|
const errorLogSpy = jest.spyOn(logger, 'error').mockImplementation(() => { });
|
|
|
|
// return good position, timeout error, good position
|
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation(
|
|
|
|
[0, 1000, 3000], [0, 3, 0]),
|
2022-03-28 13:48:38 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(0);
|
2022-03-28 19:46:39 +03:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-03-28 13:48:38 +03:00
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(5000);
|
|
|
|
|
2022-03-28 19:46:39 +03:00
|
|
|
// two good locations were sent
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
|
|
|
|
|
|
|
|
// still sharing
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).not.toHaveBeenCalled();
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(true);
|
|
|
|
expect(errorLogSpy).toHaveBeenCalledWith('Geolocation failed', 'error message');
|
2022-03-28 13:48:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('publishes last known position after 30s of inactivity', async () => {
|
|
|
|
geolocation.watchPosition.mockImplementation(
|
|
|
|
watchPositionMockImplementation([0]),
|
|
|
|
);
|
|
|
|
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
// published first location
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
advanceDateAndTime(31000);
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
// republished latest location
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not try to publish anything if there is no known position after 30s of inactivity', async () => {
|
|
|
|
// no position ever returned from geolocation
|
|
|
|
geolocation.watchPosition.mockImplementation(
|
|
|
|
watchPositionMockImplementation([]),
|
|
|
|
);
|
|
|
|
geolocation.getCurrentPosition.mockImplementation(
|
|
|
|
watchPositionMockImplementation([]),
|
|
|
|
);
|
|
|
|
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
advanceDateAndTime(31000);
|
|
|
|
|
|
|
|
// no locations published
|
|
|
|
expect(mockClient.sendEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-03-16 19:35:09 +03:00
|
|
|
});
|