2022-03-16 16:35:09 +00: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-29 16:18:34 +00:00
|
|
|
import {
|
|
|
|
Room,
|
|
|
|
Beacon,
|
|
|
|
BeaconEvent,
|
2022-04-08 08:53:06 +00:00
|
|
|
getBeaconInfoIdentifier,
|
2022-03-29 16:18:34 +00:00
|
|
|
MatrixEvent,
|
|
|
|
RoomStateEvent,
|
|
|
|
RoomMember,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-04-22 12:05:36 +00:00
|
|
|
import { makeBeaconContent, makeBeaconInfoContent } from "matrix-js-sdk/src/content-helpers";
|
2022-04-08 08:53:06 +00:00
|
|
|
import { M_BEACON } from "matrix-js-sdk/src/@types/beacon";
|
2022-03-28 16:46:39 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-02-13 11:39:16 +00:00
|
|
|
import { Mocked } from "jest-mock";
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
import { OwnBeaconStore, OwnBeaconStoreEvent } from "../../src/stores/OwnBeaconStore";
|
2022-03-28 10:48:38 +00:00
|
|
|
import {
|
|
|
|
advanceDateAndTime,
|
|
|
|
flushPromisesWithFakeTimers,
|
2022-03-29 16:18:34 +00:00
|
|
|
makeMembershipEvent,
|
2022-03-28 10:48:38 +00:00
|
|
|
resetAsyncStoreWithClient,
|
|
|
|
setupAsyncStoreWithClient,
|
|
|
|
} from "../test-utils";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { makeBeaconInfoEvent, mockGeolocation, watchPositionMockImplementation } from "../test-utils/beacon";
|
2022-03-16 16:35:09 +00:00
|
|
|
import { getMockClientWithEventEmitter } from "../test-utils/client";
|
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
// modern fake timers and lodash.debounce are a faff
|
|
|
|
// short circuit it
|
|
|
|
jest.mock("lodash", () => ({
|
2022-12-12 11:24:14 +00:00
|
|
|
...(jest.requireActual("lodash") as object),
|
|
|
|
debounce: jest.fn().mockImplementation((callback) => callback),
|
2022-03-28 10:48:38 +00:00
|
|
|
}));
|
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
jest.useFakeTimers();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("OwnBeaconStore", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
let geolocation: Mocked<Geolocation>;
|
2022-03-16 16:35:09 +00:00
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
const HOUR_MS = 3600000;
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const aliceId = "@alice:server.org";
|
|
|
|
const bobId = "@bob:server.org";
|
2022-03-16 16:35:09 +00:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getUserId: jest.fn().mockReturnValue(aliceId),
|
|
|
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
2022-12-12 11:24:14 +00:00
|
|
|
unstable_setLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
|
|
|
|
sendEvent: jest.fn().mockResolvedValue({ event_id: "1" }),
|
|
|
|
unstable_createLiveBeacon: jest.fn().mockResolvedValue({ event_id: "1" }),
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
const room1Id = "$room1:server.org";
|
|
|
|
const room2Id = "$room2:server.org";
|
2022-03-16 16:35:09 +00:00
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
// returned by default geolocation mocks
|
2022-12-12 11:24:14 +00:00
|
|
|
const defaultLocationUri = "geo:54.001927,-8.253491;u=1";
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
// beacon_info events
|
|
|
|
// created 'an hour ago'
|
|
|
|
// with timeout of 3 hours
|
|
|
|
|
|
|
|
// event creation sets timestamp to Date.now()
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(now - HOUR_MS);
|
|
|
|
const alicesRoom1BeaconInfo = makeBeaconInfoEvent(aliceId, room1Id, { isLive: true }, "$alice-room1-1");
|
|
|
|
const alicesRoom2BeaconInfo = makeBeaconInfoEvent(aliceId, room2Id, { isLive: true }, "$alice-room2-1");
|
|
|
|
const alicesOldRoomIdBeaconInfo = makeBeaconInfoEvent(aliceId, room1Id, { isLive: false }, "$alice-room1-2");
|
|
|
|
const bobsRoom1BeaconInfo = makeBeaconInfoEvent(bobId, room1Id, { isLive: true }, "$bob-room1-1");
|
|
|
|
const bobsOldRoom1BeaconInfo = makeBeaconInfoEvent(bobId, room1Id, { isLive: false }, "$bob-room1-2");
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
// make fresh rooms every time
|
|
|
|
// as we update room state
|
2023-02-13 11:39:16 +00:00
|
|
|
const makeRoomsWithStateEvents = (stateEvents: MatrixEvent[] = []): [Room, Room] => {
|
2022-03-16 16:35:09 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
const expireBeaconAndEmit = (store: OwnBeaconStore, beaconInfoEvent: MatrixEvent): void => {
|
2023-02-16 17:21:44 +00:00
|
|
|
const beacon = store.getBeaconById(getBeaconInfoIdentifier(beaconInfoEvent))!;
|
2022-03-28 10:48:38 +00:00
|
|
|
// time travel until beacon is expired
|
2023-02-16 17:21:44 +00:00
|
|
|
advanceDateAndTime(beacon.beaconInfo!.timeout + 100);
|
2022-03-28 10:48:38 +00:00
|
|
|
|
|
|
|
// force an update on the beacon
|
|
|
|
// @ts-ignore
|
|
|
|
beacon.setBeaconInfo(beaconInfoEvent);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.LivenessChange, false, beacon);
|
|
|
|
};
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
const updateBeaconLivenessAndEmit = (
|
|
|
|
store: OwnBeaconStore,
|
|
|
|
beaconInfoEvent: MatrixEvent,
|
|
|
|
isLive: boolean,
|
|
|
|
): void => {
|
2023-02-16 17:21:44 +00:00
|
|
|
const beacon = store.getBeaconById(getBeaconInfoIdentifier(beaconInfoEvent))!;
|
2022-03-28 10:48:38 +00:00
|
|
|
// matches original state of event content
|
|
|
|
// except for live property
|
|
|
|
const updateEvent = makeBeaconInfoEvent(
|
2023-02-16 17:21:44 +00:00
|
|
|
beaconInfoEvent.getSender()!,
|
|
|
|
beaconInfoEvent.getRoomId()!,
|
|
|
|
{ isLive, timeout: beacon.beaconInfo!.timeout },
|
2022-12-12 11:24:14 +00:00
|
|
|
"update-event-id",
|
2022-03-28 10:48:38 +00:00
|
|
|
);
|
|
|
|
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-12-12 11:24:14 +00:00
|
|
|
const localStorageGetSpy = jest.spyOn(localStorage.__proto__, "getItem").mockReturnValue(undefined);
|
|
|
|
const localStorageSetSpy = jest.spyOn(localStorage.__proto__, "setItem").mockImplementation(() => {});
|
2022-04-22 12:05:36 +00:00
|
|
|
|
2022-03-16 16:35:09 +00:00
|
|
|
beforeEach(() => {
|
2022-03-28 10:48:38 +00:00
|
|
|
geolocation = mockGeolocation();
|
2022-03-16 16:35:09 +00:00
|
|
|
mockClient.getVisibleRooms.mockReturnValue([]);
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.unstable_setLiveBeacon.mockClear().mockResolvedValue({ event_id: "1" });
|
|
|
|
mockClient.sendEvent.mockReset().mockResolvedValue({ event_id: "1" });
|
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(now);
|
|
|
|
jest.spyOn(OwnBeaconStore.instance, "emit").mockRestore();
|
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-04-22 12:05:36 +00:00
|
|
|
|
|
|
|
localStorageGetSpy.mockClear().mockReturnValue(undefined);
|
|
|
|
localStorageSetSpy.mockClear();
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await resetAsyncStoreWithClient(OwnBeaconStore.instance);
|
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
jest.clearAllTimers();
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-04-22 12:05:36 +00:00
|
|
|
afterAll(() => {
|
|
|
|
localStorageGetSpy.mockRestore();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("onReady()", () => {
|
|
|
|
it("initialises correctly with no beacons", async () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
makeRoomsWithStateEvents();
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
expect(store.getLiveBeaconIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not add other users beacons to beacon state", async () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
makeRoomsWithStateEvents([bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
expect(store.getLiveBeaconIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("adds own users beacons to state", async () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.beaconsByRoomId.get(room1Id)).toEqual(
|
|
|
|
new Set([getBeaconInfoIdentifier(alicesRoom1BeaconInfo)]),
|
|
|
|
);
|
|
|
|
expect(store.beaconsByRoomId.get(room2Id)).toEqual(
|
|
|
|
new Set([getBeaconInfoIdentifier(alicesRoom2BeaconInfo)]),
|
|
|
|
);
|
2022-04-22 12:05:36 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates live beacon ids when users own beacons were created on device", async () => {
|
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([alicesRoom1BeaconInfo.getId(), alicesRoom2BeaconInfo.getId()]),
|
|
|
|
);
|
2022-04-22 12:05:36 +00:00
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBeTruthy();
|
2022-03-16 16:35:09 +00:00
|
|
|
expect(store.getLiveBeaconIds()).toEqual([
|
2022-04-08 08:53:06 +00:00
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
|
|
|
getBeaconInfoIdentifier(alicesRoom2BeaconInfo),
|
2022-03-16 16:35:09 +00:00
|
|
|
]);
|
|
|
|
});
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not do any geolocation when user has no live beacons", async () => {
|
2022-03-28 10:48:38 +00:00
|
|
|
makeRoomsWithStateEvents([bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(geolocation.watchPosition).not.toHaveBeenCalled();
|
|
|
|
expect(mockClient.sendEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does geolocation and sends location immediately when user has live beacons", async () => {
|
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([alicesRoom1BeaconInfo.getId(), alicesRoom2BeaconInfo.getId()]),
|
|
|
|
);
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo, alicesRoom2BeaconInfo]);
|
2022-03-28 10:48:38 +00:00
|
|
|
await makeOwnBeaconStore();
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(geolocation.watchPosition).toHaveBeenCalled();
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
|
|
|
room1Id,
|
|
|
|
M_BEACON.name,
|
2023-02-16 17:21:44 +00:00
|
|
|
makeBeaconContent(defaultLocationUri, now, alicesRoom1BeaconInfo.getId()!),
|
2022-03-28 10:48:38 +00:00
|
|
|
);
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
|
|
|
room2Id,
|
|
|
|
M_BEACON.name,
|
2023-02-16 17:21:44 +00:00
|
|
|
makeBeaconContent(defaultLocationUri, now, alicesRoom2BeaconInfo.getId()!),
|
2022-03-28 10:48:38 +00:00
|
|
|
);
|
|
|
|
});
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("onNotReady()", () => {
|
|
|
|
it("removes listeners", async () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const removeSpy = jest.spyOn(mockClient, "removeListener");
|
2022-03-16 16:35:09 +00:00
|
|
|
// @ts-ignore
|
|
|
|
store.onNotReady();
|
|
|
|
|
|
|
|
expect(removeSpy.mock.calls[0]).toEqual(expect.arrayContaining([BeaconEvent.LivenessChange]));
|
|
|
|
expect(removeSpy.mock.calls[1]).toEqual(expect.arrayContaining([BeaconEvent.New]));
|
2022-04-08 08:53:06 +00:00
|
|
|
expect(removeSpy.mock.calls[2]).toEqual(expect.arrayContaining([BeaconEvent.Update]));
|
2022-04-22 12:05:36 +00:00
|
|
|
expect(removeSpy.mock.calls[3]).toEqual(expect.arrayContaining([BeaconEvent.Destroy]));
|
|
|
|
expect(removeSpy.mock.calls[4]).toEqual(expect.arrayContaining([RoomStateEvent.Members]));
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("destroys beacons", async () => {
|
|
|
|
const [room1] = makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2023-02-16 17:21:44 +00:00
|
|
|
const beacon = room1.currentState.beacons.get(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))!;
|
2022-12-12 11:24:14 +00:00
|
|
|
const destroySpy = jest.spyOn(beacon, "destroy");
|
2022-03-16 16:35:09 +00:00
|
|
|
// @ts-ignore
|
|
|
|
store.onNotReady();
|
|
|
|
|
|
|
|
expect(destroySpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("hasLiveBeacons()", () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
2022-12-12 11:24:14 +00:00
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([alicesRoom1BeaconInfo.getId(), alicesRoom2BeaconInfo.getId()]),
|
|
|
|
);
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns true when user has live beacons", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo, bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns false when user does not have live beacons", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesOldRoomIdBeaconInfo, bobsOldRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns true when user has live beacons for roomId", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo, bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns false when user does not have live beacons for roomId", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo, bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.hasLiveBeacons(room2Id)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("getLiveBeaconIds()", () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
2022-12-12 11:24:14 +00:00
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([alicesRoom1BeaconInfo.getId(), alicesRoom2BeaconInfo.getId()]),
|
|
|
|
);
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns live beacons when user has live beacons", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo, bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.getLiveBeaconIds()).toEqual([getBeaconInfoIdentifier(alicesRoom1BeaconInfo)]);
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns empty array when user does not have live beacons", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesOldRoomIdBeaconInfo, bobsOldRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.getLiveBeaconIds()).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns beacon ids for room when user has live beacons for roomId", async () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
makeRoomsWithStateEvents([
|
|
|
|
alicesRoom1BeaconInfo,
|
|
|
|
alicesRoom2BeaconInfo,
|
|
|
|
bobsRoom1BeaconInfo,
|
|
|
|
bobsOldRoom1BeaconInfo,
|
|
|
|
]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.getLiveBeaconIds(room1Id)).toEqual([getBeaconInfoIdentifier(alicesRoom1BeaconInfo)]);
|
|
|
|
expect(store.getLiveBeaconIds(room2Id)).toEqual([getBeaconInfoIdentifier(alicesRoom2BeaconInfo)]);
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns empty array when user does not have live beacons for roomId", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo, bobsRoom1BeaconInfo, bobsOldRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
expect(store.getLiveBeaconIds(room2Id)).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("on new beacon event", () => {
|
2022-04-22 12:05:36 +00:00
|
|
|
// assume all beacons were created on this device
|
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([alicesRoom1BeaconInfo.getId(), alicesRoom2BeaconInfo.getId()]),
|
|
|
|
);
|
2022-04-22 12:05:36 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ignores events for irrelevant beacons", async () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
makeRoomsWithStateEvents([]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const bobsLiveBeacon = new Beacon(bobsRoom1BeaconInfo);
|
2022-12-12 11:24:14 +00:00
|
|
|
const monitorSpy = jest.spyOn(bobsLiveBeacon, "monitorLiveness");
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.New, bobsRoom1BeaconInfo, bobsLiveBeacon);
|
|
|
|
|
|
|
|
// we dont care about bob
|
|
|
|
expect(monitorSpy).not.toHaveBeenCalled();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("adds users beacons to state and monitors liveness", async () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
makeRoomsWithStateEvents([]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);
|
2022-12-12 11:24:14 +00:00
|
|
|
const monitorSpy = jest.spyOn(alicesLiveBeacon, "monitorLiveness");
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);
|
|
|
|
|
|
|
|
expect(monitorSpy).toHaveBeenCalled();
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("emits a liveness change event when new beacons change live state", async () => {
|
2022-03-16 16:35:09 +00:00
|
|
|
makeRoomsWithStateEvents([]);
|
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-16 16:35:09 +00:00
|
|
|
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);
|
|
|
|
|
2022-04-08 08:53:06 +00:00
|
|
|
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, [alicesLiveBeacon.identifier]);
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("emits a liveness change event when new beacons do not change live state", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom2BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// already live
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-16 16:35:09 +00:00
|
|
|
const alicesLiveBeacon = new Beacon(alicesRoom1BeaconInfo);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.New, alicesRoom1BeaconInfo, alicesLiveBeacon);
|
|
|
|
|
2022-03-22 13:57:12 +00:00
|
|
|
expect(emitSpy).toHaveBeenCalled();
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("on liveness change event", () => {
|
2022-04-22 12:05:36 +00:00
|
|
|
// assume all beacons were created on this device
|
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([
|
|
|
|
alicesRoom1BeaconInfo.getId(),
|
|
|
|
alicesRoom2BeaconInfo.getId(),
|
|
|
|
alicesOldRoomIdBeaconInfo.getId(),
|
|
|
|
"update-event-id",
|
|
|
|
]),
|
|
|
|
);
|
2022-04-22 12:05:36 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ignores events for irrelevant beacons", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-16 16:35:09 +00:00
|
|
|
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-12-12 11:24:14 +00:00
|
|
|
it("updates state and emits beacon liveness changes from true to false", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
// live before
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-16 16:35:09 +00:00
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
await expireBeaconAndEmit(store, alicesRoom1BeaconInfo);
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(false);
|
2022-03-22 13:57:12 +00:00
|
|
|
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, []);
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("stops beacon when liveness changes from true to false and beacon is expired", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 10:48:38 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-03-18 13:38:41 +00:00
|
|
|
const prevEventContent = alicesRoom1BeaconInfo.getContent();
|
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
await expireBeaconAndEmit(store, alicesRoom1BeaconInfo);
|
2022-03-18 13:38:41 +00:00
|
|
|
|
|
|
|
// matches original state of event content
|
|
|
|
// except for live property
|
|
|
|
const expectedUpdateContent = {
|
|
|
|
...prevEventContent,
|
2022-04-08 08:53:06 +00:00
|
|
|
live: false,
|
2022-03-18 13:38:41 +00:00
|
|
|
};
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith(room1Id, expectedUpdateContent);
|
2022-03-18 13:38:41 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates state and when beacon liveness changes from false to true", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesOldRoomIdBeaconInfo]);
|
2022-03-16 16:35:09 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
// not live before
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-16 16:35:09 +00:00
|
|
|
|
2022-03-28 10:48:38 +00:00
|
|
|
updateBeaconLivenessAndEmit(store, alicesOldRoomIdBeaconInfo, true);
|
2022-03-16 16:35:09 +00:00
|
|
|
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(true);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, [
|
|
|
|
getBeaconInfoIdentifier(alicesOldRoomIdBeaconInfo),
|
|
|
|
]);
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("on room membership changes", () => {
|
2022-04-22 12:05:36 +00:00
|
|
|
// assume all beacons were created on this device
|
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([alicesRoom1BeaconInfo.getId(), alicesRoom2BeaconInfo.getId()]),
|
|
|
|
);
|
2022-04-22 12:05:36 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ignores events for rooms without beacons", async () => {
|
2022-03-29 16:18:34 +00:00
|
|
|
const membershipEvent = makeMembershipEvent(room2Id, aliceId);
|
|
|
|
// no beacons for room2
|
2022-12-12 11:24:14 +00:00
|
|
|
const [, room2] = makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-29 16:18:34 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-29 16:18:34 +00:00
|
|
|
const oldLiveBeaconIds = store.getLiveBeaconIds();
|
|
|
|
|
|
|
|
mockClient.emit(
|
|
|
|
RoomStateEvent.Members,
|
|
|
|
membershipEvent,
|
|
|
|
room2.currentState,
|
|
|
|
new RoomMember(room2Id, aliceId),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(emitSpy).not.toHaveBeenCalled();
|
|
|
|
// strictly equal
|
|
|
|
expect(store.getLiveBeaconIds()).toBe(oldLiveBeaconIds);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ignores events for membership changes that are not current user", async () => {
|
2022-03-29 16:18:34 +00:00
|
|
|
// bob joins room1
|
|
|
|
const membershipEvent = makeMembershipEvent(room1Id, bobId);
|
|
|
|
const member = new RoomMember(room1Id, bobId);
|
|
|
|
member.setMembershipEvent(membershipEvent);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const [room1] = makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-29 16:18:34 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-29 16:18:34 +00:00
|
|
|
const oldLiveBeaconIds = store.getLiveBeaconIds();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.emit(RoomStateEvent.Members, membershipEvent, room1.currentState, member);
|
2022-03-29 16:18:34 +00:00
|
|
|
|
|
|
|
expect(emitSpy).not.toHaveBeenCalled();
|
|
|
|
// strictly equal
|
|
|
|
expect(store.getLiveBeaconIds()).toBe(oldLiveBeaconIds);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ignores events for membership changes that are not leave/ban", async () => {
|
2022-03-29 16:18:34 +00:00
|
|
|
// alice joins room1
|
|
|
|
const membershipEvent = makeMembershipEvent(room1Id, aliceId);
|
|
|
|
const member = new RoomMember(room1Id, aliceId);
|
|
|
|
member.setMembershipEvent(membershipEvent);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const [room1] = makeRoomsWithStateEvents([alicesRoom1BeaconInfo, alicesRoom2BeaconInfo]);
|
2022-03-29 16:18:34 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-29 16:18:34 +00:00
|
|
|
const oldLiveBeaconIds = store.getLiveBeaconIds();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.emit(RoomStateEvent.Members, membershipEvent, room1.currentState, member);
|
2022-03-29 16:18:34 +00:00
|
|
|
|
|
|
|
expect(emitSpy).not.toHaveBeenCalled();
|
|
|
|
// strictly equal
|
|
|
|
expect(store.getLiveBeaconIds()).toBe(oldLiveBeaconIds);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("destroys and removes beacons when current user leaves room", async () => {
|
2022-03-29 16:18:34 +00:00
|
|
|
// alice leaves room1
|
2022-12-12 11:24:14 +00:00
|
|
|
const membershipEvent = makeMembershipEvent(room1Id, aliceId, "leave");
|
2022-03-29 16:18:34 +00:00
|
|
|
const member = new RoomMember(room1Id, aliceId);
|
|
|
|
member.setMembershipEvent(membershipEvent);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const [room1] = makeRoomsWithStateEvents([alicesRoom1BeaconInfo, alicesRoom2BeaconInfo]);
|
2022-03-29 16:18:34 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2023-02-16 17:21:44 +00:00
|
|
|
const room1BeaconInstance = store.beacons.get(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))!;
|
2022-12-12 11:24:14 +00:00
|
|
|
const beaconDestroySpy = jest.spyOn(room1BeaconInstance, "destroy");
|
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-29 16:18:34 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.emit(RoomStateEvent.Members, membershipEvent, room1.currentState, member);
|
2022-03-29 16:18:34 +00:00
|
|
|
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
|
|
|
OwnBeaconStoreEvent.LivenessChange,
|
|
|
|
// other rooms beacons still live
|
2022-04-08 08:53:06 +00:00
|
|
|
[getBeaconInfoIdentifier(alicesRoom2BeaconInfo)],
|
2022-03-29 16:18:34 +00:00
|
|
|
);
|
|
|
|
expect(beaconDestroySpy).toHaveBeenCalledTimes(1);
|
|
|
|
expect(store.getLiveBeaconIds(room1Id)).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("on destroy event", () => {
|
2022-04-22 12:05:36 +00:00
|
|
|
// assume all beacons were created on this device
|
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([
|
|
|
|
alicesRoom1BeaconInfo.getId(),
|
|
|
|
alicesRoom2BeaconInfo.getId(),
|
|
|
|
alicesOldRoomIdBeaconInfo.getId(),
|
|
|
|
"update-event-id",
|
|
|
|
]),
|
|
|
|
);
|
2022-04-22 12:05:36 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ignores events for irrelevant beacons", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-04-22 12:05:36 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-04-22 12:05:36 +00:00
|
|
|
const oldLiveBeaconIds = store.getLiveBeaconIds();
|
|
|
|
const bobsLiveBeacon = new Beacon(bobsRoom1BeaconInfo);
|
|
|
|
|
|
|
|
mockClient.emit(BeaconEvent.Destroy, bobsLiveBeacon.identifier);
|
|
|
|
|
|
|
|
expect(emitSpy).not.toHaveBeenCalled();
|
|
|
|
// strictly equal
|
|
|
|
expect(store.getLiveBeaconIds()).toBe(oldLiveBeaconIds);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates state and emits beacon liveness changes from true to false", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-04-22 12:05:36 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
// live before
|
|
|
|
expect(store.hasLiveBeacons()).toBe(true);
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-04-22 12:05:36 +00:00
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
const beacon = store.getBeaconById(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))!;
|
2022-04-22 12:05:36 +00:00
|
|
|
|
|
|
|
beacon.destroy();
|
|
|
|
mockClient.emit(BeaconEvent.Destroy, beacon.identifier);
|
|
|
|
|
|
|
|
expect(store.hasLiveBeacons()).toBe(false);
|
|
|
|
expect(store.hasLiveBeacons(room1Id)).toBe(false);
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(OwnBeaconStoreEvent.LivenessChange, []);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("stopBeacon()", () => {
|
2022-03-18 13:38:41 +00:00
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo, alicesOldRoomIdBeaconInfo]);
|
2022-03-18 13:38:41 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does nothing for an unknown beacon id", async () => {
|
2022-03-18 13:38:41 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
await store.stopBeacon("randomBeaconId");
|
2022-03-18 13:38:41 +00:00
|
|
|
expect(mockClient.unstable_setLiveBeacon).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does nothing for a beacon that is already not live", async () => {
|
2022-03-18 13:38:41 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-04-08 08:53:06 +00:00
|
|
|
await store.stopBeacon(getBeaconInfoIdentifier(alicesOldRoomIdBeaconInfo));
|
2022-03-18 13:38:41 +00:00
|
|
|
expect(mockClient.unstable_setLiveBeacon).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-03-16 16:35:09 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates beacon to live:false when it is unexpired", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-18 13:38:41 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
const prevEventContent = alicesRoom1BeaconInfo.getContent();
|
|
|
|
|
2022-04-08 08:53:06 +00:00
|
|
|
await store.stopBeacon(getBeaconInfoIdentifier(alicesRoom1BeaconInfo));
|
2022-03-18 13:38:41 +00:00
|
|
|
|
|
|
|
// matches original state of event content
|
|
|
|
// except for live property
|
|
|
|
const expectedUpdateContent = {
|
|
|
|
...prevEventContent,
|
2022-04-08 08:53:06 +00:00
|
|
|
live: false,
|
2022-03-18 13:38:41 +00:00
|
|
|
};
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith(room1Id, expectedUpdateContent);
|
2022-03-18 13:38:41 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("records error when stopping beacon event fails to send", async () => {
|
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-04-28 12:03:51 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
|
|
|
const error = new Error("oups");
|
2022-04-28 12:03:51 +00:00
|
|
|
mockClient.unstable_setLiveBeacon.mockRejectedValue(error);
|
|
|
|
|
|
|
|
await expect(store.stopBeacon(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).rejects.toEqual(error);
|
|
|
|
|
|
|
|
expect(store.beaconUpdateErrors.get(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toEqual(error);
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
OwnBeaconStoreEvent.BeaconUpdateError,
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
|
|
|
true,
|
2022-04-28 12:03:51 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("clears previous error and emits when stopping beacon works on retry", async () => {
|
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-04-28 12:03:51 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
|
|
|
const error = new Error("oups");
|
2022-04-28 12:03:51 +00:00
|
|
|
mockClient.unstable_setLiveBeacon.mockRejectedValueOnce(error);
|
|
|
|
|
|
|
|
await expect(store.stopBeacon(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).rejects.toEqual(error);
|
|
|
|
expect(store.beaconUpdateErrors.get(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toEqual(error);
|
|
|
|
|
|
|
|
await store.stopBeacon(getBeaconInfoIdentifier(alicesRoom1BeaconInfo));
|
|
|
|
|
|
|
|
// error cleared
|
|
|
|
expect(store.beaconUpdateErrors.get(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toBeFalsy();
|
|
|
|
|
|
|
|
// emit called for error clearing
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
OwnBeaconStoreEvent.BeaconUpdateError,
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
|
|
|
false,
|
2022-04-28 12:03:51 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not emit BeaconUpdateError when stopping succeeds and beacon did not have errors", async () => {
|
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-04-28 12:03:51 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-04-28 12:03:51 +00:00
|
|
|
// error cleared
|
|
|
|
expect(store.beaconUpdateErrors.get(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toBeFalsy();
|
|
|
|
|
|
|
|
// emit called for error clearing
|
|
|
|
expect(emitSpy).not.toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
OwnBeaconStoreEvent.BeaconUpdateError,
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
|
|
|
false,
|
2022-04-28 12:03:51 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates beacon to live:false when it is expired but live property is true", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-18 13:38:41 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
const prevEventContent = alicesRoom1BeaconInfo.getContent();
|
|
|
|
|
|
|
|
// time travel until beacon is expired
|
|
|
|
advanceDateAndTime(HOUR_MS * 3);
|
|
|
|
|
2022-04-08 08:53:06 +00:00
|
|
|
await store.stopBeacon(getBeaconInfoIdentifier(alicesRoom1BeaconInfo));
|
2022-03-18 13:38:41 +00:00
|
|
|
|
|
|
|
// matches original state of event content
|
|
|
|
// except for live property
|
|
|
|
const expectedUpdateContent = {
|
|
|
|
...prevEventContent,
|
2022-04-08 08:53:06 +00:00
|
|
|
live: false,
|
2022-03-18 13:38:41 +00:00
|
|
|
};
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith(room1Id, expectedUpdateContent);
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
2022-04-22 12:05:36 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("removes beacon event id from local store", async () => {
|
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([alicesRoom1BeaconInfo.getId(), alicesRoom2BeaconInfo.getId()]),
|
|
|
|
);
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-04-22 12:05:36 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
await store.stopBeacon(getBeaconInfoIdentifier(alicesRoom1BeaconInfo));
|
|
|
|
|
|
|
|
expect(localStorageSetSpy).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
"mx_live_beacon_created_id",
|
2022-04-22 12:05:36 +00:00
|
|
|
// stopped beacon's event_id was removed
|
|
|
|
JSON.stringify([alicesRoom2BeaconInfo.getId()]),
|
|
|
|
);
|
|
|
|
});
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("publishing positions", () => {
|
2022-04-22 12:05:36 +00:00
|
|
|
// assume all beacons were created on this device
|
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
localStorageGetSpy.mockReturnValue(
|
|
|
|
JSON.stringify([
|
|
|
|
alicesRoom1BeaconInfo.getId(),
|
|
|
|
alicesRoom2BeaconInfo.getId(),
|
|
|
|
alicesOldRoomIdBeaconInfo.getId(),
|
|
|
|
"update-event-id",
|
|
|
|
]),
|
|
|
|
);
|
2022-04-22 12:05:36 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("stops watching position when user has no more live beacons", async () => {
|
2022-03-28 10:48:38 +00:00
|
|
|
// geolocation is only going to emit 1 position
|
2022-12-12 11:24:14 +00:00
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation([0]));
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 10:48:38 +00:00
|
|
|
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 16:46:39 +00:00
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("when store is initialised with live beacons", () => {
|
|
|
|
it("starts watching position", async () => {
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 16:46:39 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(geolocation.watchPosition).toHaveBeenCalled();
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(true);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("kills live beacon when geolocation is unavailable", async () => {
|
|
|
|
const errorLogSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-28 16:46:39 +00:00
|
|
|
// remove the mock we set
|
|
|
|
// @ts-ignore
|
|
|
|
navigator.geolocation = undefined;
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 16:46:39 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(errorLogSpy).toHaveBeenCalledWith("Geolocation failed", "Unavailable");
|
2022-03-28 16:46:39 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("kills live beacon when geolocation permissions are not granted", async () => {
|
2022-03-28 16:46:39 +00:00
|
|
|
// 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
|
2022-12-12 11:24:14 +00:00
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation([0], [1]));
|
2022-03-28 16:46:39 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const errorLogSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-28 16:46:39 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 16:46:39 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(errorLogSpy).toHaveBeenCalledWith("Geolocation failed", "PermissionDenied");
|
2022-03-28 16:46:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("adding a new beacon", () => {
|
|
|
|
it("publishes position for new beacon immediately", async () => {
|
2022-03-28 16:46:39 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("kills live beacons when geolocation is unavailable", async () => {
|
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-28 16:46:39 +00:00
|
|
|
// @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);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("publishes position for new beacon immediately when there were already live beacons", async () => {
|
2022-03-28 16:46:39 +00:00
|
|
|
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 10:48:38 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("when publishing position fails", () => {
|
2022-03-30 14:01:44 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
geolocation.watchPosition.mockImplementation(
|
|
|
|
watchPositionMockImplementation([0, 1000, 3000, 3000, 3000]),
|
|
|
|
);
|
|
|
|
|
|
|
|
// eat expected console error logs
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-30 14:01:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// we need to advance time and then flush promises
|
|
|
|
// individually for each call to sendEvent
|
|
|
|
// otherwise the sendEvent doesn't reject/resolve and update state
|
|
|
|
// before the next call
|
|
|
|
// advance and flush every 1000ms
|
|
|
|
// until given ms is 'elapsed'
|
|
|
|
const advanceAndFlushPromises = async (timeMs: number) => {
|
|
|
|
while (timeMs > 0) {
|
|
|
|
jest.advanceTimersByTime(1000);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
timeMs -= 1000;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("continues publishing positions after one publish error", async () => {
|
2022-03-30 14:01:44 +00:00
|
|
|
// fail to send first event, then succeed
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.sendEvent.mockRejectedValueOnce(new Error("oups")).mockResolvedValue({ event_id: "1" });
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-30 14:01:44 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
await advanceAndFlushPromises(50000);
|
|
|
|
|
|
|
|
// called for each position from watchPosition
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(5);
|
2022-04-25 12:44:18 +00:00
|
|
|
expect(store.beaconHasLocationPublishError(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toBe(false);
|
|
|
|
expect(store.getLiveBeaconIdsWithLocationPublishError()).toEqual([]);
|
|
|
|
expect(store.hasLocationPublishErrors()).toBe(false);
|
2022-03-30 14:01:44 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("continues publishing positions when a beacon fails intermittently", async () => {
|
2022-03-30 14:01:44 +00:00
|
|
|
// every second event rejects
|
|
|
|
// meaning this beacon has more errors than the threshold
|
|
|
|
// but they are not consecutive
|
|
|
|
mockClient.sendEvent
|
2022-12-12 11:24:14 +00:00
|
|
|
.mockRejectedValueOnce(new Error("oups"))
|
|
|
|
.mockResolvedValueOnce({ event_id: "1" })
|
|
|
|
.mockRejectedValueOnce(new Error("oups"))
|
|
|
|
.mockResolvedValueOnce({ event_id: "1" })
|
|
|
|
.mockRejectedValueOnce(new Error("oups"));
|
|
|
|
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-30 14:01:44 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-30 14:01:44 +00:00
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
await advanceAndFlushPromises(50000);
|
|
|
|
|
|
|
|
// called for each position from watchPosition
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(5);
|
2022-04-25 12:44:18 +00:00
|
|
|
expect(store.beaconHasLocationPublishError(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toBe(false);
|
|
|
|
expect(store.hasLocationPublishErrors()).toBe(false);
|
2022-03-30 14:01:44 +00:00
|
|
|
expect(emitSpy).not.toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
OwnBeaconStoreEvent.LocationPublishError,
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
2022-03-30 14:01:44 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("stops publishing positions when a beacon fails consistently", async () => {
|
2022-03-30 14:01:44 +00:00
|
|
|
// always fails to send events
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.sendEvent.mockRejectedValue(new Error("oups"));
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-30 14:01:44 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-30 14:01:44 +00:00
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
// 5 positions from watchPosition in this period
|
|
|
|
await advanceAndFlushPromises(50000);
|
|
|
|
|
|
|
|
// only two allowed failures
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
|
2022-04-25 12:44:18 +00:00
|
|
|
expect(store.beaconHasLocationPublishError(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toBe(true);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(store.getLiveBeaconIdsWithLocationPublishError()).toEqual([
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
|
|
|
]);
|
|
|
|
expect(store.getLiveBeaconIdsWithLocationPublishError(room1Id)).toEqual([
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
|
|
|
]);
|
2022-04-25 12:44:18 +00:00
|
|
|
expect(store.hasLocationPublishErrors()).toBe(true);
|
2022-03-30 14:01:44 +00:00
|
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
OwnBeaconStoreEvent.LocationPublishError,
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
2022-03-30 14:01:44 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("stops publishing positions when a beacon has a stopping error", async () => {
|
2022-04-28 12:03:51 +00:00
|
|
|
// reject stopping beacon
|
2022-12-12 11:24:14 +00:00
|
|
|
const error = new Error("oups");
|
2022-04-28 12:03:51 +00:00
|
|
|
mockClient.unstable_setLiveBeacon.mockRejectedValue(error);
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-04-28 12:03:51 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
// 2 positions from watchPosition in this period
|
|
|
|
await advanceAndFlushPromises(5000);
|
|
|
|
|
|
|
|
// attempt to stop the beacon
|
|
|
|
await expect(store.stopBeacon(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).rejects.toEqual(error);
|
|
|
|
expect(store.beaconUpdateErrors.get(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toEqual(error);
|
|
|
|
|
|
|
|
// 2 more positions in this period
|
|
|
|
await advanceAndFlushPromises(50000);
|
|
|
|
|
|
|
|
// only two positions pre-stopping were sent
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("restarts publishing a beacon after resetting location publish error", async () => {
|
2022-03-30 14:01:44 +00:00
|
|
|
// always fails to send events
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.sendEvent.mockRejectedValue(new Error("oups"));
|
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-30 14:01:44 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-12-12 11:24:14 +00:00
|
|
|
const emitSpy = jest.spyOn(store, "emit");
|
2022-03-30 14:01:44 +00:00
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
// 3 positions from watchPosition in this period
|
|
|
|
await advanceAndFlushPromises(4000);
|
|
|
|
|
|
|
|
// only two allowed failures
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
|
2022-04-25 12:44:18 +00:00
|
|
|
expect(store.beaconHasLocationPublishError(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toBe(true);
|
|
|
|
expect(store.hasLocationPublishErrors()).toBe(true);
|
|
|
|
expect(store.hasLocationPublishErrors(room1Id)).toBe(true);
|
2022-03-30 14:01:44 +00:00
|
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
OwnBeaconStoreEvent.LocationPublishError,
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
2022-03-30 14:01:44 +00:00
|
|
|
);
|
|
|
|
|
2022-04-25 12:44:18 +00:00
|
|
|
// reset emitSpy mock counts to assert on locationPublishError again
|
2022-03-30 14:01:44 +00:00
|
|
|
emitSpy.mockClear();
|
2022-04-25 12:44:18 +00:00
|
|
|
store.resetLocationPublishError(getBeaconInfoIdentifier(alicesRoom1BeaconInfo));
|
2022-03-30 14:01:44 +00:00
|
|
|
|
2022-04-25 12:44:18 +00:00
|
|
|
expect(store.beaconHasLocationPublishError(getBeaconInfoIdentifier(alicesRoom1BeaconInfo))).toBe(false);
|
2022-03-30 14:01:44 +00:00
|
|
|
|
|
|
|
// 2 more positions from watchPosition in this period
|
|
|
|
await advanceAndFlushPromises(10000);
|
|
|
|
|
|
|
|
// 2 from before, 2 new ones
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(4);
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
OwnBeaconStoreEvent.LocationPublishError,
|
|
|
|
getBeaconInfoIdentifier(alicesRoom1BeaconInfo),
|
2022-03-30 14:01:44 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("publishes subsequent positions", async () => {
|
2022-03-28 16:46:39 +00:00
|
|
|
// modern fake timers + debounce + promises are not friends
|
|
|
|
// just testing that positions are published
|
|
|
|
// not that the debounce works
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation([0, 1000, 3000]));
|
2022-03-28 16:46:39 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 16:46:39 +00:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(0);
|
2022-03-28 10:48:38 +00:00
|
|
|
await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
2022-03-28 16:46:39 +00:00
|
|
|
jest.advanceTimersByTime(5000);
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-03-28 16:46:39 +00:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(3);
|
2022-03-28 10:48:38 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("stops live beacons when geolocation permissions are revoked", async () => {
|
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-28 16:46:39 +00:00
|
|
|
// return two good positions, then a permission denied error
|
2022-12-12 11:24:14 +00:00
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation([0, 1000, 3000], [0, 0, 1]));
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 16:46:39 +00:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(0);
|
|
|
|
const store = await makeOwnBeaconStore();
|
2022-03-28 10:48:38 +00:00
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
2022-03-28 16:46:39 +00:00
|
|
|
jest.advanceTimersByTime(5000);
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-03-28 16:46:39 +00:00
|
|
|
// first two events were sent successfully
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-03-28 16:46:39 +00:00
|
|
|
// stop beacon
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalled();
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(false);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("keeps sharing positions when geolocation has a non fatal error", async () => {
|
|
|
|
const errorLogSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-28 16:46:39 +00:00
|
|
|
// return good position, timeout error, good position
|
2022-12-12 11:24:14 +00:00
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation([0, 1000, 3000], [0, 3, 0]));
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 10:48:38 +00:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(0);
|
2022-03-28 16:46:39 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
2022-03-28 10:48:38 +00:00
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(5000);
|
|
|
|
|
2022-03-28 16:46:39 +00:00
|
|
|
// two good locations were sent
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledTimes(2);
|
|
|
|
|
|
|
|
// still sharing
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).not.toHaveBeenCalled();
|
|
|
|
expect(store.isMonitoringLiveLocation).toEqual(true);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(errorLogSpy).toHaveBeenCalledWith("Geolocation failed", "error message");
|
2022-03-28 10:48:38 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("publishes last known position after 30s of inactivity", async () => {
|
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation([0]));
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 10:48:38 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not try to publish anything if there is no known position after 30s of inactivity", async () => {
|
2022-03-28 10:48:38 +00:00
|
|
|
// no position ever returned from geolocation
|
2022-12-12 11:24:14 +00:00
|
|
|
geolocation.watchPosition.mockImplementation(watchPositionMockImplementation([]));
|
|
|
|
geolocation.getCurrentPosition.mockImplementation(watchPositionMockImplementation([]));
|
2022-03-28 10:48:38 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo]);
|
2022-03-28 10:48:38 +00:00
|
|
|
await makeOwnBeaconStore();
|
|
|
|
// wait for store to settle
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
advanceDateAndTime(31000);
|
|
|
|
|
|
|
|
// no locations published
|
|
|
|
expect(mockClient.sendEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-04-22 12:05:36 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("createLiveBeacon", () => {
|
|
|
|
const newEventId = "new-beacon-event-id";
|
|
|
|
const loggerErrorSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-04-22 12:05:36 +00:00
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
localStorageGetSpy.mockReturnValue(JSON.stringify([alicesRoom1BeaconInfo.getId()]));
|
2022-04-22 12:05:36 +00:00
|
|
|
|
|
|
|
localStorageSetSpy.mockClear();
|
|
|
|
|
|
|
|
mockClient.unstable_createLiveBeacon.mockResolvedValue({ event_id: newEventId });
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("creates a live beacon", async () => {
|
2022-04-22 12:05:36 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const content = makeBeaconInfoContent(100);
|
|
|
|
await store.createLiveBeacon(room1Id, content);
|
|
|
|
expect(mockClient.unstable_createLiveBeacon).toHaveBeenCalledWith(room1Id, content);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("sets new beacon event id in local storage", async () => {
|
2022-04-22 12:05:36 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const content = makeBeaconInfoContent(100);
|
|
|
|
await store.createLiveBeacon(room1Id, content);
|
|
|
|
|
|
|
|
expect(localStorageSetSpy).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
"mx_live_beacon_created_id",
|
|
|
|
JSON.stringify([alicesRoom1BeaconInfo.getId(), newEventId]),
|
2022-04-22 12:05:36 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("handles saving beacon event id when local storage has bad value", async () => {
|
|
|
|
localStorageGetSpy.mockReturnValue(JSON.stringify({ id: "1" }));
|
2022-04-22 12:05:36 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const content = makeBeaconInfoContent(100);
|
|
|
|
await store.createLiveBeacon(room1Id, content);
|
|
|
|
|
|
|
|
// stored successfully
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(localStorageSetSpy).toHaveBeenCalledWith("mx_live_beacon_created_id", JSON.stringify([newEventId]));
|
2022-04-22 12:05:36 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("creates a live beacon without error when no beacons exist for room", async () => {
|
2022-04-22 12:05:36 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
const content = makeBeaconInfoContent(100);
|
|
|
|
await store.createLiveBeacon(room1Id, content);
|
|
|
|
|
|
|
|
// didn't throw, no error log
|
|
|
|
expect(loggerErrorSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
2022-06-30 07:33:51 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("stops existing live beacon for room before creates new beacon", async () => {
|
2022-06-30 07:33:51 +00:00
|
|
|
// room1 already has a live beacon for alice
|
2022-12-12 11:24:14 +00:00
|
|
|
makeRoomsWithStateEvents([alicesRoom1BeaconInfo, alicesRoom2BeaconInfo]);
|
2022-06-30 07:33:51 +00:00
|
|
|
const store = await makeOwnBeaconStore();
|
|
|
|
|
|
|
|
const content = makeBeaconInfoContent(100);
|
|
|
|
await store.createLiveBeacon(room1Id, content);
|
|
|
|
|
|
|
|
// stop alicesRoom1BeaconInfo
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
room1Id,
|
|
|
|
expect.objectContaining({ live: false }),
|
2022-06-30 07:33:51 +00:00
|
|
|
);
|
|
|
|
// only called for beacons in room1, room2 beacon is not stopped
|
|
|
|
expect(mockClient.unstable_setLiveBeacon).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
// new beacon created
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockClient.unstable_createLiveBeacon).toHaveBeenCalledWith(room1Id, content);
|
2022-06-30 07:33:51 +00:00
|
|
|
});
|
2022-04-22 12:05:36 +00:00
|
|
|
});
|
2022-03-16 16:35:09 +00:00
|
|
|
});
|