Skip to content

Commit aedce92

Browse files
committed
Convert helper.js to typescript
1 parent 9a9e467 commit aedce92

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

tests/helper.js renamed to tests/helper.ts

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
const { DeviceLists, RequestType, KeysUploadRequest, KeysQueryRequest } = require("@matrix-org/matrix-sdk-crypto-wasm");
2-
3-
function* zip(...arrays) {
1+
/*
2+
Copyright 2022-2025 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+
DeviceLists,
19+
RequestType,
20+
KeysUploadRequest,
21+
KeysQueryRequest,
22+
OlmMachine,
23+
} from "@matrix-org/matrix-sdk-crypto-wasm";
24+
25+
export function* zip(...arrays: Array<Array<any>>): Generator<any> {
426
const len = Math.min(...arrays.map((array) => array.length));
527

628
for (let nth = 0; nth < len; ++nth) {
@@ -10,7 +32,7 @@ function* zip(...arrays) {
1032

1133
// Add a machine to another machine, i.e. be sure a machine knows
1234
// another exists.
13-
async function addMachineToMachine(machineToAdd, machine) {
35+
export async function addMachineToMachine(machineToAdd: OlmMachine, machine: OlmMachine): Promise<void> {
1436
const toDeviceEvents = JSON.stringify([]);
1537
const changedDevices = new DeviceLists();
1638
const oneTimeKeyCounts = new Map();
@@ -29,9 +51,12 @@ async function addMachineToMachine(machineToAdd, machine) {
2951
expect(outgoingRequests).toHaveLength(2);
3052

3153
let keysUploadRequest;
54+
3255
// Read the `KeysUploadRequest`.
3356
{
3457
expect(outgoingRequests[0]).toBeInstanceOf(KeysUploadRequest);
58+
keysUploadRequest = outgoingRequests[0] as KeysUploadRequest;
59+
3560
expect(outgoingRequests[0].id).toBeDefined();
3661
expect(outgoingRequests[0].type).toStrictEqual(RequestType.KeysUpload);
3762
expect(outgoingRequests[0].body).toBeDefined();
@@ -48,27 +73,26 @@ async function addMachineToMachine(machineToAdd, machine) {
4873
},
4974
});
5075
const marked = await machineToAdd.markRequestAsSent(
51-
outgoingRequests[0].id,
76+
keysUploadRequest.id,
5277
outgoingRequests[0].type,
5378
hypotheticalResponse,
5479
);
5580
expect(marked).toStrictEqual(true);
56-
57-
keysUploadRequest = outgoingRequests[0];
5881
}
5982

6083
{
6184
expect(outgoingRequests[1]).toBeInstanceOf(KeysQueryRequest);
85+
let keysQueryRequest = outgoingRequests[1] as KeysQueryRequest;
6286

6387
let bootstrapCrossSigningResult = await machineToAdd.bootstrapCrossSigning(true);
6488
let signingKeysUploadRequest = bootstrapCrossSigningResult.uploadSigningKeysRequest;
6589

6690
// Let's forge a `KeysQuery`'s response.
6791
let keyQueryResponse = {
68-
device_keys: {},
69-
master_keys: {},
70-
self_signing_keys: {},
71-
user_signing_keys: {},
92+
device_keys: {} as Record<string, any>,
93+
master_keys: {} as Record<string, any>,
94+
self_signing_keys: {} as Record<string, any>,
95+
user_signing_keys: {} as Record<string, any>,
7296
};
7397
const userId = machineToAdd.userId.toString();
7498
const deviceId = machineToAdd.deviceId.toString();
@@ -81,15 +105,10 @@ async function addMachineToMachine(machineToAdd, machine) {
81105
keyQueryResponse.user_signing_keys[userId] = keys.user_signing_key;
82106

83107
const marked = await machine.markRequestAsSent(
84-
outgoingRequests[1].id,
85-
outgoingRequests[1].type,
108+
keysQueryRequest.id,
109+
keysQueryRequest.type,
86110
JSON.stringify(keyQueryResponse),
87111
);
88112
expect(marked).toStrictEqual(true);
89113
}
90114
}
91-
92-
module.exports = {
93-
zip,
94-
addMachineToMachine,
95-
};

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"extends": "@tsconfig/node18/tsconfig.json",
33
"compilerOptions": {
44
"lib": ["DOM"],
5-
"allowJs": true
5+
"allowJs": true,
6+
// Allow .ts file extensions in `import`, otherwise Jest can't find `helper.ts`.
7+
"allowImportingTsExtensions": true
68
},
79
"typedocOptions": {
810
"entryPoints": ["index.d.ts"],

0 commit comments

Comments
 (0)