1
1
import type { StreamId } from '#message/index.js' ;
2
+ import type WebSocketConnection from '#WebSocketConnection.js' ;
2
3
import Logger , { formatting , LogLevel , StreamHandler } from '@matrixai/logger' ;
3
4
import { fc , test } from '@fast-check/jest' ;
4
5
import * as messageTestUtils from './message/utils.js' ;
5
6
import WebSocketStream from '#WebSocketStream.js' ;
6
- import WebSocketConnection from '#WebSocketConnection.js' ;
7
7
import * as events from '#events.js' ;
8
8
import * as utils from '#utils.js' ;
9
9
import * as messageUtils from '#message/utils.js' ;
@@ -28,121 +28,118 @@ const logger2 = new Logger('stream 2', LogLevel.WARN, [
28
28
29
29
let streamIdCounter = 0n ;
30
30
31
- jest . mock ( '#WebSocketConnection.js' , ( ) => {
32
- return jest . fn ( ) . mockImplementation ( ( streamOptions : StreamOptions = { } ) => {
33
- const instance = new EventTarget ( ) as EventTarget & {
34
- peerConnection : WebSocketConnection | undefined ;
35
- connectTo : ( connection : WebSocketConnection ) => void ;
36
- send : ( data : Uint8Array ) => Promise < void > ;
37
- newStream : ( ) => Promise < WebSocketStream > ;
38
- streamMap : Map < StreamId , WebSocketStream > ;
39
- } ;
40
- instance . peerConnection = undefined ;
41
- instance . connectTo = ( peerConnection : any ) => {
42
- instance . peerConnection = peerConnection ;
43
- peerConnection . peerConnection = instance ;
44
- } ;
45
- instance . streamMap = new Map < StreamId , WebSocketStream > ( ) ;
46
- instance . newStream = async ( ) => {
47
- const stream = new WebSocketStream ( {
48
- initiated : 'local' ,
49
- streamId : streamIdCounter as StreamId ,
31
+ function createMockedWebSocketConnection ( streamOptions : StreamOptions = { } ) {
32
+ const instance = new EventTarget ( ) as EventTarget & {
33
+ peerConnection : WebSocketConnection | undefined ;
34
+ connectTo : ( connection : WebSocketConnection ) => void ;
35
+ send : ( data : Uint8Array ) => Promise < void > ;
36
+ newStream : ( ) => Promise < WebSocketStream > ;
37
+ streamMap : Map < StreamId , WebSocketStream > ;
38
+ } ;
39
+ instance . peerConnection = undefined ;
40
+ instance . connectTo = ( peerConnection : any ) => {
41
+ instance . peerConnection = peerConnection ;
42
+ peerConnection . peerConnection = instance ;
43
+ } ;
44
+ instance . streamMap = new Map < StreamId , WebSocketStream > ( ) ;
45
+ instance . newStream = async ( ) => {
46
+ const stream = new WebSocketStream ( {
47
+ initiated : 'local' ,
48
+ streamId : streamIdCounter as StreamId ,
49
+ bufferSize : STREAM_BUFFER_SIZE ,
50
+ connection : instance as any ,
51
+ logger : logger1 ,
52
+ ...streamOptions ,
53
+ } ) ;
54
+ stream . addEventListener (
55
+ events . EventWebSocketStreamSend . name ,
56
+ async ( evt : any ) => {
57
+ await instance . send ( evt . msg ) ;
58
+ } ,
59
+ ) ;
60
+ stream . addEventListener (
61
+ events . EventWebSocketStreamStopped . name ,
62
+ ( ) => {
63
+ instance . streamMap . delete ( stream . streamId ) ;
64
+ } ,
65
+ { once : true } ,
66
+ ) ;
67
+ instance . streamMap . set ( stream . streamId , stream ) ;
68
+ await stream . start ( ) ;
69
+ streamIdCounter ++ ;
70
+ return stream ;
71
+ } ;
72
+ instance . send = async ( array : Uint8Array | Array < Uint8Array > ) => {
73
+ let data : Uint8Array ;
74
+ if ( ArrayBuffer . isView ( array ) ) {
75
+ data = array ;
76
+ } else {
77
+ data = messageUtils . concatUInt8Array ( ...array ) ;
78
+ }
79
+ const { data : streamId , remainder } = messageUtils . parseStreamId ( data ) ;
80
+ // @ts -ignore: protected property
81
+ let stream = instance . peerConnection ! . streamMap . get ( streamId ) ;
82
+ if ( stream == null ) {
83
+ if (
84
+ ! ( remainder . at ( 0 ) === 0 && remainder . at ( 1 ) === StreamMessageType . Ack )
85
+ ) {
86
+ return ;
87
+ }
88
+ stream = new WebSocketStream ( {
89
+ initiated : 'peer' ,
90
+ streamId,
50
91
bufferSize : STREAM_BUFFER_SIZE ,
51
- connection : instance as any ,
52
- logger : logger1 ,
92
+ connection : instance . peerConnection ! ,
93
+ logger : logger2 ,
53
94
...streamOptions ,
54
95
} ) ;
55
96
stream . addEventListener (
56
97
events . EventWebSocketStreamSend . name ,
57
98
async ( evt : any ) => {
58
- await instance . send ( evt . msg ) ;
99
+ // @ts -ignore: protected property
100
+ await instance . peerConnection ! . send ( evt . msg ) ;
59
101
} ,
60
102
) ;
61
103
stream . addEventListener (
62
104
events . EventWebSocketStreamStopped . name ,
63
105
( ) => {
64
- instance . streamMap . delete ( stream . streamId ) ;
106
+ // @ts -ignore: protected property
107
+ instance . peerConnection ! . streamMap . delete ( streamId ) ;
65
108
} ,
66
109
{ once : true } ,
67
110
) ;
68
- instance . streamMap . set ( stream . streamId , stream ) ;
69
- await stream . start ( ) ;
70
- streamIdCounter ++ ;
71
- return stream ;
72
- } ;
73
- instance . send = async ( array : Uint8Array | Array < Uint8Array > ) => {
74
- let data : Uint8Array ;
75
- if ( ArrayBuffer . isView ( array ) ) {
76
- data = array ;
77
- } else {
78
- data = messageUtils . concatUInt8Array ( ...array ) ;
79
- }
80
- const { data : streamId , remainder } = messageUtils . parseStreamId ( data ) ;
81
111
// @ts -ignore: protected property
82
- let stream = instance . peerConnection ! . streamMap . get ( streamId ) ;
83
- if ( stream == null ) {
84
- if (
85
- ! ( remainder . at ( 0 ) === 0 && remainder . at ( 1 ) === StreamMessageType . Ack )
86
- ) {
87
- return ;
88
- }
89
- stream = new WebSocketStream ( {
90
- initiated : 'peer' ,
91
- streamId,
92
- bufferSize : STREAM_BUFFER_SIZE ,
93
- connection : instance . peerConnection ! ,
94
- logger : logger2 ,
95
- ...streamOptions ,
96
- } ) ;
97
- stream . addEventListener (
98
- events . EventWebSocketStreamSend . name ,
99
- async ( evt : any ) => {
100
- // @ts -ignore: protected property
101
- await instance . peerConnection ! . send ( evt . msg ) ;
102
- } ,
103
- ) ;
104
- stream . addEventListener (
105
- events . EventWebSocketStreamStopped . name ,
106
- ( ) => {
107
- // @ts -ignore: protected property
108
- instance . peerConnection ! . streamMap . delete ( streamId ) ;
109
- } ,
110
- { once : true } ,
111
- ) ;
112
- // @ts -ignore: protected property
113
- instance . peerConnection ! . streamMap . set ( stream . streamId , stream ) ;
114
- await stream . start ( ) ;
115
- instance . peerConnection ! . dispatchEvent (
116
- new events . EventWebSocketConnectionStream ( {
117
- detail : stream ,
118
- } ) ,
119
- ) ;
120
- }
121
- await stream . streamRecv ( remainder ) ;
122
- } ;
123
- return instance ;
124
- } ) ;
125
- } ) ;
126
-
127
- const connectionMock = jest . mocked ( WebSocketConnection , { shallow : true } ) ;
112
+ instance . peerConnection ! . streamMap . set ( stream . streamId , stream ) ;
113
+ await stream . start ( ) ;
114
+ instance . peerConnection ! . dispatchEvent (
115
+ new events . EventWebSocketConnectionStream ( {
116
+ detail : stream ,
117
+ } ) ,
118
+ ) ;
119
+ }
120
+ await stream . streamRecv ( remainder ) ;
121
+ } ;
122
+ return instance ;
123
+ }
128
124
129
125
describe ( WebSocketStream . name , ( ) => {
130
- beforeEach ( async ( ) => {
131
- connectionMock . mockClear ( ) ;
132
- } ) ;
133
-
134
126
async function createConnectionPair (
135
127
streamOptions : StreamOptions = { } ,
136
- ) : Promise < [ WebSocketConnection , WebSocketConnection ] > {
137
- const connection1 = new ( WebSocketConnection as any ) ( streamOptions ) ;
138
- const connection2 = new ( WebSocketConnection as any ) ( streamOptions ) ;
128
+ ) : Promise <
129
+ [
130
+ ReturnType < typeof createMockedWebSocketConnection > ,
131
+ ReturnType < typeof createMockedWebSocketConnection > ,
132
+ ]
133
+ > {
134
+ const connection1 = createMockedWebSocketConnection ( streamOptions ) ;
135
+ const connection2 = createMockedWebSocketConnection ( streamOptions ) ;
139
136
( connection1 as any ) . connectTo ( connection2 ) ;
140
137
return [ connection1 , connection2 ] ;
141
138
}
142
139
143
140
async function createStreamPairFrom (
144
- connection1 : WebSocketConnection ,
145
- connection2 : WebSocketConnection ,
141
+ connection1 : ReturnType < typeof createMockedWebSocketConnection > ,
142
+ connection2 : ReturnType < typeof createMockedWebSocketConnection > ,
146
143
) : Promise < [ WebSocketStream , WebSocketStream ] > {
147
144
const stream1 = await connection1 . newStream ( ) ;
148
145
const createStream2Prom = utils . promise < WebSocketStream > ( ) ;
0 commit comments