Stop migrating to MSC4278 if the config exists. (#29924)

* Stop migrationg to MSC4278 if the config exists.

* Run migration after we have synced the client.

* Setup the SettingsController with a client.

* Add tests to check migration behaviour.

* update copyright

* Wait for sync properly

* Catch failure

* Docs

* licence

* Inline async code

* Fix migrateURLPreviewsE2EE too

* drop an import

* go away
This commit is contained in:
Will Hunt
2025-05-12 13:16:47 +01:00
committed by GitHub
parent 308f892cef
commit fb5c4ffc8b
3 changed files with 96 additions and 38 deletions

View File

@@ -164,6 +164,7 @@ export function createTestClient(): MatrixClient {
getVisibleRooms: jest.fn().mockReturnValue([]),
loginFlows: jest.fn(),
on: eventEmitter.on.bind(eventEmitter),
once: eventEmitter.once.bind(eventEmitter),
off: eventEmitter.off.bind(eventEmitter),
removeListener: eventEmitter.removeListener.bind(eventEmitter),
emit: eventEmitter.emit.bind(eventEmitter),

View File

@@ -1,5 +1,5 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2024, 2025 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
@@ -7,6 +7,7 @@ Please see LICENSE files in the repository root for full details.
*/
import { ClientEvent, type MatrixClient, type Room, SyncState } from "matrix-js-sdk/src/matrix";
import { waitFor } from "jest-matrix-react";
import type BasePlatform from "../../../src/BasePlatform";
import SdkConfig from "../../../src/SdkConfig";
@@ -14,6 +15,7 @@ import { SettingLevel } from "../../../src/settings/SettingLevel";
import SettingsStore from "../../../src/settings/SettingsStore";
import { mkStubRoom, mockPlatformPeg, stubClient } from "../../test-utils";
import { type SettingKey } from "../../../src/settings/Settings.tsx";
import MatrixClientBackedController from "../../../src/settings/controllers/MatrixClientBackedController.ts";
const TEST_DATA = [
{
@@ -139,5 +141,61 @@ describe("SettingsStore", () => {
expect(room.getAccountData).not.toHaveBeenCalled();
});
describe("Migrate media preview configuration", () => {
beforeEach(() => {
MatrixClientBackedController.matrixClient = client;
client.getAccountData = jest.fn().mockImplementation((type) => {
if (type === "im.vector.web.settings") {
return {
getContent: jest.fn().mockReturnValue({
showImages: false,
showAvatarsOnInvites: false,
}),
};
} else {
return undefined;
}
});
});
it("migrates media preview configuration immediately", async () => {
client.setAccountData = jest.fn();
SettingsStore.runMigrations(false);
expect(client.setAccountData).toHaveBeenCalledWith("io.element.msc4278.media_preview_config", {
invite_avatars: "off",
media_previews: "off",
});
});
it("migrates media preview configuration once client is ready", async () => {
client.setAccountData = jest.fn();
const mockInitialSync = (client.isInitialSyncComplete = jest.fn().mockReturnValue(false));
SettingsStore.runMigrations(false);
mockInitialSync.mockReturnValue(true);
client.emit(ClientEvent.Sync, SyncState.Prepared, null);
// Update is asynchronous
waitFor(() => {
expect(client.setAccountData).toHaveBeenCalledWith("io.element.msc4278.media_preview_config", {
invite_avatars: "off",
media_previews: "off",
});
});
});
it("does not migrate media preview configuration if the session is fresh", async () => {
client.setAccountData = jest.fn();
SettingsStore.runMigrations(true);
client.emit(ClientEvent.Sync, SyncState.Prepared, null);
expect(client.setAccountData).not.toHaveBeenCalled();
});
it("does not migrate media preview configuration if the account data is already set", async () => {
client.setAccountData = jest.fn();
client.getAccountData = jest.fn().mockReturnValue({});
SettingsStore.runMigrations(false);
client.emit(ClientEvent.Sync, SyncState.Prepared, null);
expect(client.setAccountData).not.toHaveBeenCalled();
});
});
});
});