2022-10-04 15:53:23 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-04 15:53:23 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08: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-04 15:53:23 +08:00
|
|
|
*/
|
|
|
|
|
2022-10-04 21:12:23 +08:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import BasePlatform from "../../../../src/BasePlatform";
|
|
|
|
import { IConfigOptions } from "../../../../src/IConfigOptions";
|
|
|
|
import { getDeviceClientInformation, recordClientInformation } from "../../../../src/utils/device/clientInformation";
|
|
|
|
import { getMockClientWithEventEmitter } from "../../../test-utils";
|
|
|
|
import { DEFAULTS } from "../../../../src/SdkConfig";
|
|
|
|
import { DeepReadonly } from "../../../../src/@types/common";
|
2022-10-04 15:53:23 +08:00
|
|
|
|
|
|
|
describe("recordClientInformation()", () => {
|
|
|
|
const deviceId = "my-device-id";
|
|
|
|
const version = "1.2.3";
|
|
|
|
const isElectron = window.electron;
|
|
|
|
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getDeviceId: jest.fn().mockReturnValue(deviceId),
|
|
|
|
setAccountData: jest.fn(),
|
|
|
|
});
|
|
|
|
|
2023-04-26 17:36:00 +08:00
|
|
|
const sdkConfig: DeepReadonly<IConfigOptions> = {
|
|
|
|
...DEFAULTS,
|
2022-10-04 15:53:23 +08:00
|
|
|
brand: "Test Brand",
|
2022-10-08 02:10:17 +08:00
|
|
|
element_call: { url: "", use_exclusively: false, brand: "Element Call" },
|
2022-10-04 15:53:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const platform = {
|
|
|
|
getAppVersion: jest.fn().mockResolvedValue(version),
|
|
|
|
} as unknown as BasePlatform;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
2024-10-15 21:57:26 +08:00
|
|
|
window.electron = undefined;
|
2022-10-04 15:53:23 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
// restore global
|
|
|
|
window.electron = isElectron;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("saves client information without url for electron clients", async () => {
|
2024-10-15 21:57:26 +08:00
|
|
|
window.electron = {} as Electron;
|
2022-10-04 15:53:23 +08:00
|
|
|
|
|
|
|
await recordClientInformation(mockClient, sdkConfig, platform);
|
2022-12-12 19:24:14 +08:00
|
|
|
|
2022-10-04 15:53:23 +08:00
|
|
|
expect(mockClient.setAccountData).toHaveBeenCalledWith(`io.element.matrix_client_information.${deviceId}`, {
|
|
|
|
name: sdkConfig.brand,
|
|
|
|
version,
|
|
|
|
url: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("saves client information with url for non-electron clients", async () => {
|
|
|
|
await recordClientInformation(mockClient, sdkConfig, platform);
|
2022-12-12 19:24:14 +08:00
|
|
|
|
2022-10-04 15:53:23 +08:00
|
|
|
expect(mockClient.setAccountData).toHaveBeenCalledWith(`io.element.matrix_client_information.${deviceId}`, {
|
|
|
|
name: sdkConfig.brand,
|
|
|
|
version,
|
|
|
|
url: "localhost",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-10-04 21:12:23 +08:00
|
|
|
|
|
|
|
describe("getDeviceClientInformation()", () => {
|
|
|
|
const deviceId = "my-device-id";
|
|
|
|
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getAccountData: jest.fn(),
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns an empty object when no event exists for the device", () => {
|
|
|
|
expect(getDeviceClientInformation(mockClient, deviceId)).toEqual({});
|
|
|
|
|
|
|
|
expect(mockClient.getAccountData).toHaveBeenCalledWith(`io.element.matrix_client_information.${deviceId}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns client information for the device", () => {
|
|
|
|
const eventContent = {
|
|
|
|
name: "Element Web",
|
|
|
|
version: "1.2.3",
|
|
|
|
url: "test.com",
|
|
|
|
};
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
type: `io.element.matrix_client_information.${deviceId}`,
|
|
|
|
content: eventContent,
|
|
|
|
});
|
|
|
|
mockClient.getAccountData.mockReturnValue(event);
|
|
|
|
expect(getDeviceClientInformation(mockClient, deviceId)).toEqual(eventContent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("excludes values with incorrect types", () => {
|
|
|
|
const eventContent = {
|
|
|
|
extraField: "hello",
|
|
|
|
name: "Element Web",
|
|
|
|
// wrong format
|
|
|
|
version: { value: "1.2.3" },
|
|
|
|
url: "test.com",
|
|
|
|
};
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
type: `io.element.matrix_client_information.${deviceId}`,
|
|
|
|
content: eventContent,
|
|
|
|
});
|
|
|
|
mockClient.getAccountData.mockReturnValue(event);
|
|
|
|
// invalid fields excluded
|
|
|
|
expect(getDeviceClientInformation(mockClient, deviceId)).toEqual({
|
|
|
|
name: eventContent.name,
|
|
|
|
url: eventContent.url,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|