imageReformaterTest.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var should = require('chai').should();
  2. var imageReformater = require('../../lib/tools/redownload/imageReformater');
  3. var fs = require('fs');
  4. var path = require('path');
  5. describe('imageReformater', function() {
  6. it('should convert a JPEG image to WebP and AVIF', async function() {
  7. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  8. let entry = {
  9. isImage: true,
  10. type: 'image',
  11. contentType: 'image/jpeg',
  12. weightCheck: {
  13. bodyBuffer: fileContent,
  14. uncompressedSize: fileContent.length
  15. }
  16. };
  17. var newEntry = await imageReformater.reformatImage(entry);
  18. newEntry.weightCheck.should.have.a.property('webpSize');
  19. newEntry.weightCheck.webpSize.should.be.below(fileContent.length);
  20. newEntry.weightCheck.should.have.a.property('avifSize');
  21. newEntry.weightCheck.avifSize.should.be.below(fileContent.length);
  22. });
  23. it('should convert a PNG image to WebP and AVIF', async function() {
  24. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  25. let entry = {
  26. isImage: true,
  27. type: 'image',
  28. contentType: 'image/png',
  29. weightCheck: {
  30. bodyBuffer: fileContent,
  31. uncompressedSize: fileContent.length
  32. }
  33. };
  34. var newEntry = await imageReformater.reformatImage(entry);
  35. newEntry.weightCheck.should.have.a.property('webpSize');
  36. newEntry.weightCheck.webpSize.should.be.below(fileContent.length);
  37. newEntry.weightCheck.should.have.a.property('avifSize');
  38. newEntry.weightCheck.avifSize.should.be.below(fileContent.length);
  39. });
  40. it('should convert a WebP image to AVIF', async function() {
  41. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  42. let entry = {
  43. isImage: true,
  44. type: 'image',
  45. contentType: 'image/webp',
  46. weightCheck: {
  47. bodyBuffer: fileContent,
  48. uncompressedSize: fileContent.length
  49. }
  50. };
  51. var newEntry = await imageReformater.reformatImage(entry);
  52. newEntry.weightCheck.should.not.have.a.property('webpSize');
  53. newEntry.weightCheck.should.have.a.property('avifSize');
  54. newEntry.weightCheck.avifSize.should.be.below(fileContent.length);
  55. });
  56. it('should recognize an animated WebP', async function() {
  57. // Test on an animated image
  58. let fileContent = fs.readFileSync(path.resolve(__dirname, '../www/animated.webp'));
  59. let entry = {
  60. isImage: true,
  61. type: 'image',
  62. contentType: 'image/webp',
  63. weightCheck: {
  64. bodyBuffer: fileContent,
  65. uncompressedSize: fileContent.length
  66. }
  67. };
  68. (await imageReformater.isAnimated(entry)).should.equal(true);
  69. // Test on a not animated image
  70. fileContent = fs.readFileSync(path.resolve(__dirname, '../www/monster.webp'));
  71. entry.weightCheck.bodyBuffer = fileContent;
  72. (await imageReformater.isAnimated(entry)).should.equal(false);
  73. });
  74. it('should not convert an animated WebP', async function() {
  75. // Test on an animated image
  76. let fileContent = fs.readFileSync(path.resolve(__dirname, '../www/animated.webp'));
  77. let entry = {
  78. isImage: true,
  79. type: 'image',
  80. contentType: 'image/webp',
  81. weightCheck: {
  82. bodyBuffer: fileContent,
  83. uncompressedSize: fileContent.length
  84. }
  85. };
  86. var newEntry = await imageReformater.reformatImage(entry);
  87. // Test on a not animated image
  88. newEntry.weightCheck.should.not.have.a.property('avifSize');
  89. });
  90. it('should determine if gain is enough', function() {
  91. imageReformater.gainIsEnough(20000, 10000).should.equal(true);
  92. imageReformater.gainIsEnough(2000, 1000).should.equal(true);
  93. imageReformater.gainIsEnough(20000, 21000).should.equal(false);
  94. imageReformater.gainIsEnough(20000, 40000).should.equal(false);
  95. imageReformater.gainIsEnough(20000, 19500).should.equal(false);
  96. imageReformater.gainIsEnough(250, 120).should.equal(true);
  97. imageReformater.gainIsEnough(200, 120).should.equal(false);
  98. imageReformater.gainIsEnough(2000, 1900).should.equal(false);
  99. imageReformater.gainIsEnough(200000, 197000).should.equal(true);
  100. });
  101. });