1
1
import { strict as assert } from 'node:assert' ;
2
2
import { SinonSpy , spy } from 'sinon' ;
3
3
import { Decoder , RESP_TYPES } from './decoder' ;
4
- import { BlobError , ErrorReply , SimpleError } from '../errors' ;
4
+ import { BlobError , SimpleError } from '../errors' ;
5
5
import { TypeMapping } from './types' ;
6
+ import { VerbatimString } from './verbatim-string' ;
6
7
7
8
interface Test {
8
9
toWrite : Buffer ;
@@ -107,6 +108,14 @@ describe('RESP Decoder', () => {
107
108
toWrite : Buffer . from ( ':-1\r\n' ) ,
108
109
replies : [ - 1 ]
109
110
} ) ;
111
+
112
+ test ( '1 as string' , {
113
+ typeMapping : {
114
+ [ RESP_TYPES . NUMBER ] : String
115
+ } ,
116
+ toWrite : Buffer . from ( ':1\r\n' ) ,
117
+ replies : [ '1' ]
118
+ } ) ;
110
119
} ) ;
111
120
112
121
describe ( 'BigNumber' , ( ) => {
@@ -129,6 +138,14 @@ describe('RESP Decoder', () => {
129
138
toWrite : Buffer . from ( '(-1\r\n' ) ,
130
139
replies : [ - 1n ]
131
140
} ) ;
141
+
142
+ test ( '1 as string' , {
143
+ typeMapping : {
144
+ [ RESP_TYPES . BIG_NUMBER ] : String
145
+ } ,
146
+ toWrite : Buffer . from ( '(1\r\n' ) ,
147
+ replies : [ '1' ]
148
+ } ) ;
132
149
} ) ;
133
150
134
151
describe ( 'Double' , ( ) => {
@@ -182,15 +199,33 @@ describe('RESP Decoder', () => {
182
199
replies : [ 1e1 ]
183
200
} ) ;
184
201
185
- test ( '-1.1E1' , {
186
- toWrite : Buffer . from ( ',-1.1E1\r\n' ) ,
187
- replies : [ - 1.1E1 ]
202
+ test ( '-1.1E+1' , {
203
+ toWrite : Buffer . from ( ',-1.1E+1\r\n' ) ,
204
+ replies : [ - 1.1E+1 ]
205
+ } ) ;
206
+
207
+ test ( '1 as string' , {
208
+ typeMapping : {
209
+ [ RESP_TYPES . DOUBLE ] : String
210
+ } ,
211
+ toWrite : Buffer . from ( ',1\r\n' ) ,
212
+ replies : [ '1' ]
188
213
} ) ;
189
214
} ) ;
190
215
191
- test ( 'SimpleString' , {
192
- toWrite : Buffer . from ( '+OK\r\n' ) ,
193
- replies : [ 'OK' ]
216
+ describe ( 'SimpleString' , ( ) => {
217
+ test ( "'OK'" , {
218
+ toWrite : Buffer . from ( '+OK\r\n' ) ,
219
+ replies : [ 'OK' ]
220
+ } ) ;
221
+
222
+ test ( "'OK' as Buffer" , {
223
+ typeMapping : {
224
+ [ RESP_TYPES . SIMPLE_STRING ] : Buffer
225
+ } ,
226
+ toWrite : Buffer . from ( '+OK\r\n' ) ,
227
+ replies : [ Buffer . from ( 'OK' ) ]
228
+ } ) ;
194
229
} ) ;
195
230
196
231
describe ( 'BlobString' , ( ) => {
@@ -208,6 +243,14 @@ describe('RESP Decoder', () => {
208
243
toWrite : Buffer . from ( '$-1\r\n' ) ,
209
244
replies : [ null ]
210
245
} ) ;
246
+
247
+ test ( "'OK' as Buffer" , {
248
+ typeMapping : {
249
+ [ RESP_TYPES . BLOB_STRING ] : Buffer
250
+ } ,
251
+ toWrite : Buffer . from ( '$2\r\nOK\r\n' ) ,
252
+ replies : [ Buffer . from ( 'OK' ) ]
253
+ } ) ;
211
254
} ) ;
212
255
213
256
describe ( 'VerbatimString' , ( ) => {
@@ -220,6 +263,22 @@ describe('RESP Decoder', () => {
220
263
toWrite : Buffer . from ( '=10\r\ntxt:123456\r\n' ) ,
221
264
replies : [ '123456' ]
222
265
} ) ;
266
+
267
+ test ( "'OK' as VerbatimString" , {
268
+ typeMapping : {
269
+ [ RESP_TYPES . VERBATIM_STRING ] : VerbatimString
270
+ } ,
271
+ toWrite : Buffer . from ( '=6\r\ntxt:OK\r\n' ) ,
272
+ replies : [ new VerbatimString ( 'txt' , 'OK' ) ]
273
+ } ) ;
274
+
275
+ test ( "'OK' as Buffer" , {
276
+ typeMapping : {
277
+ [ RESP_TYPES . VERBATIM_STRING ] : Buffer
278
+ } ,
279
+ toWrite : Buffer . from ( '=6\r\ntxt:OK\r\n' ) ,
280
+ replies : [ Buffer . from ( 'OK' ) ]
281
+ } ) ;
223
282
} ) ;
224
283
225
284
test ( 'SimpleError' , {
@@ -256,9 +315,17 @@ describe('RESP Decoder', () => {
256
315
} ) ;
257
316
258
317
test ( 'of 0..9' , {
259
- toWrite : Buffer . from ( `* 10\r\n:0\r\n:1\r\n:2\r\n:3\r\n:4\r\n:5\r\n:6\r\n:7\r\n:8\r\n:9\r\n` ) ,
318
+ toWrite : Buffer . from ( `~ 10\r\n:0\r\n:1\r\n:2\r\n:3\r\n:4\r\n:5\r\n:6\r\n:7\r\n:8\r\n:9\r\n` ) ,
260
319
replies : [ [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ]
261
320
} ) ;
321
+
322
+ test ( '0..9 as Set' , {
323
+ typeMapping : {
324
+ [ RESP_TYPES . SET ] : Set
325
+ } ,
326
+ toWrite : Buffer . from ( `~10\r\n:0\r\n:1\r\n:2\r\n:3\r\n:4\r\n:5\r\n:6\r\n:7\r\n:8\r\n:9\r\n` ) ,
327
+ replies : [ new Set ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ) ]
328
+ } ) ;
262
329
} ) ;
263
330
264
331
describe ( 'Map' , ( ) => {
@@ -282,5 +349,44 @@ describe('RESP Decoder', () => {
282
349
9 : { value : '9' , enumerable : true }
283
350
} ) ]
284
351
} ) ;
352
+
353
+ test ( "{ '0'..'9': <key> } as Map" , {
354
+ typeMapping : {
355
+ [ RESP_TYPES . MAP ] : Map
356
+ } ,
357
+ toWrite : Buffer . from ( `%10\r\n+0\r\n+0\r\n+1\r\n+1\r\n+2\r\n+2\r\n+3\r\n+3\r\n+4\r\n+4\r\n+5\r\n+5\r\n+6\r\n+6\r\n+7\r\n+7\r\n+8\r\n+8\r\n+9\r\n+9\r\n` ) ,
358
+ replies : [ new Map ( [
359
+ [ '0' , '0' ] ,
360
+ [ '1' , '1' ] ,
361
+ [ '2' , '2' ] ,
362
+ [ '3' , '3' ] ,
363
+ [ '4' , '4' ] ,
364
+ [ '5' , '5' ] ,
365
+ [ '6' , '6' ] ,
366
+ [ '7' , '7' ] ,
367
+ [ '8' , '8' ] ,
368
+ [ '9' , '9' ]
369
+ ] ) ]
370
+ } ) ;
371
+
372
+ test ( "{ '0'..'9': <key> } as Array" , {
373
+ typeMapping : {
374
+ [ RESP_TYPES . MAP ] : Array
375
+ } ,
376
+ toWrite : Buffer . from ( `%10\r\n+0\r\n+0\r\n+1\r\n+1\r\n+2\r\n+2\r\n+3\r\n+3\r\n+4\r\n+4\r\n+5\r\n+5\r\n+6\r\n+6\r\n+7\r\n+7\r\n+8\r\n+8\r\n+9\r\n+9\r\n` ) ,
377
+ replies : [ [ '0' , '0' , '1' , '1' , '2' , '2' , '3' , '3' , '4' , '4' , '5' , '5' , '6' , '6' , '7' , '7' , '8' , '8' , '9' , '9' ] ]
378
+ } ) ;
379
+ } ) ;
380
+
381
+ describe ( 'Push' , ( ) => {
382
+ test ( '[]' , {
383
+ toWrite : Buffer . from ( '>0\r\n' ) ,
384
+ pushReplies : [ [ ] ]
385
+ } ) ;
386
+
387
+ test ( '[0..9]' , {
388
+ toWrite : Buffer . from ( `>10\r\n:0\r\n:1\r\n:2\r\n:3\r\n:4\r\n:5\r\n:6\r\n:7\r\n:8\r\n:9\r\n` ) ,
389
+ pushReplies : [ [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ]
390
+ } ) ;
285
391
} ) ;
286
392
} ) ;
0 commit comments