@@ -14,9 +14,17 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
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 > {
20
28
const len = Math . min ( ...arrays . map ( ( array ) => array . length ) ) ;
21
29
22
30
for ( let nth = 0 ; nth < len ; ++ nth ) {
@@ -45,9 +53,12 @@ export async function addMachineToMachine(machineToAdd: OlmMachine, machine: Olm
45
53
expect ( outgoingRequests ) . toHaveLength ( 2 ) ;
46
54
47
55
let keysUploadRequest ;
56
+
48
57
// Read the `KeysUploadRequest`.
49
58
{
50
59
expect ( outgoingRequests [ 0 ] ) . toBeInstanceOf ( KeysUploadRequest ) ;
60
+ keysUploadRequest = outgoingRequests [ 0 ] as KeysUploadRequest ;
61
+
51
62
expect ( outgoingRequests [ 0 ] . id ) . toBeDefined ( ) ;
52
63
expect ( outgoingRequests [ 0 ] . type ) . toStrictEqual ( RequestType . KeysUpload ) ;
53
64
expect ( outgoingRequests [ 0 ] . body ) . toBeDefined ( ) ;
@@ -64,27 +75,26 @@ export async function addMachineToMachine(machineToAdd: OlmMachine, machine: Olm
64
75
} ,
65
76
} ) ;
66
77
const marked = await machineToAdd . markRequestAsSent (
67
- outgoingRequests [ 0 ] . id ,
78
+ keysUploadRequest . id ,
68
79
outgoingRequests [ 0 ] . type ,
69
80
hypotheticalResponse ,
70
81
) ;
71
82
expect ( marked ) . toStrictEqual ( true ) ;
72
-
73
- keysUploadRequest = outgoingRequests [ 0 ] ;
74
83
}
75
84
76
85
{
77
86
expect ( outgoingRequests [ 1 ] ) . toBeInstanceOf ( KeysQueryRequest ) ;
87
+ let keysQueryRequest = outgoingRequests [ 1 ] as KeysQueryRequest ;
78
88
79
89
let bootstrapCrossSigningResult = await machineToAdd . bootstrapCrossSigning ( true ) ;
80
90
let signingKeysUploadRequest = bootstrapCrossSigningResult . uploadSigningKeysRequest ;
81
91
82
92
// Let's forge a `KeysQuery`'s response.
83
93
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 > ,
88
98
} ;
89
99
const userId = machineToAdd . userId . toString ( ) ;
90
100
const deviceId = machineToAdd . deviceId . toString ( ) ;
@@ -97,10 +107,52 @@ export async function addMachineToMachine(machineToAdd: OlmMachine, machine: Olm
97
107
keyQueryResponse . user_signing_keys [ userId ] = keys . user_signing_key ;
98
108
99
109
const marked = await machine . markRequestAsSent (
100
- outgoingRequests [ 1 ] . id ,
101
- outgoingRequests [ 1 ] . type ,
110
+ keysQueryRequest . id ,
111
+ keysQueryRequest . type ,
102
112
JSON . stringify ( keyQueryResponse ) ,
103
113
) ;
104
114
expect ( marked ) . toStrictEqual ( true ) ;
105
115
}
106
116
}
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