2022-10-31 17:35:02 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-31 17:35:02 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00: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-10-31 17:35:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { VoiceBroadcastChunkEvents } from "../../../../src/voice-broadcast/utils/VoiceBroadcastChunkEvents";
|
2022-10-31 17:35:02 +00:00
|
|
|
import { mkVoiceBroadcastChunkEvent } from "./test-utils";
|
|
|
|
|
|
|
|
describe("VoiceBroadcastChunkEvents", () => {
|
|
|
|
const userId = "@user:example.com";
|
|
|
|
const roomId = "!room:example.com";
|
2022-11-09 11:17:54 +00:00
|
|
|
const txnId = "txn-id";
|
2022-10-31 17:35:02 +00:00
|
|
|
let eventSeq1Time1: MatrixEvent;
|
|
|
|
let eventSeq2Time4: MatrixEvent;
|
|
|
|
let eventSeq3Time2: MatrixEvent;
|
2022-11-09 11:17:54 +00:00
|
|
|
let eventSeq3Time2T: MatrixEvent;
|
2022-10-31 17:35:02 +00:00
|
|
|
let eventSeq4Time1: MatrixEvent;
|
|
|
|
let eventSeqUTime3: MatrixEvent;
|
|
|
|
let eventSeq2Time4Dup: MatrixEvent;
|
|
|
|
let chunkEvents: VoiceBroadcastChunkEvents;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-16 18:23:29 +00:00
|
|
|
eventSeq1Time1 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 7, 1, 1);
|
|
|
|
eventSeq2Time4 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 23, 2, 4);
|
|
|
|
eventSeq2Time4Dup = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 3141, 2, 4);
|
2022-10-31 17:35:02 +00:00
|
|
|
jest.spyOn(eventSeq2Time4Dup, "getId").mockReturnValue(eventSeq2Time4.getId());
|
2022-12-16 18:23:29 +00:00
|
|
|
eventSeq3Time2 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 42, 3, 2);
|
2022-11-09 11:17:54 +00:00
|
|
|
eventSeq3Time2.setTxnId(txnId);
|
2022-12-16 18:23:29 +00:00
|
|
|
eventSeq3Time2T = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 42, 3, 2);
|
2022-11-09 11:17:54 +00:00
|
|
|
eventSeq3Time2T.setTxnId(txnId);
|
2022-12-16 18:23:29 +00:00
|
|
|
eventSeq4Time1 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 69, 4, 1);
|
|
|
|
eventSeqUTime3 = mkVoiceBroadcastChunkEvent("info1", userId, roomId, 314, undefined, 3);
|
2022-10-31 17:35:02 +00:00
|
|
|
chunkEvents = new VoiceBroadcastChunkEvents();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when adding events that all have a sequence", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
chunkEvents.addEvent(eventSeq2Time4);
|
|
|
|
chunkEvents.addEvent(eventSeq1Time1);
|
|
|
|
chunkEvents.addEvents([eventSeq4Time1, eventSeq2Time4Dup, eventSeq3Time2]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should provide the events sort by sequence", () => {
|
|
|
|
expect(chunkEvents.getEvents()).toEqual([
|
|
|
|
eventSeq1Time1,
|
|
|
|
eventSeq2Time4Dup,
|
|
|
|
eventSeq3Time2,
|
|
|
|
eventSeq4Time1,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2022-12-28 09:32:49 +00:00
|
|
|
it("getNumberOfEvents should return 4", () => {
|
|
|
|
expect(chunkEvents.getNumberOfEvents()).toBe(4);
|
|
|
|
});
|
|
|
|
|
2022-10-31 17:35:02 +00:00
|
|
|
it("getLength should return the total length of all chunks", () => {
|
|
|
|
expect(chunkEvents.getLength()).toBe(3259);
|
|
|
|
});
|
|
|
|
|
2022-11-04 10:50:19 +00:00
|
|
|
it("getLengthTo(first event) should return 0", () => {
|
|
|
|
expect(chunkEvents.getLengthTo(eventSeq1Time1)).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getLengthTo(some event) should return the time excl. that event", () => {
|
|
|
|
expect(chunkEvents.getLengthTo(eventSeq3Time2)).toBe(7 + 3141);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("getLengthTo(last event) should return the time excl. that event", () => {
|
|
|
|
expect(chunkEvents.getLengthTo(eventSeq4Time1)).toBe(7 + 3141 + 42);
|
|
|
|
});
|
|
|
|
|
2022-10-31 17:35:02 +00:00
|
|
|
it("should return the expected next chunk", () => {
|
|
|
|
expect(chunkEvents.getNext(eventSeq2Time4Dup)).toBe(eventSeq3Time2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return undefined for next last chunk", () => {
|
|
|
|
expect(chunkEvents.getNext(eventSeq4Time1)).toBeUndefined();
|
|
|
|
});
|
2022-11-04 10:50:19 +00:00
|
|
|
|
|
|
|
it("findByTime(0) should return the first chunk", () => {
|
|
|
|
expect(chunkEvents.findByTime(0)).toBe(eventSeq1Time1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("findByTime(some time) should return the chunk with this time", () => {
|
|
|
|
expect(chunkEvents.findByTime(7 + 3141 + 21)).toBe(eventSeq3Time2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("findByTime(entire duration) should return the last chunk", () => {
|
|
|
|
expect(chunkEvents.findByTime(7 + 3141 + 42 + 69)).toBe(eventSeq4Time1);
|
|
|
|
});
|
2022-11-09 11:17:54 +00:00
|
|
|
|
|
|
|
describe("and adding an event with a known transaction Id", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
chunkEvents.addEvent(eventSeq3Time2T);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should replace the previous event", () => {
|
|
|
|
expect(chunkEvents.getEvents()).toEqual([
|
|
|
|
eventSeq1Time1,
|
|
|
|
eventSeq2Time4Dup,
|
|
|
|
eventSeq3Time2T,
|
|
|
|
eventSeq4Time1,
|
|
|
|
]);
|
2022-12-28 09:32:49 +00:00
|
|
|
expect(chunkEvents.getNumberOfEvents()).toBe(4);
|
2022-11-09 11:17:54 +00:00
|
|
|
});
|
|
|
|
});
|
2022-10-31 17:35:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("when adding events where at least one does not have a sequence", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
chunkEvents.addEvent(eventSeq2Time4);
|
|
|
|
chunkEvents.addEvent(eventSeq1Time1);
|
|
|
|
chunkEvents.addEvents([eventSeq4Time1, eventSeqUTime3, eventSeq2Time4Dup, eventSeq3Time2]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should provide the events sort by timestamp without duplicates", () => {
|
|
|
|
expect(chunkEvents.getEvents()).toEqual([
|
|
|
|
eventSeq1Time1,
|
|
|
|
eventSeq4Time1,
|
|
|
|
eventSeq3Time2,
|
|
|
|
eventSeqUTime3,
|
|
|
|
eventSeq2Time4Dup,
|
|
|
|
]);
|
2022-12-28 09:32:49 +00:00
|
|
|
expect(chunkEvents.getNumberOfEvents()).toBe(5);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getSequenceForEvent", () => {
|
|
|
|
it("should return the sequence if provided by the event", () => {
|
|
|
|
expect(chunkEvents.getSequenceForEvent(eventSeq3Time2)).toBe(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return the index if no sequence provided by event", () => {
|
|
|
|
expect(chunkEvents.getSequenceForEvent(eventSeqUTime3)).toBe(4);
|
|
|
|
});
|
2022-10-31 17:35:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|