Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 4a04be6

Browse files
author
Kerry
authored
Test typescriptification continued (#8327)
* test/utils/MegolmExportEncryption-test.js -> test/utils/MegolmExportEncryption-test.ts Signed-off-by: Kerry Archibald <[email protected]> * test/utils/ShieldUtils-test.js - test/utils/ShieldUtils-test.ts Signed-off-by: Kerry Archibald <[email protected]> * type fixes for ShieldUtils-test Signed-off-by: Kerry Archibald <[email protected]> * test/DecryptionFailureTracker-test.js -> test/DecryptionFailureTracker-test.ts Signed-off-by: Kerry Archibald <[email protected]> * remove unsupported assertion failure messages Signed-off-by: Kerry Archibald <[email protected]> * fix ts issues in DecryptionFailureTracker Signed-off-by: Kerry Archibald <[email protected]> * add mock restores Signed-off-by: Kerry Archibald <[email protected]> * newline Signed-off-by: Kerry Archibald <[email protected]> * remove commented decriptionfailuretracker code and test Signed-off-by: Kerry Archibald <[email protected]> * make should aggregate error codes correctly pass Signed-off-by: Kerry Archibald <[email protected]> * cheaters types in MegolmExportEncryption Signed-off-by: Kerry Archibald <[email protected]> * lint Signed-off-by: Kerry Archibald <[email protected]> * Revert "fix ts issues in DecryptionFailureTracker" This reverts commit 1ae748c. Signed-off-by: Kerry Archibald <[email protected]> * Revert "remove unsupported assertion failure messages" This reverts commit 7bd93d0. * Revert "test/DecryptionFailureTracker-test.js -> test/DecryptionFailureTracker-test.ts" This reverts commit 1670025. * revert change to DecryptionFailureTracker Signed-off-by: Kerry Archibald <[email protected]>
1 parent 633229c commit 4a04be6

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

test/utils/MegolmExportEncryption-test.js renamed to test/utils/MegolmExportEncryption-test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { Crypto } from "@peculiar/webcrypto";
2020

2121
const webCrypto = new Crypto();
2222

23-
function getRandomValues(buf) {
23+
function getRandomValues<T extends ArrayBufferView>(buf: T): T {
24+
// @ts-ignore fussy generics
2425
return nodeCrypto.randomFillSync(buf);
2526
}
2627

@@ -64,15 +65,18 @@ const TEST_VECTORS=[
6465
],
6566
];
6667

67-
function stringToArray(s) {
68+
function stringToArray(s: string): ArrayBufferLike {
6869
return new TextEncoder().encode(s).buffer;
6970
}
7071

7172
describe('MegolmExportEncryption', function() {
7273
let MegolmExportEncryption;
7374

7475
beforeAll(() => {
75-
window.crypto = { subtle: webCrypto.subtle, getRandomValues };
76+
window.crypto = {
77+
subtle: webCrypto.subtle,
78+
getRandomValues,
79+
};
7680
MegolmExportEncryption = require("../../src/utils/MegolmExportEncryption");
7781
});
7882

test/utils/ShieldUtils-test.js renamed to test/utils/ShieldUtils-test.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
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 {
18+
MatrixClient,
19+
Room,
20+
} from 'matrix-js-sdk/src/matrix';
21+
122
import { shieldStatusForRoom } from '../../src/utils/ShieldUtils';
223
import DMRoomMap from '../../src/utils/DMRoomMap';
324

4-
function mkClient(selfTrust) {
25+
function mkClient(selfTrust = false) {
526
return {
627
getUserId: () => "@self:localhost",
728
checkUserTrust: (userId) => ({
@@ -12,7 +33,7 @@ function mkClient(selfTrust) {
1233
isVerified: () => userId === "@self:localhost" ? selfTrust : userId[2] == "T",
1334
}),
1435
getStoredDevicesForUser: (userId) => ["DEVICE"],
15-
};
36+
} as unknown as MatrixClient;
1637
}
1738

1839
describe("mkClient self-test", function() {
@@ -42,9 +63,14 @@ describe("mkClient self-test", function() {
4263

4364
describe("shieldStatusForMembership self-trust behaviour", function() {
4465
beforeAll(() => {
45-
DMRoomMap.sharedInstance = {
66+
const mockInstance = {
4667
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
47-
};
68+
} as unknown as DMRoomMap;
69+
jest.spyOn(DMRoomMap, 'shared').mockReturnValue(mockInstance);
70+
});
71+
72+
afterAll(() => {
73+
jest.spyOn(DMRoomMap, 'shared').mockRestore();
4874
});
4975

5076
it.each(
@@ -55,7 +81,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
5581
const room = {
5682
roomId: dm ? "DM" : "other",
5783
getEncryptionTargetMembers: () => ["@self:localhost", "@FF1:h", "@FF2:h"].map((userId) => ({ userId })),
58-
};
84+
} as unknown as Room;
5985
const status = await shieldStatusForRoom(client, room);
6086
expect(status).toEqual("normal");
6187
});
@@ -68,7 +94,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
6894
const room = {
6995
roomId: dm ? "DM" : "other",
7096
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@TT2:h"].map((userId) => ({ userId })),
71-
};
97+
} as unknown as Room;
7298
const status = await shieldStatusForRoom(client, room);
7399
expect(status).toEqual(result);
74100
});
@@ -81,7 +107,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
81107
const room = {
82108
roomId: dm ? "DM" : "other",
83109
getEncryptionTargetMembers: () => ["@self:localhost", "@TT1:h", "@FF2:h"].map((userId) => ({ userId })),
84-
};
110+
} as unknown as Room;
85111
const status = await shieldStatusForRoom(client, room);
86112
expect(status).toEqual(result);
87113
});
@@ -94,7 +120,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
94120
const room = {
95121
roomId: dm ? "DM" : "other",
96122
getEncryptionTargetMembers: () => ["@self:localhost"].map((userId) => ({ userId })),
97-
};
123+
} as unknown as Room;
98124
const status = await shieldStatusForRoom(client, room);
99125
expect(status).toEqual(result);
100126
});
@@ -107,7 +133,7 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
107133
const room = {
108134
roomId: dm ? "DM" : "other",
109135
getEncryptionTargetMembers: () => ["@self:localhost", "@TT:h"].map((userId) => ({ userId })),
110-
};
136+
} as unknown as Room;
111137
const status = await shieldStatusForRoom(client, room);
112138
expect(status).toEqual(result);
113139
});
@@ -120,17 +146,18 @@ describe("shieldStatusForMembership self-trust behaviour", function() {
120146
const room = {
121147
roomId: dm ? "DM" : "other",
122148
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h"].map((userId) => ({ userId })),
123-
};
149+
} as unknown as Room;
124150
const status = await shieldStatusForRoom(client, room);
125151
expect(status).toEqual(result);
126152
});
127153
});
128154

