@@ -19,20 +19,23 @@ import nodeCrypto from "crypto";
1919
2020import { TestClient } from '../../../TestClient' ;
2121import { MatrixEvent } from "../../../../src/models/event" ;
22+ import { IRoomTimelineData } from "../../../../src/models/event-timeline-set" ;
23+ import { Room , RoomEvent } from "../../../../src/models/room" ;
2224import { logger } from '../../../../src/logger' ;
25+ import { MatrixClient , ClientEvent } from '../../../../src/client' ;
2326
24- export async function makeTestClients ( userInfos , options ) {
25- const clients = [ ] ;
26- const timeouts = [ ] ;
27- const clientMap = { } ;
28- const sendToDevice = function ( type , map ) {
27+ export async function makeTestClients ( userInfos , options ) : Promise < [ TestClient [ ] , ( ) => void ] > {
28+ const clients : TestClient [ ] = [ ] ;
29+ const timeouts : ReturnType < typeof setTimeout > [ ] = [ ] ;
30+ const clientMap : Record < string , Record < string , MatrixClient > > = { } ;
31+ const makeSendToDevice = ( matrixClient : MatrixClient ) : MatrixClient [ ' sendToDevice' ] => async ( type , map ) => {
2932 // logger.log(this.getUserId(), "sends", type, map);
3033 for ( const [ userId , devMap ] of Object . entries ( map ) ) {
3134 if ( userId in clientMap ) {
3235 for ( const [ deviceId , msg ] of Object . entries ( devMap ) ) {
3336 if ( deviceId in clientMap [ userId ] ) {
3437 const event = new MatrixEvent ( {
35- sender : this . getUserId ( ) , // eslint-disable-line @babel/no-invalid-this
38+ sender : matrixClient . getUserId ( ) ! ,
3639 type : type ,
3740 content : msg ,
3841 } ) ;
@@ -42,18 +45,19 @@ export async function makeTestClients(userInfos, options) {
4245 Promise . resolve ( ) ;
4346
4447 decryptionPromise . then (
45- ( ) => client . emit ( "toDeviceEvent" , event ) ,
48+ ( ) => client . emit ( ClientEvent . ToDeviceEvent , event ) ,
4649 ) ;
4750 }
4851 }
4952 }
5053 }
54+ return { } ;
5155 } ;
52- const sendEvent = function ( room , type , content ) {
56+ const makeSendEvent = ( matrixClient : MatrixClient ) => ( room , type , content ) => {
5357 // make up a unique ID as the event ID
54- const eventId = "$" + this . makeTxnId ( ) ; // eslint-disable-line @babel/no-invalid-this
58+ const eventId = "$" + matrixClient . makeTxnId ( ) ;
5559 const rawEvent = {
56- sender : this . getUserId ( ) , // eslint-disable-line @babel/no-invalid-this
60+ sender : matrixClient . getUserId ( ) ! ,
5761 type : type ,
5862 content : content ,
5963 room_id : room ,
@@ -63,22 +67,24 @@ export async function makeTestClients(userInfos, options) {
6367 const event = new MatrixEvent ( rawEvent ) ;
6468 const remoteEcho = new MatrixEvent ( Object . assign ( { } , rawEvent , {
6569 unsigned : {
66- transaction_id : this . makeTxnId ( ) , // eslint-disable-line @babel/no-invalid-this
70+ transaction_id : matrixClient . makeTxnId ( ) ,
6771 } ,
6872 } ) ) ;
6973
7074 const timeout = setTimeout ( ( ) => {
7175 for ( const tc of clients ) {
72- if ( tc . client === this ) { // eslint-disable-line @babel/no-invalid-this
76+ const room = new Room ( 'test' , tc . client , tc . client . getUserId ( ) ! ) ;
77+ const roomTimelineData = { } as unknown as IRoomTimelineData ;
78+ if ( tc . client === matrixClient ) {
7379 logger . log ( "sending remote echo!!" ) ;
74- tc . client . emit ( "Room.timeline" , remoteEcho ) ;
80+ tc . client . emit ( RoomEvent . Timeline , remoteEcho , room , false , false , roomTimelineData ) ;
7581 } else {
76- tc . client . emit ( "Room.timeline" , event ) ;
82+ tc . client . emit ( RoomEvent . Timeline , event , room , false , false , roomTimelineData ) ;
7783 }
7884 }
7985 } ) ;
8086
81- timeouts . push ( timeout ) ;
87+ timeouts . push ( timeout as unknown as ReturnType < typeof setTimeout > ) ;
8288
8389 return Promise . resolve ( { event_id : eventId } ) ;
8490 } ;
@@ -99,8 +105,8 @@ export async function makeTestClients(userInfos, options) {
99105 clientMap [ userInfo . userId ] = { } ;
100106 }
101107 clientMap [ userInfo . userId ] [ userInfo . deviceId ] = testClient . client ;
102- testClient . client . sendToDevice = sendToDevice ;
103- testClient . client . sendEvent = sendEvent ;
108+ testClient . client . sendToDevice = makeSendToDevice ( testClient . client ) ;
109+ testClient . client . sendEvent = makeSendEvent ( testClient . client ) ;
104110 clients . push ( testClient ) ;
105111 }
106112
@@ -116,11 +122,12 @@ export async function makeTestClients(userInfos, options) {
116122export function setupWebcrypto ( ) {
117123 global . crypto = {
118124 getRandomValues : ( buf ) => {
119- return nodeCrypto . randomFillSync ( buf ) ;
125+ return nodeCrypto . randomFillSync ( buf as any ) ;
120126 } ,
121- } ;
127+ } as unknown as Crypto ;
122128}
123129
124130export function teardownWebcrypto ( ) {
131+ // @ts -ignore undefined != Crypto
125132 global . crypto = undefined ;
126133}
0 commit comments