2022-04-01 16:36:10 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-20 17:03:33 +02:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { Widget, ClientWidgetApi, MatrixWidgetType, IWidgetApiRequest } from "matrix-widget-api";
|
2022-06-09 15:19:52 +02:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2022-04-01 16:36:10 +02:00
|
|
|
|
2022-05-16 22:54:08 +02:00
|
|
|
import { stubClient, setupAsyncStoreWithClient, mkRoom } from "../test-utils";
|
2022-04-01 16:36:10 +02:00
|
|
|
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
|
2022-04-20 17:03:33 +02:00
|
|
|
import WidgetStore, { IApp } from "../../src/stores/WidgetStore";
|
2022-04-01 16:36:10 +02:00
|
|
|
import { WidgetMessagingStore } from "../../src/stores/widgets/WidgetMessagingStore";
|
2022-04-20 17:03:33 +02:00
|
|
|
import { ElementWidgetActions } from "../../src/stores/widgets/ElementWidgetActions";
|
2022-06-09 15:19:52 +02:00
|
|
|
import { VIDEO_CHANNEL_MEMBER, STUCK_DEVICE_TIMEOUT_MS } from "../../src/utils/VideoChannelUtils";
|
2022-04-01 16:36:10 +02:00
|
|
|
import VideoChannelStore, { VideoChannelEvent } from "../../src/stores/VideoChannelStore";
|
|
|
|
|
|
|
|
describe("VideoChannelStore", () => {
|
2022-04-20 17:03:33 +02:00
|
|
|
const store = VideoChannelStore.instance;
|
2022-04-01 16:36:10 +02:00
|
|
|
|
2022-06-02 15:10:22 +02:00
|
|
|
const widget = { id: "1" } as unknown as Widget;
|
2022-04-20 17:03:33 +02:00
|
|
|
const app = {
|
2022-06-02 15:10:22 +02:00
|
|
|
id: "1",
|
2022-04-01 16:36:10 +02:00
|
|
|
eventId: "$1:example.org",
|
|
|
|
roomId: "!1:example.org",
|
|
|
|
type: MatrixWidgetType.JitsiMeet,
|
|
|
|
url: "",
|
|
|
|
name: "Video channel",
|
|
|
|
creatorUserId: "@alice:example.org",
|
|
|
|
avatar_url: null,
|
2022-06-02 15:10:22 +02:00
|
|
|
data: { isVideoChannel: true },
|
2022-04-20 17:03:33 +02:00
|
|
|
} as IApp;
|
2022-04-01 16:36:10 +02:00
|
|
|
|
2022-04-20 17:03:33 +02:00
|
|
|
// Set up mocks to simulate the remote end of the widget API
|
|
|
|
let messageSent: Promise<void>;
|
|
|
|
let messageSendMock: () => void;
|
|
|
|
let onMock: (action: string, listener: (ev: CustomEvent<IWidgetApiRequest>) => void) => void;
|
|
|
|
let onceMock: (action: string, listener: (ev: CustomEvent<IWidgetApiRequest>) => void) => void;
|
|
|
|
let messaging: ClientWidgetApi;
|
2022-06-09 15:19:52 +02:00
|
|
|
let cli: MatrixClient;
|
2022-04-01 16:36:10 +02:00
|
|
|
beforeEach(() => {
|
2022-04-20 17:03:33 +02:00
|
|
|
stubClient();
|
2022-06-09 15:19:52 +02:00
|
|
|
cli = MatrixClientPeg.get();
|
2022-04-20 17:03:33 +02:00
|
|
|
setupAsyncStoreWithClient(WidgetMessagingStore.instance, cli);
|
|
|
|
setupAsyncStoreWithClient(store, cli);
|
2022-05-16 22:54:08 +02:00
|
|
|
mocked(cli).getRoom.mockReturnValue(mkRoom(cli, "!1:example.org"));
|
2022-04-20 17:03:33 +02:00
|
|
|
|
|
|
|
let resolveMessageSent: () => void;
|
|
|
|
messageSent = new Promise(resolve => resolveMessageSent = resolve);
|
|
|
|
messageSendMock = jest.fn().mockImplementation(() => resolveMessageSent());
|
|
|
|
onMock = jest.fn();
|
|
|
|
onceMock = jest.fn();
|
2022-04-01 16:36:10 +02:00
|
|
|
|
2022-04-20 17:03:33 +02:00
|
|
|
jest.spyOn(WidgetStore.instance, "getApps").mockReturnValue([app]);
|
|
|
|
messaging = {
|
|
|
|
on: onMock,
|
|
|
|
off: () => {},
|
|
|
|
stop: () => {},
|
|
|
|
once: onceMock,
|
|
|
|
transport: {
|
|
|
|
send: messageSendMock,
|
|
|
|
reply: () => {},
|
|
|
|
},
|
|
|
|
} as unknown as ClientWidgetApi;
|
2022-04-01 16:36:10 +02:00
|
|
|
});
|
|
|
|
|
2022-06-09 15:19:52 +02:00
|
|
|
afterEach(() => jest.useRealTimers());
|
|
|
|
|
2022-04-20 17:03:33 +02:00
|
|
|
const widgetReady = () => {
|
|
|
|
// Tell the WidgetStore that the widget is ready
|
|
|
|
const [, ready] = mocked(onceMock).mock.calls.find(([action]) =>
|
|
|
|
action === `action:${ElementWidgetActions.WidgetReady}`,
|
|
|
|
);
|
|
|
|
ready({ detail: {} } as unknown as CustomEvent<IWidgetApiRequest>);
|
|
|
|
};
|
2022-04-01 16:36:10 +02:00
|
|
|
|
2022-04-20 17:03:33 +02:00
|
|
|
const confirmConnect = async () => {
|
|
|
|
// Wait for the store to contact the widget API
|
|
|
|
await messageSent;
|
|
|
|
// Then, locate the callback that will confirm the join
|
|
|
|
const [, join] = mocked(onMock).mock.calls.find(([action]) =>
|
|
|
|
action === `action:${ElementWidgetActions.JoinCall}`,
|
|
|
|
);
|
|
|
|
// Confirm the join, and wait for the store to update
|
2022-04-01 16:36:10 +02:00
|
|
|
const waitForConnect = new Promise<void>(resolve =>
|
2022-04-20 17:03:33 +02:00
|
|
|
store.once(VideoChannelEvent.Connect, resolve),
|
2022-04-01 16:36:10 +02:00
|
|
|
);
|
2022-04-20 17:03:33 +02:00
|
|
|
join({ detail: {} } as unknown as CustomEvent<IWidgetApiRequest>);
|
2022-04-01 16:36:10 +02:00
|
|
|
await waitForConnect;
|
2022-04-20 17:03:33 +02:00
|
|
|
};
|
2022-04-01 16:36:10 +02:00
|
|
|
|
2022-04-20 17:03:33 +02:00
|
|
|
const confirmDisconnect = async () => {
|
|
|
|
// Locate the callback that will perform the hangup
|
|
|
|
const [, hangup] = mocked(onceMock).mock.calls.find(([action]) =>
|
|
|
|
action === `action:${ElementWidgetActions.HangupCall}`,
|
2022-04-01 16:36:10 +02:00
|
|
|
);
|
2022-04-20 17:03:33 +02:00
|
|
|
// Hangup and wait for the store, once again
|
|
|
|
const waitForHangup = new Promise<void>(resolve =>
|
|
|
|
store.once(VideoChannelEvent.Disconnect, resolve),
|
|
|
|
);
|
|
|
|
hangup({ detail: {} } as unknown as CustomEvent<IWidgetApiRequest>);
|
|
|
|
await waitForHangup;
|
|
|
|
};
|
|
|
|
|
|
|
|
it("connects and disconnects", async () => {
|
2022-06-09 15:19:52 +02:00
|
|
|
jest.useFakeTimers();
|
|
|
|
jest.setSystemTime(0);
|
|
|
|
|
2022-04-20 17:03:33 +02:00
|
|
|
WidgetMessagingStore.instance.storeMessaging(widget, "!1:example.org", messaging);
|
|
|
|
widgetReady();
|
|
|
|
expect(store.roomId).toBeFalsy();
|
|
|
|
expect(store.connected).toEqual(false);
|
|
|
|
|
2022-05-24 13:43:27 +02:00
|
|
|
const connectPromise = store.connect("!1:example.org", null, null);
|
2022-04-20 17:03:33 +02:00
|
|
|
await confirmConnect();
|
2022-05-24 13:43:27 +02:00
|
|
|
await expect(connectPromise).resolves.toBeUndefined();
|
2022-04-20 17:03:33 +02:00
|
|
|
expect(store.roomId).toEqual("!1:example.org");
|
|
|
|
expect(store.connected).toEqual(true);
|
|
|
|
|
2022-06-09 15:19:52 +02:00
|
|
|
// Our device should now appear as connected
|
|
|
|
expect(cli.sendStateEvent).toHaveBeenLastCalledWith(
|
|
|
|
"!1:example.org",
|
|
|
|
VIDEO_CHANNEL_MEMBER,
|
|
|
|
{ devices: [cli.getDeviceId()], expires_ts: expect.any(Number) },
|
|
|
|
cli.getUserId(),
|
|
|
|
);
|
|
|
|
mocked(cli).sendStateEvent.mockClear();
|
|
|
|
|
|
|
|
// Our devices should be resent within the timeout period to prevent
|
|
|
|
// the data from becoming stale
|
|
|
|
jest.advanceTimersByTime(STUCK_DEVICE_TIMEOUT_MS);
|
|
|
|
expect(cli.sendStateEvent).toHaveBeenLastCalledWith(
|
|
|
|
"!1:example.org",
|
|
|
|
VIDEO_CHANNEL_MEMBER,
|
|
|
|
{ devices: [cli.getDeviceId()], expires_ts: expect.any(Number) },
|
|
|
|
cli.getUserId(),
|
|
|
|
);
|
|
|
|
mocked(cli).sendStateEvent.mockClear();
|
|
|
|
|
2022-05-24 13:43:27 +02:00
|
|
|
const disconnectPromise = store.disconnect();
|
2022-04-20 17:03:33 +02:00
|
|
|
await confirmDisconnect();
|
2022-05-24 13:43:27 +02:00
|
|
|
await expect(disconnectPromise).resolves.toBeUndefined();
|
2022-04-20 17:03:33 +02:00
|
|
|
expect(store.roomId).toBeFalsy();
|
|
|
|
expect(store.connected).toEqual(false);
|
|
|
|
WidgetMessagingStore.instance.stopMessaging(widget, "!1:example.org");
|
2022-06-09 15:19:52 +02:00
|
|
|
|
|
|
|
// Our device should now be marked as disconnected
|
|
|
|
expect(cli.sendStateEvent).toHaveBeenLastCalledWith(
|
|
|
|
"!1:example.org",
|
|
|
|
VIDEO_CHANNEL_MEMBER,
|
|
|
|
{ devices: [], expires_ts: expect.any(Number) },
|
|
|
|
cli.getUserId(),
|
|
|
|
);
|
2022-04-20 17:03:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("waits for messaging when connecting", async () => {
|
2022-05-24 13:43:27 +02:00
|
|
|
const connectPromise = store.connect("!1:example.org", null, null);
|
2022-04-20 17:03:33 +02:00
|
|
|
WidgetMessagingStore.instance.storeMessaging(widget, "!1:example.org", messaging);
|
|
|
|
widgetReady();
|
|
|
|
await confirmConnect();
|
2022-05-24 13:43:27 +02:00
|
|
|
await expect(connectPromise).resolves.toBeUndefined();
|
2022-04-20 17:03:33 +02:00
|
|
|
expect(store.roomId).toEqual("!1:example.org");
|
|
|
|
expect(store.connected).toEqual(true);
|
2022-04-01 16:36:10 +02:00
|
|
|
|
2022-04-20 17:03:33 +02:00
|
|
|
store.disconnect();
|
|
|
|
await confirmDisconnect();
|
|
|
|
WidgetMessagingStore.instance.stopMessaging(widget, "!1:example.org");
|
2022-04-01 16:36:10 +02:00
|
|
|
});
|
2022-05-24 13:43:27 +02:00
|
|
|
|
|
|
|
it("rejects if the widget's messaging gets stopped mid-connect", async () => {
|
|
|
|
WidgetMessagingStore.instance.storeMessaging(widget, "!1:example.org", messaging);
|
|
|
|
widgetReady();
|
|
|
|
expect(store.roomId).toBeFalsy();
|
|
|
|
expect(store.connected).toEqual(false);
|
|
|
|
|
|
|
|
const connectPromise = store.connect("!1:example.org", null, null);
|
|
|
|
// Wait for the store to contact the widget API, then stop the messaging
|
|
|
|
await messageSent;
|
|
|
|
WidgetMessagingStore.instance.stopMessaging(widget, "!1:example.org");
|
|
|
|
await expect(connectPromise).rejects.toBeDefined();
|
|
|
|
expect(store.roomId).toBeFalsy();
|
|
|
|
expect(store.connected).toEqual(false);
|
|
|
|
});
|
2022-04-01 16:36:10 +02:00
|
|
|
});
|