fix a couple of more tests.

This commit is contained in:
Timo K
2025-11-14 18:45:07 +01:00
parent 6d8d2d2384
commit b9ad83bf3b
8 changed files with 378 additions and 496 deletions

View File

@@ -8,14 +8,18 @@ Please see LICENSE in the repository root for full details.
import {
type ICallNotifyContent,
type IRTCNotificationContent,
MatrixRTCSessionEvent,
} from "matrix-js-sdk/lib/matrixrtc";
import { describe, it } from "vitest";
import {
EventType,
type IEvent,
type IRoomTimelineData,
MatrixEvent,
type Room,
} from "matrix-js-sdk";
import { E2eeType } from "../../e2ee/e2eeType";
import { withTestScheduler } from "../../utils/test";
import {
aliceParticipant,
aliceRtcMember,
local,
localRtcMember,
@@ -25,7 +29,6 @@ import {
type Props as CallNotificationLifecycleProps,
} from "./CallNotificationLifecycle";
import { trackEpoch } from "../ObservableScope";
import { withCallViewModel } from "./CallViewModelTestUtils.test";
const mockLegacyRingEvent = {} as { event_id: string } & ICallNotifyContent;
function mockRingEvent(
@@ -60,7 +63,9 @@ describe("waitForCallPickup$", () => {
},
localUser: localRtcMember,
};
const lifecycle = createCallNotificationLifecycle$(props);
expectObservable(lifecycle.callPickupState$).toBe("a 9ms b 29ms c", {
a: "unknown",
b: "ringing",
@@ -68,6 +73,7 @@ describe("waitForCallPickup$", () => {
});
});
});
it("ringing -> success if someone joins before timeout is reached", () => {
withTestScheduler(({ scope, hot, behavior, expectObservable }) => {
// Someone joins at 20ms (both LiveKit participant and MatrixRTC member)
@@ -154,179 +160,195 @@ describe("waitForCallPickup$", () => {
});
it("stays null when waitForCallPickup=false", () => {
withTestScheduler(({ behavior, schedule, expectObservable }) => {
withCallViewModel(
{
remoteParticipants$: behavior("a--b", {
a: [],
b: [aliceParticipant],
}),
rtcMembers$: behavior("a--b", {
withTestScheduler(({ scope, hot, behavior, expectObservable }) => {
// Someone joins at 20ms (both LiveKit participant and MatrixRTC member)
const validProps: CallNotificationLifecycleProps = {
scope,
memberships$: scope.behavior(
behavior("a--b", {
a: [localRtcMember],
b: [localRtcMember, aliceRtcMember],
}),
}).pipe(trackEpoch()),
),
sentCallNotification$: hot("10ms a", {
a: [mockRingEvent("$notif5", 30), mockLegacyRingEvent],
}),
receivedDecline$: hot(""),
options: {
waitForCallPickup: true,
autoLeaveWhenOthersLeft: false,
},
(vm, rtcSession) => {
schedule(" 5ms r", {
r: () => {
rtcSession.emit(
MatrixRTCSessionEvent.DidSendCallNotification,
mockRingEvent("$notif5", 30),
mockLegacyRingEvent,
);
},
});
expectObservable(vm.callPickupState$).toBe("(n)", {
n: null,
});
},
{
localUser: localRtcMember,
};
const propsDeactivated = {
...validProps,
options: {
...validProps.options,
waitForCallPickup: false,
encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
},
};
const lifecycle = createCallNotificationLifecycle$(propsDeactivated);
expectObservable(lifecycle.callPickupState$).toBe("n", {
n: null,
});
const lifecycleReference = createCallNotificationLifecycle$(validProps);
expectObservable(lifecycleReference.callPickupState$).toBe("u--s", {
u: "unknown",
s: "success",
});
});
});
it("decline before timeout window ends -> decline", () => {
withTestScheduler(({ scope, hot, behavior, expectObservable }) => {
// Someone joins at 20ms (both LiveKit participant and MatrixRTC member)
const props: CallNotificationLifecycleProps = {
scope,
memberships$: scope.behavior(
behavior("a", {
a: [localRtcMember],
}).pipe(trackEpoch()),
),
sentCallNotification$: hot("10ms a", {
a: [mockRingEvent("$decl1", 50), mockLegacyRingEvent],
}),
receivedDecline$: hot("40ms d", {
d: [
new MatrixEvent({
type: EventType.RTCDecline,
content: {
"m.relates_to": {
rel_type: "m.reference",
event_id: "$decl1",
},
},
}),
{} as Room,
undefined,
false,
{} as IRoomTimelineData,
],
}),
options: {
waitForCallPickup: true,
autoLeaveWhenOthersLeft: false,
},
localUser: localRtcMember,
};
const lifecycle = createCallNotificationLifecycle$(props);
expectObservable(lifecycle.callPickupState$).toBe("a 9ms b 29ms e", {
a: "unknown",
b: "ringing",
e: "decline",
});
});
});
it("decline after timeout window ends -> stays timeout", () => {
withTestScheduler(({ scope, hot, behavior, expectObservable }) => {
// Someone joins at 20ms (both LiveKit participant and MatrixRTC member)
const props: CallNotificationLifecycleProps = {
scope,
memberships$: scope.behavior(
behavior("a", {
a: [localRtcMember],
}).pipe(trackEpoch()),
),
sentCallNotification$: hot("10ms a", {
a: [mockRingEvent("$decl", 20), mockLegacyRingEvent],
}),
receivedDecline$: hot("40ms d", {
d: [
new MatrixEvent({
type: EventType.RTCDecline,
content: {
"m.relates_to": {
rel_type: "m.reference",
event_id: "$decl",
},
},
}),
{} as Room,
undefined,
false,
{} as IRoomTimelineData,
],
}),
options: {
waitForCallPickup: true,
autoLeaveWhenOthersLeft: false,
},
localUser: localRtcMember,
};
const lifecycle = createCallNotificationLifecycle$(props);
expectObservable(lifecycle.callPickupState$, "50ms !").toBe(
"a 9ms b 19ms e",
{
a: "unknown",
b: "ringing",
e: "timeout",
},
);
});
});
// it("decline before timeout window ends -> decline", () => {
// withTestScheduler(({ schedule, expectObservable }) => {
// withCallViewModel(
// {},
// (vm, rtcSession) => {
// // Notify at 10ms with 50ms lifetime, decline at 40ms with matching id
// schedule(" 10ms r 29ms d", {
// r: () => {
// rtcSession.emit(
// MatrixRTCSessionEvent.DidSendCallNotification,
// mockRingEvent("$decl1", 50),
// mockLegacyRingEvent,
// );
// },
// d: () => {
// // Emit decline timeline event with id matching the notification
// rtcSession.room.emit(
// MatrixRoomEvent.Timeline,
// new MatrixEvent({
// type: EventType.RTCDecline,
// content: {
// "m.relates_to": {
// rel_type: "m.reference",
// event_id: "$decl1",
// },
// },
// }),
// rtcSession.room,
// undefined,
// false,
// {} as IRoomTimelineData,
// );
// },
// });
// expectObservable(vm.callPickupState$).toBe("a 9ms b 29ms e", {
// a: "unknown",
// b: "ringing",
// e: "decline",
// });
// },
// {
// waitForCallPickup: true,
// encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
// },
// );
// });
// });
// it("decline after timeout window ends -> stays timeout", () => {
// withTestScheduler(({ schedule, expectObservable }) => {
// withCallViewModel(
// {},
// (vm, rtcSession) => {
// // Notify at 10ms with 20ms lifetime (timeout at 30ms), decline at 40ms
// schedule(" 10ms r 20ms t 10ms d", {
// r: () => {
// rtcSession.emit(
// MatrixRTCSessionEvent.DidSendCallNotification,
// mockRingEvent("$decl2", 20),
// mockLegacyRingEvent,
// );
// },
// t: () => {},
// d: () => {
// rtcSession.room.emit(
// MatrixRoomEvent.Timeline,
// new MatrixEvent({
// event_id: "$decl2",
// type: "m.rtc.decline",
// }),
// rtcSession.room,
// undefined,
// false,
// {} as IRoomTimelineData,
// );
// },
// });
// expectObservable(vm.callPickupState$).toBe("a 9ms b 19ms c", {
// a: "unknown",
// b: "ringing",
// c: "timeout",
// });
// },
// {
// waitForCallPickup: true,
// encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
// },
// );
// });
// });
// function testStaysRinging(declineEvent: Partial<IEvent>): void {
// withTestScheduler(({ schedule, expectObservable }) => {
// withCallViewModel(
// {},
// (vm, rtcSession) => {
// // Notify at 10ms with id A, decline arrives at 20ms with id B
// schedule(" 10ms r 10ms d", {
// r: () => {
// rtcSession.emit(
// MatrixRTCSessionEvent.DidSendCallNotification,
// mockRingEvent("$right", 50),
// mockLegacyRingEvent,
// );
// },
// d: () => {
// rtcSession.room.emit(
// MatrixRoomEvent.Timeline,
// new MatrixEvent(declineEvent),
// rtcSession.room,
// undefined,
// false,
// {} as IRoomTimelineData,
// );
// },
// });
// // We assert up to 21ms to see the ringing at 10ms and no change at 20ms
// expectObservable(vm.callPickupState$, "21ms !").toBe("a 9ms b", {
// a: "unknown",
// b: "ringing",
// });
// },
// {
// waitForCallPickup: true,
// encryptionSystem: { kind: E2eeType.PER_PARTICIPANT },
// },
// );
// });
// }
// it("decline with wrong id is ignored (stays ringing)", () => {
// testStaysRinging({
// event_id: "$wrong",
// type: "m.rtc.decline",
// sender: local.userId,
// });
// });
// it("decline with sender being the local user is ignored (stays ringing)", () => {
// testStaysRinging({
// event_id: "$right",
// type: "m.rtc.decline",
// sender: alice.userId,
// });
// });
//
function testStaysRinging(
declineEvent: Partial<IEvent>,
expectDecline: boolean,
): void {
withTestScheduler(({ scope, hot, behavior, expectObservable }) => {
// Someone joins at 20ms (both LiveKit participant and MatrixRTC member)
const props: CallNotificationLifecycleProps = {
scope,
memberships$: scope.behavior(
behavior("a", {
a: [localRtcMember],
}).pipe(trackEpoch()),
),
sentCallNotification$: hot("10ms a", {
a: [mockRingEvent("$right", 50), mockLegacyRingEvent],
}),
receivedDecline$: hot("20ms d", {
d: [
new MatrixEvent(declineEvent),
{} as Room,
undefined,
false,
{} as IRoomTimelineData,
],
}),
options: {
waitForCallPickup: true,
autoLeaveWhenOthersLeft: false,
},
localUser: localRtcMember,
};
const lifecycle = createCallNotificationLifecycle$(props);
const marbles = expectDecline ? "a 9ms b 9ms d" : "a 9ms b";
expectObservable(lifecycle.callPickupState$, "21ms !").toBe(marbles, {
a: "unknown",
b: "ringing",
d: "decline",
});
});
}
const reference = (refId?: string, sender?: string): Partial<IEvent> => ({
event_id: "$decline",
type: EventType.RTCDecline,
sender: sender ?? "@other:example.org",
content: {
"m.relates_to": {
rel_type: "m.reference",
event_id: refId ?? "$right",
},
},
});
it("decline reference works", () => {
testStaysRinging(reference(), true);
});
it("decline with wrong id is ignored (stays ringing)", () => {
testStaysRinging(reference("$wrong"), false);
});
it("decline with wrong id is ignored (stays ringing)", () => {
testStaysRinging(reference(undefined, local.userId), false);
});
});