Skip to content

Commit aaacc35

Browse files
committed
test: pull forwardToDeviceMessage out to helper.ts
This is a useful helper for other tests.
1 parent c02c3bd commit aaacc35

File tree

2 files changed

+65
-46
lines changed

2 files changed

+65
-46
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: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { DeviceLists, RequestType, KeysUploadRequest, KeysQueryRequest } from "@matrix-org/matrix-sdk-crypto-wasm";
18-
19-
export function* zip(...arrays) {
17+
import {
18+
DeviceLists,
19+
RequestType,
20+
KeysUploadRequest,
21+
KeysQueryRequest,
22+
ToDeviceRequest,
23+
OlmMachine,
24+
UserId,
25+
} from "@matrix-org/matrix-sdk-crypto-wasm";
26+
27+
export function* zip(...arrays: Array<Array<any>>): Generator<any> {
2028
const len = Math.min(...arrays.map((array) => array.length));
2129

2230
for (let nth = 0; nth < len; ++nth) {
@@ -45,9 +53,12 @@ export async function addMachineToMachine(machineToAdd: OlmMachine, machine: Olm
4553
expect(outgoingRequests).toHaveLength(2);
4654

4755
let keysUploadRequest;
56+
4857
// Read the `KeysUploadRequest`.
4958
{
5059
expect(outgoingRequests[0]).toBeInstanceOf(KeysUploadRequest);
60+
keysUploadRequest = outgoingRequests[0] as KeysUploadRequest;
61+
5162
expect(outgoingRequests[0].id).toBeDefined();
5263
expect(outgoingRequests[0].type).toStrictEqual(RequestType.KeysUpload);
5364
expect(outgoingRequests[0].body).toBeDefined();
@@ -64,27 +75,26 @@ export async function addMachineToMachine(machineToAdd: OlmMachine, machine: Olm
6475
},
6576
});
6677
const marked = await machineToAdd.markRequestAsSent(
67-
outgoingRequests[0].id,
78+
keysUploadRequest.id,
6879
outgoingRequests[0].type,
6980
hypotheticalResponse,
7081
);
7182
expect(marked).toStrictEqual(true);
72-
73-
keysUploadRequest = outgoingRequests[0];
7483
}
7584

7685
{
7786
expect(outgoingRequests[1]).toBeInstanceOf(KeysQueryRequest);
87+
let keysQueryRequest = outgoingRequests[1] as KeysQueryRequest;
7888

7989
let bootstrapCrossSigningResult = await machineToAdd.bootstrapCrossSigning(true);
8090
let signingKeysUploadRequest = bootstrapCrossSigningResult.uploadSigningKeysRequest;
8191

8292
// Let's forge a `KeysQuery`'s response.
8393
let keyQueryResponse = {
84-
device_keys: {},
85-
master_keys: {},
86-
self_signing_keys: {},
87-
user_signing_keys: {},
94+
device_keys: {} as Record<string, any>,
95+
master_keys: {} as Record<string, any>,
96+
self_signing_keys: {} as Record<string, any>,
97+
user_signing_keys: {} as Record<string, any>,
8898
};
8999
const userId = machineToAdd.userId.toString();
90100
const deviceId = machineToAdd.deviceId.toString();
@@ -97,10 +107,52 @@ export async function addMachineToMachine(machineToAdd: OlmMachine, machine: Olm
97107
keyQueryResponse.user_signing_keys[userId] = keys.user_signing_key;
98108

99109
const marked = await machine.markRequestAsSent(
100-
outgoingRequests[1].id,
101-
outgoingRequests[1].type,
110+
keysQueryRequest.id,
111+
keysQueryRequest.type,
102112
JSON.stringify(keyQueryResponse),
103113
);
104114
expect(marked).toStrictEqual(true);
105115
}
106116
}
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)