129155
describe("shieldStatusForMembership other-trust behaviour", function() {
130156
beforeAll(() => {
131-
DMRoomMap.sharedInstance = {
157+
const mockInstance = {
132158
getUserIdForRoomId: (roomId) => roomId === "DM" ? "@any:h" : null,
133-
};
159+
} as unknown as DMRoomMap;
160+
jest.spyOn(DMRoomMap, 'shared').mockReturnValue(mockInstance);
134161
});
135162

136163
it.each(
@@ -140,7 +167,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
140167
const room = {
141168
roomId: dm ? "DM" : "other",
142169
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h"].map((userId) => ({ userId })),
143-
};
170+
} as unknown as Room;
144171
const status = await shieldStatusForRoom(client, room);
145172
expect(status).toEqual(result);
146173
});
@@ -152,7 +179,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
152179
const room = {
153180
roomId: dm ? "DM" : "other",
154181
getEncryptionTargetMembers: () => ["@self:localhost", "@TF:h", "@TT:h"].map((userId) => ({ userId })),
155-
};
182+
} as unknown as Room;
156183
const status = await shieldStatusForRoom(client, room);
157184
expect(status).toEqual(result);
158185
});
@@ -164,7 +191,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
164191
const room = {
165192
roomId: dm ? "DM" : "other",
166193
getEncryptionTargetMembers: () => ["@self:localhost", "@FF:h", "@FT:h"].map((userId) => ({ userId })),
167-
};
194+
} as unknown as Room;
168195
const status = await shieldStatusForRoom(client, room);
169196
expect(status).toEqual(result);
170197
});
@@ -176,7 +203,7 @@ describe("shieldStatusForMembership other-trust behaviour", function() {
176203
const room = {
177204
roomId: dm ? "DM" : "other",
178205
getEncryptionTargetMembers: () => ["@self:localhost", "@WF:h", "@FT:h"].map((userId) => ({ userId })),
179-
};
206+
} as unknown as Room;
180207
const status = await shieldStatusForRoom(client, room);
181208
expect(status).toEqual(result);
182209
});

0 commit comments

Comments
 (0)