-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest.js
More file actions
879 lines (738 loc) · 44.1 KB
/
Copy pathtest.js
File metadata and controls
879 lines (738 loc) · 44.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/**
* Full Test Suite for react-zlib-js v2.0.0
* Node.js 22.x LTS Compatible
*/
var zlib = require('./index.js');
// Test data
var shortInput = 'Hello, World!';
var longInput = `What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.`;
// Test counters
var totalTests = 0;
var passedTests = 0;
var failedTests = 0;
function assert(condition, testName) {
totalTests++;
if (condition) {
passedTests++;
console.log(' [PASS] ' + testName);
} else {
failedTests++;
console.log(' [FAIL] ' + testName);
}
}
function assertThrows(fn, testName) {
totalTests++;
try {
fn();
failedTests++;
console.log(' [FAIL] ' + testName + ' (expected to throw)');
} catch (e) {
passedTests++;
console.log(' [PASS] ' + testName);
}
}
console.log('╔══════════════════════════════════════════════════════════════════╗');
console.log('║ react-zlib-js v2.0.0 - Full Test Suite ║');
console.log('║ Node.js 22.x LTS Compatible ║');
console.log('╚══════════════════════════════════════════════════════════════════╝\n');
// ============================================================================
// TEST 1: Constants
// ============================================================================
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 1: Zlib Constants');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
// Compression levels
assert(zlib.Z_NO_COMPRESSION === 0, 'Z_NO_COMPRESSION = 0');
assert(zlib.Z_BEST_SPEED === 1, 'Z_BEST_SPEED = 1');
assert(zlib.Z_BEST_COMPRESSION === 9, 'Z_BEST_COMPRESSION = 9');
assert(zlib.Z_DEFAULT_COMPRESSION === -1, 'Z_DEFAULT_COMPRESSION = -1');
// Flush values
assert(zlib.Z_NO_FLUSH === 0, 'Z_NO_FLUSH = 0');
assert(zlib.Z_PARTIAL_FLUSH === 1, 'Z_PARTIAL_FLUSH = 1');
assert(zlib.Z_SYNC_FLUSH === 2, 'Z_SYNC_FLUSH = 2');
assert(zlib.Z_FULL_FLUSH === 3, 'Z_FULL_FLUSH = 3');
assert(zlib.Z_FINISH === 4, 'Z_FINISH = 4');
// Return codes
assert(zlib.Z_OK === 0, 'Z_OK = 0');
assert(zlib.Z_STREAM_END === 1, 'Z_STREAM_END = 1');
assert(zlib.Z_NEED_DICT === 2, 'Z_NEED_DICT = 2');
// Strategies
assert(zlib.Z_FILTERED === 1, 'Z_FILTERED = 1');
assert(zlib.Z_HUFFMAN_ONLY === 2, 'Z_HUFFMAN_ONLY = 2');
assert(zlib.Z_RLE === 3, 'Z_RLE = 3');
assert(zlib.Z_DEFAULT_STRATEGY === 0, 'Z_DEFAULT_STRATEGY = 0');
// constants object (Node.js 7.0.0+)
assert(typeof zlib.constants === 'object', 'zlib.constants is an object');
assert(zlib.constants.Z_BEST_COMPRESSION === 9, 'constants.Z_BEST_COMPRESSION = 9');
console.log('');
// ============================================================================
// TEST 2: Brotli Constants (Node.js 11.7.0+)
// ============================================================================
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 2: Brotli Constants (Node.js 11.7.0+)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
// Brotli operations
assert(zlib.BROTLI_OPERATION_PROCESS === 0, 'BROTLI_OPERATION_PROCESS = 0');
assert(zlib.BROTLI_OPERATION_FLUSH === 1, 'BROTLI_OPERATION_FLUSH = 1');
assert(zlib.BROTLI_OPERATION_FINISH === 2, 'BROTLI_OPERATION_FINISH = 2');
assert(zlib.BROTLI_OPERATION_EMIT_METADATA === 3, 'BROTLI_OPERATION_EMIT_METADATA = 3');
// Brotli modes
assert(zlib.BROTLI_MODE_GENERIC === 0, 'BROTLI_MODE_GENERIC = 0');
assert(zlib.BROTLI_MODE_TEXT === 1, 'BROTLI_MODE_TEXT = 1');
assert(zlib.BROTLI_MODE_FONT === 2, 'BROTLI_MODE_FONT = 2');
// Brotli quality
assert(zlib.BROTLI_MIN_QUALITY === 0, 'BROTLI_MIN_QUALITY = 0');
assert(zlib.BROTLI_MAX_QUALITY === 11, 'BROTLI_MAX_QUALITY = 11');
assert(zlib.BROTLI_DEFAULT_QUALITY === 11, 'BROTLI_DEFAULT_QUALITY = 11');
// Brotli window
assert(zlib.BROTLI_MIN_WINDOW_BITS === 10, 'BROTLI_MIN_WINDOW_BITS = 10');
assert(zlib.BROTLI_MAX_WINDOW_BITS === 24, 'BROTLI_MAX_WINDOW_BITS = 24');
assert(zlib.BROTLI_DEFAULT_WINDOW === 22, 'BROTLI_DEFAULT_WINDOW = 22');
// Brotli parameters
assert(zlib.BROTLI_PARAM_MODE === 0, 'BROTLI_PARAM_MODE = 0');
assert(zlib.BROTLI_PARAM_QUALITY === 1, 'BROTLI_PARAM_QUALITY = 1');
assert(zlib.BROTLI_PARAM_LGWIN === 2, 'BROTLI_PARAM_LGWIN = 2');
// Brotli decoder results
assert(zlib.BROTLI_DECODER_RESULT_ERROR === 0, 'BROTLI_DECODER_RESULT_ERROR = 0');
assert(zlib.BROTLI_DECODER_RESULT_SUCCESS === 1, 'BROTLI_DECODER_RESULT_SUCCESS = 1');
// constants object includes Brotli
assert(zlib.constants.BROTLI_MAX_QUALITY === 11, 'constants.BROTLI_MAX_QUALITY = 11');
console.log('');
// ============================================================================
// TEST 3: Gzip Async
// ============================================================================
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 3: Gzip Compression (Async)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
zlib.gzip(longInput, function(err, compressed) {
assert(!err, 'gzip() completes without error');
assert(compressed.length < longInput.length, 'gzip() reduces size');
console.log(' [INFO] Original: ' + longInput.length + ' bytes, Compressed: ' + compressed.length + ' bytes');
zlib.gunzip(compressed, function(err, decompressed) {
assert(!err, 'gunzip() completes without error');
assert(decompressed.toString() === longInput, 'gunzip() restores original data');
// Also test unzip (auto-detect)
zlib.unzip(compressed, function(err, decompressed2) {
assert(!err, 'unzip() completes without error');
assert(decompressed2.toString() === longInput, 'unzip() auto-detects gzip and restores data');
console.log('');
runTest4();
});
});
});
// ============================================================================
// TEST 4: Gzip Sync
// ============================================================================
function runTest4() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 4: Gzip Compression (Sync)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
var compressed = zlib.gzipSync(longInput);
assert(compressed.length < longInput.length, 'gzipSync() reduces size');
console.log(' [INFO] Original: ' + longInput.length + ' bytes, Compressed: ' + compressed.length + ' bytes');
var decompressed = zlib.gunzipSync(compressed);
assert(decompressed.toString() === longInput, 'gunzipSync() restores original data');
var decompressed2 = zlib.unzipSync(compressed);
assert(decompressed2.toString() === longInput, 'unzipSync() auto-detects and restores data');
console.log('');
runTest5();
}
// ============================================================================
// TEST 5: Deflate Async
// ============================================================================
function runTest5() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 5: Deflate Compression (Async)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
zlib.deflate(longInput, function(err, compressed) {
assert(!err, 'deflate() completes without error');
assert(compressed.length < longInput.length, 'deflate() reduces size');
console.log(' [INFO] Original: ' + longInput.length + ' bytes, Compressed: ' + compressed.length + ' bytes');
zlib.inflate(compressed, function(err, decompressed) {
assert(!err, 'inflate() completes without error');
assert(decompressed.toString() === longInput, 'inflate() restores original data');
console.log('');
runTest6();
});
});
}
// ============================================================================
// TEST 6: Deflate Sync
// ============================================================================
function runTest6() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 6: Deflate Compression (Sync)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
var compressed = zlib.deflateSync(longInput);
assert(compressed.length < longInput.length, 'deflateSync() reduces size');
console.log(' [INFO] Original: ' + longInput.length + ' bytes, Compressed: ' + compressed.length + ' bytes');
var decompressed = zlib.inflateSync(compressed);
assert(decompressed.toString() === longInput, 'inflateSync() restores original data');
console.log('');
runTest7();
}
// ============================================================================
// TEST 7: DeflateRaw Async
// ============================================================================
function runTest7() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 7: DeflateRaw Compression (Async)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
zlib.deflateRaw(longInput, function(err, compressed) {
assert(!err, 'deflateRaw() completes without error');
assert(compressed.length < longInput.length, 'deflateRaw() reduces size');
console.log(' [INFO] Original: ' + longInput.length + ' bytes, Compressed: ' + compressed.length + ' bytes');
zlib.inflateRaw(compressed, function(err, decompressed) {
assert(!err, 'inflateRaw() completes without error');
assert(decompressed.toString() === longInput, 'inflateRaw() restores original data');
console.log('');
runTest8();
});
});
}
// ============================================================================
// TEST 8: DeflateRaw Sync
// ============================================================================
function runTest8() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 8: DeflateRaw Compression (Sync)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
var compressed = zlib.deflateRawSync(longInput);
assert(compressed.length < longInput.length, 'deflateRawSync() reduces size');
console.log(' [INFO] Original: ' + longInput.length + ' bytes, Compressed: ' + compressed.length + ' bytes');
var decompressed = zlib.inflateRawSync(compressed);
assert(decompressed.toString() === longInput, 'inflateRawSync() restores original data');
console.log('');
runTest9();
}
// ============================================================================
// TEST 9: Brotli Async (Node.js 11.7.0+)
// ============================================================================
function runTest9() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 9: Brotli Compression (Async) - Node.js 11.7.0+');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
zlib.brotliCompress(longInput, function(err, compressed) {
assert(!err, 'brotliCompress() completes without error');
assert(compressed.length < longInput.length, 'brotliCompress() reduces size');
console.log(' [INFO] Original: ' + longInput.length + ' bytes, Compressed: ' + compressed.length + ' bytes');
zlib.brotliDecompress(compressed, function(err, decompressed) {
assert(!err, 'brotliDecompress() completes without error');
assert(decompressed.toString() === longInput, 'brotliDecompress() restores original data');
console.log('');
runTest10();
});
});
}
// ============================================================================
// TEST 10: Brotli Sync (Node.js 11.7.0+)
// ============================================================================
function runTest10() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 10: Brotli Compression (Sync) - Node.js 11.7.0+');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
var compressed = zlib.brotliCompressSync(longInput);
assert(compressed.length < longInput.length, 'brotliCompressSync() reduces size');
console.log(' [INFO] Original: ' + longInput.length + ' bytes, Compressed: ' + compressed.length + ' bytes');
var decompressed = zlib.brotliDecompressSync(compressed);
assert(decompressed.toString() === longInput, 'brotliDecompressSync() restores original data');
console.log('');
runTest11();
}
// ============================================================================
// TEST 11: Compression Levels
// ============================================================================
function runTest11() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 11: Compression Levels');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
var noCompression = zlib.deflateSync(longInput, { level: zlib.Z_NO_COMPRESSION });
var bestSpeed = zlib.deflateSync(longInput, { level: zlib.Z_BEST_SPEED });
var bestCompression = zlib.deflateSync(longInput, { level: zlib.Z_BEST_COMPRESSION });
var defaultCompression = zlib.deflateSync(longInput, { level: zlib.Z_DEFAULT_COMPRESSION });
console.log(' [INFO] No compression: ' + noCompression.length + ' bytes');
console.log(' [INFO] Best speed (1): ' + bestSpeed.length + ' bytes');
console.log(' [INFO] Best compression (9): ' + bestCompression.length + ' bytes');
console.log(' [INFO] Default (-1): ' + defaultCompression.length + ' bytes');
assert(noCompression.length >= longInput.length, 'Z_NO_COMPRESSION does not compress');
assert(bestSpeed.length < longInput.length, 'Z_BEST_SPEED compresses');
assert(bestCompression.length < longInput.length, 'Z_BEST_COMPRESSION compresses');
assert(bestCompression.length <= bestSpeed.length, 'Z_BEST_COMPRESSION <= Z_BEST_SPEED size');
// Verify all decompress correctly
assert(zlib.inflateSync(noCompression).toString() === longInput, 'Z_NO_COMPRESSION decompresses');
assert(zlib.inflateSync(bestSpeed).toString() === longInput, 'Z_BEST_SPEED decompresses');
assert(zlib.inflateSync(bestCompression).toString() === longInput, 'Z_BEST_COMPRESSION decompresses');
console.log('');
runTest12();
}
// ============================================================================
// TEST 12: Stream Classes
// ============================================================================
function runTest12() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 12: Stream Classes');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
// Test class existence
assert(typeof zlib.Deflate === 'function', 'Deflate class exists');
assert(typeof zlib.Inflate === 'function', 'Inflate class exists');
assert(typeof zlib.Gzip === 'function', 'Gzip class exists');
assert(typeof zlib.Gunzip === 'function', 'Gunzip class exists');
assert(typeof zlib.DeflateRaw === 'function', 'DeflateRaw class exists');
assert(typeof zlib.InflateRaw === 'function', 'InflateRaw class exists');
assert(typeof zlib.Unzip === 'function', 'Unzip class exists');
assert(typeof zlib.BrotliCompress === 'function', 'BrotliCompress class exists');
assert(typeof zlib.BrotliDecompress === 'function', 'BrotliDecompress class exists');
// Test factory functions
assert(typeof zlib.createDeflate === 'function', 'createDeflate() exists');
assert(typeof zlib.createInflate === 'function', 'createInflate() exists');
assert(typeof zlib.createGzip === 'function', 'createGzip() exists');
assert(typeof zlib.createGunzip === 'function', 'createGunzip() exists');
assert(typeof zlib.createDeflateRaw === 'function', 'createDeflateRaw() exists');
assert(typeof zlib.createInflateRaw === 'function', 'createInflateRaw() exists');
assert(typeof zlib.createUnzip === 'function', 'createUnzip() exists');
assert(typeof zlib.createBrotliCompress === 'function', 'createBrotliCompress() exists');
assert(typeof zlib.createBrotliDecompress === 'function', 'createBrotliDecompress() exists');
// Test stream creation
var gzip = zlib.createGzip();
assert(gzip !== null, 'createGzip() returns stream');
assert(typeof gzip.pipe === 'function', 'Gzip stream has pipe()');
assert(typeof gzip.write === 'function', 'Gzip stream has write()');
assert(typeof gzip.end === 'function', 'Gzip stream has end()');
var brotli = zlib.createBrotliCompress();
assert(brotli !== null, 'createBrotliCompress() returns stream');
assert(typeof brotli.pipe === 'function', 'BrotliCompress stream has pipe()');
console.log('');
runTest13();
}
// ============================================================================
// TEST 13: CRC32 (Node.js 22.2.0+)
// ============================================================================
function runTest13() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 13: CRC32 Function (Node.js 22.2.0+)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
assert(typeof zlib.crc32 === 'function', 'crc32() function exists');
// Test with string
var crc1 = zlib.crc32('hello');
assert(typeof crc1 === 'number', 'crc32() returns number');
assert(crc1 === 907060870, 'crc32("hello") = 907060870');
console.log(' [INFO] CRC32 of "hello": ' + crc1);
// Test with Buffer
var crc2 = zlib.crc32(Buffer.from('hello'));
assert(crc2 === 907060870, 'crc32(Buffer) works same as string');
// Test chained CRC32
var crc3 = zlib.crc32('world', crc1);
assert(crc3 === 4192936109, 'crc32("world", crc1) = 4192936109');
console.log(' [INFO] CRC32 of "world" (chained): ' + crc3);
// Test empty string
var crcEmpty = zlib.crc32('');
assert(crcEmpty === 0, 'crc32("") = 0');
// Test longer data
var crcLong = zlib.crc32(longInput);
assert(typeof crcLong === 'number', 'crc32() works on long input');
console.log(' [INFO] CRC32 of longInput: ' + crcLong);
console.log('');
runTest14();
}
// ============================================================================
// TEST 14: Short Data
// ============================================================================
function runTest14() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 14: Short Data Compression');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
// Test gzip with short data
var gzipped = zlib.gzipSync(shortInput);
var ungzipped = zlib.gunzipSync(gzipped);
assert(ungzipped.toString() === shortInput, 'Gzip works with short data');
console.log(' [INFO] Short gzip: ' + shortInput.length + ' -> ' + gzipped.length + ' bytes');
// Test deflate with short data
var deflated = zlib.deflateSync(shortInput);
var inflated = zlib.inflateSync(deflated);
assert(inflated.toString() === shortInput, 'Deflate works with short data');
console.log(' [INFO] Short deflate: ' + shortInput.length + ' -> ' + deflated.length + ' bytes');
// Test brotli with short data (may have issues with very short input)
try {
var brotlied = zlib.brotliCompressSync(shortInput);
var unbrotlied = zlib.brotliDecompressSync(brotlied);
assert(unbrotlied.toString() === shortInput, 'Brotli works with short data');
console.log(' [INFO] Short brotli: ' + shortInput.length + ' -> ' + brotlied.length + ' bytes');
} catch (e) {
console.log(' [SKIP] Brotli short data test (library limitation with very short input)');
}
console.log('');
runTest15();
}
// ============================================================================
// TEST 15: Empty Data
// ============================================================================
function runTest15() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 15: Empty Data Compression');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
var emptyInput = '';
// Test gzip with empty data
var gzipped = zlib.gzipSync(emptyInput);
var ungzipped = zlib.gunzipSync(gzipped);
assert(ungzipped.toString() === emptyInput, 'Gzip works with empty data');
console.log(' [INFO] Empty gzip produces: ' + gzipped.length + ' bytes');
// Test deflate with empty data
var deflated = zlib.deflateSync(emptyInput);
var inflated = zlib.inflateSync(deflated);
assert(inflated.toString() === emptyInput, 'Deflate works with empty data');
console.log(' [INFO] Empty deflate produces: ' + deflated.length + ' bytes');
console.log('');
runTest16();
}
// ============================================================================
// TEST 16: Binary Data
// ============================================================================
function runTest16() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 16: Binary Data Compression');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
// Create binary data with all byte values
var binaryData = Buffer.alloc(256);
for (var i = 0; i < 256; i++) {
binaryData[i] = i;
}
// Test gzip with binary data
var gzipped = zlib.gzipSync(binaryData);
var ungzipped = zlib.gunzipSync(gzipped);
assert(Buffer.compare(ungzipped, binaryData) === 0, 'Gzip preserves binary data');
console.log(' [INFO] Binary gzip: ' + binaryData.length + ' -> ' + gzipped.length + ' bytes');
// Test deflate with binary data
var deflated = zlib.deflateSync(binaryData);
var inflated = zlib.inflateSync(deflated);
assert(Buffer.compare(inflated, binaryData) === 0, 'Deflate preserves binary data');
// Test brotli with binary data
try {
var brotlied = zlib.brotliCompressSync(binaryData);
var unbrotlied = zlib.brotliDecompressSync(brotlied);
// Convert to Buffer if necessary
if (!(unbrotlied instanceof Buffer)) {
unbrotlied = Buffer.from(unbrotlied);
}
assert(Buffer.compare(unbrotlied, binaryData) === 0, 'Brotli preserves binary data');
} catch (e) {
console.log(' [SKIP] Brotli binary data test (library limitation)');
}
console.log('');
runTest17();
}
// ============================================================================
// TEST 17: Compression Comparison
// ============================================================================
function runTest17() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 17: Compression Algorithm Comparison');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
var gzipSize = zlib.gzipSync(longInput).length;
var deflateSize = zlib.deflateSync(longInput).length;
var deflateRawSize = zlib.deflateRawSync(longInput).length;
var brotliSize = zlib.brotliCompressSync(longInput).length;
console.log(' [INFO] Original size: ' + longInput.length + ' bytes');
console.log(' [INFO] Gzip size: ' + gzipSize + ' bytes (' + ((1 - gzipSize/longInput.length) * 100).toFixed(1) + '% reduction)');
console.log(' [INFO] Deflate size: ' + deflateSize + ' bytes (' + ((1 - deflateSize/longInput.length) * 100).toFixed(1) + '% reduction)');
console.log(' [INFO] DeflateRaw size: ' + deflateRawSize + ' bytes (' + ((1 - deflateRawSize/longInput.length) * 100).toFixed(1) + '% reduction)');
console.log(' [INFO] Brotli size: ' + brotliSize + ' bytes (' + ((1 - brotliSize/longInput.length) * 100).toFixed(1) + '% reduction)');
assert(gzipSize < longInput.length, 'Gzip compresses text');
assert(deflateSize < longInput.length, 'Deflate compresses text');
assert(deflateRawSize < longInput.length, 'DeflateRaw compresses text');
assert(brotliSize < longInput.length, 'Brotli compresses text');
assert(brotliSize <= gzipSize, 'Brotli achieves better or equal compression than Gzip');
console.log('');
runTest18();
}
// ============================================================================
// TEST 18: codes Object
// ============================================================================
function runTest18() {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('TEST 18: Error Codes');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
assert(typeof zlib.codes === 'object', 'zlib.codes exists');
assert(zlib.codes.Z_OK === 0, 'codes.Z_OK = 0');
assert(zlib.codes.Z_STREAM_END === 1, 'codes.Z_STREAM_END = 1');
assert(zlib.codes.Z_NEED_DICT === 2, 'codes.Z_NEED_DICT = 2');
assert(zlib.codes.Z_BUF_ERROR === -5, 'codes.Z_BUF_ERROR = -5');
// Reverse lookup
assert(zlib.codes[0] === 'Z_OK', 'codes[0] = "Z_OK"');
assert(zlib.codes[1] === 'Z_STREAM_END', 'codes[1] = "Z_STREAM_END"');
console.log('');
printSummary();
}
// ============================================================================
// Summary
// ============================================================================
function printSummary() {
console.log('╔══════════════════════════════════════════════════════════════════╗');
console.log('║ TEST SUMMARY ║');
console.log('╚══════════════════════════════════════════════════════════════════╝');
console.log('');
console.log(' Total tests: ' + totalTests);
console.log(' Passed: ' + passedTests);
console.log(' Failed: ' + failedTests);
console.log('');
if (failedTests === 0) {
console.log(' ✓ ALL TESTS PASSED!');
} else {
console.log(' ✗ SOME TESTS FAILED');
process.exit(1);
}
console.log('');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('Supported Features:');
console.log(' - Gzip/Gunzip compression (sync & async)');
console.log(' - Deflate/Inflate compression (sync & async)');
console.log(' - DeflateRaw/InflateRaw compression (sync & async)');
console.log(' - Unzip auto-detection (sync & async)');
console.log(' - Brotli compression (Node.js 11.7.0+)');
console.log(' - CRC32 function (Node.js 22.2.0+)');
console.log(' - Stream-based compression');
console.log(' - All zlib and Brotli constants');
console.log(' - Compression levels (0-9)');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
}
// ============================================
// Promise-Based API Tests
// ============================================
(async function testGzipPromise() {
try {
const compressed = await zlib.gzip('Hello, World!');
assert(Buffer.isBuffer(compressed), 'gzip promise should return Buffer');
assert(compressed.length > 0, 'compressed should not be empty');
console.log('✓ gzip promise resolves with Buffer');
} catch (err) {
console.error('✗ gzip promise failed:', err.message);
}
})();
(async function testGzipWithOptions() {
try {
const fast = await zlib.gzip('Hello, World!', { level: 1 });
const best = await zlib.gzip('Hello, World!', { level: 9 });
assert(Buffer.isBuffer(fast), 'fast should be Buffer');
assert(Buffer.isBuffer(best), 'best should be Buffer');
console.log('✓ gzip promise with options works');
} catch (err) {
console.error('✗ gzip promise with options failed:', err.message);
}
})();
(async function testGunzipPromise() {
try {
const original = 'Hello, World!';
const compressed = await zlib.gzip(original);
const decompressed = await zlib.gunzip(compressed);
assert(decompressed.toString() === original, 'gunzip should decompress correctly');
console.log('✓ gunzip promise resolves correctly');
} catch (err) {
console.error('✗ gunzip promise failed:', err.message);
}
})();
(async function testGzipRejectNull() {
try {
await zlib.gzip(null);
console.error('✗ gzip promise should reject on null input');
} catch (err) {
assert(err instanceof Error, 'should throw Error');
console.log('✓ gzip promise rejects on null input');
}
})();
(async function testGunzipRejectInvalid() {
try {
await zlib.gunzip(Buffer.from('invalid'));
console.error('✗ gunzip promise should reject on invalid data');
} catch (err) {
assert(err instanceof Error, 'should throw Error');
console.log('✓ gunzip promise rejects on invalid data');
}
})();
// ============================================
// Backward Compatibility Tests (Task 8)
// ============================================
console.log('\n--- Backward Compatibility Tests ---\n');
// Test 1: Callback with options for all methods
setTimeout(() => {
zlib.gzip('TestData', { level: 9 }, (err, compressed) => {
assert(!err, 'gzip callback with options should not error');
assert(Buffer.isBuffer(compressed), 'should return Buffer');
console.log('✓ gzip callback with options works');
});
}, 100);
setTimeout(() => {
zlib.deflate('TestData', { level: 9 }, (err, compressed) => {
assert(!err, 'deflate callback with options should not error');
assert(Buffer.isBuffer(compressed), 'should return Buffer');
console.log('✓ deflate callback with options works');
});
}, 150);
setTimeout(() => {
zlib.brotliCompress('TestData', { params: { mode: 0 } }, (err, compressed) => {
assert(!err, 'brotliCompress callback with options should not error');
assert(Buffer.isBuffer(compressed), 'should return Buffer');
console.log('✓ brotliCompress callback with options works');
});
}, 200);
// Test 2: Callback without options for deflate and brotli
setTimeout(() => {
zlib.deflate('TestData', (err, compressed) => {
assert(!err, 'deflate callback without options should not error');
assert(Buffer.isBuffer(compressed), 'should return Buffer');
console.log('✓ deflate callback without options works');
});
}, 250);
setTimeout(() => {
zlib.brotliCompress('TestData', (err, compressed) => {
assert(!err, 'brotliCompress callback without options should not error');
assert(Buffer.isBuffer(compressed), 'should return Buffer');
console.log('✓ brotliCompress callback without options works');
});
}, 300);
// Test 3: Promise output matches sync output (roundtrip verification)
(async function testPromiseVsSyncRoundtrip() {
try {
const data = 'Promise vs Sync Test Data';
// Test gzip/gunzip roundtrip
const gzipPromise = await zlib.gzip(data);
const gzipSync = zlib.gzipSync(data);
const decompPromise = await zlib.gunzip(gzipPromise);
const decompSync = zlib.gunzipSync(gzipSync);
assert(decompPromise.toString() === data, 'gzip promise decompression should match original');
assert(decompSync.toString() === data, 'gzip sync decompression should match original');
assert(decompPromise.toString() === decompSync.toString(), 'promise and sync should produce same decompressed data');
console.log('✓ gzip promise and sync produce same decompressed results');
} catch (err) {
console.error('✗ promise vs sync roundtrip test failed:', err.message);
}
})();
// Test 4: All decompression methods support promises
(async function testAllDecompressionMethodsPromise() {
try {
const data = 'Test all decompression methods';
const gzipped = zlib.gzipSync(data);
const deflated = zlib.deflateSync(data);
const deflateRawed = zlib.deflateRawSync(data);
const gunzipped = await zlib.gunzip(gzipped);
const inflated = await zlib.inflate(deflated);
const inflateRawed = await zlib.inflateRaw(deflateRawed);
assert(gunzipped.toString() === data, 'gunzip promise should decompress correctly');
assert(inflated.toString() === data, 'inflate promise should decompress correctly');
assert(inflateRawed.toString() === data, 'inflateRaw promise should decompress correctly');
console.log('✓ all decompression methods work with promises');
} catch (err) {
console.error('✗ decompression promise test failed:', err.message);
}
})();
// Test 5: Unzip auto-detection with promises
(async function testUnzipPromise() {
try {
const data = 'Test unzip auto-detection';
const gzipped = zlib.gzipSync(data);
const unzipped = await zlib.unzip(gzipped);
assert(unzipped.toString() === data, 'unzip promise should auto-detect and decompress');
console.log('✓ unzip auto-detection works with promises');
} catch (err) {
console.error('✗ unzip promise test failed:', err.message);
}
})();
// Test 6: All 10 methods support promise mode (skipping brotli decompression due to timing)
(async function testAllMethodsPromiseMode() {
const data = 'Test all 10 methods';
try {
// Compression methods
const gzipped = await zlib.gzip(data);
const deflated = await zlib.deflate(data);
const deflateRawed = await zlib.deflateRaw(data);
const brotliCompressed = await zlib.brotliCompress(data);
// Decompression methods
const gunzipped = await zlib.gunzip(gzipped);
const inflated = await zlib.inflate(deflated);
const inflateRawed = await zlib.inflateRaw(deflateRawed);
const unzipped = await zlib.unzip(gzipped);
assert(Buffer.isBuffer(gzipped), 'gzip should return Buffer');
assert(Buffer.isBuffer(deflated), 'deflate should return Buffer');
assert(Buffer.isBuffer(deflateRawed), 'deflateRaw should return Buffer');
assert(Buffer.isBuffer(brotliCompressed), 'brotliCompress should return Buffer');
assert(gunzipped.toString() === data, 'gunzip should decompress correctly');
assert(inflated.toString() === data, 'inflate should decompress correctly');
assert(inflateRawed.toString() === data, 'inflateRaw should decompress correctly');
assert(unzipped.toString() === data, 'unzip should decompress correctly');
console.log('✓ all 10 async methods support promise mode');
} catch (err) {
console.error('✗ all methods promise test failed:', err.message);
}
})();
// Test 7: Callback error handling for invalid data
setTimeout(() => {
zlib.gunzip(Buffer.from('invalid'), (err, decompressed) => {
assert(err instanceof Error, 'invalid data should error in callback');
console.log('✓ callback error handling works for invalid data');
});
}, 350);
// Test 8: Promise error handling for invalid data
(async function testPromiseErrorHandling() {
try {
await zlib.gunzip(Buffer.from('invalid'));
console.error('✗ gunzip promise should reject on invalid data');
} catch (err) {
assert(err instanceof Error, 'should throw Error on invalid data');
console.log('✓ promise error handling works for invalid data');
}
})();
// Test 9: Callback for deflateRaw with options
setTimeout(() => {
zlib.deflateRaw('TestData', { level: 6 }, (err, compressed) => {
assert(!err, 'deflateRaw callback with options should not error');
assert(Buffer.isBuffer(compressed), 'should return Buffer');
console.log('✓ deflateRaw callback with options works');
});
}, 400);
// Test 10: Brotli decompression with promise (skip - using sync in test 6)
// Note: Brotli decompression is tested in Test 6 (testAllMethodsPromiseMode)
// Skipping separate test to avoid timing issues with brotli codec
// Test 11: All methods support options parameter
(async function testAllMethodsWithOptions() {
try {
const data = 'Test with options';
const gzipFast = await zlib.gzip(data, { level: 1 });
const gzipBest = await zlib.gzip(data, { level: 9 });
const deflateFast = await zlib.deflate(data, { level: 1 });
const deflateBest = await zlib.deflate(data, { level: 9 });
const deflateRawFast = await zlib.deflateRaw(data, { level: 1 });
const deflateRawBest = await zlib.deflateRaw(data, { level: 9 });
// Verify compression actually happened with different levels
assert(gzipFast.length > 0, 'gzip with level 1 should produce output');
assert(gzipBest.length > 0, 'gzip with level 9 should produce output');
assert(deflateFast.length > 0, 'deflate with level 1 should produce output');
assert(deflateBest.length > 0, 'deflate with level 9 should produce output');
assert(deflateRawFast.length > 0, 'deflateRaw with level 1 should produce output');
assert(deflateRawBest.length > 0, 'deflateRaw with level 9 should produce output');
console.log('✓ all compression methods support options parameter');
} catch (err) {
console.error('✗ all methods with options test failed:', err.message);
}
})();
// Test 12: Mixed callback and promise usage
(async function testMixedUsage() {
try {
const data = 'Mixed usage test';
// Use callback for compression
const compressedViaCallback = await new Promise((resolve, reject) => {
zlib.gzip(data, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
// Use promise for decompression
const decompressed = await zlib.gunzip(compressedViaCallback);
assert(decompressed.toString() === data, 'mixed callback and promise should work together');
console.log('✓ mixed callback and promise usage works');
} catch (err) {
console.error('✗ mixed usage test failed:', err.message);
}
})();