imageOptimizerTest.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. var should = require('chai').should();
  2. var imageOptimizer = require('../../lib/tools/redownload/imageOptimizer');
  3. var fs = require('fs');
  4. var path = require('path');
  5. describe('imageOptimizer', function() {
  6. it('should optimize a JPEG image losslessly', function(done) {
  7. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  8. var fileSize = fileContent.length;
  9. imageOptimizer.compressJpegLosslessly(fileContent).then(function(newFile) {
  10. var newFileSize = newFile.length;
  11. newFileSize.should.be.below(fileSize);
  12. done();
  13. }).fail(function(err) {
  14. done(err);
  15. });
  16. });
  17. it('should optimize a JPEG image lossly', function(done) {
  18. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  19. var fileSize = fileContent.length;
  20. imageOptimizer.compressJpegLossly(fileContent).then(function(newFile) {
  21. var newFileSize = newFile.length;
  22. newFileSize.should.be.below(fileSize);
  23. done();
  24. }).fail(function(err) {
  25. done(err);
  26. });
  27. });
  28. it('should find the best optimization for a jpeg', function(done) {
  29. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  30. var fileSize = fileContent.length;
  31. var entry = {
  32. method: 'GET',
  33. url: 'http://localhost:8388/an-image.jpg',
  34. requestHeaders: {
  35. 'User-Agent': 'something',
  36. Referer: 'http://www.google.fr/',
  37. Accept: '*/*',
  38. 'Accept-Encoding': 'gzip, deflate'
  39. },
  40. status: 200,
  41. isImage: true,
  42. type: 'image',
  43. contentType: 'image/jpeg',
  44. contentLength: 999,
  45. weightCheck: {
  46. bodyBuffer: fileContent,
  47. totalWeight: fileSize + 200,
  48. headersSize: 200,
  49. bodySize: fileSize,
  50. isCompressed: false,
  51. uncompressedSize: fileSize
  52. }
  53. };
  54. imageOptimizer.optimizeImage(entry)
  55. .then(function(newEntry) {
  56. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  57. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  58. newEntry.weightCheck.should.have.a.property('lossy').that.is.below(newEntry.weightCheck.lossless);
  59. done();
  60. })
  61. .fail(function(err) {
  62. done(err);
  63. });
  64. });
  65. it('should optimize a PNG image losslessly', function(done) {
  66. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
  67. var fileSize = fileContent.length;
  68. imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
  69. var newFileSize = newFile.length;
  70. newFileSize.should.be.below(fileSize);
  71. done();
  72. }).fail(function(err) {
  73. done(err);
  74. });
  75. });
  76. it('should fail to optimize an already optimized PNG', function(done) {
  77. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/logo-large.png'));
  78. var fileSize = fileContent.length;
  79. imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
  80. var newFileSize = newFile.length;
  81. newFileSize.should.equal(fileSize);
  82. done();
  83. }).fail(function(err) {
  84. done(err);
  85. });
  86. });
  87. it('should fail to optimize a non-PNG', function(done) {
  88. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  89. var fileSize = fileContent.length;
  90. imageOptimizer.compressPngLosslessly(fileContent).then(function(newFile) {
  91. var newFileSize = newFile.length;
  92. newFileSize.should.equal(fileSize);
  93. done();
  94. }).fail(function(err) {
  95. done(err);
  96. });
  97. });
  98. it('should optimize a png', function(done) {
  99. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
  100. var fileSize = fileContent.length;
  101. var entry = {
  102. method: 'GET',
  103. url: 'http://localhost:8388/an-image.png',
  104. requestHeaders: {
  105. 'User-Agent': 'something',
  106. Referer: 'http://www.google.fr/',
  107. Accept: '*/*',
  108. 'Accept-Encoding': 'gzip, deflate'
  109. },
  110. status: 200,
  111. isImage: true,
  112. type: 'image',
  113. contentType: 'image/png',
  114. contentLength: 999,
  115. weightCheck: {
  116. bodyBuffer: fileContent,
  117. totalWeight: fileSize + 200,
  118. headersSize: 200,
  119. bodySize: fileSize,
  120. isCompressed: false,
  121. uncompressedSize: fileSize
  122. }
  123. };
  124. imageOptimizer.optimizeImage(entry)
  125. .then(function(newEntry) {
  126. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  127. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  128. done();
  129. })
  130. .fail(function(err) {
  131. done(err);
  132. });
  133. });
  134. it('should optimize an SVG image losslessly', function(done) {
  135. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  136. var fileSize = fileContent.length;
  137. imageOptimizer.compressSvgLosslessly(fileContent).then(function(newFile) {
  138. var newFileSize = newFile.length;
  139. newFileSize.should.be.below(fileSize);
  140. done();
  141. }).fail(function(err) {
  142. done(err);
  143. });
  144. });
  145. it('should optimize an SVG', function(done) {
  146. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  147. var fileSize = fileContent.length;
  148. var entry = {
  149. method: 'GET',
  150. url: 'http://localhost:8388/an-image.svg',
  151. requestHeaders: {
  152. 'User-Agent': 'something',
  153. Referer: 'http://www.google.fr/',
  154. Accept: '*/*',
  155. 'Accept-Encoding': 'gzip, deflate'
  156. },
  157. status: 200,
  158. isImage: true,
  159. isSVG: true,
  160. type: 'image',
  161. contentType: 'image/svg+xml',
  162. contentLength: 999,
  163. weightCheck: {
  164. bodyBuffer: fileContent,
  165. totalWeight: fileSize + 200,
  166. headersSize: 200,
  167. bodySize: fileSize,
  168. isCompressed: false,
  169. uncompressedSize: fileSize
  170. }
  171. };
  172. imageOptimizer.optimizeImage(entry)
  173. .then(function(newEntry) {
  174. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  175. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  176. newEntry.weightCheck.should.have.a.property('optimized').that.equals(newEntry.weightCheck.lossless);
  177. newEntry.weightCheck.should.have.a.property('bodyAfterOptimization').that.is.a('string');
  178. done();
  179. })
  180. .fail(function(err) {
  181. done(err);
  182. });
  183. });
  184. it('shouldn\'t fail optimizing a corrupted jpeg', function(done) {
  185. // In this test, we try to optimize a PNG but with a falsy "image/jpeg" content type
  186. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
  187. var fileSize = fileContent.length;
  188. var entry = {
  189. method: 'GET',
  190. url: 'http://localhost:8388/an-image.png',
  191. requestHeaders: {
  192. 'User-Agent': 'something',
  193. Referer: 'http://www.google.fr/',
  194. Accept: '*/*',
  195. 'Accept-Encoding': 'gzip, deflate'
  196. },
  197. status: 200,
  198. isImage: true,
  199. type: 'image',
  200. contentType: 'image/jpeg',
  201. contentLength: 999,
  202. weightCheck: {
  203. bodyBuffer: fileContent,
  204. totalWeight: fileSize + 200,
  205. headersSize: 200,
  206. bodySize: fileSize,
  207. isCompressed: false,
  208. uncompressedSize: fileSize
  209. }
  210. };
  211. imageOptimizer.optimizeImage(entry)
  212. .then(function(newEntry) {
  213. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  214. done();
  215. })
  216. .fail(function(err) {
  217. done(err);
  218. });
  219. });
  220. it('shouldn\'t fail optimizing a corrupted png', function(done) {
  221. // In this test, we try to optimize a JPEG but with a falsy "image/png" content type
  222. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  223. var fileSize = fileContent.length;
  224. var entry = {
  225. method: 'GET',
  226. url: 'http://localhost:8388/an-image.jpg',
  227. requestHeaders: {
  228. 'User-Agent': 'something',
  229. Referer: 'http://www.google.fr/',
  230. Accept: '*/*',
  231. 'Accept-Encoding': 'gzip, deflate'
  232. },
  233. status: 200,
  234. isImage: true,
  235. type: 'image',
  236. contentType: 'image/png',
  237. contentLength: 999,
  238. weightCheck: {
  239. bodyBuffer: fileContent,
  240. totalWeight: fileSize + 200,
  241. headersSize: 200,
  242. bodySize: fileSize,
  243. isCompressed: false,
  244. uncompressedSize: fileSize
  245. }
  246. };
  247. imageOptimizer.optimizeImage(entry)
  248. .then(function(newEntry) {
  249. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  250. done();
  251. })
  252. .fail(function(err) {
  253. done(err);
  254. });
  255. });
  256. it('should determine if gain is enough', function() {
  257. imageOptimizer.gainIsEnough(20000, 10000).should.equal(true);
  258. imageOptimizer.gainIsEnough(2000, 1000).should.equal(true);
  259. imageOptimizer.gainIsEnough(20000, 21000).should.equal(false);
  260. imageOptimizer.gainIsEnough(20000, 40000).should.equal(false);
  261. imageOptimizer.gainIsEnough(20000, 19500).should.equal(false);
  262. imageOptimizer.gainIsEnough(250, 120).should.equal(true);
  263. imageOptimizer.gainIsEnough(200, 120).should.equal(false);
  264. imageOptimizer.gainIsEnough(2000, 1900).should.equal(false);
  265. imageOptimizer.gainIsEnough(200000, 197000).should.equal(true);
  266. });
  267. });