Skip to content

Commit bd6dda1

Browse files
committed
use makeKey to reduce test loc
1 parent 985ead7 commit bd6dda1

File tree

3 files changed

+23
-108
lines changed

3 files changed

+23
-108
lines changed

spec/unit/matrixrtc/MatrixRTCSession.spec.ts

+18-103
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { DEFAULT_EXPIRE_DURATION, type SessionMembershipData } from "../../../sr
2020
import { MatrixRTCSession, MatrixRTCSessionEvent } from "../../../src/matrixrtc/MatrixRTCSession";
2121
import { type EncryptionKeysEventContent } from "../../../src/matrixrtc/types";
2222
import { secureRandomString } from "../../../src/randomstring";
23-
import { makeMockEvent, makeMockRoom, makeMockRoomState, membershipTemplate } from "./mocks";
23+
import { makeMockEvent, makeMockRoom, makeMockRoomState, membershipTemplate, makeKey } from "./mocks";
2424

2525
const mockFocus = { type: "mock" };
2626

@@ -534,12 +534,7 @@ describe("MatrixRTCSession", () => {
534534
{
535535
call_id: "",
536536
device_id: "AAAAAAA",
537-
keys: [
538-
{
539-
index: 0,
540-
key: expect.stringMatching(".*"),
541-
},
542-
],
537+
keys: [makeKey(0, expect.stringMatching(".*"))],
543538
sent_ts: Date.now(),
544539
},
545540
);
@@ -742,12 +737,7 @@ describe("MatrixRTCSession", () => {
742737
{
743738
call_id: "",
744739
device_id: "AAAAAAA",
745-
keys: [
746-
{
747-
index: 0,
748-
key: expect.stringMatching(".*"),
749-
},
750-
],
740+
keys: [makeKey(0, expect.stringMatching(".*"))],
751741
sent_ts: Date.now(),
752742
},
753743
);
@@ -796,12 +786,7 @@ describe("MatrixRTCSession", () => {
796786
{
797787
call_id: "",
798788
device_id: "AAAAAAA",
799-
keys: [
800-
{
801-
index: 0,
802-
key: expect.stringMatching(".*"),
803-
},
804-
],
789+
keys: [makeKey(0, expect.stringMatching(".*"))],
805790
sent_ts: Date.now(),
806791
},
807792
);
@@ -834,12 +819,7 @@ describe("MatrixRTCSession", () => {
834819
{
835820
call_id: "",
836821
device_id: "AAAAAAA",
837-
keys: [
838-
{
839-
index: 0,
840-
key: expect.stringMatching(".*"),
841-
},
842-
],
822+
keys: [makeKey(0, expect.stringMatching(".*"))],
843823
sent_ts: Date.now(),
844824
},
845825
);
@@ -996,12 +976,7 @@ describe("MatrixRTCSession", () => {
996976
makeMockEvent("io.element.call.encryption_keys", "@bob:example.org", "1234roomId", {
997977
device_id: "bobsphone",
998978
call_id: "",
999-
keys: [
1000-
{
1001-
index: 0,
1002-
key: "dGhpcyBpcyB0aGUga2V5",
1003-
},
1004-
],
979+
keys: [makeKey(0, "dGhpcyBpcyB0aGUga2V5")],
1005980
}),
1006981
);
1007982
await jest.advanceTimersToNextTimerAsync();
@@ -1026,12 +1001,7 @@ describe("MatrixRTCSession", () => {
10261001
makeMockEvent("io.element.call.encryption_keys", "@bob:example.org", "1234roomId", {
10271002
device_id: "bobsphone",
10281003
call_id: "",
1029-
keys: [
1030-
{
1031-
index: 4,
1032-
key: "dGhpcyBpcyB0aGUga2V5",
1033-
},
1034-
],
1004+
keys: [makeKey(4, "dGhpcyBpcyB0aGUga2V5")],
10351005
}),
10361006
);
10371007
await jest.advanceTimersToNextTimerAsync();
@@ -1057,12 +1027,7 @@ describe("MatrixRTCSession", () => {
10571027
makeMockEvent("io.element.call.encryption_keys", "@bob:example.org", "1234roomId", {
10581028
device_id: "bobsphone",
10591029
call_id: "",
1060-
keys: [
1061-
{
1062-
index: 0,
1063-
key: "dGhpcyBpcyB0aGUga2V5",
1064-
},
1065-
],
1030+
keys: [makeKey(0, "dGhpcyBpcyB0aGUga2V5")],
10661031
}),
10671032
);
10681033
await jest.advanceTimersToNextTimerAsync();
@@ -1083,12 +1048,7 @@ describe("MatrixRTCSession", () => {
10831048
makeMockEvent("io.element.call.encryption_keys", "@bob:example.org", "1234roomId", {
10841049
device_id: "bobsphone",
10851050
call_id: "",
1086-
keys: [
1087-
{
1088-
index: 4,
1089-
key: "dGhpcyBpcyB0aGUga2V5",
1090-
},
1091-
],
1051+
keys: [makeKey(4, "dGhpcyBpcyB0aGUga2V5")],
10921052
}),
10931053
);
10941054
await jest.advanceTimersToNextTimerAsync();
@@ -1122,12 +1082,7 @@ describe("MatrixRTCSession", () => {
11221082
{
11231083
device_id: "bobsphone",
11241084
call_id: "",
1125-
keys: [
1126-
{
1127-
index: 0,
1128-
key: encodeBase64(Buffer.from("newer key", "utf-8")),
1129-
},
1130-
],
1085+
keys: [makeKey(0, encodeBase64(Buffer.from("newer key", "utf-8")))],
11311086
},
11321087
2000,
11331088
),
@@ -1141,12 +1096,7 @@ describe("MatrixRTCSession", () => {
11411096
{
11421097
device_id: "bobsphone",
11431098
call_id: "",
1144-
keys: [
1145-
{
1146-
index: 0,
1147-
key: encodeBase64(Buffer.from("newer key", "utf-8")),
1148-
},
1149-
],
1099+
keys: [makeKey(0, encodeBase64(Buffer.from("newer key", "utf-8")))],
11501100
},
11511101
2000,
11521102
),
@@ -1159,12 +1109,7 @@ describe("MatrixRTCSession", () => {
11591109
{
11601110
device_id: "bobsphone",
11611111
call_id: "",
1162-
keys: [
1163-
{
1164-
index: 0,
1165-
key: encodeBase64(Buffer.from("older key", "utf-8")),
1166-
},
1167-
],
1112+
keys: [makeKey(0, encodeBase64(Buffer.from("older key", "utf-8")))],
11681113
},
11691114
1000,
11701115
),
@@ -1196,12 +1141,7 @@ describe("MatrixRTCSession", () => {
11961141
{
11971142
device_id: "bobsphone",
11981143
call_id: "",
1199-
keys: [
1200-
{
1201-
index: 0,
1202-
key: encodeBase64(Buffer.from("first key", "utf-8")),
1203-
},
1204-
],
1144+
keys: [makeKey(0, encodeBase64(Buffer.from("older key", "utf-8")))],
12051145
},
12061146
1000,
12071147
),
@@ -1215,12 +1155,7 @@ describe("MatrixRTCSession", () => {
12151155
{
12161156
device_id: "bobsphone",
12171157
call_id: "",
1218-
keys: [
1219-
{
1220-
index: 0,
1221-
key: encodeBase64(Buffer.from("second key", "utf-8")),
1222-
},
1223-
],
1158+
keys: [makeKey(0, encodeBase64(Buffer.from("second key", "utf-8")))],
12241159
},
12251160
1000,
12261161
),
@@ -1247,12 +1182,7 @@ describe("MatrixRTCSession", () => {
12471182
makeMockEvent("io.element.call.encryption_keys", client.getUserId()!, "1234roomId", {
12481183
device_id: client.getDeviceId(),
12491184
call_id: "",
1250-
keys: [
1251-
{
1252-
index: 4,
1253-
key: "dGhpcyBpcyB0aGUga2V5",
1254-
},
1255-
],
1185+
keys: [makeKey(4, "dGhpcyBpcyB0aGUga2V5")],
12561186
}),
12571187
);
12581188

@@ -1281,12 +1211,7 @@ describe("MatrixRTCSession", () => {
12811211
{
12821212
device_id: "bobsphone",
12831213
call_id: "",
1284-
keys: [
1285-
{
1286-
index: 0,
1287-
key: "dGhpcyBpcyB0aGUga2V5",
1288-
},
1289-
],
1214+
keys: [makeKey(0, "dGhpcyBpcyB0aGUga2V5")],
12901215
},
12911216
0,
12921217
),
@@ -1301,12 +1226,7 @@ describe("MatrixRTCSession", () => {
13011226
makeMockEvent("io.element.call.encryption_keys", "@bob:example.org", "1234roomId", {
13021227
device_id: "bobsphone",
13031228
call_id: "",
1304-
keys: [
1305-
{
1306-
index: 0,
1307-
key: "dGhpcyBpcyB0aGUga2V5",
1308-
},
1309-
],
1229+
keys: [makeKey(0, "dGhpcyBpcyB0aGUga2V5")],
13101230
sent_ts: 0,
13111231
}),
13121232
);
@@ -1319,12 +1239,7 @@ describe("MatrixRTCSession", () => {
13191239
makeMockEvent("io.element.call.encryption_keys", "@bob:example.org", "1234roomId", {
13201240
device_id: "bobsphone",
13211241
call_id: "",
1322-
keys: [
1323-
{
1324-
index: 0,
1325-
key: "dGhpcyBpcyB0aGUga2V5",
1326-
},
1327-
],
1242+
keys: [makeKey(0, "dGhpcyBpcyB0aGUga2V5")],
13281243
sent_ts: 1000,
13291244
}),
13301245
);

spec/unit/matrixrtc/RoomKeyTransport.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { makeMockEvent, makeMockRoom, membershipTemplate, mockKey } from "./mocks";
17+
import { makeMockEvent, makeMockRoom, membershipTemplate, makeKey } from "./mocks";
1818
import { RoomKeyTransport } from "../../../src/matrixrtc/RoomKeyTransport";
1919
import { KeyTransportEvents } from "../../../src/matrixrtc/IKeyTransport";
2020
import { EventType, MatrixClient, RoomEvent } from "../../../src";
@@ -58,7 +58,7 @@ describe("RoomKyTransport", () => {
5858
client.decryptEventIfNeeded = () => Promise.resolve();
5959
const timelineEvent = makeMockEvent(EventType.CallEncryptionKeysPrefix, "@mock:user.example", "!room:id", {
6060
call_id: "",
61-
keys: [mockKey(0, "testKey")],
61+
keys: [makeKey(0, "testKey")],
6262
sent_ts: Date.now(),
6363
device_id: "AAAAAAA",
6464
});
@@ -82,7 +82,7 @@ describe("RoomKyTransport", () => {
8282
const timelineEvent = Object.assign(
8383
makeMockEvent(EventType.CallEncryptionKeysPrefix, "@mock:user.example", "!room:id", {
8484
call_id: "",
85-
keys: [mockKey(0, "testKey")],
85+
keys: [makeKey(0, "testKey")],
8686
sent_ts: Date.now(),
8787
device_id: "AAAAAAA",
8888
}),
@@ -110,7 +110,7 @@ describe("RoomKyTransport", () => {
110110
const timelineEvent = Object.assign(
111111
makeMockEvent(EventType.CallEncryptionKeysPrefix, "@mock:user.example", "!room:id", {
112112
call_id: "",
113-
keys: [mockKey(0, "testKey")],
113+
keys: [makeKey(0, "testKey")],
114114
sent_ts: Date.now(),
115115
device_id: "AAAAAAA",
116116
}),

spec/unit/matrixrtc/mocks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export function mockCallMembership(membershipData: MembershipData, roomId: strin
147147
return new CallMembership(mockRTCEvent(membershipData, roomId, sender), membershipData);
148148
}
149149

150-
export function mockKey(id: number, key: string): { key: string; index: number } {
150+
export function makeKey(id: number, key: string): { key: string; index: number } {
151151
return {
152152
key: key,
153153
index: id,

0 commit comments

Comments
 (0)