2022-03-22 11:46:47 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-03-22 11:46:47 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C
|
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-03-22 11:46:47 +01:00
|
|
|
*/
|
|
|
|
|
2023-08-23 11:04:25 +02:00
|
|
|
import { M_TIMESTAMP, Beacon } from "matrix-js-sdk/src/matrix";
|
2022-03-22 11:46:47 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import { msUntilExpiry, sortBeaconsByLatestExpiry, sortBeaconsByLatestCreation } from "../../../src/utils/beacon";
|
2022-03-22 11:46:47 +01:00
|
|
|
import { makeBeaconInfoEvent } from "../../test-utils";
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("beacon utils", () => {
|
2022-03-22 11:46:47 +01:00
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
const HOUR_MS = 3600000;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(now);
|
2022-03-22 11:46:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(global.Date, "now").mockRestore();
|
2022-03-22 11:46:47 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("msUntilExpiry", () => {
|
|
|
|
it("returns remaining duration", () => {
|
2022-03-22 11:46:47 +01:00
|
|
|
const start = now - HOUR_MS;
|
|
|
|
const durationMs = HOUR_MS * 3;
|
|
|
|
|
|
|
|
expect(msUntilExpiry(start, durationMs)).toEqual(HOUR_MS * 2);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("returns 0 when expiry has already passed", () => {
|
2022-03-22 11:46:47 +01:00
|
|
|
// created 3h ago
|
|
|
|
const start = now - HOUR_MS * 3;
|
|
|
|
// 1h durations
|
|
|
|
const durationMs = HOUR_MS;
|
|
|
|
|
|
|
|
expect(msUntilExpiry(start, durationMs)).toEqual(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("sortBeaconsByLatestExpiry()", () => {
|
|
|
|
const roomId = "!room:server";
|
|
|
|
const aliceId = "@alive:server";
|
2022-03-22 11:46:47 +01:00
|
|
|
|
|
|
|
// 12h old, 12h left
|
2022-12-12 12:24:14 +01:00
|
|
|
const beacon1 = new Beacon(
|
|
|
|
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS * 24, timestamp: now - 12 * HOUR_MS }, "$1"),
|
|
|
|
);
|
2022-03-22 11:46:47 +01:00
|
|
|
// 10h left
|
2022-12-12 12:24:14 +01:00
|
|
|
const beacon2 = new Beacon(
|
|
|
|
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS * 10, timestamp: now }, "$2"),
|
|
|
|
);
|
2022-03-22 11:46:47 +01:00
|
|
|
|
|
|
|
// 1ms left
|
2022-12-12 12:24:14 +01:00
|
|
|
const beacon3 = new Beacon(
|
|
|
|
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS + 1, timestamp: now - HOUR_MS }, "$3"),
|
|
|
|
);
|
|
|
|
|
2023-03-13 22:55:50 +01:00
|
|
|
const noTimestampEvent = makeBeaconInfoEvent(
|
|
|
|
aliceId,
|
|
|
|
roomId,
|
|
|
|
{ timeout: HOUR_MS + 1, timestamp: undefined },
|
|
|
|
"$3",
|
|
|
|
);
|
|
|
|
// beacon info helper defaults to date when timestamp is falsy
|
|
|
|
// hard set it to undefined
|
|
|
|
// @ts-ignore
|
|
|
|
noTimestampEvent.event.content[M_TIMESTAMP.name] = undefined;
|
|
|
|
const beaconNoTimestamp = new Beacon(noTimestampEvent);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("sorts beacons by descending expiry time", () => {
|
|
|
|
expect([beacon2, beacon3, beacon1].sort(sortBeaconsByLatestExpiry)).toEqual([beacon1, beacon2, beacon3]);
|
2022-03-22 11:46:47 +01:00
|
|
|
});
|
2023-03-13 22:55:50 +01:00
|
|
|
|
|
|
|
it("sorts beacons with timestamps before beacons without", () => {
|
|
|
|
expect([beaconNoTimestamp, beacon3].sort(sortBeaconsByLatestExpiry)).toEqual([beacon3, beaconNoTimestamp]);
|
|
|
|
});
|
2022-03-22 11:46:47 +01:00
|
|
|
});
|
2022-03-31 13:51:44 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("sortBeaconsByLatestCreation()", () => {
|
|
|
|
const roomId = "!room:server";
|
|
|
|
const aliceId = "@alive:server";
|
2022-03-31 13:51:44 +02:00
|
|
|
|
|
|
|
// 12h old, 12h left
|
2022-12-12 12:24:14 +01:00
|
|
|
const beacon1 = new Beacon(
|
|
|
|
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS * 24, timestamp: now - 12 * HOUR_MS }, "$1"),
|
|
|
|
);
|
2022-03-31 13:51:44 +02:00
|
|
|
// 10h left
|
2022-12-12 12:24:14 +01:00
|
|
|
const beacon2 = new Beacon(
|
|
|
|
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS * 10, timestamp: now }, "$2"),
|
|
|
|
);
|
2022-03-31 13:51:44 +02:00
|
|
|
|
|
|
|
// 1ms left
|
2022-12-12 12:24:14 +01:00
|
|
|
const beacon3 = new Beacon(
|
|
|
|
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS + 1, timestamp: now - HOUR_MS }, "$3"),
|
|
|
|
);
|
|
|
|
|
|
|
|
it("sorts beacons by descending creation time", () => {
|
|
|
|
expect([beacon1, beacon2, beacon3].sort(sortBeaconsByLatestCreation)).toEqual([beacon2, beacon3, beacon1]);
|
2022-03-31 13:51:44 +02:00
|
|
|
});
|
|
|
|
});
|
2022-03-22 11:46:47 +01:00
|
|
|
});
|