|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import "fake-indexeddb/auto"; |
| 18 | +import { IDBFactory } from "fake-indexeddb"; |
| 19 | + |
| 20 | +import { createClient } from "../../src"; |
| 21 | + |
| 22 | +afterEach(() => { |
| 23 | + // reset fake-indexeddb after each test, to make sure we don't leak connections |
| 24 | + // cf https://github.com/dumbmatter/fakeIndexedDB#wipingresetting-the-indexeddb-for-a-fresh-state |
| 25 | + // eslint-disable-next-line no-global-assign |
| 26 | + indexedDB = new IDBFactory(); |
| 27 | +}); |
| 28 | + |
| 29 | +describe("MatrixClient.initRustCrypto", () => { |
| 30 | + it("should raise if userId or deviceId is unknown", async () => { |
| 31 | + const unknownUserClient = createClient({ |
| 32 | + baseUrl: "http://test.server", |
| 33 | + deviceId: "aliceDevice", |
| 34 | + }); |
| 35 | + await expect(() => unknownUserClient.initRustCrypto()).rejects.toThrow("unknown userId"); |
| 36 | + |
| 37 | + const unknownDeviceClient = createClient({ |
| 38 | + baseUrl: "http://test.server", |
| 39 | + userId: "@alice:test", |
| 40 | + }); |
| 41 | + await expect(() => unknownDeviceClient.initRustCrypto()).rejects.toThrow("unknown deviceId"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should create the indexed dbs", async () => { |
| 45 | + const matrixClient = createClient({ |
| 46 | + baseUrl: "http://test.server", |
| 47 | + userId: "@alice:localhost", |
| 48 | + deviceId: "aliceDevice", |
| 49 | + }); |
| 50 | + |
| 51 | + // No databases. |
| 52 | + expect(await indexedDB.databases()).toHaveLength(0); |
| 53 | + |
| 54 | + await matrixClient.initRustCrypto(); |
| 55 | + |
| 56 | + // should have two dbs now |
| 57 | + const databaseNames = (await indexedDB.databases()).map((db) => db.name); |
| 58 | + expect(databaseNames).toEqual( |
| 59 | + expect.arrayContaining(["matrix-js-sdk::matrix-sdk-crypto", "matrix-js-sdk::matrix-sdk-crypto-meta"]), |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should ignore a second call", async () => { |
| 64 | + const matrixClient = createClient({ |
| 65 | + baseUrl: "http://test.server", |
| 66 | + userId: "@alice:localhost", |
| 67 | + deviceId: "aliceDevice", |
| 68 | + }); |
| 69 | + |
| 70 | + await matrixClient.initRustCrypto(); |
| 71 | + await matrixClient.initRustCrypto(); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("MatrixClient.clearStores", () => { |
| 76 | + it("should clear the indexeddbs", async () => { |
| 77 | + const matrixClient = createClient({ |
| 78 | + baseUrl: "http://test.server", |
| 79 | + userId: "@alice:localhost", |
| 80 | + deviceId: "aliceDevice", |
| 81 | + }); |
| 82 | + |
| 83 | + await matrixClient.initRustCrypto(); |
| 84 | + expect(await indexedDB.databases()).toHaveLength(2); |
| 85 | + await matrixClient.stopClient(); |
| 86 | + |
| 87 | + await matrixClient.clearStores(); |
| 88 | + expect(await indexedDB.databases()).toHaveLength(0); |
| 89 | + }); |
| 90 | +}); |
0 commit comments