* Take client creating functionality out of MatrixClientPeg Because all sorts of things import MatrixClientPeg and this means they pull in all manner of things related to creating a client, when all they need is to get the current one (and specifically it fixes the import cycle that means I can't add my test). * Remove fake indexeddb as it seems like the tests didn't actually see the fake indexeddb before (somehow) but now do, and are failing because they have that but no postmessage. It feels like the right solution is for these tests to not need indexeddb. * Don't mock a refresh token The tests don't mock out enough for the token refreshing setup to work, it wa somehow always ending up as null previously and now wasn't, at which point it broke, so just make it actually unset. * Move formatter instatiation to lazy rather than eagerly at parse time, as this apparently shifted one test to hit this import cycle instead. * Tests for room name generator
118 lines
5.1 KiB
TypeScript
118 lines
5.1 KiB
TypeScript
/*
|
|
Copyright 2024 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
|
|
Please see LICENSE files in the repository root for full details.
|
|
*/
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
import { type MatrixClient } from "matrix-js-sdk/src/matrix";
|
|
import fetchMock from "@fetch-mock/jest";
|
|
|
|
import { advanceDateAndTime, createTestClient, stubClient } from "../test-utils";
|
|
import { type IMatrixClientPeg, MatrixClientPeg as peg } from "../../src/MatrixClientPeg";
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const PegClass = Object.getPrototypeOf(peg).constructor;
|
|
|
|
describe("MatrixClientPeg", () => {
|
|
beforeEach(() => {
|
|
// stub out Logger.log which gets called a lot and clutters up the test output
|
|
jest.spyOn(logger, "log").mockImplementation(() => {});
|
|
});
|
|
|
|
afterEach(() => {
|
|
localStorage.clear();
|
|
jest.restoreAllMocks();
|
|
|
|
// some of the tests assign `MatrixClientPeg.matrixClient`: clear it, to prevent leakage between tests
|
|
peg.unset();
|
|
});
|
|
|
|
it("setJustRegisteredUserId", () => {
|
|
stubClient();
|
|
(peg as any).matrixClient = peg.get();
|
|
peg.setJustRegisteredUserId("@userId:matrix.org");
|
|
expect(peg.safeGet().credentials.userId).toBe("@userId:matrix.org");
|
|
expect(peg.currentUserIsJustRegistered()).toBe(true);
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(true);
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
|
|
advanceDateAndTime(1 * 60 * 60 * 1000 + 1);
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(true);
|
|
advanceDateAndTime(24 * 60 * 60 * 1000);
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
|
|
});
|
|
|
|
it("setJustRegisteredUserId(null)", () => {
|
|
stubClient();
|
|
(peg as any).matrixClient = peg.get();
|
|
peg.setJustRegisteredUserId(null);
|
|
expect(peg.currentUserIsJustRegistered()).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
|
|
advanceDateAndTime(1 * 60 * 60 * 1000 + 1);
|
|
expect(peg.userRegisteredWithinLastHours(0)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(1)).toBe(false);
|
|
expect(peg.userRegisteredWithinLastHours(24)).toBe(false);
|
|
});
|
|
|
|
describe(".start", () => {
|
|
let testPeg: IMatrixClientPeg;
|
|
|
|
beforeEach(() => {
|
|
// instantiate a MatrixClientPegClass instance, with a new MatrixClient
|
|
testPeg = new PegClass();
|
|
fetchMock.get("http://example.com/_matrix/client/versions", {});
|
|
|
|
const mockClient = createTestClient();
|
|
mockClient.initRustCrypto = jest.fn();
|
|
mockClient.startClient = jest.fn();
|
|
testPeg.set(mockClient as unknown as MatrixClient);
|
|
});
|
|
|
|
it("should initialise the rust crypto library by default", async () => {
|
|
const mockInitRustCrypto = jest.spyOn(testPeg.safeGet(), "initRustCrypto").mockResolvedValue(undefined);
|
|
|
|
const cryptoStoreKey = new Uint8Array([1, 2, 3, 4]);
|
|
await testPeg.start({ rustCryptoStoreKey: cryptoStoreKey });
|
|
expect(mockInitRustCrypto).toHaveBeenCalledWith({ storageKey: cryptoStoreKey });
|
|
});
|
|
|
|
it("should try to start dehydration if dehydration is enabled", async () => {
|
|
const mockInitRustCrypto = jest.spyOn(testPeg.safeGet(), "initRustCrypto").mockResolvedValue(undefined);
|
|
const mockStartDehydration = jest.fn();
|
|
jest.spyOn(testPeg.safeGet(), "getCrypto").mockReturnValue({
|
|
isDehydrationSupported: jest.fn().mockResolvedValue(true),
|
|
startDehydration: mockStartDehydration,
|
|
setDeviceIsolationMode: jest.fn(),
|
|
} as any);
|
|
jest.spyOn(testPeg.safeGet(), "waitForClientWellKnown").mockResolvedValue({
|
|
"m.homeserver": {
|
|
base_url: "http://example.com",
|
|
},
|
|
"org.matrix.msc3814": true,
|
|
} as any);
|
|
|
|
const cryptoStoreKey = new Uint8Array([1, 2, 3, 4]);
|
|
await testPeg.start({ rustCryptoStoreKey: cryptoStoreKey });
|
|
expect(mockInitRustCrypto).toHaveBeenCalledWith({ storageKey: cryptoStoreKey });
|
|
expect(mockStartDehydration).toHaveBeenCalledWith({ onlyIfKeyCached: true, rehydrate: false });
|
|
});
|
|
|
|
it("Should migrate existing login", async () => {
|
|
const mockInitRustCrypto = jest.spyOn(testPeg.safeGet(), "initRustCrypto").mockResolvedValue(undefined);
|
|
|
|
await testPeg.start();
|
|
expect(mockInitRustCrypto).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
});
|