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