Files
healthchat-web/apps/web/test/unit-tests/stores/CallStore-test.ts
T
Valere Fedronic d732fa807f Rtc: Let the widget driver decide to fallback to well-known or not for rtc discovery. (#34581)
* rtc: fallback to well-known only if config allows

* Use existing rtc transports cached by client

* post rebase fixes

* post rebase test update

* cleanup callstore tests

* Lint + fmt fixes

* postrebase: update useCall tests

* fixup: fix mocking on useRoomCall task

* fix lint

* fixup lint

* fix missing act in react test

* fix client mocks

* fix test mocking for rtc discovery

* fix test mocking

* fix Callstore testing

* lint:js-fix

* add missing mocks

* Fix duplicate ElementWidgetDriver test cases

* Avoid logging warning about well-known foci when they are undefined

---------

Co-authored-by: Robin <robin@robin.town>
2026-08-14 14:41:24 +00:00

87 lines
3.6 KiB
TypeScript

/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import { type CallMembership, MatrixRTCSessionManagerEvents } from "matrix-js-sdk/src/matrixrtc";
import { type MatrixClient, type Room } from "matrix-js-sdk/src/matrix";
import { type MockedObject } from "jest-mock";
import { ElementCall } from "../../../src/models/Call";
import { CallStore } from "../../../src/stores/CallStore";
import {
setUpClientRoomAndStores,
cleanUpClientRoomAndStores,
setupAsyncStoreWithClient,
enableCalls,
} from "../../test-utils";
import SdkConfig from "../../../src/SdkConfig.ts";
describe("CallStore", () => {
let client: MockedObject<MatrixClient>;
let room: Room;
beforeEach(() => {
enableCalls();
const res = setUpClientRoomAndStores();
client = res.client;
room = res.room;
});
afterEach(() => {
cleanUpClientRoomAndStores(client, room);
jest.restoreAllMocks();
});
it("constructs one call for one MatrixRTC session", () => {
setupAsyncStoreWithClient(CallStore.instance, client);
const getSpy = jest.spyOn(ElementCall, "get");
// Simulate another user starting a new MatrixRTC session
const session = client.matrixRTC.getRoomSession(room);
session.memberships.push({} as CallMembership);
client.matrixRTC.emit(MatrixRTCSessionManagerEvents.SessionStarted, room.roomId, session);
expect(getSpy).toHaveBeenCalledTimes(1);
expect(getSpy).toHaveReturnedWith(expect.any(ElementCall));
expect(CallStore.instance.getCall(room.roomId)).not.toBe(null);
expect(CallStore.instance.getConfiguredRTCTransports()).toHaveLength(0);
});
it("delegates transport discovery to the client", async () => {
client.cachedRtcTransports.get.mockReturnValue([
{ type: "type-a", some_data: "value" },
{ type: "type-b", some_data: "foo" },
]);
client.getClientWellKnown.mockReturnValue({
"org.matrix.msc4143.rtc_foci": [
{ type: "type-c", other_data: "bar" },
{ type: "type-d", other_data: "baz" },
],
});
await setupAsyncStoreWithClient(CallStore.instance, client);
expect(CallStore.instance.getConfiguredRTCTransports()).toEqual([
{ type: "type-a", some_data: "value" },
{ type: "type-b", some_data: "foo" },
]);
});
it("does not fall back to client well-known when enable_client_well_known_lookups is false", async () => {
const sdkConfigGet = SdkConfig.get;
jest.spyOn(SdkConfig, "get").mockImplementation((key?: any, altCaseName?: string): any => {
if (key === "enable_client_well_known_lookups") return false;
return sdkConfigGet(key, altCaseName);
});
client.cachedRtcTransports.get.mockReturnValue([{ type: "type-a", some_data: "value" }]);
client.getClientWellKnown.mockReturnValue({
"org.matrix.msc4143.rtc_foci": [{ type: "type-c", other_data: "bar" }],
});
await setupAsyncStoreWithClient(CallStore.instance, client);
// Only the modern endpoint contributes; the legacy well-known fallback is skipped entirely.
expect(CallStore.instance.getConfiguredRTCTransports()).toEqual([{ type: "type-a", some_data: "value" }]);
expect(client.waitForClientWellKnown).not.toHaveBeenCalled();
expect(client.getClientWellKnown).not.toHaveBeenCalled();
});
});