@@ -41,7 +41,7 @@ const wait = (ms: number) =>
41
41
setTimeout ( resolve , ms )
42
42
) ;
43
43
44
- function healthCheckerMock ( ) : MockedHealthChecker {
44
+ function healthCheckerMock ( ) : MockedHealthChecker {
45
45
let cb : ( health : SmoldotHealth ) => void = ( ) => undefined ;
46
46
let sendJsonRpc : ( request : string ) => void = ( ) => undefined ;
47
47
let isActive = false ;
@@ -66,7 +66,7 @@ function healthCheckerMock (): MockedHealthChecker {
66
66
} ;
67
67
}
68
68
69
- function healthCheckerFactory ( ) {
69
+ function healthCheckerFactory ( ) {
70
70
const _healthCheckers : MockedHealthChecker [ ] = [ ] ;
71
71
72
72
return {
@@ -82,13 +82,33 @@ function healthCheckerFactory () {
82
82
} ;
83
83
}
84
84
85
- function getFakeChain ( spec : string , callback : Sc . JsonRpcCallback ) : MockChain {
85
+ function getFakeChain ( spec : string ) : MockChain {
86
86
const _receivedRequests : string [ ] = [ ] ;
87
87
let _isTerminated = false ;
88
88
89
89
let terminateInterceptor = Function . prototype ;
90
90
let sendJsonRpcInterceptor = Function . prototype ;
91
91
92
+ const responseQueue : string [ ] = [ ]
93
+
94
+ const nextJsonRpcResponse = async ( ) => {
95
+ if ( responseQueue . length === 0 ) {
96
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) ) ;
97
+ return nextJsonRpcResponse ( ) ;
98
+ }
99
+
100
+ return responseQueue . shift ( ) ! ;
101
+ }
102
+
103
+ async function * jsonRpcResponsesGenerator ( ) : AsyncIterableIterator < string > {
104
+ while ( true ) {
105
+ const response = await nextJsonRpcResponse ( ) ;
106
+ if ( response ) {
107
+ yield response ;
108
+ }
109
+ }
110
+ }
111
+
92
112
return {
93
113
_getLatestRequest : ( ) => _receivedRequests [ _receivedRequests . length - 1 ] ,
94
114
_isTerminated : ( ) => _isTerminated ,
@@ -101,14 +121,15 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
101
121
} ,
102
122
_spec : ( ) => spec ,
103
123
_triggerCallback : ( response ) => {
104
- callback (
105
- typeof response === 'string'
106
- ? response
107
- : stringify ( response )
108
- ) ;
124
+ const message = typeof response === 'string'
125
+ ? response
126
+ : stringify ( response )
127
+ responseQueue . push ( message )
128
+
109
129
} ,
110
- addChain : ( chainSpec , jsonRpcCallback ) =>
111
- Promise . resolve ( getFakeChain ( chainSpec , jsonRpcCallback ?? noop ) ) ,
130
+ nextJsonRpcResponse,
131
+ jsonRpcResponses : jsonRpcResponsesGenerator ( ) ,
132
+ addChain : ( chainSpec ) => Promise . resolve ( getFakeChain ( chainSpec ) ) ,
112
133
remove : ( ) => {
113
134
terminateInterceptor ( ) ;
114
135
_isTerminated = true ;
@@ -120,11 +141,27 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
120
141
} ;
121
142
}
122
143
123
- function getFakeClient ( ) {
144
+ function getFakeClient ( ) {
124
145
const chains : MockChain [ ] = [ ] ;
125
146
let addChainInterceptor : Promise < void > = Promise . resolve ( ) ;
126
147
let addWellKnownChainInterceptor : Promise < void > = Promise . resolve ( ) ;
127
148
149
+ const addChain : Sc . AddChain = async ( chainSpec ) => addChainInterceptor . then ( ( ) => {
150
+ const result = getFakeChain ( chainSpec ) ;
151
+
152
+ chains . push ( result ) ;
153
+
154
+ return result ;
155
+ } )
156
+
157
+ const addWellKnownChain : Sc . AddWellKnownChain = async ( wellKnownChain ) => addWellKnownChainInterceptor . then ( ( ) => {
158
+ const result = getFakeChain ( wellKnownChain ) ;
159
+
160
+ chains . push ( result ) ;
161
+
162
+ return result ;
163
+ } )
164
+
128
165
return {
129
166
_chains : ( ) => chains ,
130
167
_setAddChainInterceptor : ( interceptor : Promise < void > ) => {
@@ -133,29 +170,12 @@ function getFakeClient () {
133
170
_setAddWellKnownChainInterceptor : ( interceptor : Promise < void > ) => {
134
171
addWellKnownChainInterceptor = interceptor ;
135
172
} ,
136
- addChain : ( chainSpec : string , cb : Sc . JsonRpcCallback ) : Promise < MockChain > =>
137
- addChainInterceptor . then ( ( ) => {
138
- const result = getFakeChain ( chainSpec , cb ) ;
139
-
140
- chains . push ( result ) ;
141
-
142
- return result ;
143
- } ) ,
144
- addWellKnownChain : (
145
- wellKnownChain : string ,
146
- cb : Sc . JsonRpcCallback
147
- ) : Promise < MockChain > =>
148
- addWellKnownChainInterceptor . then ( ( ) => {
149
- const result = getFakeChain ( wellKnownChain , cb ) ;
150
-
151
- chains . push ( result ) ;
152
-
153
- return result ;
154
- } )
173
+ addChain,
174
+ addWellKnownChain
155
175
} ;
156
176
}
157
177
158
- function connectorFactory ( ) : MockSc {
178
+ function connectorFactory ( ) : MockSc {
159
179
const clients : ReturnType < typeof getFakeClient > [ ] = [ ] ;
160
180
const latestClient = ( ) => clients [ clients . length - 1 ] ;
161
181
@@ -175,7 +195,7 @@ function connectorFactory (): MockSc {
175
195
} as unknown as MockSc ;
176
196
}
177
197
178
- function setChainSyncyingStatus ( isSyncing : boolean ) : void {
198
+ function setChainSyncyingStatus ( isSyncing : boolean ) : void {
179
199
getCurrentHealthChecker ( ) . _triggerHealthUpdate ( {
180
200
isSyncing,
181
201
peers : 1 ,
@@ -206,7 +226,7 @@ describe('ScProvider', () => {
206
226
expect ( onConnected ) . not . toHaveBeenCalled ( ) ;
207
227
setChainSyncyingStatus ( false ) ;
208
228
expect ( onConnected ) . toHaveBeenCalled ( ) ;
209
- } ) ;
229
+ } , 5000 ) ;
210
230
211
231
it ( 'stops receiving notifications after unsubscribing' , async ( ) => {
212
232
const provider = new ScProvider ( mockSc , '' ) ;
@@ -220,7 +240,7 @@ describe('ScProvider', () => {
220
240
221
241
setChainSyncyingStatus ( false ) ;
222
242
expect ( onConnected ) . not . toHaveBeenCalled ( ) ;
223
- } ) ;
243
+ } , 5000 ) ;
224
244
225
245
it ( 'synchronously emits connected if the Provider is already `connected`' , async ( ) => {
226
246
const provider = new ScProvider ( mockSc , '' ) ;
@@ -233,7 +253,7 @@ describe('ScProvider', () => {
233
253
234
254
provider . on ( 'connected' , onConnected ) ;
235
255
expect ( onConnected ) . toHaveBeenCalled ( ) ;
236
- } ) ;
256
+ } , 5000 ) ;
237
257
238
258
it ( 'emits `disconnected` once the chain goes back to syncing' , async ( ) => {
239
259
const provider = new ScProvider ( mockSc , '' ) ;
@@ -256,7 +276,7 @@ describe('ScProvider', () => {
256
276
257
277
expect ( onConnected ) . not . toHaveBeenCalled ( ) ;
258
278
expect ( onDisconnected ) . toHaveBeenCalled ( ) ;
259
- } ) ;
279
+ } , 5000 ) ;
260
280
} ) ;
261
281
262
282
describe ( 'hasSubscriptions' , ( ) => {
@@ -288,7 +308,7 @@ describe('ScProvider', () => {
288
308
289
309
await provider . connect ( undefined , mockedHealthChecker . healthChecker ) ;
290
310
expect ( chain ) . toBe ( mockSc . latestChain ( ) ) ;
291
- } ) ;
311
+ } , 5000 ) ;
292
312
293
313
it ( 'throws when trying to connect on an already connected Provider' , async ( ) => {
294
314
const provider = new ScProvider ( mockSc , '' ) ;
@@ -300,7 +320,7 @@ describe('ScProvider', () => {
300
320
await expect (
301
321
provider . connect ( undefined , mockedHealthChecker . healthChecker )
302
322
) . rejects . toThrow ( / A l r e a d y c o n n e c t e d / ) ;
303
- } ) ;
323
+ } , 5000 ) ;
304
324
} ) ;
305
325
306
326
describe ( 'disconnect' , ( ) => {
@@ -313,7 +333,7 @@ describe('ScProvider', () => {
313
333
await provider . disconnect ( ) ;
314
334
315
335
expect ( chain . _isTerminated ( ) ) . toBe ( true ) ;
316
- } ) ;
336
+ } , 5000 ) ;
317
337
318
338
// eslint-disable-next-line jest/expect-expect
319
339
it ( 'does not throw when disconnecting on an already disconnected Provider' , async ( ) => {
@@ -360,7 +380,7 @@ describe('ScProvider', () => {
360
380
const response = await responsePromise ;
361
381
362
382
expect ( response ) . toEqual ( result ) ;
363
- } ) ;
383
+ } , 5000 ) ;
364
384
365
385
it ( "rejects when the response can't be deserialized" , async ( ) => {
366
386
const provider = new ScProvider ( mockSc , '' ) ;
@@ -378,7 +398,7 @@ describe('ScProvider', () => {
378
398
} , 0 ) ;
379
399
380
400
await expect ( provider . send ( 'getData' , [ 'foo' ] ) ) . rejects . toThrow ( ) ;
381
- } ) ;
401
+ } , 5000 ) ;
382
402
383
403
it ( 'rejects when the smoldot chain has crashed' , async ( ) => {
384
404
const provider = new ScProvider ( mockSc , '' ) ;
@@ -397,7 +417,7 @@ describe('ScProvider', () => {
397
417
provider . send ( 'getData' , [ 'foo' ] )
398
418
) . rejects . toThrow ( / D i s c o n n e c t e d / ) ;
399
419
expect ( provider . isConnected ) . toBe ( false ) ;
400
- } ) ;
420
+ } , 5000 ) ;
401
421
} ) ;
402
422
403
423
describe ( 'subscribe' , ( ) => {
@@ -466,7 +486,7 @@ describe('ScProvider', () => {
466
486
} ) ;
467
487
expect ( cb ) . toHaveBeenCalledTimes ( 2 ) ;
468
488
expect ( cb ) . toHaveBeenLastCalledWith ( null , 2 ) ;
469
- } ) ;
489
+ } , 5000 ) ;
470
490
471
491
it ( 'ignores subscription messages that were received before the subscription token' , async ( ) => {
472
492
const provider = new ScProvider ( mockSc , '' ) ;
@@ -504,7 +524,7 @@ describe('ScProvider', () => {
504
524
505
525
expect ( token ) . toBe ( unsubscribeToken ) ;
506
526
expect ( cb ) . not . toHaveBeenCalled ( ) ;
507
- } ) ;
527
+ } , 5000 ) ;
508
528
509
529
it ( 'emits the error when the message has an error' , async ( ) => {
510
530
const provider = new ScProvider ( mockSc , '' ) ;
@@ -545,7 +565,7 @@ describe('ScProvider', () => {
545
565
expect ( token ) . toBe ( unsubscribeToken ) ;
546
566
expect ( cb ) . toHaveBeenCalledTimes ( 1 ) ;
547
567
expect ( cb ) . toHaveBeenLastCalledWith ( expect . any ( Error ) , undefined ) ;
548
- } ) ;
568
+ } , 5000 ) ;
549
569
550
570
it ( 'errors when subscribing to an unsupported method' , async ( ) => {
551
571
const provider = new ScProvider ( mockSc , '' ) ;
@@ -558,7 +578,7 @@ describe('ScProvider', () => {
558
578
await expect (
559
579
provider . subscribe ( 'foo' , 'bar' , [ 'baz' ] , ( ) => undefined )
560
580
) . rejects . toThrow ( / U n s u p p o r t e d s u b s c r i b e m e t h o d : b a r / ) ;
561
- } ) ;
581
+ } , 5000 ) ;
562
582
} ) ;
563
583
564
584
describe ( 'unsubscribe' , ( ) => {
@@ -572,7 +592,7 @@ describe('ScProvider', () => {
572
592
await expect (
573
593
provider . unsubscribe ( '' , '' , '' )
574
594
) . rejects . toThrow ( / U n a b l e t o f i n d a c t i v e s u b s c r i p t i o n / ) ;
575
- } ) ;
595
+ } , 5000 ) ;
576
596
} ) ;
577
597
578
598
it ( 'cleans up the stale subscriptions once it reconnects' , async ( ) => {
@@ -634,5 +654,5 @@ describe('ScProvider', () => {
634
654
`{"id":2,"jsonrpc":"2.0","method":"chain_unsubscribeNewHeads","params":["${ token } "]}` ,
635
655
'{"id":3,"jsonrpc":"2.0","method":"chain_subscribeNewHeads","params":["baz"]}'
636
656
] ) ;
637
- } ) ;
657
+ } , 5000 ) ;
638
658
} ) ;
0 commit comments