imageOptimizerTest.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. this.timeout(5000);
  136. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  137. var fileSize = fileContent.length;
  138. imageOptimizer.compressSvgLosslessly(fileContent).then(function(newFile) {
  139. var newFileSize = newFile.length;
  140. newFileSize.should.be.below(fileSize);
  141. done();
  142. }).fail(function(err) {
  143. done(err);
  144. });
  145. });
  146. it('should optimize an SVG', function(done) {
  147. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/svg-image.svg'));
  148. var fileSize = fileContent.length;
  149. var entry = {
  150. method: 'GET',
  151. url: 'http://localhost:8388/an-image.svg',
  152. requestHeaders: {
  153. 'User-Agent': 'something',
  154. Referer: 'http://www.google.fr/',
  155. Accept: '*/*',
  156. 'Accept-Encoding': 'gzip, deflate'
  157. },
  158. status: 200,
  159. isImage: true,
  160. isSVG: true,
  161. type: 'image',
  162. contentType: 'image/svg+xml',
  163. contentLength: 999,
  164. weightCheck: {
  165. bodyBuffer: fileContent,
  166. totalWeight: fileSize + 200,
  167. headersSize: 200,
  168. bodySize: fileSize,
  169. isCompressed: false,
  170. uncompressedSize: fileSize
  171. }
  172. };
  173. imageOptimizer.optimizeImage(entry)
  174. .then(function(newEntry) {
  175. newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
  176. newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
  177. newEntry.weightCheck.should.have.a.property('optimized').that.equals(newEntry.weightCheck.lossless);
  178. newEntry.weightCheck.should.have.a.property('bodyAfterOptimization').that.is.a('string');
  179. done();
  180. })
  181. .fail(function(err) {
  182. done(err);
  183. });
  184. });
  185. it('shouldn\'t fail optimizing a corrupted jpeg', function(done) {
  186. // In this test, we try to optimize a PNG but with a falsy "image/jpeg" content type
  187. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/png-image.png'));
  188. var fileSize = fileContent.length;
  189. var entry = {
  190. method: 'GET',
  191. url: 'http://localhost:8388/an-image.png',
  192. requestHeaders: {
  193. 'User-Agent': 'something',
  194. Referer: 'http://www.google.fr/',
  195. Accept: '*/*',
  196. 'Accept-Encoding': 'gzip, deflate'
  197. },
  198. status: 200,
  199. isImage: true,
  200. type: 'image',
  201. contentType: 'image/jpeg',
  202. contentLength: 999,
  203. weightCheck: {
  204. bodyBuffer: fileContent,
  205. totalWeight: fileSize + 200,
  206. headersSize: 200,
  207. bodySize: fileSize,
  208. isCompressed: false,
  209. uncompressedSize: fileSize
  210. }
  211. };
  212. imageOptimizer.optimizeImage(entry)
  213. .then(function(newEntry) {
  214. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  215. done();
  216. })
  217. .fail(function(err) {
  218. done(err);
  219. });
  220. });
  221. it('shouldn\'t fail optimizing a corrupted png', function(done) {
  222. // In this test, we try to optimize a JPEG but with a falsy "image/png" content type
  223. var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jpeg-image.jpg'));
  224. var fileSize = fileContent.length;
  225. var entry = {
  226. method: 'GET',
  227. url: 'http://localhost:8388/an-image.jpg',
  228. requestHeaders: {
  229. 'User-Agent': 'something',
  230. Referer: 'http://www.google.fr/',
  231. Accept: '*/*',
  232. 'Accept-Encoding': 'gzip, deflate'
  233. },
  234. status: 200,
  235. isImage: true,
  236. type: 'image',
  237. contentType: 'image/png',
  238. contentLength: 999,
  239. weightCheck: {
  240. bodyBuffer: fileContent,
  241. totalWeight: fileSize + 200,
  242. headersSize: 200,
  243. bodySize: fileSize,
  244. isCompressed: false,
  245. uncompressedSize: fileSize
  246. }
  247. };
  248. imageOptimizer.optimizeImage(entry)
  249. .then(function(newEntry) {
  250. newEntry.weightCheck.should.not.have.a.property('isOptimized');
  251. done();
  252. })
  253. .fail(function(err) {
  254. done(err);
  255. });
  256. });
  257. it('should determine if gain is enough', function() {
  258. imageOptimizer.gainIsEnough(20000, 10000).should.equal(true);
  259. imageOptimizer.gainIsEnough(2000, 1000).should.equal(true);
  260. imageOptimizer.gainIsEnough(20000, 21000).should.equal(false);
  261. imageOptimizer.gainIsEnough(20000, 40000).should.equal(false);
  262. imageOptimizer.gainIsEnough(20000, 19500).should.equal(false);
  263. imageOptimizer.gainIsEnough(250, 120).should.equal(true);
  264. imageOptimizer.gainIsEnough(200, 120).should.equal(false);
  265. imageOptimizer.gainIsEnough(2000, 1900).should.equal(false);
  266. imageOptimizer.gainIsEnough(200000, 197000).should.equal(true);
  267. });
  268. });