|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { EventType } from "../../../src"; |
| 18 | +import { M_BEACON_INFO } from "../../../src/@types/beacon"; |
| 19 | +import { |
| 20 | + isTimestampInDuration, |
| 21 | + isBeaconInfoEventType, |
| 22 | + Beacon, |
| 23 | + BeaconEvent, |
| 24 | +} from "../../../src/models/beacon"; |
| 25 | +import { makeBeaconInfoEvent } from "../../test-utils/beacon"; |
| 26 | + |
| 27 | +describe('Beacon', () => { |
| 28 | + describe('isTimestampInDuration()', () => { |
| 29 | + const startTs = new Date('2022-03-11T12:07:47.592Z').getTime(); |
| 30 | + const HOUR_MS = 3600000; |
| 31 | + it('returns false when timestamp is before start time', () => { |
| 32 | + // day before |
| 33 | + const timestamp = new Date('2022-03-10T12:07:47.592Z').getTime(); |
| 34 | + expect(isTimestampInDuration(startTs, HOUR_MS, timestamp)).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns false when timestamp is after start time + duration', () => { |
| 38 | + // 1 second later |
| 39 | + const timestamp = new Date('2022-03-10T12:07:48.592Z').getTime(); |
| 40 | + expect(isTimestampInDuration(startTs, HOUR_MS, timestamp)).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns true when timestamp is exactly start time', () => { |
| 44 | + expect(isTimestampInDuration(startTs, HOUR_MS, startTs)).toBe(true); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns true when timestamp is exactly the end of the duration', () => { |
| 48 | + expect(isTimestampInDuration(startTs, HOUR_MS, startTs + HOUR_MS)).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns true when timestamp is within the duration', () => { |
| 52 | + const twoHourDuration = HOUR_MS * 2; |
| 53 | + const now = startTs + HOUR_MS; |
| 54 | + expect(isTimestampInDuration(startTs, twoHourDuration, now)).toBe(true); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('isBeaconInfoEventType', () => { |
| 59 | + it.each([ |
| 60 | + EventType.CallAnswer, |
| 61 | + `prefix.${M_BEACON_INFO.name}`, |
| 62 | + `prefix.${M_BEACON_INFO.altName}`, |
| 63 | + ])('returns false for %s', (type) => { |
| 64 | + expect(isBeaconInfoEventType(type)).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it.each([ |
| 68 | + M_BEACON_INFO.name, |
| 69 | + M_BEACON_INFO.altName, |
| 70 | + `${M_BEACON_INFO.name}.@test:server.org.12345`, |
| 71 | + `${M_BEACON_INFO.altName}.@test:server.org.12345`, |
| 72 | + ])('returns true for %s', (type) => { |
| 73 | + expect(isBeaconInfoEventType(type)).toBe(true); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Beacon', () => { |
| 78 | + const userId = '@user:server.org'; |
| 79 | + const roomId = '$room:server.org'; |
| 80 | + // 14.03.2022 16:15 |
| 81 | + const now = 1647270879403; |
| 82 | + const HOUR_MS = 3600000; |
| 83 | + |
| 84 | + // beacon_info events |
| 85 | + // created 'an hour ago' |
| 86 | + // without timeout of 3 hours |
| 87 | + let liveBeaconEvent; |
| 88 | + let notLiveBeaconEvent; |
| 89 | + beforeEach(() => { |
| 90 | + // go back in time to create the beacon |
| 91 | + jest.spyOn(global.Date, 'now').mockReturnValue(now - HOUR_MS); |
| 92 | + liveBeaconEvent = makeBeaconInfoEvent(userId, roomId, { timeout: HOUR_MS * 3, isLive: true }, '$live123'); |
| 93 | + notLiveBeaconEvent = makeBeaconInfoEvent( |
| 94 | + userId, |
| 95 | + roomId, |
| 96 | + { timeout: HOUR_MS * 3, isLive: false }, |
| 97 | + '$dead123', |
| 98 | + ); |
| 99 | + |
| 100 | + // back to now |
| 101 | + jest.spyOn(global.Date, 'now').mockReturnValue(now); |
| 102 | + }); |
| 103 | + |
| 104 | + afterAll(() => { |
| 105 | + jest.spyOn(global.Date, 'now').mockRestore(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('creates beacon from event', () => { |
| 109 | + const beacon = new Beacon(liveBeaconEvent); |
| 110 | + |
| 111 | + expect(beacon.beaconInfoId).toEqual(liveBeaconEvent.getId()); |
| 112 | + expect(beacon.isLive).toEqual(true); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('isLive()', () => { |
| 116 | + it('returns false when beacon is explicitly set to not live', () => { |
| 117 | + const beacon = new Beacon(notLiveBeaconEvent); |
| 118 | + expect(beacon.isLive).toEqual(false); |
| 119 | + }); |
| 120 | + |
| 121 | + it('returns false when beacon is expired', () => { |
| 122 | + // time travel to beacon creation + 3 hours |
| 123 | + jest.spyOn(global.Date, 'now').mockReturnValue(now - 3 * HOUR_MS); |
| 124 | + const beacon = new Beacon(liveBeaconEvent); |
| 125 | + expect(beacon.isLive).toEqual(false); |
| 126 | + }); |
| 127 | + |
| 128 | + it('returns false when beacon timestamp is in future', () => { |
| 129 | + // time travel to before beacon events timestamp |
| 130 | + // event was created now - 1 hour |
| 131 | + jest.spyOn(global.Date, 'now').mockReturnValue(now - HOUR_MS - HOUR_MS); |
| 132 | + const beacon = new Beacon(liveBeaconEvent); |
| 133 | + expect(beacon.isLive).toEqual(false); |
| 134 | + }); |
| 135 | + |
| 136 | + it('returns true when beacon was created in past and not yet expired', () => { |
| 137 | + // liveBeaconEvent was created 1 hour ago |
| 138 | + const beacon = new Beacon(liveBeaconEvent); |
| 139 | + expect(beacon.isLive).toEqual(true); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('update()', () => { |
| 144 | + it('does not update with different event', () => { |
| 145 | + const beacon = new Beacon(liveBeaconEvent); |
| 146 | + |
| 147 | + expect(beacon.beaconInfoId).toEqual(liveBeaconEvent.getId()); |
| 148 | + |
| 149 | + expect(() => beacon.update(notLiveBeaconEvent)).toThrow(); |
| 150 | + expect(beacon.isLive).toEqual(true); |
| 151 | + }); |
| 152 | + |
| 153 | + it('updates event', () => { |
| 154 | + const beacon = new Beacon(liveBeaconEvent); |
| 155 | + const emitSpy = jest.spyOn(beacon, 'emit'); |
| 156 | + |
| 157 | + expect(beacon.isLive).toEqual(true); |
| 158 | + |
| 159 | + const updatedBeaconEvent = makeBeaconInfoEvent( |
| 160 | + userId, roomId, { timeout: HOUR_MS * 3, isLive: false }, '$live123'); |
| 161 | + |
| 162 | + beacon.update(updatedBeaconEvent); |
| 163 | + expect(beacon.isLive).toEqual(false); |
| 164 | + expect(emitSpy).toHaveBeenCalledWith(BeaconEvent.Update, updatedBeaconEvent, beacon); |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments