Skip to content

Commit 6804f18

Browse files
committed
test: pull forwardToDeviceMessage out to helper.ts
This is a useful helper for other tests.
1 parent 6d00023 commit 6804f18

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

tests/device.test.js

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const {
2626
QrCode,
2727
QrCodeScan,
2828
} = require("@matrix-org/matrix-sdk-crypto-wasm");
29-
const { zip, addMachineToMachine } = require("./helper");
29+
const { zip, addMachineToMachine, forwardToDeviceMessage } = require("./helper");
3030
const { VerificationRequestPhase, QrState } = require("@matrix-org/matrix-sdk-crypto-wasm");
3131

3232
// Uncomment to enable debug logging for tests
@@ -1122,36 +1122,3 @@ describe("VerificationMethod", () => {
11221122
expect(VerificationMethod.ReciprocateV1).toStrictEqual(3);
11231123
});
11241124
});
1125-
1126-
/**
1127-
* Forward an outgoing to-device message returned by one OlmMachine into another OlmMachine.
1128-
*/
1129-
async function forwardToDeviceMessage(sendingUser, recipientMachine, outgoingVerificationRequest) {
1130-
expect(outgoingVerificationRequest).toBeInstanceOf(ToDeviceRequest);
1131-
await sendToDeviceMessageIntoMachine(
1132-
sendingUser,
1133-
outgoingVerificationRequest.event_type,
1134-
JSON.parse(outgoingVerificationRequest.body).messages[recipientMachine.userId.toString()][
1135-
recipientMachine.deviceId.toString()
1136-
],
1137-
recipientMachine,
1138-
);
1139-
}
1140-
1141-
/**
1142-
* Send a to-device message into an OlmMachine.
1143-
*/
1144-
async function sendToDeviceMessageIntoMachine(sendingUser, eventType, content, recipientMachine) {
1145-
await recipientMachine.receiveSyncChanges(
1146-
JSON.stringify([
1147-
{
1148-
sender: sendingUser.toString(),
1149-
type: eventType,
1150-
content: content,
1151-
},
1152-
]),
1153-
new DeviceLists(),
1154-
new Map(),
1155-
undefined,
1156-
);
1157-
}

tests/helper.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import {
1919
RequestType,
2020
KeysUploadRequest,
2121
KeysQueryRequest,
22+
ToDeviceRequest,
2223
OlmMachine,
24+
UserId,
2325
} from "@matrix-org/matrix-sdk-crypto-wasm";
2426

2527
export function* zip(...arrays: Array<Array<any>>): Generator<any> {
@@ -112,3 +114,45 @@ export async function addMachineToMachine(machineToAdd: OlmMachine, machine: Olm
112114
expect(marked).toStrictEqual(true);
113115
}
114116
}
117+
118+
/**
119+
* Forward an outgoing to-device message returned by one OlmMachine into another OlmMachine.
120+
*/
121+
export async function forwardToDeviceMessage(
122+
sendingUser: UserId,
123+
recipientMachine: OlmMachine,
124+
toDeviceRequest: ToDeviceRequest,
125+
): Promise<void> {
126+
expect(toDeviceRequest).toBeInstanceOf(ToDeviceRequest);
127+
await sendToDeviceMessageIntoMachine(
128+
sendingUser,
129+
toDeviceRequest.event_type,
130+
JSON.parse(toDeviceRequest.body).messages[recipientMachine.userId.toString()][
131+
recipientMachine.deviceId.toString()
132+
],
133+
recipientMachine,
134+
);
135+
}
136+
137+
/**
138+
* Send a to-device message into an OlmMachine.
139+
*/
140+
export async function sendToDeviceMessageIntoMachine(
141+
sendingUser: UserId,
142+
eventType: string,
143+
content: object,
144+
recipientMachine: OlmMachine,
145+
): Promise<void> {
146+
await recipientMachine.receiveSyncChanges(
147+
JSON.stringify([
148+
{
149+
sender: sendingUser.toString(),
150+
type: eventType,
151+
content: content,
152+
},
153+
]),
154+
new DeviceLists(),
155+
new Map(),
156+
undefined,
157+
);
158+
}

0 commit comments

Comments
 (0)